def __init__(self, filename, search_string, replace_string): self.ex = ZipProcessor(filename) self.filename = filename self.temp_directory = str(filename[:-4]) #super().__init__(filename) self.search_string = search_string self.replace_string = replace_string
class ZipReplace: def __init__(self, filename, search_string, replace_string): """ Init class (str, str, str)->None Need path to zip file, string to search and string to replace """ self.processor = ZipProcessor(filename) self.search_string = search_string self.replace_string = replace_string self.processor.process_zip(self) def process_files(self): """ function, which replace content """ for filename in os.listdir(self.processor.temp_directory): with open(self.processor._full_filename( self.processor.zipname)) as file: contents = file.read() contents = contents.replace(self.search_string, self.replace_string) with open(self.processor._full_filename(self.processor.zipname), "w") as file: file.write(contents)
def __init__(self, filename): """ Init class (str)->None Need path to zip file """ self.processor = ZipProcessor(filename) self.processor.process_zip(self)
def __init__(self, filename, search_string, replace_string): """ Init class (str, str, str)->None Need path to zip file, string to search and string to replace """ self.processor = ZipProcessor(filename) self.search_string = search_string self.replace_string = replace_string self.processor.process_zip(self)
def test_text(): """ The function creating new zip with replaced text """ text_link = "data.txt.zip" replace_string = str(input("Enter what string to replace to:")) object = ZipReplace(text_link, "Sleeping", replace_string) ZipProcessor(text_link, object).process_zip()
class ScaleZip(): """Represents process of changing image extension""" def __init__(self, zipname, extension): self.process = ZipProcessor(zipname) self.extension = extension def process_files(self): """Changes images extension""" for filename in self.process.temp_directory.iterdir(): img = Image.open(str(filename)) scaled = img.resize(self.extension) scaled.save(str(filename)) def process_zip(self): """Run process""" self.process.unzip_files() self.process_files() self.process.zip_files()
class ScaleZip: def __init__(self, filename): """ Init class (str)->None Need path to zip file """ self.processor = ZipProcessor(filename) self.processor.process_zip(self) def process_files(self): '''Scale each image in the directory to 640x480''' for filename in os.listdir(self.processor.temp_directory): try: im = image.load(self.processor._full_filename(filename)) scaled = scale(im, (640,480)) image.save(scaled, self.processor._full_filename(filename)) except: pass
def test_picture(): """ The function creating new zip with reformed photo """ zip_link = "python-basics.jpg.zip" width = str(input("Enter width:")) height = str(input("Enter height:")) object = Scalezip(zip_link, (width, height)) ZipProcessor(zip_link, object).process_zip()
class ZipReplace(): """Represents process of changing file's text""" def __init__(self, filename, search_string, replace_string): self.process = ZipProcessor(filename) self.search_string = search_string self.replace_string = replace_string def process_files(self): """Changes file's text""" for filename in self.process.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) def process_zip(self): """Run process""" self.process.unzip_files() self.process_files() self.process.zip_files()
class ZipReplace: def __init__(self, filename, search_string, replace_string): self.ex = ZipProcessor(filename) self.filename = filename self.temp_directory = str(filename[:-4]) #super().__init__(filename) self.search_string = search_string self.replace_string = replace_string def process_files(self): '''perform a search and replace strings on all files in the temporary directory''' for filename in os.listdir(self.temp_directory): with open( self.ex._full_filename(filename).replace('unzipped-', '')) as file: contents = file.read() contents = contents.replace(self.search_string, self.replace_string) with open( self.ex._full_filename(filename).replace('unzipped-', ''), "w") as file: file.write(contents)
from zip_replace import ZipReplace from zip_processor import ZipProcessor from scale_zip import ScaleZip import os replace = ZipReplace("1.zip", "search", "result") for subdir, dirs, files in os.walk('./'): assert "1.zip" in files break assert isinstance(replace, ZipReplace) assert replace.search_string == "search" z = ZipProcessor(replace) assert isinstance(z, ZipProcessor) assert "1.zip" == z.zip_name scale = ScaleZip("images.zip", 100, 500) assert isinstance(scale, ScaleZip) for subdir, dirs, files in os.walk('./'): assert "images.zip" in files break y = ZipProcessor(scale) assert isinstance(y, ZipProcessor) assert "images.zip" == y.zip_name
''' testing module for scale_zip photos from "filename.zip" will be in unzipped-filename ''' from zip_processor import ZipProcessor from scale_zip import ScaleZip scale = ScaleZip('photos.zip') unziper = ZipProcessor('photos.zip', scale ) unziper.process_zip()
from zip_processor import ZipProcessor import sys import os class ZipReplace: def __init__(self, search_string, replace_string): self.search_string = search_string self.replace_string = replace_string def process(self, zipprocessor): '''perform a search and replace on all files in the temporary directory''' for filename in os.listdir(zipprocessor.temp_directory): with open(zipprocessor._full_filename(filename)) as file: contents = file.read() contents = contents.replace(self.search_string, self.replace_string) with open(zipprocessor._full_filename(filename), "w") as file: file.write(contents) if __name__ == "__main__": zipreplace = ZipReplace(*sys.argv[2:4]) ZipProcessor(sys.argv[1], zipreplace).process_zip()
from zip_processor import ZipProcessor from zip_replace import ZipReplace from scale_zip import ScaleZip print("Enter the word, which will be replaced:") word_to_replace = input() print("Enter the word, which will replace the word you entered:") word_to_replace_with = input() zipreplace = ZipReplace(word_to_replace, word_to_replace_with) print("Enter the full name of zip (with .zip in the end):") zip_name = input() ZipProcessor(zip_name, zipreplace).process_zip() zipscale = ScaleZip() ZipProcessor("test_img.zip", zipscale).process_zip()
from pathlib import Path import sys from zip_processor import ZipProcessor class ZipReplace: """ A class that lets user to replace a given piece of text in all .txt files in the zip file""" def __init__(self, filename, search_string, replace_string): """ Initializes an instance of this class""" self.filename = filename self.search_string = search_string self.replace_string = replace_string self.temp_directory = Path(f"unzipped-{self.filename[:-4]}") def process_files(self): """ Performs a search and replace on all files in the temporary directory""" for filename in self.temp_directory.iterdir(): with filename.open() as file: content = file.read() content = content.replace(self.search_string, self.replace_string) with filename.open("w") as file: file.write(content) if __name__ == '__main__': replace = ZipReplace(*sys.argv[1:4]) ZipProcessor(replace).process_zip()
def __init__(self, zipname, extension): self.process = ZipProcessor(zipname) self.extension = extension
def process_scale(self, zipname): '''Perform scaling and saving file''' ex = ZipProcessor(zipname) ex.process_scale()
import sys from zip_processor import ZipProcessor class ZipReplace: def __init__(self, search_string, replace_string): self.search_string = search_string self.replace_string = replace_string def process_files(self, temp_directory): """perform a search and replace on all files in the temporary directory""" for filename in 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__": processor = ZipProcessor(sys.argv[1], ZipReplace(*sys.argv[2:4])) processor.process_zip() # Comando: # python zip_replace.py zipeado.zip palo pal
def __init__(self, filename, search_string, replace_string): self.process = ZipProcessor(filename) self.search_string = search_string self.replace_string = replace_string