Python 3 Map function is not Calling up function

map() returns an iterator, and will not process elements until you ask it to. Turn it into a list to force all elements to be processed: list(map(self.do_someting,range(10))) or use collections.deque() with the length set to 0 to not produce a list if you don’t need the map output: from collections import deque deque(map(self.do_someting, range(10))) but … Read more

Create a list of tuples with adjacent list elements if a condition is true

Cleaner Pythonic approach: >>> [(x,y) for x,y in zip(myList, myList[1:]) if y == 9] [(8, 9), (4, 9), (7, 9)] What is the code above doing: zip(some_list, some_list[1:]) would generate a list of pairs of adjacent elements. Now with that tuple, filter on the condition that the second element is equal to 9. You’re done … Read more

Can a conversion to string raise an error? [duplicate]

If you encounter a custom class that explicitly raises an exception in __str__ (or __repr__ if __str__ is not defined). Or, for example a class that returns a bytes object from __str__: class Foo: def __str__(self): return b” str(Foo()) # TypeError: __str__ returned non-string (type bytes) But personally, I have never seen this and I’m … Read more

Send Post Request in Scrapy

The answer above do not really solved the problem. They are sending the data as paramters instead of JSON data as the body of the request. From http://bajiecc.cc/questions/1135255/scrapy-formrequest-sending-json: my_data = {‘field1’: ‘value1’, ‘field2’: ‘value2′} request = scrapy.Request( url, method=’POST’, body=json.dumps(my_data), headers={‘Content-Type’:’application/json’} )

Python 3.2: How to pass a dictionary into str.format()

This does the job: stats = { ‘copied’: 5, ‘skipped’: 14 } print( ‘Copied: {copied}, Skipped: {skipped}’.format( **stats ) ) #use ** to “unpack” a dictionary For more info please refer to: http://docs.python.org/py3k/library/string.html#format-examples and http://docs.python.org/py3k/tutorial/controlflow.html#keyword-arguments