def makeDBwithClass(): bob = Person('Bob Smith', 42, 30000, 'Software') sue = Person('Sue Jones', 45, 40000, 'hardware') tom = Manager('Tom Doe', 50, 50000) db = shelve.open('class-shelve') db['bob'] = bob db['sue'] = sue db['tom'] = tom db.close()
#!/usr/bin/python from person import Person, Manager bob = Person('Bob Smith', job='Cleaner', pay=500) sue = Person('Sue Jones', job='Tech', pay=1000) tom = Manager('Tom Jonse', 2000) import shelve db = shelve.open('persondb') for object in (bob, sue, tom): db[object.name] = object db.close()
from person import Person, Manager bob = Person('Bob Clinton') thomas = Person('Thomas Jeb', job = 'dev', pay = 100000) sarah = Manager('Sarah Parker', 50000) import shelve db = shelve.open('persondb') # имя файла хранилища for obj in (bob, thomas, sarah): db[obj.name] = obj db.close() # закрыть после внесения изменений
from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=100000) tom = Manager('Tom Jones', pay=50000) import shelve db = shelve.open('persondb') for obj in (bob, sue, tom): db[obj.name] = obj db.close()
def __str__(self): return '[Person: %s, %s]' % (self.name, self.pay) class Manager(Person): def giveRaise(self, percent, bonus=.10): # Redefine at this level Person.giveRaise(self, percent + bonus) # Call Person's version if __name__ == '__main__': bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=100000) print(bob) print(sue) print(bob.lastName(), sue.lastName()) sue.giveRaise(.10) print(sue) tom = Manager('Tom Jones', 'mgr', 50000) # Make a Manager: __init__ tom.giveRaise(.10) # Runs custom version print(tom.lastName()) # Runs inherited method print(tom) # Runs inherited __str__ if __name__ == '__main__': ... print('--All three--') for object in (bob, sue, tom): # Process objects generically object.giveRaise(.10) # Run this object’s giveRaise print(object) # Run the common __str__
"""Store objects on a shelve database""" from person import Person, Manager a = Person("a", "worker", 10) b = Person("b", "worker", 20) c = Manager("c", 50) import shelve db = shelve.open('persondb') for object in [a, b, c]: db[object.name] = object db.close()
# File makedb.py: store Person objects on a shelve database from person import Person, Manager # Load our classes bob = Person('Bob Smith') # Re-create objects to be stored sue = Person('Sue Jones', job='dev', pay=100000) tom = Manager('Tom Jones', 50000) #import shelve #db = shelve.open('persondb') # Filename where objects are stored #for object in (bob, sue, tom): # Use object's name attr as key # db[object.name] = object # Store object on shelve by key #db.close() # Close after making changes mustafa = Person('Mustafa Kıyga', job='Çaycı', pay=2020) hasan = Person('Hasan Kağıt') furkan = Manager('Furkan Çankaya', 50000) import shelve db = shelve.open('bizimkilerdb') for object in (hasan, mustafa, furkan): db[object.name] = object db.close()
from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', 'dev', pay='100000') tom = Manager('Tom Jones', pay='150000') import shelve db = shelve.open('persondb') for obj in (bob, sue, tom): db[obj.name] = obj db.close()
if __name__ == '__main__': class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E: pass class F(D, E): pass instance_tree(B()) instance_tree(F()) from person import Manager tom = Manager('Tomasz Czarny', 1000) instance_tree(tom) from collections import defaultdict instance_tree(defaultdict())
from person import Person, Manager bob = Person("Bob Smith") sue = Person("Sue Jones", job='dev', pay=10000) tom = Manager("Tom Lane", 40000) import shelve db = shelve.open('python/persondb') for obj in (bob, sue, tom): db[obj.name] = obj db.close() dir(tom) bob.shout()
from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=80000) tom = Manager('Tom Jones', 60000) import shelve db = shelve.open('mt1db') db['bob'] = bob db['sue'] = sue db['tom'] = tom db.close()
from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=100000) tom = Manager('Tom Jones', 100000) team = [bob, sue, tom] print(team) import shelve db = shelve.open('persondb') for obj in team: db[obj.name] = obj db.close
# file makedb.py from person import Person, Manager josh = Person('Josh Tate') emily = Person('Emily Tate', job='My wife', pay=50000) winry = Manager('Winry Tate', 100000) import shelve db = shelve.open('persondb') for obj in (josh, emily, winry): db[obj.name] = obj db.close()
from person import Person # импортирует наши классы from person import Manager bob = Person('Bob Smith') # создание обьектов для сохранения sue = Person ('Sue jonse', job = 'dev', pay = 10000) tom = Manager ('Tom Jonse', 50000) import shelve db = shelve.open('persondb') # Имя файла хранилища for object in (bob, sue, tom): # Вкачестве ключа используеться атрибут name db[object.name] = object # Сохранить объект db.close() # Закрыть после внесения изменений
def update_record(self, key, cur_record): self.db[key] = cur_record def delete_record(self, key): self.db.pop(key) def clear_base(self): self.db.clear() # Small self test if __name__ == '__main__': bob = Person('Bob Smith', 44, 35000, 'software') sue = Person('Sue Jones', 47, 40000, 'hardware') tom = Manager(name='Tom Doe', age=50, pay=50000) pb = PeopleBase() pb.clear_base() pb.update_record('bob', bob) pb.update_record('tom', tom) pb.update_record('sue', sue) print("Finding sue:") pb.fetch_record('sue') print(pb.key, ' => ', pb.record.name, pb.record.age, pb.record.pay, pb.record.job) print("Database after deleting sue:") pb.delete_record('sue') for key in pb.db:
from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=50000) tom = Manager('Tom Jones', 40000) import shelve db = shelve.open('bnrdb') db['bob'] = bob db['sue'] = sue db['tom'] = tom db.close()
from person import Person, Manager p1 = Person('Venkat') p2 = Person("Gagan Datta", job="Dev", pay=100000) p3 = Manager("Pawan Kumar", 50000) import shelve db = shelve.open('persondb') for obj in (p1, p2, p3): db[obj.name] = obj db.close() db = shelve.open('persondb') for key in db: print(key, '=>', db[key]) db.close()
# Specifically, in this chapter we’re going to code two classes: # • Person—a class that creates and processes information about people # • Manager—a customization of Person that modifies inherited behavior from person import Person, Manager bob = Person('Bob Smith') # Test the class sue = Person('Sue Jones', job='dev', pay=100000) # Runs __init__ automatically tom = Manager('Tom Jones', 50000) # Job name not needed: import shelve db = shelve.open('persondb') # Filename where objects are stored for obj in (bob, sue, tom): # Use object's name attr as key db[obj.name] = obj # Store object on shelve by key db.close() # Close after making changes db = shelve.open('persondb') # Reopen the shelve len(db) # Three 'records' stored list(db.keys()) # keys is the index bob = db['Bob Smith'] # Fetch bob by key bob.lastName() for key in db: # Iterate, fetch, print print(key, '=>', db[key]) #bob.giveRaise(.10) #sue.giveRaise(.10) #print(bob) # Fetch attached attributes #print(sue) #tom.giveRaise(.10) # Runs custom version #print(tom.lastName()) # Runs inherited method #print(tom) # Runs inherited __repr__
from person import Person, Manager joey = Person('Joey Tribbiani') rachel = Person('Rachel Green', job='waitress', pay=1500) chandler = Manager('Chandler Bing', 50000) import shelve db = shelve.open('persondb') for object in (joey, rachel, chandler): db[object.name] = object db.close() import shelve db = shelve.open('persondb') # print(len(db)) # print(list(db.keys())) # joey = db['Joey Tribbiani'] # print(joey) # print(joey.lastName()) # for key in db: # print(key, '=>', db[key]) for key in sorted(db): print(key, '=>', db[key])
import person from person import Person, Manager bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=10000) tom = Manager('Tom Jones', 50000) import shelve db = shelve.open('persondb') for object in (bob, sue, tom): db[object.name] = object db.close() db = shelve.open('persondb') print("DB length=", len(db)) print(list(db.keys())) for key in db: print(key, '=>', db[key])
#! /usr/bin/env python # -*- coding: utf-8 -*- import shelve from person import Person from person import Manager wen = Manager('wenduzi', 18, 50000) han = Person('hanmeimei', 15, 10000, None) li = Person('lilei', 16, 30000, 'dev') db = shelve.open('person-shelve') db['wen'] = wen db['han'] = han db['li'] = li db.close()