Functional Programming in Python - A Sampler (Cliff)
Functional programming languages make big promises
Makes it easy to write robust, modular, decoupled code
They’re easy to test (since output is solely dependent on input)
They make programming fun
Do they deliver?
Didn’t find it worth the switching costs, personally.
Has a problem with the no I/O thing
I don’t think he gets the “no state” thing; there’s no mutable, shared state,
not no state at all.
Hallmarks of Functional Code
Functions are first-class objects
Functions are typically very small
Closures
Partials/curried functions
Recursion
Immutable
Various Places that Functional Idioms Can Be Useful
He’s using templating as an example of how to use partials; that’s
interesting. I’m going to have to look into this.
Interesting point about Python 3’s crazy argument ordering options and
partials (how does partial application work with some of the wilder ones?)
Takeaways
Maybe use Python in a more functional style
Admits he doesn’t do this currently, so it’s aspirational
Use list comprehensions more
Isolating I/O looks like a good idea
Closures, decorators, and partials are a yes
Still doesn’t like recursion
Random observations by me
I didn’t realize iPython Notebook had presentation capabilities; I’m going to
have to play with that.
GNU Radio and Python (Andrew Henshaw)
According to Andrew, it’s more him showing off instead of demonstrating
programming techniques.
GNU Radio is for signal processing
It’s a C++ kernel wrapped in beautiful Python goodness
It’s dataflow-oriented
Software-defined radio is apparently a big thing, lately
Apparently, hardware has gotten fast enough that software Digital
Signal Processing (DSP)
is fast enough to replace dedicated hardware, which brings all the
standard flexibility wins that software tends to provide over dedicated
hardware.
There’s some crazy $20 dongle that can work as a receiver for just about
every frequency band you could want, and feed it into software-defined radio
Fancy: you can draw a dataflow diagram to define your
DSP pipeline.
That’s pretty awesome: assuming I didn’t miss anything, he just ran
a touch-tone dialer program, in Python, generated from his fancy-pants
dataflow diagram. It was a 7.
The gremlins are out in full force (technical presentations are like crack
for these things); his GUI example with a volume slider suddenly stopped
working despite working earlier.
My computer’s battery is now toast, so you’ll have to talk to him if you want
the details of the presentation/demo.
distribute and setuptools have finally merged (see also:
the announcement).
The Atlanta chapter of PyLadies will be giving the presentations on their
1-year anniversary (or the closest night to their 1-year anniversary).
J.R. is starting a Smyrna tech group.
One-liner fizz-buzz (Doug Hellmann)
I’m excited; I don’t think I’ve ever seen Doug golfing code before.
He starts by discussing somebody else’s golfed version; I has a sad.
But here’s his solution. I dig it.
PANDAS: Python Data Analysis Library (Andy Henshaw)
Interesting; why do a custom thing to drive PowerPoint from iPython. Isn’t that a pretty close parallel to iPython Notebook?
First primitive: a Series
It’s an array-like that contains data and labels (or indices).
The data must be of the same type.
Asking for multiple labels gives you a view into the series; providing a single label gives you the value for that label.
Labels don’t have to be unique; if you have multiple values for a single
label, then requesting that label’s value gives you a view, much like
asking for multiple labels’ values.
Can accommodate incomplete data; labels without values represent the value
as NaN.
NaN is used even for a Series of strings.
Data is automatically aligned (that’s pretty sweet).
Second primitive: a Dataframe
It’s tabular data, a la a spreadsheet.
Easy to create one from a dict of equal-length lists.
Or you could pass a dict of dicts.
You can add new columns to existing Dataframes using either direct assignment or calculation.
You can re-index.
Bring on the stats!
It’s smart about figuring out how to apply descriptive stats like sum to columns/rows.
All sorts of useful import functionality for structured data, e.g., JSON, CSV, et al.
OK, once he started talking about crosstabs, I was in the territory where I have a hard time keeping up during a talk.
Plotting w/ matplotlib is just cool. I need to get better at this sort of stuff.
TimeSeries sounds totally awesome.
[None]: True or False? (Cliff Kachinske)
I wonder how you’d address this in a general way.
The consensus in the room seems to be that any and all are the best way
to go.
Creating Python Bindings for C++ using boost::python (Aleksey Vitebskiy)
When it comes to text editing, it doesn’t get much better than
vim—and by extension, MacVim. If this
is the first time you’ve heard of vim, or the first time you’ve
seriously considered using it, please stop now and go read
Coming Home to Vim, by Steve Losh1. That
captures the Zen of Vim better than anything else I’ve read.
If you’re still around (or back), then congratulations: you have great
taste in editors. Now on to one of the most irritating things about
vim: getting your vim all tricked out with just the right set of
plugins, color schemes, and keybindings, then getting stuck on a remote
server somewhere with a vanilla version of vim, or maybe even a
different vi clone, like nvi. Enter bcvi.
bcvi is a clever little tool that basically copies the file you want
to edit to your machine, invokes gvim on it, then copies it back when
you are done. Presto: you get to edit in the comforts of your own,
highly optimized vim environment.
All you need to do to get bcvi is install it via CPAN;
it even comes with a couple of helper commands to give you handy shell
aliases, etc. But if you’re running OSX , oh-my-zsh
(if you’re not, go read Steve’s My Extravagant Zsh Prompt
for a good explanation of just how awesome it can be), and MacVim, then
there are a couple of things that you’ll probably want to do.
As an aside, you might want to update CPAN so it stops nagging you every
time you try to install something. The default perl that ships with
OSX is under /usr/bin, so upgrading CPAN is going to require sudo.
It’s also rather old. If you’d rather, you can simply install a current
perl via Homebrew. This will get you up-to-date and allow
you to upgrade CPAN without needing sudo. You could also look into
local::lib, which I’ve used with success on servers.
The instructions I used (NOTE: some of these are probably unique to my
box, but I don’t have a spare handy to test) are:
123
$ brew tap homebrew/dupes
$ brew install perl
$ brew link --overwrite perl
With that out of the way, here’s how to get started with bcvi on a Mac:
Install bcvi. This is as simple as install App::BCVI from the
CPAN prompt.
To make bcvi use MacVim (or possibly even work, if you don’t have
an X-based version of vim), you need to symlink gvim to mvim.
Since I have my mvim situated in /usr/local/bin, that’s where I
put the gvim symlink to it, as well.
Get those lovely aliases. Execute bcvi --add-aliases. NOTE: This
only adds them to .bashrc, so if you’re using ZSH, you need to copy
and paste them to your .zshrc.
Start the listener process. This is as simple as
bcvi --listener &, but I would never remember to type this until
the first time I tried to use bcvi and it errored out. Instead, I
use a launchd job. You can get it from
this BitBucket repo. If you’ve never used
launchd jobs before, instructions are included in the README.
Install bcvi on the remote servers where you’d like to use it using
bcvi --install hostname. This will install bcvi in $HOME/bin, so
permissions shouldn’t be a problem.
Finally, I like to add an alias so I don’t have to type
bcvi --wrap-ssh -- hostname every time I connect to a server and
want to have access to bcvi. I added the following to my .zshrc:
1
alias ssh-bcvi='bcvi --wrap-ssh --'
At this point, you should be able to ssh-bcvi to your remote server,
find the file you need to edit, and vi filename to edit it in the
comfort of your own vim.
Back to flipping out…
As a matter of fact, just go read everything Steve writes. I steal ideas from him all the time.↩
Fun gotcha: A trailing , will often create you a tuple, usually when
you really don’t want one. If you start seeing complaints about
mismatches in the number of arguments, it’s a good idea to look for
trailing commas.
What really frustrates me is when I try to get clever and do something
like this only to find out that Python isn’t creating me a tuple in
this particular instance (for whatever reason).
A/B Testing with Cleaver (Ryan Petrello)
Didn’t use Visual Website Optimizer, Google Analytics, Optimizely, etc.
because they wanted a Python-based solution that targeted developers,
not marketers.
Found plenty of solutions, but they were all opinionated (to the point
of requiring specific frameworks). This might not be a show-stopper
for me, because tight integration usually means less config to mess
with.
They wound up creating Cleaver (MIT licensed?), which
only requires WSGI, basically.
Basically, you use WSGI middleware to add tracking.
It has a number of pluggable backends to store the experiment data.
You can add a weight to the tuple, easy-peasy. Does not currently
support functions, but he seems confident it can be extended fairly
easily.
Comes bundled with a lightweight web UI for viewing the results.
It’s cool. I wonder how much of the statistical heavy lifting it does
(statistical power, significance, correlation with other changes, etc.)
Ryan says: They don’t do power, but it does do significance.
I missed this discussion while I was reading the README (curses!),
but I believe the control is just the first option.
If we had a more serious doc project, I’d be writing custom roles left and
right; they’re simple, yet powerful.
The implementation is a simple function that returns a list of nodes
Nice: it gives you error helpers; nice to see they thought about the failure
cases
You use the setup function to wire your extension into the Sphinx machinery
Directives
More powerful than roles, at the expense of lots more pieces to keep track of
These are things like the .. note:: directive in vanilla Sphinx
I like Doug’s sample directive; nothing like using a database to build your
docs to make sure your docs are legit.
Since it’s so much more sophisticated, the implementation is a class.
You’ll probably need to implement the run method, if nothing else. Like
a role processor, this function returns a list of nodes.
Sphinx-specific extensions
Extending the Sphinx Builder
This is how Doug made his spellchecker for Sphinx docs.
The concepts here aren’t hard, but you’re gonna get really familiar with
Sphinx internals if you start doing this.
A good thing to remember: set the exit code to something that is not 0 when
you encounter an error. Remember, only YOU can fight crappy CLI experiences.
### Simple Calculator Applications Using Bottle.py and Google App Engine
Lots of introductory material explaining how analytics works, some of the
problems you need to solve to make it work, etc.
Google App Engine (GAE) is a Platform as
a Service (PaaS).
Pros
You can get started for free.
You get to run on Google’s infrastructure.
Cons
You run on Google’s infrastructure:
You get BigTable, and (this is apparently new) some sort of MySQL in the
cloud thing. No traditional RDBMS.
Short-lived requests; they will kill your request if you take more than 60
seconds or so. They have at least added something called a “back-end
instance” that works around this, at the expense of the auto-scaling feature.
No writes to the filesystem.
No arbitrary access, e.g., FTP.
You’re pretty tied to GAE.
How did we do last month? ()
### or Fun with date.date, timedelta
### or dateutil to the rescue
Ever start trying to do something useful with dates in Python? It sucked, huh.
Time to write some boilerplate code! But wait! Surely Someone Has Already Solved This (SSHAST). Enter dateutil.