diff --git a/python_3_oop/chapter02/menu.py b/python_3_oop/chapter02/menu.py index 7245f01..8fe0ec2 100644 --- a/python_3_oop/chapter02/menu.py +++ b/python_3_oop/chapter02/menu.py @@ -5,7 +5,7 @@ import base64 import getpass import gzip # own modules -from notebook import Notebook, Note +from notebook import Notebook # cryptography module from cryptography.fernet import Fernet, InvalidToken from cryptography.hazmat.primitives import hashes @@ -13,9 +13,8 @@ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.backends import default_backend - class Menu: - '''Display a menu and respond to choices when run.''' + """Display a menu and respond to choices when run.""" def __init__(self): self.salt = "a683c64de226677703f56e6b6ead94bbc3690ec5293c3de3ffdc" @@ -47,7 +46,7 @@ Notebook Menu 9. Quit """) def run(self): - '''Display the menu and respond to choices.''' + """Display the menu and respond to choices.""" while True: self.display_menu() choice = input("Enter an option: ") @@ -58,26 +57,26 @@ Notebook Menu print("{0} is not a valid choice".format(choice)) def show_notes(self, notes=None): - '''Display all notes stored in notebook object''' + """Display all notes stored in notebook object""" if not notes: notes = self.notebook.notes for note in notes: print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo)) def search_notes(self): - '''Search for a note containing given string''' + """Search for a note containing given string""" filter = input("Search for: ") notes = self.notebook.search(filter) self.show_notes(notes) def add_note(self): - '''Add a given not to notebook object''' + """Add a given not to notebook object""" memo = input("Enter a memo: ") self.notebook.new_note(memo) print("Your note has been added.") def modify_note(self): - '''Modify tag and memo of note with given id''' + """Modify tag and memo of note with given id""" id = input("Enter a note id: ") memo = input("Enter a memo: ") tags = input("Enter tags: ") @@ -90,7 +89,7 @@ Notebook Menu print("Note with id {0} doesn't exist.".format(id)) def remove_note(self): - '''Remove note with given id from note list''' + """Remove note with given id from note list""" id = input("Enter a note id: ") if self.notebook.remove_note(id): print("Note with id {0} removed.".format(id)) @@ -98,7 +97,7 @@ Notebook Menu print("Note with id {0} doesn't exist.".format(id)) def load_notes(self): - '''Decrypt notebook safe file and load it into notebook object''' + """Decrypt notebook safe file and load it into notebook object""" try: f = open(self.savefile, 'rb') except IOError: @@ -112,7 +111,7 @@ Notebook Menu self.notebook._set_id() def save_notes(self): - '''Encrypt notebook object and store it into notebook safe file''' + """Encrypt notebook object and store it into notebook safe file""" cipher = self._encode_notefile() try: f = open(self.savefile, 'wb') @@ -143,23 +142,22 @@ Notebook Menu return crypt.encrypt(plain) def _get_password(self): - '''Request passphrase and derive key from it''' + """Request passphrase and derive key from it""" passphrase = getpass.getpass() kdf = PBKDF2HMAC( - algorithm = hashes.SHA256(), - length = 32, - salt = self.salt.encode('utf-8'), - iterations = 10000, - backend = default_backend() + algorithm=hashes.SHA256(), + length=32, + salt=self.salt.encode('utf-8'), + iterations=10000, + backend=default_backend() ) return base64.urlsafe_b64encode(kdf.derive(passphrase.encode('utf-8'))) def quit(self): - '''Quit application''' + """Quit application""" print("Thank you for using your notebook today.") sys.exit(0) - if __name__ == "__main__": Menu().run() diff --git a/python_3_oop/chapter02/notebook.py b/python_3_oop/chapter02/notebook.py index 65f4fa5..ef7adf0 100644 --- a/python_3_oop/chapter02/notebook.py +++ b/python_3_oop/chapter02/notebook.py @@ -3,13 +3,14 @@ import datetime # Store the next available id for all new notes last_id = 0 + class Note: - '''Represent a note in the notebook. Match against a - string in searches and store tags for each note.''' + """Represent a note in the notebook. Match against a + string in searches and store tags for each note.""" def __init__(self, memo, tags=''): - '''initialize a note with memo and optional space-separated tags. - Automatically set the note's creation date and a unique id.''' + """initialize a note with memo and optional space-separated tags. + Automatically set the note's creation date and a unique id.""" self.memo = memo self.tags = tags self.creation_date = datetime.date.today() @@ -18,27 +19,27 @@ class Note: self.id = last_id def match(self, filter): - '''Determine if this note matches the filter text. + """Determine if this note matches the filter text. Return True if it matches, False otherwise. - Search is case sensitive and matches both text and tags.''' + Search is case sensitive and matches both text and tags.""" return filter in self.memo or filter in self.tags class Notebook: - '''Represent a collection of notes that can be tagged, modified and - searched.''' + """Represent a collection of notes that can be tagged, modified and + searched.""" def __init__(self): - '''Initialize a notebook with an empty list.''' + """Initialize a notebook with an empty list.""" self.notes = [] def new_note(self, memo, tags=''): - '''Create a new note and add it to the list.''' + """Create a new note and add it to the list.""" self.notes.append(Note(memo, tags)) def modify_memo(self, note_id, memo): - '''Find the note with the given id and change its - memo to the given value.''' + """Find the note with the given id and change its + memo to the given value.""" note = self._find_note(note_id) if note: note.memo = memo @@ -46,8 +47,8 @@ class Notebook: return False def modify_tags(self, note_id, tags): - '''Find the note with the given id and change its tags - to the given value.''' + """Find the note with the given id and change its tags + to the given value.""" note = self._find_note(note_id) if note: note.tags = tags @@ -55,18 +56,18 @@ class Notebook: return False def search(self, filter): - '''Find all notes that match the given filter string.''' + """Find all notes that match the given filter string.""" return [note for note in self.notes if note.match(filter)] def _find_note(self, note_id): - '''Locate the note with the given id.''' + """Locate the note with the given id.""" for note in self.notes: if str(note.id) == str(note_id): return note return None def remove_note(self, id): - '''Remove note(s) with given id from note list''' + """Remove note(s) with given id from note list""" if self._find_note(id): removed = [] for index, note in enumerate(self.notes): @@ -77,7 +78,7 @@ class Notebook: return False def _set_id(self): - '''set global last_id to highest id found in notebook''' + """set global last_id to highest id found in notebook""" id = 1 for note in self.notes: if note.id > id: diff --git a/python_3_oop/chapter03/real_estate.py b/python_3_oop/chapter03/real_estate.py index fb2c844..2f84e40 100644 --- a/python_3_oop/chapter03/real_estate.py +++ b/python_3_oop/chapter03/real_estate.py @@ -31,7 +31,7 @@ class Apartment(Property): valid_laundries = ("coin", "ensuite", "none") valid_balconies = ("yes", "no", "solarium") - def __init__(self, balcony = "", laundry = "", **kwargs): + def __init__(self, balcony="", laundry="", **kwargs): super().__init__(**kwargs) self.balcony = balcony self.laundry = laundry @@ -51,8 +51,8 @@ class Apartment(Property): balcony = get_valid_input("Does the property have a balcony?", Apartment.valid_balconies) parent_init.update({ - "laundry" : laundry, - "balcony" : balcony + "laundry": laundry, + "balcony": balcony }) return parent_init @@ -63,7 +63,7 @@ class House(Property): valid_garage = ("attached", "detached", "none") valid_fenced = ("yes", "no") - def __init__(self, num_stories = "", garage = "", fenced = "", **kwargs): + def __init__(self, num_stories="", garage="", fenced="", **kwargs): super().__init__(**kwargs) self.garage = garage self.fenced = fenced @@ -72,9 +72,9 @@ class House(Property): def display(self): super().display() print("HOUSE DETAILS") - print ("# of stories: {}".format(self.num_stories)) - print ("garage: {}".format(self.garage)) - print ("fenced yard: {}".format(self.fenced)) + print("# of stories: {}".format(self.num_stories)) + print("garage: {}".format(self.garage)) + print("fenced yard: {}".format(self.fenced)) print() @staticmethod @@ -84,16 +84,16 @@ class House(Property): garage = get_valid_input("Is there a garage?", House.valid_garage) num_stories = input("How many stories? ") parent_init.update({ - "fenced" : fenced, - "garage" : garage, - "num_stories" : num_stories + "fenced": fenced, + "garage": garage, + "num_stories": num_stories }) return parent_init class Purchase: - def __init__(self, price = "", taxes = "", **kwargs): + def __init__(self, price="", taxes="", **kwargs): super().__init__(**kwargs) self.price = price self.taxes = taxes @@ -108,14 +108,14 @@ class Purchase: @staticmethod def prompt_init(): return dict( - price = input("What's the selling price? "), - taxes = input("What are the estimated taxes? ") + price=input("What's the selling price? "), + taxes=input("What are the estimated taxes? ") ) class Rental: - def __init__(self, furnished = "", utilities = "", rent = "", **kwargs): + def __init__(self, furnished="", utilities="", rent="", **kwargs): super().__init__(**kwargs) self.furnished = furnished self.utilities = utilities @@ -133,8 +133,8 @@ class Rental: def prompt_init(): return dict( rent = input("What is the monthly rent? "), - utilities = input("What are the estimated utilities? "), - furnished = get_valid_input("Is the property furnished?", + utilities=input("What are the estimated utilities? "), + furnished=get_valid_input("Is the property furnished?", ("yes", "no")) ) @@ -178,10 +178,10 @@ class ApartmentPurchase(Purchase, Apartment): class Agent: type_map = { - ("house", "rental") : HouseRental, - ("house", "purchase") : HousePurchase, - ("apartment", "rental") : ApartmentRental, - ("apartment", "purchase") : ApartmentPurchase + ("house", "rental"): HouseRental, + ("house", "purchase"): HousePurchase, + ("apartment", "rental"): ApartmentRental, + ("apartment", "purchase"): ApartmentPurchase } def __init__(self): @@ -196,9 +196,9 @@ class Agent: ("house", "apartment")).lower() payment_type = get_valid_input("What payment type?", ("purchase", "rental")).lower() - PropertyClass = self.type_map[(property_type, payment_type)] - init_args = PropertyClass.prompt_init() - self.property_list.append(PropertyClass(**init_args)) + property_class = self.type_map[(property_type, payment_type)] + init_args = property_class.prompt_init() + self.property_list.append(property_class(**init_args)) def get_valid_input(input_string, valid_options): diff --git a/python_3_oop/chapter05/zipreplace.py b/python_3_oop/chapter05/zipreplace.py index 63433a2..8b0eaf7 100644 --- a/python_3_oop/chapter05/zipreplace.py +++ b/python_3_oop/chapter05/zipreplace.py @@ -11,8 +11,7 @@ class ZipReplace(ZipProcessorInh): self.replace_string = replace_string def process_files(self): - '''perform a search and replace on all files in the temporrary - directory''' + """perform a search and replace on all files in the temporary directory""" for filename in os.listdir(self.temp_directory): with open(self._full_filename(filename)) as file: contents = file.read() diff --git a/python_3_oop/chapter05/zipscale.py b/python_3_oop/chapter05/zipscale.py index 3887406..9c7b24a 100644 --- a/python_3_oop/chapter05/zipscale.py +++ b/python_3_oop/chapter05/zipscale.py @@ -1,18 +1,19 @@ -from zipprocessor import ZipProcessor +from zipprocessor import ZipProcessorInh import sys import os from pygame import image from pygame.transform import scale -class ZipScale(ZipProcessor): +class ZipScale(ZipProcessorInh): def process_files(self): - '''Scale each image in the directory to 640x480''' + """Scale each image in the directory to 640x480""" for filename in os.listdir(self.temp_directory): im = image.load(self._full_filename(filename)) scaled = scale(im, (640, 480)) image.save(scaled, self._full_filename(filename)) + if __name__ == "__main__": ZipScale(*sys.argv[1:2]).process_zip() \ No newline at end of file