Author Dusty Phillips Reviewers



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

[
 143 
]
self.temp_directory = Path("unzipped-{}".format(
zipname[:-4]))
def process_zip(self):
self.unzip_files()
self.process_files()
self.zip_files()
def unzip_files(self):
self.temp_directory.mkdir()
with zipfile.ZipFile(self.zipname) as zip:
zip.extractall(str(self.temp_directory))
def zip_files(self):
with zipfile.ZipFile(self.zipname, 'w') as file:
for filename in self.temp_directory.iterdir():
file.write(str(filename), filename.name)
shutil.rmtree(str(self.temp_directory))
We changed the 
filename
property to 
zipname
to avoid confusion with the 
filename
local variables inside the various methods. This helps make the code
more readable even though it isn't actually a change in design.
We also dropped the two parameters to 
__init__
(
search_string
and 
replace_
string
) that were specific to 
ZipReplace
. Then we renamed the 
zip_find_replace
method to 
process_zip
and made it call an (as yet undefined) 
process_files
method instead of 
find_replace
; these name changes help demonstrate the more 
generalized nature of our new class. Notice that we have removed the 
find_replace
method altogether; that code is specific to 
ZipReplace
and has no business here.
This new 
ZipProcessor
class doesn't actually define a 
process_files
method;
so if we ran it directly, it would raise an exception. Because it isn't meant to run 
directly, we removed the main call at the bottom of the original script.
Now, before we move on to our image processing app, let's fix up our original 
zipsearch
class to make use of this parent class:
from zip_processor import ZipProcessor
import sys
import os
class ZipReplace(ZipProcessor):
def __init__(self, filename, search_string,
replace_string):
super().__init__(filename)
www.it-ebooks.info


When to Use Object-oriented Programming

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   136   137   138   139   140   141   142   143   ...   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