SlideShare a Scribd company logo
Manager Objects
Damian Gordon
Manager Objects
• Manager Objects are like managers in offices, they don’t do
anything themselves, but they tell other people what to do.
• The manager class and objects don’t really do much activity
themselves, and instead they call other methods, and pass
messages between those methods.
Manager Objects
• As an example, let’s imagine we wanted to search for text in a
few different text files, and replace that text with something
new in each of the files it found it in.
• To make it more challenging, let’s imagine all of the files are
compressed into a single ZIP file.
Manager Objects
• The manager object will have to do the following:
–Unzip the compressed file
–Preform the search and replace
–Zip up the new files
Manager Objects
• The class will do the following:
class ZipReplace:
def __init__(self, filename, search_string, replace_string):
self.filename = filename
self.search_string = search_string
self.replace_string = replace_string
self.temp_directory = "unzipped-{}".format(filename)
# END init
Manager Objects
• The manager object will do the following:
def zip_find_replace(self):
self.unzip_files()
self.find_replace()
self.zip_files()
# END zip_find_replace
Manager Objects
• This method delegates responsibility to the other methods.
• Why do it this way?
• We could do all three steps in a single method, but there are a
few benefits to doing it like this:
Manager Objects
• Readability: It’s easy to understand the basic structure of the
program (with three steps) when it’s presented like this.
• Extensibility: If we wanted to change the program to use
compressed TAR files instead of ZIP files, the find_replace
method wouldn’t need to be changed (or duplicated).
• Partitioning: The find and replace method can be called
directly on some uncompressed files without any change.
Manager Objects
• The unzip_files method will do the following:
def unzip_files(self):
os.mkdir(self.temp_directory)
zip = zipfile.ZipFile(self.filename)
try:
zip.extractall(self.temp_directory)
finally:
zip.close()
# END upzip_files
Manager Objects
• The find_replace method will do the following:
def find_replace(self):
for filename in os.listdir(self.temp_directory):
with open(self._full_filename(filename)) as file:
contents = file.read()
contents = contents.replace(
self.search_string, self.replace_string)
with open(
self._full_filename(filename), "w") as file:
file.write(contents)
# END find_replace
Manager Objects
• The zip_files method will do the following:
def zip_files(self):
file = zipfile.ZipFile(self.filename, 'w')
for filename in os.listdir(self.temp_directory):
file.write(
self._full_filename(filename), filename)
shutil.rmtree(self.temp_directory)
# END zip_files
Manager Objects
• A key point here is that you create each method to “do one
thing well”. Don’t have methods that do five different things,
just one thing well.
• Also methods remove duplicate code, and therefore reduce
errors in the long term.
etc.

More Related Content

PPTX
Publishing and delivery of mobile application
PPTX
SDLC (Software development life Cycle)
PPT
UML Diagrams
PPTX
Operating system deign and implementation
PDF
Object Oriented Analysis Design using UML
PPT
Class diagrams
PPTX
Polymorphism in c++(ppt)
PDF
Identifying classes and objects ooad
Publishing and delivery of mobile application
SDLC (Software development life Cycle)
UML Diagrams
Operating system deign and implementation
Object Oriented Analysis Design using UML
Class diagrams
Polymorphism in c++(ppt)
Identifying classes and objects ooad

What's hot (20)

PPTX
Cohesion and coupling
PPTX
System calls
PPT
Black box and white box testing
PPTX
Software myths | Software Engineering Notes
PPTX
Software Process Models
PPTX
Characteristics of OOPS
PDF
3. ch 2-process model
PPTX
Stream classes in C++
PPTX
Object Oriented Programming Using C++
PPT
Software Testing Strategies
PPTX
Java Notes by C. Sreedhar, GPREC
PPTX
Loops c++
PDF
Android intents
PPTX
Object oriented testing
PPT
Introduction to .NET Framework
PPTX
Software Engineering Layered Technology Software Process Framework
PPT
Operating system services 9
PPTX
COCOMO (Software Engineering)
PDF
NFA to DFA
DOCX
Uml Common Mechanism
Cohesion and coupling
System calls
Black box and white box testing
Software myths | Software Engineering Notes
Software Process Models
Characteristics of OOPS
3. ch 2-process model
Stream classes in C++
Object Oriented Programming Using C++
Software Testing Strategies
Java Notes by C. Sreedhar, GPREC
Loops c++
Android intents
Object oriented testing
Introduction to .NET Framework
Software Engineering Layered Technology Software Process Framework
Operating system services 9
COCOMO (Software Engineering)
NFA to DFA
Uml Common Mechanism
Ad

