Need Clarification on Using OOP and DRY Method in Python -


i'm trying keep code clean applying oop , dry method; however, found myself stuck following questions.

1) since checkremote , backup method dependent on sshlogin method, there way write object initialized?

2) if there isn't better way, write procedure phonebook class execute in following manner (1 - checklocal, 2 - sshlogin, 3 - checkremote, 4 - backup)? in main?

class phonebook:      def __init__ (self, name, phone_number, birthdate, location):         self.name = name         self.phone_number = phone_number         self.birthdate = birthdate         self.location = location         self.ssh = none      def checklocal (self):         # check local phonebook existing names         pass       def sshlogin (self):         # ssh remote server       def checkremote (self):         # check remote phonebook existing names         pass       def backup (self):         # backup remote phonebook 

in case, want use context manager , with keyword.

since using phonebook object requires "set up" phase beforehand, want make sure handled correctly every time use it. able write code this:

with phonebook(name, phone_number, birthdate, location) phbk:      #do stuff phonebook     phbk.add(name, phone_number, birthdate, location) 

all setup phase steps - checking local copy, connecting (and disconnecting) ssh session, checking remote copy, backup, etc. - happen "behind scenes" part of setup/tear down of context manager (in other words, with statement takes care of that). similar how supposed use open():

with open('myfile') file:     lines = file.readlines() 

cleanly opening , closing file happens "behind scenes", automagically. want happen phonebook.

to context manager working, use python __enter__ , __exit__ magic methods. might this:

class phonebook:      def __init__ (self, name, phone_number, birthdate, location):         self.name = name         self.phone_number = phone_number         self.birthdate = birthdate         self.location = location         self.ssh = none      def sshlogin(self):         # ssh remote server      def sshlogout(self):         # ssh out of remote server      def checklocal(self):         # check local phonebook existing names         pass              def checkremote (self):         # check remote phonebook existing names         pass                  def backup (self):         # backup remote phonebook      def __enter__(self):         self.checklocal()         self.sshlogin()         self.checkremote()         self.backup()         return self      def __exit__(self, cls, value, traceback):         self.sshlogout()         return false 

as add (and delete) class, shouldn't class. should either be:

  • a method of phonebook class, so:

    class phonebook:     def __init__(self):         self.ssh = none     def add(self, name, phone_number, birthdate, location):         self.name = name         self.phone_number = phone_number         self.birthdate = birthdate         self.location = location 

    or

  • a generic function, so:

    def add(phbk, name, phone_number, birthdate, location):     # add remote phonebook 

a further point: phonebook class isn't named well. based on __init__ method, it's more of phonebook entry. may want consider implementing phonebook (as context manager, explained above) separately container of kind hold entries (including add , delete methods), , focus phonebook class have (renamed entry or something) on being more of phonebook entry object can added new phonebook object, in case add method might more this:

    def add(self, entry):         try:             self.entries.append(entry)         except attributeerror:             self.entries = [entry]  

one other thing: noticed you're using tab characters define classes, functions, , not. against recommended practice - use 4 spaces instead.

a further suggestion: simple suggested entry class, might want use collections.namedtuple, so:

from collections import namedtuple nt entry = nt('entry', 'name phone_number birthdate location') 

now can stuff this:

e = entry('rick', '8675309', '1/1/2001', '125 sesame st`) print(e.name) 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -