Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn't find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like:
def iterlen(x):
n = 0
try:
while True:
next(x)
n += 1
except StopIt...
Hello people.
I'm trying to figure out how to implement a function in a tree node which returns all its descendant leaves (whether direct or indirect). However, I don't want to pass a container in which the leaf nodes will be put recursively (the tree could be huge), instead I'd like to use a generator to iterate through the tree. I've ...
I'm trying to insert some new objects into a firebird database using NHibernate.
I get the error "could not get next sequence value[SQL: SQL not available]"
Here is the mapping I'm using at present. Note ANML_EVNT is the name of the generator I want to use.
<id name="Id" column="ID" type="integer">
<generator class="sequence">
...
I'm writing a program as follows:
Find all files with the correct extension in a given directory
Foreach, find all occurrences of a given string in those files
Print each line
I'd like to write this in a functional way, as a series of generator functions (things that call yield return and only return one item at a time lazily-loaded)...
Given a regular expression, I'm looking for a package which will dynamically generate the code for a finite state machine that implements the RE.
C/C++ and Python preferred, but other languages are of interest as well.
...
I created a utility function to return the expected single item from an generator expression
print one(name for name in ('bob','fred') if name=='bob')
Is this a good way to go about it?
def one(g):
try:
val = g.next()
try:
g.next()
except StopIteration:
return val
else:
...
Given a source XML document and a sample of what it should look like after it is transformed, is there a code generator that could create an XSL transform to accomplish that.
I am not expecting it to be perfect but, much like the code generators that can create XSD from sample XML, it would be a good starting point to then refine by han...
I have recently started playing around with libnet and using it to generate IPV6 packets. I am very new at programming, however, I am quite happy with the library.
I have one problem with it though. It seems that libnet currently does not have the ability to properly calculate checksums on IPV6 packets. Being so new to programming,...
I need to read and write octet streams to send over various networks to communicate with smart electric meters. There is an ANSI standard, ANSI C12.19, that describes the binary data format. While the data format is not overly complex the standard is very large (500+ pages) in that it describes many distinct types. The standard is ful...
I have a very big file 4GB and when I try to read it my computer hangs.
So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece.
Is there any method to yield these pieces ?
I would love to have a lazy method.
...
I've the coordinates of the letters (e.g. using SVG). How could I generate a TrueType/PostScript font for my computer ?
(edited : changed vectorial to TrueType/PostScript )
...
Is it possible to append elements to a python generator?
I'm currently trying to get all images from a set of disorganized folders and write them to a new directory. To get the files, I'm using os.walk() which returns a list of image files in a single directory. While I can make a generator out of this single list, I don't know how to c...
Quickcheck and its variants (even there is one in java), seems to be interesting. However, apart from academic interest, is it really useful in a real application testing (Eg. a GUI application or Client/Server or even take StackOverflow itself)? Any experiences you had with similar test generators is appreciated.
...
I want to export a function which depends on name of class where is exported into. I thought that it should be easy with Sub::Exporter but unfortunately the into key is not passed to generators. I have ended up with those ugly example code:
use strict;
use warnings;
package MyLog;
use Log::Log4perl qw(:easy get_logger);
use Sub::Expo...
I'm a big fan of Python's for...else syntax - it's surprising how often it's applicable, and how effectively it can simplify code.
However, I've not figured out a nice way to use it in a generator expression, for example:
def iterate(i):
for value in i:
yield value
else:
print 'i is empty'
In the above example...
I'm writing a Python function to split text into words, ignoring specified punctuation. Here is some working code. I'm not convinced that constructing strings out of lists (buf = [] in the code) is efficient though. Does anyone have a suggestion for a better way to do this?
def getwords(text, splitchars=' \t|!?.;:"'):
"""
Genera...
I need a simple random English sentence generator. I need to populate it with my own words, but it needs to be capable of making longer sentences that at least follow the rules of English, even if they don't make sense.
I expect there are millions of them out there, so rather than re-inventing the wheel, I'm hoping you know of a source...
Is there a simple way of testing if the generator has no items, like peek, hasNext, isEmpty, something along those lines?
...
Is the a way to generate an arbitrary number of rows that can be used in a join similar to the oracle syntax:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=10
...
How can an iterator over a non-empty sequence, with no filtering and no aggregation (sum(), etc.), yield nothing?
Consider a simple example:
sequence = ['a', 'b', 'c']
list((el, ord(el)) for el in sequence)
This yields [('a', 97), ('b', 98), ('c', 99)] as expected.
Now, just swap the ord(el) out for an expression that takes the firs...