I have a ~10M record MySQL table that I interface with using SqlAlchemy. I have found that queries on large subsets of this table will consume too much memory even though I thought I was using a built-in generator that intelligently fetched bite-sized chunks of the dataset: To avoid this, I find I have to bui…
How do you find out what the “system default encoding” is?
The documentation for fileobject.encoding mentions that it can be None, and in that case, the “system default encoding” is used. How can I find out what this encoding is? Answer You should use sys.getdefaultencoding()
Make matplotlib autoscaling ignore some of the plots
I use matplotib’s Axes API to plot some figures. One of the lines I plot represents the theoretical expected line. It has no meaning outside of the original y and x limits. What I want, is for matlplotlib to ignore it when autoscaling the limits. What I used to do, is to check what are the current limit…
Add single element to array in numpy
I have a numpy array containing: I want to create an array containing: That is, I want to add the first element on to the end of the array. I have tried the obvious: But I get an error saying ValueError: arrays must have same number of dimensions I don’t understand this – the arrays are both just …
Print a list in reverse order with range()?
How can you produce the following list with range() in Python? Answer use reversed() function: It’s much more meaningful. Update: If you want it to be a list (as btk pointed out): Update: If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step) F…
List from string of digits
I’m getting a string from input() which consists of digits separated by spaces (1 5 6 3). First I’m trying to check that the input only consists of digits with the isdigit() function but because of the spaces I can’t get it to work. Then I convert the string to a list using the split() funct…
What does a . in an import statement in Python mean?
I’m looking over the code for Python’s multiprocessing module, and it contains this line: instead of the subtle difference being the period before _multiprocessing. What does that mean? Why the period? Answer That’s the syntax for explicit relative imports. It means import from the current p…
Automatically call common initialization code without creating __init__.py file
I have two directories in my project: “src” contains my polished code, and “scripts” contains one-off Python scripts. I would like all the scripts to have “../src” added to their sys.path, so that they can access the modules under the “src” tree. One way to do t…
How to replace values at specific indexes of a python list?
If I have a list: And then declare two other lists: How can I take to_modify’s elements as index to indexes, then set corresponding elements in to_modify to replacements, i.e. after running, indexes should be [0,0,3,0,1,0]. Apparently, I can do this through a for loop: But is there other way to do this?…
How to merge dictionaries of dictionaries?
I need to merge multiple dictionaries, here’s what I have for instance: With A B C and D being leaves of the tree, like {“info1″:”value”, “info2″:”value2”} There is an unknown level(depth) of dictionaries, it could be {2:{“c”:{“z”:{…