Viewers also liked (13)

PPTX
Creating Objects in Python
PPTX
Python: Polymorphism
PPTX
Python: Access Control
PPTX
Python: Design Patterns
PPTX
Python: The Iterator Pattern
PPTX
Python: Third-Party Libraries
PPTX
Python: Modules and Packages
PPTX
Python: Basic Inheritance
PPTX
Introduction to Python programming
PPTX
Object-Orientated Design
PPTX
Python: Multiple Inheritance
PPTX
The Extreme Programming (XP) Model
PPTX
Python: Migrating from Procedural to Object-Oriented Programming
Creating Objects in Python
Python: Polymorphism
Python: Access Control
Python: Design Patterns
Python: The Iterator Pattern
Python: Third-Party Libraries
Python: Modules and Packages
Python: Basic Inheritance
Introduction to Python programming
Object-Orientated Design
Python: Multiple Inheritance
The Extreme Programming (XP) Model
Python: Migrating from Procedural to Object-Oriented Programming
Ad

Similar to Python: Manager Objects (20)

PPTX
File handling.pptx
PDF
Course 102: Lecture 24: Archiving and Compression of Files
PDF
Machine Learning Applications
PPT
14 file handling
 
DOC
Zipnotes
PPTX
Managing files chapter 7
PPTX
Managing files chapter 7
PPTX
Inside tempdb
PPTX
Object Oriented Programming in Python.pptx
PDF
Learning PHP for Drupal Theming, DC Chicago 2009
PPTX
Introduction to Linux Slides.pptx
ODP
Intro to drupal module internals asheville
PPTX
Python training
PDF
Linux fundamental - Chap 03 file
PDF
Command Line Tools
PDF
Drupal Front End PHP
PPTX
Linux Fundamentals
PDF
PDF
Find and Locate: Two Commands
File handling.pptx
Course 102: Lecture 24: Archiving and Compression of Files
Machine Learning Applications
14 file handling
 
Zipnotes
Managing files chapter 7
Managing files chapter 7
Inside tempdb
Object Oriented Programming in Python.pptx
Learning PHP for Drupal Theming, DC Chicago 2009
Introduction to Linux Slides.pptx
Intro to drupal module internals asheville
Python training
Linux fundamental - Chap 03 file
Command Line Tools
Drupal Front End PHP
Linux Fundamentals
Find and Locate: Two Commands

More from Damian T. Gordon (20)

