generator

Why passing a list as a parameter performs better than passing a generator?

你离开我真会死。 提交于 2020-03-02 06:28:06
问题 I was making an answer for this question, and when I tested the timing for my solution I came up with a contradiction to what I thought was correct. The guy who made the question wanted to find a way to know how many different lists were contained within another list. (for more information, you can check the question) My answer was basically this function: def how_many_different_lists(lists): s = set(str(list_) for list_ in lists) return len(s) Now, the situation came when I measured the time

read corpus of text files in spacy

荒凉一梦 提交于 2020-02-24 08:45:09
问题 All the examples that I see for using spacy just read in a single text file (that is small in size). How does one load a corpus of text files into spacy? I can do this with textacy by pickling all the text in the corpus: docs = textacy.io.spacy.read_spacy_docs('E:/spacy/DICKENS/dick.pkl', lang='en') for doc in docs: print(doc) But I am not clear as to how to use this generator object (docs) for further analysis. Also, I would rather use spacy, not textacy. spacy also fails to read in a single

read corpus of text files in spacy

谁都会走 提交于 2020-02-24 08:44:09
问题 All the examples that I see for using spacy just read in a single text file (that is small in size). How does one load a corpus of text files into spacy? I can do this with textacy by pickling all the text in the corpus: docs = textacy.io.spacy.read_spacy_docs('E:/spacy/DICKENS/dick.pkl', lang='en') for doc in docs: print(doc) But I am not clear as to how to use this generator object (docs) for further analysis. Also, I would rather use spacy, not textacy. spacy also fails to read in a single

Control recursion on nested lists / strings

不羁岁月 提交于 2020-02-04 02:28:03
问题 Assume I have the following input: items = [1, 2, [3, 4], (5, 6), 'ciao', range(3), (i for i in range(3, 6))] and I want to perform some recursive operation on items . For the sake of simplicity, let's say I want to flatten items (but could be anything else), one way of doing this would be: def flatten(items, shallow=(str, bytes, bytearray)): for item in items: if isinstance(item, shallow): yield item else: try: yield from flatten(item) except TypeError: yield item this would produce: print

Control recursion on nested lists / strings

旧街凉风 提交于 2020-02-04 02:27:12
问题 Assume I have the following input: items = [1, 2, [3, 4], (5, 6), 'ciao', range(3), (i for i in range(3, 6))] and I want to perform some recursive operation on items . For the sake of simplicity, let's say I want to flatten items (but could be anything else), one way of doing this would be: def flatten(items, shallow=(str, bytes, bytearray)): for item in items: if isinstance(item, shallow): yield item else: try: yield from flatten(item) except TypeError: yield item this would produce: print

Optional yield or return in python3. How to?

荒凉一梦 提交于 2020-02-03 08:16:19
问题 I would like to have a function that can, optionally, return or yield the result. Here is an example. def f(option=True): ... for...: if option: yield result else: results.append(result) if not option: return results Of course, this doesn't work, I have tried with python3 and I always get a generator no matter what option value I set. As far I have understood, python checks the body of the function and if a yield is present, then the result will be a generator. Is there any way to get around

Python generator objects and .join

霸气de小男生 提交于 2020-01-31 04:32:10
问题 Just a fundamental question regarding python and .join() method: file1 = open(f1,"r") file2 = open(f2,"r") file3 = open("results","w") diff = difflib.Differ() result = diff.compare(file1.read(),file2.read()) file3.write("".join(result)), The above snippet of code yields a nice output stored in a file called "results", in string format, showing the differences between the two files line-by-line. However I notice that if I just print "result" without using .join(), the compiler returns a

MySQL custom primary key generator

一世执手 提交于 2020-01-30 09:29:07
问题 I have written an invoice module for our reservation system. So when I create a new invoice, I automatically generate a primary key through MySQL. However for the moment this is just a simple integer counting up. The problem is that we are obligated to have an invoice number in the form of "#year#id" where #year is e.g. 2013 and #id is an id that start every year back at 1. So e.g. 20131, 20132, 20133 and in 2014 it wil be 20141, 20142. How can I fix this with a custom primary key generator?

Joining a set of ordered-integer yielding Python iterators

99封情书 提交于 2020-01-28 03:33:07
问题 Here is a seemingly simple problem: given a list of iterators that yield sequences of integers in ascending order, write a concise generator that yields only the integers that appear in every sequence. After reading a few papers last night, I decided to hack up a completely minimal full text indexer in Python, as seen here (though that version is quite old now). My problem is with the search() function, which must iterate over each posting list and yield only the document IDs that appear on

Error in PHP Generator

旧街凉风 提交于 2020-01-25 13:08:13
问题 What is the best way to inform who use my generator function if something errors occurs, instead of writing weird return or raising exception like this piece of code function csv_file_generator($csvFilename, $delimiter = ";", $enclousure = '"') { if(($csvHandler = fopen($csvFilename, 'rb')) === false) { return; } while (($row = fgetcsv($csvHandler, 0, $delimiter, $enclousure)) !== false) { yield $row; } if (feof($csvHandler) === false) { return; } if (fclose($csvHandler) === false) { return;