generator

Yield only as many are required from a generator

I wish to yield from a generator only as many items are required. In the following code a, b, c = itertools.count() I receive this exception: ValueError: too many values to unpack I've seen several related questions, however I have zero interest in the remaining items from the generator, I only wish to receive as many as I ask for...

invoking yield for a generator in another function

suppose I have some manager object. This object's API has a main_hook function, that gets another function f as it's argument, and runs the given f in a loop, doing some stuff in between each iteration: def main_hook(self,f): while (self.shouldContinue()): #do some preparations f(self) #do some tear down No...

Symfony Admin Generator - Raw HTML in Form Filter

Hi there! At my company, we had developed a athletes management solution, were each athlete is inserted in the application by administrators users. For the referred solution, it was used the symfony admin generator. On the second project iteration, one of the clients request was to turn the printed athletes list more legible. To accomp...

generate 100 color combination html

Hello guys, i have an html template with 4 basic color Textheader, page basckgroung, navebar color,content backbround. I want to generate different color styles so i can apply it dynamically and get different color scheme i have tried css onine color chemers and the rest, but am looking of a service that will eneble me preview the colo...

Dynamically generating a generator function from a blob of text

(Simplified from an excessively verbose question I posted earlier!) Given a Python string containing valid Python code that contains a "yield" statement, how can I construct a generator that exec's that string? For example, given the string: code_string = """for x in range(0, 10): yield x """ I want to construct a generator f th...

Symfony Admin Generator - New / Update

Hi there! I had used the symfony admin generator to create an web application for athletes management. One of the last client's requirement was to add a feature to notice the user and send an e-mail to the administrators when an athlete with the same number is inserted on the database. Until now, the column number of the Athlete table h...

Free/OSS Data model diagram generator from DDL

I am looking for some free/oss to reverse engineer from DDL a data model diagram. My database is Oracle if that matters. Do any exist? ...

Python: get number of items in generator without storing the items

I have a generator for a large set of items. I want to iterate through them once, outputting them to a file. However, with the file format I currently have, I first have to output the number of items I have. I don't want to build a list of the items in memory, as there are too many of them and that would take a lot of time and memory. Is...

Stop generator from block in Python

I have a generator that yields nodes from a Directed Acyclic Graph (DAG), depth first: def depth_first_search(self): yield self, 0 # root for child in self.get_child_nodes(): for node, depth in child.depth_first_search(): yield node, depth+1 I can iterate over the nodes like this for node, depth in graph.d...

Algorithm to generate a Turing Machine from a Regular Expression

Hi, I'm developing a software to generate a Turing Machine from a regular expression. [ EDIT: To clarify, the OP wants to take a regular expression as input, and programmatically generate a Turing Machine to perform the same task. OP is seeking to perform the task of creating a TM from a regular expression, not using a regular expre...

Generating combination of letters

Hi All, Given a set of letters, say from A.. F, how can one generate a combination of these letters for a specific length. i.e for length 4, generate all string containing these letters {AAAA, ABCD, ...} (duplicates included). I am not able to understand how to come out with a code that does it.This is pertaining to the Mastermind game ...

random number generator dev/random

I read that the random number generator dev/random on Mac and Solaris includes 160 bits of entropy. What can I do, if I need more entropy, for example, 200 bits? Thanks in advance ...

How to convert generator or iterator to list recursively

I want to convert generator or iterator to list recursively. I wrote a code in below, but it looks naive and ugly, and may be dropped case in doctest. Q1. Help me good version. Q2. How to specify object is immutable or not? import itertools def isiterable(datum): return hasattr(datum, '__iter__') def issubscriptable(datum): ...

How to make an informational web site from a git repository

I would like to see some statistic and visual states of my git repository. Is there any nice tools around? I found http://gitstats.sourceforge.net/ but I would like to know if there are alternatives. ...

What is the most pythonic way to have a generator expression executed?

More and more features of Python move to be "lazy executable", like generator expressions and other kind of iterators. Sometimes, however, I see myself wanting to roll a one liner "for" loop, just to perform some action. What would be the most pythonic thing to get the loop actually executed? For example: a = open("numbers.txt", "w") ...

How to join two generators in Python?

Dear all, I want to change the following code for directory, dirs, files in os.walk(directory_1): do_something() for directory, dirs, files in os.walk(directory_2): do_something() to this code: for directory, dirs, files in os.walk(directory_1) + os.walk(directory_2): do_something() I get the error: unsupported op...

Can iterator be restored and can its value/status be assigned?

Hi All, I have below snippet which use the generator to give the new ID ... def __init__(self, id_generator = None): if id_generator is None: id_generator = 0 if isinstance(id_generator, int): import itertools self._generator = itertools.count(id_generator) else: self....

Scapy SYN send on our own IP address

Hello, I tried to send SYN packets on my local network and monitoring them with Wireshark and everything works just fine, except when i try to send a packet to my own ip address it "seems" to work because it says Sent 1 packet, but it is not really sent, i can't see the packet in Wireshark nor any answers to the packet. My setup is a co...

Get a subset of a generator

I have a generator function and want to get the first ten items from it; my first attempt was: my_generator()[:10] This doesn't work because generators aren't subscriptable, as the error tells me. Right now I have worked around that with: list(my_generator())[:10] This works since it converts the generator to a list; however, it's ...

Preventing the browser caching a linked file

I've been tasked to maintain a PHP website with a function which automatically generates RTF files and provides a link to download. Each time the previously generated file is overwritten by the new one. However, it seems that upon attempting to download the generated file, the browser will sometimes retrieve a cached version which is no...