generator

Does this benchmark seem relevant?

自古美人都是妖i 提交于 2020-01-16 18:39:44
问题 I am trying to benchmark a few method of itertools against generators and list comprehensions. The idea is that I want to build an iterator by filtering some entries from a base list. Here is the code I came up with(Edited after accepted answer): from itertools import ifilter import collections import random import os from timeit import Timer os.system('cls') # define large arrays listArrays = [xrange(100), xrange(1000), xrange(10000), xrange(100000)] #Number of element to be filtered out nb

NPM installation issues on Mac OS Mavericks

你。 提交于 2020-01-15 12:15:10
问题 I am trying to get to grips with AngularJS, NodeJS and Ruby on Rails. When trying to install npm packages like yeoman, grunt etc. I receive an error in my Terminal which I have listed below: npm ERR! Error: EACCES, open '/Users/Jabane/.npm/glob/3.2.11/package/package.json.25961' npm ERR! { [Error: EACCES, open '/Users/Jabane/.npm/glob/3.2.11/package/package.json.25961'] npm ERR! errno: 3, npm ERR! code: 'EACCES', npm ERR! path: '/Users/Jabane/.npm/glob/3.2.11/package/package.json.25961', npm

Python - Why is this code considered a generator?

拈花ヽ惹草 提交于 2020-01-15 03:45:06
问题 I have a list called 'mb', its format is: ['Company Name', 'Rep', Mth 1 Calls, Mth 1 Inv Totals, Mth 1 Inv Vol, Mth 2 ...And so on In the below code I simply append a new list of 38 0's. This is fine. However in the next line I get an error: 'generator' object does not support item assignment Can anyone tell me: 1) how to correct this error, and 2) why len(mb)-1 below is considered a generator. Note: row[0] is merely a 'Company Name' held in another list. mb.append(0 for x in range(38)) mb

How to create a custom generator class that is correctly garbage collected

隐身守侯 提交于 2020-01-15 02:37:13
问题 I'm trying to write a class in Python that behaves as a generator object, particularly in that when it's garbage collected .close() is called on it. That's important because it means that when the generator is interrupted I can make sure it'll clean up after itself, for example closing files or releasing locks. Here's some explanatory code: If you interupt a generator, then when it's garbage collected, Python calls .close() on the generator object, which throws a GeneratorExit error into the

How do I display a random phrase from a list when a button is clicked in a web page?

僤鯓⒐⒋嵵緔 提交于 2020-01-14 19:22:07
问题 I am creating a web page where someone can visit it. They type a question in a field and click a button, and a response is passed back to them. (Kind of like a magic 8 ball). What I'm trying to do is set it up something like this: http://img585.imageshack.us/img585/997/layoutoi.png I'm still new to hand-coding things - I have a book on HTML/CSS and one on PHP, which lay still unread, so I'll probably need a step-by-step process. (I have a host and everything, so that's already taken care of.)

What's the difference between the call/return protocol of oldstyle and newstyle coroutines in Python?

旧巷老猫 提交于 2020-01-14 13:02:42
问题 I'm transitioning from old-style coroutines (where 'yield' returns a value supplied by 'send', but which are otherwise essentially generators) to new-style coroutines with 'async def' and 'await'. There are a couple of things that really puzzle me. Consider the following old-style coroutine that computes the running average of numbers supplied to it by 'send', at each point returning the mean-so-far. (This example is from Chapter 16 of Fluent Python by Luciano Ramalho.) def averager(): total

hibernate generator increment conflict with table auto increment

佐手、 提交于 2020-01-14 04:39:07
问题 I set generator as 'increment' in hbm file, and also i put auto_increment thing in database table also. This creteria showing issue .... This both thing is conflicting ? ? 回答1: Yes they are conflicting, makes no sense.. When using hibernate built-in generator 'increment', it's hibernate that generate the identifiers by counting from the maximum primary key value at startup, not safe in multi JVM and using the database AUTO_INCREMENT it's the database that generate a unique identity for new

Why are generators faster?

試著忘記壹切 提交于 2020-01-14 01:58:29
问题 I understand that generators are faster than iterators. I also understand that generators can be implemented using for loop syntax. For example: import time startT = time.time() def myGen(n): for i in range(n): yield x def myIter(n): for i in range(n): pass def main(): n=100 startT=time.time() myIter(n) print 'myIter took ', time.time() - startT startT=time.time() myGen(n) print 'myGen(n) took ', time.time() - startT This is just one example of the results: myIter took 0.09234782 myGen(n)

How two consecutive yield statement work in python?

痴心易碎 提交于 2020-01-13 08:43:26
问题 I stumble upon this code from pymotw.com in merging and splitting section. from itertools import * def make_iterables_to_chain(): yield [1, 2, 3] yield ['a', 'b', 'c'] for i in chain.from_iterable(make_iterables_to_chain()): print(i, end=' ') print() I can not understand how make_iterables_to_chain() is working. It contains two yield statement, how does it work? I know how generators work but there but there was only single yield statement. Help, please! 回答1: The same way a single yield works

How two consecutive yield statement work in python?

前提是你 提交于 2020-01-13 08:42:32
问题 I stumble upon this code from pymotw.com in merging and splitting section. from itertools import * def make_iterables_to_chain(): yield [1, 2, 3] yield ['a', 'b', 'c'] for i in chain.from_iterable(make_iterables_to_chain()): print(i, end=' ') print() I can not understand how make_iterables_to_chain() is working. It contains two yield statement, how does it work? I know how generators work but there but there was only single yield statement. Help, please! 回答1: The same way a single yield works