def updateRecord(db, form): if not 'key' in form: fields = dict.fromkeys(fieldnames, '???') fields['key'] = 'Missing key argument' else: key = form['key'].value if key in db: record = db[key] else: from person_start import Person record = Person('?', '?') for field in fieldnames: val = form[field].value try: val = eval(val) except: pass setattr(record, field, val) db[key] = record fields = record.__dict__ fields['key'] = key return fields
def updateRecord(db, entries): key = entries['key'].get() if key in db: record = db[key] else: from person_start import Person record = Person('', '') for field in fieldnames: value = entries[field].get() try: value = eval(value) except: pass setattr(record, field, value) db[key] = record
import shelve from person_start import Person __author__ = 'stclaus' fieldnames = ('name', 'age', 'pay', 'job') maxfield = max([len(field) for field in fieldnames]) db = shelve.open('class-shelve') while True: key = input('\nKey? =>') if not key: break if key in db: record = db[key] else: record = Person(name='?', age='?') for field in fieldnames: curval = getattr(record, field) newval = input('\t[%s]=%s\n\t\tnew=>?' % (field, curval)) if newval: setattr(record, field, eval(newval)) db[key] = record db.close()
__author__ = 'stclaus' import shelve from person_start import Person from manager import Manager bob = Person('Bob Smith', 42, 40000, 'software') sue = Person('Sue Jones', 45, 50000, 'hardware') tom = Manager('Tom Doe', 50, 50000) db = shelve.open('class-shelve') db['bob'] = bob db['sue'] = sue db['tom'] = tom db.close()
# coding: utf-8 from person_start import Person bob = Person('bob', 42) sue = Person('sue jones', 45) bob = Person('bob smith', 42) jonas = Person('jonas t', 26) people = [bob, sue, jonas] sue = Person('sue jones', 45, 400000') sue = Person('sue jones', 45, 400000) get_ipython().run_line_magic('ls', '') for person in people: print(person.name.title(), ' ', person.pay) people = [bob, sue, jonas] for person in people: print(person.name.title(), ' ', person.pay) x = [(person.name, person.pay) for person in people] x [rec.name for rec in people if rec.age >= 45] # sql-ish query [(rec.age**2 if rec.age >= 45 else rec.age) for rec in people]
import shelve from person_start import Person, Manager shelveFile = 'class-shelve' if __name__ == "__main__": bob = Person('Bob Smith', 40, 'software', 30000) sue = Person('Sue Jones', 44, 'hardware', 40000) tom = Manager('Tom Cruse', 39, 54000) edgar = Manager('Edgar Allan Po', 55, 70000) db = shelve.open(shelveFile) db['bob'] = bob db['sue'] = sue db['tom'] = tom db['edgar'] = edgar db.close()
def test_person_create(): bob = Person('Bob Smith', 42, 30000, 'software') sue = Person('Sue Jones', 45, 40000, 'hardware') assert bob.name == 'Bob Smith' assert sue.pay == 40000
def test_person_lastName(): sue = Person('Sue Jones', 45, 40000, 'hardware') assert sue.lastName() == 'Jones'
def test_person_giveRaise(): sue = Person('Sue Jones', 45, 40000, 'hardware') sue.giveRaise(0.2) assert sue.pay == 48000
from person_start import Person bob = Person('Bob Smith', 42) sue = Person('Sue Jones', 45, 40000) people = [bob, sue] for person in people: print(person.name, person.pay) x = [(person.name, person.pay) for person in people] print(x) x = [rec.name for rec in people if rec.age >= 45] print(x) x = [(rec.age**2 if rec.age >= 45 else rec.age) for rec in people] print(x) from person import Person from manager import Manager bob = Person(name='Bob Smith', age=42, pay=10000) sue = Person(name='Sue Jones', age=45, pay=20000) tom = Manager(name='Tom Doe', age=55, pay=30000) db = [bob, sue, tom] for obj in db: obj.giveRaise(.10) for obj in db:
__author__ = 'stclaus' from person_start import Person class Manager(Person): def __init__(self, name, age, pay=0, job=None): super(Manager, self).__init__(name, age, pay, 'manager') def give_raise(self, percent, bonus=0.1): super(Manager, self).give_raise(percent + bonus) if __name__ == '__main__': bob = Person('Bob Smith', 42, 40000, 'software') sue = Person('Sue Jones', 45, 50000, 'hardware') tom = Manager('Tom Doe', 50, 50000) print(sue, sue.pay, sue.last_name()) for person in (bob, sue, tom): person.give_raise(.10) print(person)
from person_start import Person bob = Person('Bob smith', 42) sue = Person('Sue Jones', 45, 40000) people = [bob, sue] for person in people: print(person.name, person.pay) x = [(person.name, person.pay) for person in people] print x print([rec.name for rec in people if rec.age >= 45]) print([(rec.age**2 if rec.age >= 45 else rec.age) for rec in people])
from person_start import Person bob = Person('Bob Smith', 42, 20650, 'janitor') sue = Person('Sue Jones', 45, 40000) people = [bob, sue] for person in people: print(person.name, person.pay, person.job)
from person_start import Person bob = Person('Bob Smith', 42) sue = Person('Sue jones', 45, 40000) people = [bob, sue] for person in people: print(person.name, person.pay) x = [(person.name, person.pay) for person in people] print(x) print([rec.name for rec in people if rec.age >= 45]) print([(rec.age**2 if rec.age >= 45 else rec.age) for rec in people])
def giveRaise(self, percent, bonus=0.1): Person.giveRaise(self, percent+bonus )
def giveRaise(self, percent, bonus=0.1): Person.giveRaise(self, percent + bonus)