Just came across a cool little decorator use.
I'm using Python fabric for Djangy.com deployment. We have a production server, a stage server, and a local VM for development. The IP addresses for the two servers stay the same, while my VM's IP address changes according to whatever network I happen to be on.
Ideally, I'd like to run any of the following:
Now, I could simply alias 'stage' and 'production' to their respective ip addresses. But where's the fun in that? Instead, I added these two strings and their corresponding IP addresses into constants and then put them into a HOSTS dictionary:
Then I created a python decorator that checks the host argument and swaps it out for the right address, but only if it's included in the HOSTS dictionary. Otherwise, it lets it pass through unchanged.
The new thing I learned here was the ability to unpack the calling arguments in a decorator. You can specify them individually and then say *args to denote "the rest" of the arguments. Useful to know! Now, I can call any of those hosts by their aliases, or I can simply specify a hostname or IP address.
If you run into this error when you first try to clone your gitosis-admin repository, the solution is to run:
git --exec-path
and copy that into your gitosis user's .bashrc. (Mine was /usr/local/git/libexec/git-core):
PATH=/usr/local/git/libexec/git-core:$PATH
Done!
Yes, another rails post. This time I'm sharing a solution.
I have a class I wrote to utilize the Delayed::Job functionality of Heroku. I tried to use the standard rails logger.info("some error") and it kept throwing exceptions.
The solution is to add the following line to your class:
logger = RAILS_DEFAULT_LOGGER
Everything is perfect after that. (Well, okay, rails is far from perfect, but whatever).
My newest project, Footprint Analytics, led me to try to be more productive by using Ruby on Rails. I'd tried RoR 4 years ago with a project for ACM@UIUC called WebChalk, and I've improved by leaps and bounds in my hacking skills since then. I've mostly taught myself how to solve all my programming problems with python, I "think" in Python when I think about code.
Ruby shares quite a bit with Python, which has been written about elsewhere on the web. However, a huge gripe of mine with rails is the lack of organization and namespaces. They exist, but they're not as strictly enforced.
For example, let's say I'm writing a client to access the twitter api. Their OAauth requires a consumer key and a consumer secret. I have two of these, one that redirects back to my localhost for development and one that redirects back to the actual footprintanalytics.com URL. In Django, I can put these into different configuration files (development.py and production.py) and point my server to those. Then, in my controller, I just say "config.CONSUMER_KEY" and I grab the correct one, regardless of whether or not I'm developing or in a production environment.
You can do the same thing in Rails, but with one caveat: the config constants are global variables. I don't say "config.CONSUMER_KEY", I just say CONSUMER_KEY.
If I'm looking at someone else's code and I see a constant, I have zero clue where that constant is defined. I must resort to grep-ing through the source tree to find all instances, and you better hope there aren't many of them. In Django (Pylons, web.py, and turbogears are all similar enough), it's utterly clear where everything comes from because only locally or class-scoped variables don't have namespaces appened to the beginning of them.
Python's "Zen" has the line "Namespaces are one honking great idea -- let's do more of those!". Ruby/Rails seems to be missing this. I don't like it.
I went to a record store recently and went from listening station to listening station, writing down each of the albums currently playing. It's old fashioned, but it's a good way to find new / forgotten music. Here are the results:
Neil Young - Harvest (1972)

Erykah Badu - Baduizm (1997)

Mazzy Star - So Tonight That I Might See (1993)

KMFDM - Xtort (1996)

Massive Attack - Heligoland (2010)

Lisa Hannigan - Sea Stew (2008) thanks to Caroline at The Decanting Room

Femi Kuti - Femi Kuti (1995)

Humble Pie - Smokin' (1972)

This was an experiment, and frankly, a successful one. I'll try to keep a regular schedule of updates, so we'll see what happens. Oh, and add me on Last.FM if you have an account: http://www.last.fm/user/davezor94
Reading rand's latest post (http://www.randsinrepose.com/archives/2010/06/28/how_to_write_a_book.html), he mentions a tool I use every once in awhile called WriteRoom. Here's what it looks like:
Rands mentions that, during the process of writing a book, distractions appear everywhere and become the truest obstacles to your productivity and success. I agree completely. However, it struck me that MOST people who use WriteRoom probably aren't successful authors. And I think WriteRoom knows that. So they add these features so that their users can tweak and fiddle with them all day and FEEL productive. Only a small fraction of a given software product's users are actually going to be using that product to its fullest potential, so I think alot of software products add features not just to make their software more (or less) useful, but to keep their users busy.Dan Freedman posted a nice perl script for finding all of a username's verified followers on twitter. I ran into a problem (because I don't know perl), so I decided to port it to python for a fun exercise:
#! /usr/bin/python
import urllib2
import sys
import json
def get_cursor(cursor = -1):
return urllib2.urlopen("%s&cursor=%s" % (host, cursor)).read()
def main():
# start off
twitter_json = get_cursor()
while True:
from_json = json.loads(twitter_json)
if from_json['next_cursor'] == 0:
break
followers = from_json['users']
for follower in followers:
if follower['verified'] == True:
print follower['screen_name'], follower['name']
twitter_json = get_cursor(from_json['next_cursor'])
if len(sys.argv) < 2:
print "Usage: python verified_followers.py <username>"
sys.exit(1)
host = "http://api.twitter.com/1/statuses/followers.json?screen_name=%s" % str(sys.argv[1])
main()
It will print username and then real name on the same line.