PPTX
Introduction to Prompts and Prompt Engineering
PPTX
Introduction to Vibe Coding and Vibe Engineering
PPTX
TRIZ: Theory of Inventive Problem Solving
PPTX
Some Ethical Considerations of AI and GenAI
PPTX
Some Common Errors that Generative AI Produces
PPTX
The Use of Data and Datasets in Data Science
PPTX
A History of Different Versions of Microsoft Windows
PPTX
Writing an Abstract: A Question-based Approach
PPTX
Using GenAI for Universal Design for Learning
DOC
A CheckSheet for Inclusive Software Design
PPTX
A History of Versions of the Apple MacOS
PPTX
68 Ways that Data Science and AI can help address the UN Sustainability Goals
PPTX
Copyright and Creative Commons Considerations
PPTX
Exam Preparation: Some Ideas and Suggestions
PPTX
Studying and Notetaking: Some Suggestions
PPTX
The Growth Mindset: Explanations and Activities
PPTX
Hyperparameter Tuning in Neural Networks
PPTX
Early 20th Century Modern Art: Movements and Artists
PPTX
An Introduction to Generative Artificial Intelligence
PPTX
An Introduction to Green Computing with a fun quiz.
Introduction to Prompts and Prompt Engineering
Introduction to Vibe Coding and Vibe Engineering
TRIZ: Theory of Inventive Problem Solving
Some Ethical Considerations of AI and GenAI
Some Common Errors that Generative AI Produces
The Use of Data and Datasets in Data Science
A History of Different Versions of Microsoft Windows
Writing an Abstract: A Question-based Approach
Using GenAI for Universal Design for Learning
A CheckSheet for Inclusive Software Design
A History of Versions of the Apple MacOS
68 Ways that Data Science and AI can help address the UN Sustainability Goals
Copyright and Creative Commons Considerations
Exam Preparation: Some Ideas and Suggestions
Studying and Notetaking: Some Suggestions
The Growth Mindset: Explanations and Activities
Hyperparameter Tuning in Neural Networks
Early 20th Century Modern Art: Movements and Artists
An Introduction to Generative Artificial Intelligence
An Introduction to Green Computing with a fun quiz.

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Cell Types and Its function , kingdom of life
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Lesson notes of climatology university.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
master seminar digital applications in india
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Trump Administration's workforce development strategy
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Cell Types and Its function , kingdom of life
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Lesson notes of climatology university.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
master seminar digital applications in india
Chinmaya Tiranga quiz Grand Finale.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Trump Administration's workforce development strategy

Python: Manager Objects

  • 2. Manager Objects • Manager Objects are like managers in offices, they don’t do anything themselves, but they tell other people what to do. • The manager class and objects don’t really do much activity themselves, and instead they call other methods, and pass messages between those methods.
  • 3. Manager Objects • As an example, let’s imagine we wanted to search for text in a few different text files, and replace that text with something new in each of the files it found it in. • To make it more challenging, let’s imagine all of the files are compressed into a single ZIP file.
  • 4. Manager Objects • The manager object will have to do the following: –Unzip the compressed file –Preform the search and replace –Zip up the new files
  • 5. Manager Objects • The class will do the following: class ZipReplace: def __init__(self, filename, search_string, replace_string): self.filename = filename self.search_string = search_string self.replace_string = replace_string self.temp_directory = "unzipped-{}".format(filename) # END init
  • 6. Manager Objects • The manager object will do the following: def zip_find_replace(self): self.unzip_files() self.find_replace() self.zip_files() # END zip_find_replace
  • 7. Manager Objects • This method delegates responsibility to the other methods. • Why do it this way? • We could do all three steps in a single method, but there are a few benefits to doing it like this:
  • 8. Manager Objects • Readability: It’s easy to understand the basic structure of the program (with three steps) when it’s presented like this. • Extensibility: If we wanted to change the program to use compressed TAR files instead of ZIP files, the find_replace method wouldn’t need to be changed (or duplicated). • Partitioning: The find and replace method can be called directly on some uncompressed files without any change.
  • 9. Manager Objects • The unzip_files method will do the following: def unzip_files(self): os.mkdir(self.temp_directory) zip = zipfile.ZipFile(self.filename) try: zip.extractall(self.temp_directory) finally: zip.close() # END upzip_files
  • 10. Manager Objects • The find_replace method will do the following: def find_replace(self): for filename in os.listdir(self.temp_directory): with open(self._full_filename(filename)) as file: contents = file.read() contents = contents.replace( self.search_string, self.replace_string) with open( self._full_filename(filename), "w") as file: file.write(contents) # END find_replace
  • 11. Manager Objects • The zip_files method will do the following: def zip_files(self): file = zipfile.ZipFile(self.filename, 'w') for filename in os.listdir(self.temp_directory): file.write( self._full_filename(filename), filename) shutil.rmtree(self.temp_directory) # END zip_files
  • 12. Manager Objects • A key point here is that you create each method to “do one thing well”. Don’t have methods that do five different things, just one thing well. • Also methods remove duplicate code, and therefore reduce errors in the long term.
  • 13. etc.