Is there a library function that can enumerate the prime numbers (in sequence) in Python? I found this question Fastest way to list all primes below N but I’d rather use someone else’s reliable library than roll my own. I’d be happy to do import math; for n in math.primes: Answer Since askin…
Tag: python
Get list of all routes defined in the Flask app
I have a complex Flask-based web app. There are lots of separate files with view functions. Their URLs are defined with the @app.route(‘/…’) decorator. Is there a way to get a list of all the routes that have been declared throughout my app? Perhaps there is some method I can call on the app…
Can you “stream” images to ffmpeg to construct a video, instead of saving them to disk?
My work recently involves programmatically making videos. In python, the typical workflow looks something like this: This workflow creates an image for each frame in the video and saves it to disk. After all images have been saved, ffmpeg is called to construct a video from all of the images. Saving the image…
pandas pivot dataframe to 3d data
There seem to be a lot of possibilities to pivot flat table data into a 3d array but I’m somehow not finding one that works: Suppose I have some data with columns=[‘name’, ‘type’, ‘date’, ‘value’]. When I try to pivot via I get Am I reading docs from dev p…
How to define an empty generator function?
A generator function can be defined by putting the yield keyword in the function’s body: How to define an empty generator function? The following code doesn’t work, since Python cannot know that it is supposed to be a generator function instead of a normal function: I could do something like this: But that wo…
Principal Component Analysis (PCA) in Python
I have a (26424 x 144) array and I want to perform PCA over it using Python. However, there is no particular place on the web that explains about how to achieve this task (There are some sites which just do PCA according to their own – there is no generalized way of doing so that I can find). Anybody
Pretty print 2D list?
Is there a simple, built-in way to print a 2D Python list as a 2D matrix? So this: would become something like I found the pprint module, but it doesn’t seem to do what I want. Answer To make things interesting, let’s try with a bigger matrix: Output: UPD: for multiline cells, something like this …
regular expression match starting clause with end
I want to be able to capture the value of an HTML attribute with a python regexp. currently I use My problem is that I want the regular expression to “remember” whether the attribute started with a single or a double quote. I found the bug in my current approach with the following attribute my reg…
Get time of last commit for Git repository files via Python?
I have a Git repository with several thousand files, and would like to get the date and time of the last commit for each individual file. Can this be done using Python (e.g., by using something like os.path.getmtime(path))? Answer An interesting question. Below is a quick and dirty implementation. I’ve …
Return JSON response from Flask view
I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response? Answer A view can directly return a Python dict or list and Flask will call jsonify automatically. For older Flask…