Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet141/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   137   138   139   140   141   142   143   144   ...   183
Bog'liq
python3-oop

[
 144 
]
self.search_string = search_string
self.replace_string = replace_string
def process_files(self):
'''perform a search and replace on all files in the
temporary directory'''
for filename in self.temp_directory.iterdir():
with filename.open() as file:
contents = file.read()
contents = contents.replace(
self.search_string, self.replace_string)
with filename.open("w") as file:
file.write(contents)
if __name__ == "__main__":
ZipReplace(*sys.argv[1:4]).process_zip()
This code is a bit shorter than the original version, since it inherits its ZIP processing 
abilities from the parent class. We first import the base class we just wrote and make 
ZipReplace
extend that class. Then we use 
super()
to initialize the parent class.
The 
find_replace
method is still here, but we renamed it to 
process_files
so
the parent class can call it from its management interface. Because this name isn't
as descriptive as the old one, we added a docstring to describe what it is doing.
Now, that was quite a bit of work, considering that all we have now is a program 
that is functionally not different from the one we started with! But having done that 
work, it is now much easier for us to write other classes that operate on files in a ZIP 
archive, such as the (hypothetically requested) photo scaler. Further, if we ever want 
to improve or bug fix the zip functionality, we can do it for all classes by changing 
only the one 
ZipProcessor
base class. Maintenance will be much more effective.
See how simple it is now to create a photo scaling class that takes advantage of the 
ZipProcessor
functionality. (Note: this class requires the third-party 
pillow
library 
to get the 
PIL
module. You can install it with 
pip install pillow
.)
from zip_processor import ZipProcessor
import sys
from PIL import Image
class ScaleZip(ZipProcessor):
def process_files(self):
'''Scale each image in the directory to 640x480'''
for filename in self.temp_directory.iterdir():
im = Image.open(str(filename))
www.it-ebooks.info


Chapter 5

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   137   138   139   140   141   142   143   144   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish