Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Created September 1, 2014 10:44
Show Gist options
  • Save jorge-lavin/acbb4aef37feedd7b500 to your computer and use it in GitHub Desktop.
Save jorge-lavin/acbb4aef37feedd7b500 to your computer and use it in GitHub Desktop.
# WARNING Untested
# Assume the 'employees' table has the following columns: first_name, last_name, department,
# manager, salary, hire_date
from collections import namedtuple
EmployeeRow = namedtuple('EmployeeRow', ['first_name', 'last_name', 'department',
'manager', 'salary', 'hire_date'])
EMPLOYEE_INFO_FMT = '{last_name}, {first_name} was hired on \
e_date} (for ${salary} per annum) into the {department} \
rtment and reports to {manager}'
def print_employee_information(db_connection):
db_cursor = db_connection.cursor()
results = db_cursor.execute('select * from employees').fetchall()
for row in results:
employee = EmployeeRow._make(row)
# It's now almost impossible to print a field in the wrong place
print(EMPLOYEE_INFO_FMT.format(**employee._asdict()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment