I have a module from a child package that is imported dynamically; how can I iterate over the classes that it contains? I have been importing the module and listing the names like this: This only prints module attributes and not the class types that the module defines: It seems that my classes are not in the …
Tag: python
Fastest way to check if a value exists in a list
What is the fastest way to check if a value exists in a very large list? Answer Clearest and fastest way to do it. You can also consider using a set, but constructing that set from your list may take more time than faster membership testing will save. The only way to be certain is to benchmark well. (this als…
Python. IOError: [Errno 13] Permission denied: when i’m copying file
I have two folders: In, Out – it is not system folder on disk D: – Windows 7. Out contain “myfile.txt” I run the following command in python: What’s the problem? Answer Read the docs: shutil.copyfile(src, dst) Copy the contents (no metadata) of the file named src to a file named …
Django – how to create a file and save it to a model’s FileField?
Here’s my model. What I want to do is generate a new file and overwrite the existing one whenever a model instance is saved: I see lots of documentation about how to upload a file. But how do I generate a file, assign it to a model field and have Django store it in the right place? Answer You want
Difference between Python datetime vs time modules
I am trying to figure out the differences between the datetime and time modules, and what each should be used for. I know that datetime provides both dates and time. What is the use of the time module? Examples would be appreciated and differences concerning timezones would especially be of interest. Answer T…
Create dynamic URLs in Flask with url_for()
Half of my Flask routes requires a variable say, /<variable>/add or /<variable>/remove. How do I create links to those locations? url_for() takes one argument for the function to route to but I can’t add arguments? Answer It takes keyword arguments for the variables: The flask-server would h…
python subprocess with gzip
I am trying to stream data through a subprocess, gzip it and write to a file. The following works. I wonder if it is possible to use python’s native gzip library instead. THE QUESTION: How do I do this instead .. where the gzip package of python is used? I’m mostly curious to know why the followin…
Django – Template display model verbose_names & objects
I need to display several models name & objects in a template Here is my view And my template Of course objs._meta.verbose_name doesn’t work Is there a way to access to this verbose name without having to create a function for each model or to assign the value from the view for each model ? Answer F…
memory-efficient built-in SqlAlchemy iterator/generator?
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()