yield

How to enable harmony syntax support in coffeescript?

妖精的绣舞 提交于 2019-12-21 02:27:08
问题 I used node.js(0.11.13) with --harmony flag and used function *() and yield keywords. I tried to simplify my development on node.js with help of coffeescript, so far it works great but I went into troubles with yield and declaring a generator - it complains about 'reserved keyword yield' . Any ideas? 回答1: Another way to open the gate to the black dimension is: co = require 'co' sleep = require 'co-sleep' co(`function*(){1` console.log 'hi!' `yield sleep(1000)` console.log 'bye!' `1}`)() It's

Python: How to use a generator to avoid sql memory issue

旧时模样 提交于 2019-12-20 16:51:40
问题 I have following method that access mysql database and the query get executed in a server that I don't have any access to change anything on regarding increasing memory. I am new to generators and started to read more about it and thought I could convert this to be use generator. def getUNames(self): globalUserQuery = ur'''SELECT gu_name FROM globaluser WHERE gu_locked = 0''' global_user_list = [] try: self.gdbCursor.execute(globalUserQuery) rows = self.gdbCursor.fetchall() for row in rows:

Python: How to use a generator to avoid sql memory issue

白昼怎懂夜的黑 提交于 2019-12-20 16:51:15
问题 I have following method that access mysql database and the query get executed in a server that I don't have any access to change anything on regarding increasing memory. I am new to generators and started to read more about it and thought I could convert this to be use generator. def getUNames(self): globalUserQuery = ur'''SELECT gu_name FROM globaluser WHERE gu_locked = 0''' global_user_list = [] try: self.gdbCursor.execute(globalUserQuery) rows = self.gdbCursor.fetchall() for row in rows:

Yield keyword value added?

非 Y 不嫁゛ 提交于 2019-12-20 10:11:20
问题 still trying to find where i would use the "yield" keyword in a real situation. I see this thread on the subject What is the yield keyword used for in C#? but in the accepted answer, they have this as an example where someone is iterating around Integers() public IEnumerable<int> Integers() { yield return 1; yield return 2; yield return 4; yield return 8; yield return 16; yield return 16777216; } but why not just use list<int> here instead. seems more straightforward.. 回答1: If you build and

Interesting use of the C# yield keyword in Nerd Dinner tutorial

廉价感情. 提交于 2019-12-20 09:58:01
问题 Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code: public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title required", "Title"); if (String.IsNullOrEmpty(Description)) yield return new RuleViolation("Description required","Description"); if (String.IsNullOrEmpty(HostedBy)) yield return new RuleViolation("HostedBy required", "HostedBy"); if (String.IsNullOrEmpty(Address))

Does the C# Yield free a lock?

拟墨画扇 提交于 2019-12-20 09:29:37
问题 I have the following method: public static IEnumerable<Dictionary<string, object>> GetRowsIter (this SqlCeResultSet resultSet) { // Make sure we don't multi thread the database. lock (Database) { if (resultSet.HasRows) { resultSet.Read(); do { var resultList = new Dictionary<string, object>(); for (int i = 0; i < resultSet.FieldCount; i++) { var value = resultSet.GetValue(i); resultList.Add(resultSet.GetName(i), value == DBNull.Value ? null : value); } yield return resultList; } while

yield break in Python

微笑、不失礼 提交于 2019-12-20 08:47:33
问题 According to answer to this question, yield break in C# is equivalent to return in Python. In the normal case, return indeed stops a generator. But if your function does nothing but return , you will get a None not an empty iterator, which is returned by yield break in C# def generate_nothing(): return for i in generate_nothing(): print i You will get a TypeError: 'NoneType' object is not iterable , but if I add and never run yield before return , this function returns what I expect. def

Yield in a recursive function

感情迁移 提交于 2019-12-20 08:22:56
问题 I am trying to do something to all the files under a given path. I don't want to collect all the file names beforehand then do something with them, so I tried this: import os import stat def explore(p): s = '' list = os.listdir(p) for a in list: path = p + '/' + a stat_info = os.lstat(path ) if stat.S_ISDIR(stat_info.st_mode): explore(path) else: yield path if __name__ == "__main__": for x in explore('.'): print '-->', x But this code skips over directories when it hits them, instead of

关于Python中的yield

老子叫甜甜 提交于 2019-12-19 19:15:54
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor)。 一、迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表、元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议的对象,Python中的迭代器协议就是有next方法的对象会前进到下一结果,而在一系列结果的末尾是,则会引发StopIteration。任何这类的对象在Python中都可以用for循环或其他遍历工具迭代,迭代工具内部会在每次迭代时调用next方法,并且捕捉StopIteration异常来确定何时离开。 使用迭代器一个显而易见的好处就是:每次只从对象中读取一条数据,不会造成内存的过大开销。 比如要逐行读取一个文件的内容,利用readlines()方法,我们可以这么写: 1 2 line .: line 这样虽然可以工作,但不是最好的方法。因为他实际上是把文件一次加载到内存中,然后逐行打印。当文件很大时,这个方法的内存开销就很大了。 利用file的迭代器,我们可以这样写: 1 2 line : line 这是最简单也是运行速度最快的写法,他并没显式的读取文件,而是利用迭代器每次读取下一行。 二、生成器

Python: Weird behavior while using `yield from`

蓝咒 提交于 2019-12-19 10:29:28
问题 In the following code, I have run into a RecursionError: maximum recursion depth exceeded . def unpack(given): for i in given: if hasattr(i, '__iter__'): yield from unpack(i) else: yield i some_list = ['a', ['b', 'c'], 'd'] unpacked = list(unpack(some_list)) This works fine if I use some_list = [1, [2, [3]]] , but not when I try it with strings. I suspect my lack of knowledge in python. Any guidance appreciated. 回答1: Strings are infinitely iterable. Even a one-character string is iterable.