In this post I’ll provide an example of using session management in Flask. This is useful when you need to recover persistent data across different endpoints in your application. In this example, we set the permanent
attribute of the session
object to True
in order to ensure that the session data lasts indefinitely until it is cleared when the user accesses the root endpoint again. The best practice is to have a timeout on the session data.
Tag Archives: Python
Scraping Yahoo! Finance Data with BeautifulSoup
This weekend I wanted to work on collecting and plotting historical option contract prices. I used the following API call to pull option contract data from Yahoo!
curl -X GET "http://finance.yahoo.com/q/op?s=AAPL&m=2016-01" | cat > aapl
Continue reading Scraping Yahoo! Finance Data with BeautifulSoup
Using SVGs in IPython Notebooks
Sometimes I like working directly with SVGs in order to generate images from code. I like using the IPython Notebook for this because of the instant feedback. Here is an example for creating and viewing a small SVG within the IPython Notebook.
Running Nosetests with Multiple Versions of Python
This is a short post–just a TIL sort of thing. I had an issue today where the system Python was 2.6, but I needed to run a test on some code that used Python 2.7. After some jiggering I figured out how to test my Python 2.7 code. Rather that simply running,
nosetests tests.py
I ran, instead,
/usr/local/bin/python2.7 /usr/bin/nosetests tests.py
Boom. Now we’re testing.
Pretty-printing JSON in the Terminal
I use cURL quite a bit when debugging APIs and I found this neat trick for pretty-printing JSON output. Add the following line to your .bashrc
or .zshrc
file,
alias json="python -m json.tool"
Then you can pipe your cURL output through your new json
tool and print everything nicely,
curl -g http://some.domain/api/call | json
Using app2py for OS X Distribution
I thought that .pex files were the way to go for distributing Python applications to OS X users, but it was only partially successful. For one, users needed to reinstall Python with Homebrew, which isn’t difficult, it’s just awkward explaining to people that the Python distribution that ships with Mac isn’t the same Python distribution that exists in the wild. Their next question is invariably, “how will this affect the rest of my system?” I can’t answer that. I can’t guarantee that things will be future-proof.
And then I found app2py (or five dollars) which creates a Mac application bundle out of a Python script. It worked like a charm. The best thing is that you don’t have to write a stupid setup.py
file, it writes one for you. That’s five minutes of your precious time you can look at cat videos with, or whatever.
You can install this using pip, so that’s cool. Next, you do as the tutorial explains,
py2applet --make-setup MyApplication.py
This will create a setup.py
file. Finally, for deployment, you build the distribution with a non-Mac Python installation. (If you haven’t already run brew install python
you’ll want to run that.) My brew installation of the Python landed somewhere in /usr/local/Cellar/...
.
/usr/local/Cellar/python/2.7.8_2/bin/python setup.py py2app
This will create a standalone Python application that you can distribute painlessly to your Mac colleagues.
Simple Reservoir Algorithm in Python
I needed to sample lines from a file, and I found this SO post on on the Reservoir Algorithm. The code below lets you sample a single line from a file in O(n) time. What makes this code special, besides it’s linear time, simplicity and elegance, is the fact that the file you’re sampling from doesn’t need to fit in memory.
import random def random_line( filename ): hdl = open( filename, "r" ) line = next( hdl ) for num, aline in enumerate( hdl ): if random.randrange(num + 2): continue line = aline return line
Using TinyDB
I just found this neat Python module called TinyDB. It’s a document based database that is written in pure Python with ~1800(!) lines of code, 40% of which I hear is comments(!!). And there’s full(!!!) test coverage? Which implies that they performed any kind of testing at all? Crazy stuff. In this post I’ll provide a small sample of inserting recipes and querying them.
Linting in Python
Working with Timestamps from Logs
Sometimes you need to parse timestamps from logs (because you don’t have a splunk account) and you can use Python’s datetime module to do that. The trick is feeding the datetime.datetime.strptime()
function the correct format string or you get a weird ValueError: unconverted data remains
error message.