Created
September 1, 2014 10:44
-
-
Save jorge-lavin/acbb4aef37feedd7b500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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