Skip to content

Tag: python

Iterate the classes defined in a module imported dynamically

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 …

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…

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…

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…