Skip to content

Instantly share code, notes, and snippets.

@onjin
Created March 24, 2014 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onjin/9737643 to your computer and use it in GitHub Desktop.
Save onjin/9737643 to your computer and use it in GitHub Desktop.
tuple to namedtuple
from collections import namedtuple
# magic data accessed with magic numbers, f.i.:
# print "{} {}".format(row[2], row[3])
row = ('SS', 'asdf', 12.3, 'USD')
# let's create something self describing
Price = namedtuple('Price', ['symbol', 'name', 'amount', 'currency'])
# fill it from previous tuple
price = Price(*row)
# use it with joy
print "{} {}".format(price.amount, price.currency)
print price
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment