Vagrant is a tool that you can use to set up, configure, and access a VM through the command line. This is a life changer. I love it. In this post I’ll walk through setting up an OEL6 virtual machine, installing a non-ancient version of Python, and configuring the port forwarding so that you can use it for backend web development. (The port forwarding is not obvious on RHEL/OEL.)
Recently, I thought I needed to use
simpleldap–it turned out that I instead needed to reconfigure NGINX. At any rate, this was my experience with
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.
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
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.
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,
I ran, instead,
/usr/local/bin/python2.7 /usr/bin/nosetests tests.py
Boom. Now we’re testing.
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
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
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/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.
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