generator

Speed difference between iterating over generators and lists

烂漫一生 提交于 2019-12-23 12:43:39
问题 In the following trivial examples there are two functions that sort a list of random numbers. The first method passes sorted a generator expression, the second method creates a list first: import random l = [int(1000*random.random()) for i in xrange(10*6)] def sort_with_generator(): return sorted(a for a in l) def sort_with_list(): return sorted([a for a in l]) Benchmarking with line profiler indicates that the second option ( sort_with_list ) is about twice as fast as the generator

Is it impossible to tell if a function is a generator function if .bind() has been called on it?

二次信任 提交于 2019-12-23 11:00:19
问题 Looks like calling .bind(this) on any generator function breaks my ability to see if the function is a generator. Any ideas on how to fix this? var isGenerator = function(fn) { if(!fn) { return false; } var isGenerator = false; // Faster method first // Calling .bind(this) causes fn.constructor.name to be 'Function' if(fn.constructor.name === 'GeneratorFunction') { isGenerator = true; } // Slower method second // Calling .bind(this) causes this test to fail else if(/^function\s*\*/.test(fn

Python: how to make a recursive generator function

我只是一个虾纸丫 提交于 2019-12-23 09:56:26
问题 I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably large pretty fast (N=12 is just possible in the example below, N>12 uses too much memory). So I wanted to convert it to a generator function using yield instead, but I'm stuck. My working recursive function looks like this: def submodel_list(result, pat, current, maxn): ''' result is a list to

Python: how to make a recursive generator function

杀马特。学长 韩版系。学妹 提交于 2019-12-23 09:56:11
问题 I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably large pretty fast (N=12 is just possible in the example below, N>12 uses too much memory). So I wanted to convert it to a generator function using yield instead, but I'm stuck. My working recursive function looks like this: def submodel_list(result, pat, current, maxn): ''' result is a list to

Webstorm generator function not accepted

断了今生、忘了曾经 提交于 2019-12-23 09:20:11
问题 I'm trying to use iojs with koa , what works well. But Webstorm doesn't accept the generator functions as valid. /** gets marked as syntactically invalid code */ app.use(function *() { this.body = 'Hello World'; }); My actual version is Webstorm 9 . Is there maybe a workaround? I couldn't find a matching option for it. 回答1: Go to the Preferences > Languages & Frameworks > JavaScript and chose ECMAScript 6 for JavaScript language version . 来源: https://stackoverflow.com/questions/28131742

What is the Rails (>=3.1) generator syntax for creating a subclass model / scaffold?

梦想的初衷 提交于 2019-12-23 08:36:32
问题 What is the command line syntax for generating a subclass model or scaffold in Rails? rails g model Mysubclass my_field:string .... How do I specify the parent class? 回答1: You can use "--parent=ParentClass". Example: 1) Create a parent class "User". rails g scaffold User login:string 2) Create a child class "Teacher". rails g scaffold Teacher url:string --parent=User But remember: you still need to create migrations(adding columns in datatables) and change views(adding fields in forms). 回答2:

How to use PHP generators without foreach?

帅比萌擦擦* 提交于 2019-12-23 07:26:44
问题 Here's a simple JavaScript generator (via: http://blog.carbonfive.com/2013/12/01/hanging-up-on-callbacks-generators-in-ecmascript-6/) function* powGenerator() { var result = Math.pow(yield "a", yield "b"); return result; } var g = powGenerator(); console.log(g.next().value); // "a", from the first yield console.log(g.next(10).value); // "b", from the second console.log(g.next(2).value); // 100, the result I'm trying to model something similar with PHP but it's a bit of a headache. <?php

rails g scaffold for existing model and DB table

不羁的心 提交于 2019-12-23 07:16:16
问题 I would like to create a structure with rails g scaffold Article , but I have already created the table Articles and model Articles . Is there any way to do that? 回答1: rails generate scaffold_controller Article 回答2: Use rails generate scaffold_controller Article with parameters, example: rails g scaffold_controller Article title:string body:text Create views with attributes in tables 来源: https://stackoverflow.com/questions/9808668/rails-g-scaffold-for-existing-model-and-db-table

Android ToneGenerator example code [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 07:07:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Just wondering if anyone has come by any example code using the ToneGenerator class? I would like to generate tones in the frequency range of about 200Hz to 900Hz. Thanks... 来源: https://stackoverflow.com/questions/5199224/android-tonegenerator-example-code

Read, format, then write large CSV files

柔情痞子 提交于 2019-12-23 04:43:18
问题 I have fairly large csv files that I need to manipulate/amend line-by-line (as each line may require different amending rules) then write them out to another csv with the proper formatting. Currently, I have: import multiprocessing def read(buffer): pool = multiprocessing.Pool(4) with open("/path/to/file.csv", 'r') as f: while True: lines = pool.map(format_data, f.readlines(buffer)) if not lines: break yield lines def format_data(row): row = row.split(',') # Because readlines() returns a