我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用zipfile.py()。
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") zipfp = zipfile.PyZipFile(TemporaryFile(), "w") zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertCompiledIn('mod1.py', names) self.assertCompiledIn('mod2.py', names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def test_write_pyfile_bad_syntax(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("Bad syntax in python file\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: # syntax errors are printed to stdout with captured_stdout() as s: zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) self.assertIn("SyntaxError", s.getvalue()) # as it will not have compiled the python file, it will # include the .py file not .pyc or .pyo names = zipfp.namelist() self.assertIn('mod1.py', names) self.assertNotIn('mod1.pyc', names) self.assertNotIn('mod1.pyo', names) finally: shutil.rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertCompiledIn('mod1.py', names) self.assertCompiledIn('mod2.py', names) self.assertNotIn('mod2.txt', names) finally: rmtree(TESTFN2)
def test_write_python_directory_filtered(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: zipfp.writepy(TESTFN2, filterfunc=lambda fn: not fn.endswith('mod2.py')) names = zipfp.namelist() self.assertCompiledIn('mod1.py', names) self.assertNotIn('mod2.py', names) finally: rmtree(TESTFN2)
def test_write_pyfile_bad_syntax(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("Bad syntax in python file\n") with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: # syntax errors are printed to stdout with captured_stdout() as s: zipfp.writepy(os.path.join(TESTFN2, "mod1.py")) self.assertIn("SyntaxError", s.getvalue()) # as it will not have compiled the python file, it will # include the .py file not .pyc or .pyo names = zipfp.namelist() self.assertIn('mod1.py', names) self.assertNotIn('mod1.pyc', names) self.assertNotIn('mod1.pyo', names) finally: rmtree(TESTFN2)
def test_write_python_directory(self): os.mkdir(TESTFN2) try: with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: fp.write("print(42)\n") with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: fp.write("print(42 * 42)\n") with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: fp.write("bla bla bla\n") zipfp = zipfile.PyZipFile(TemporaryFile(), "w") zipfp.writepy(TESTFN2) names = zipfp.namelist() self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) self.assertNotIn('mod2.txt', names) finally: shutil.rmtree(TESTFN2)
def open(self, name_or_info, mode="r", pwd=None): """Return file-like object for 'name'.""" # A non-monkey-patched version would contain most of zipfile.py ef = zipfile.ZipFile.open(self, name_or_info, mode, pwd) if isinstance(name_or_info, zipfile.ZipInfo): name = name_or_info.filename else: name = name_or_info if (name in self._expected_hashes and self._expected_hashes[name] != None): expected_hash = self._expected_hashes[name] try: _update_crc_orig = ef._update_crc except AttributeError: warnings.warn('Need ZipExtFile._update_crc to implement ' 'file hash verification (in Python >= 2.7)') return ef running_hash = self._hash_algorithm() if hasattr(ef, '_eof'): # py33 def _update_crc(data): _update_crc_orig(data) running_hash.update(data) if ef._eof and running_hash.digest() != expected_hash: raise BadWheelFile("Bad hash for file %r" % ef.name) else: def _update_crc(data, eof=None): _update_crc_orig(data, eof=eof) running_hash.update(data) if eof and running_hash.digest() != expected_hash: raise BadWheelFile("Bad hash for file %r" % ef.name) ef._update_crc = _update_crc elif self.strict and name not in self._expected_hashes: raise BadWheelFile("No expected hash for file %r" % ef.name) return ef
def open(self, name_or_info, mode="r", pwd=None): """Return file-like object for 'name'.""" # A non-monkey-patched version would contain most of zipfile.py ef = zipfile.ZipFile.open(self, name_or_info, mode, pwd) if isinstance(name_or_info, zipfile.ZipInfo): name = name_or_info.filename else: name = name_or_info if name in self._expected_hashes and self._expected_hashes[name] is not None: expected_hash = self._expected_hashes[name] try: _update_crc_orig = ef._update_crc except AttributeError: warnings.warn('Need ZipExtFile._update_crc to implement ' 'file hash verification (in Python >= 2.7)') return ef running_hash = self._hash_algorithm() if hasattr(ef, '_eof'): # py33 def _update_crc(data): _update_crc_orig(data) running_hash.update(data) if ef._eof and running_hash.digest() != expected_hash: raise BadWheelFile("Bad hash for file %r" % ef.name) else: def _update_crc(data, eof=None): _update_crc_orig(data, eof=eof) running_hash.update(data) if eof and running_hash.digest() != expected_hash: raise BadWheelFile("Bad hash for file %r" % ef.name) ef._update_crc = _update_crc elif self.strict and name not in self._expected_hashes: raise BadWheelFile("No expected hash for file %r" % ef.name) return ef
def choose_subtitle(self, sub_dict): """ ???????? ???????????????????????? ????????????????????????? """ if not self.query: chosen_sub = list(sub_dict.keys())[0] link = sub_dict[chosen_sub]['link'] return chosen_sub, link for i, key in enumerate(sub_dict.keys()): if i == self.sub_num: break lang_info = '' lang_info += '???' if 4 & sub_dict[key]['lan'] else ' ' lang_info += '???' if 2 & sub_dict[key]['lan'] else ' ' lang_info += '???' if 1 & sub_dict[key]['lan'] else ' ' lang_info += '???' if 8 & sub_dict[key]['lan'] else ' ' a_sub_info = ' %3s) %s %s' % (i + 1, lang_info, key) if py == 2: a_sub_info = a_sub_info.decode('utf8') a_sub_info = a_sub_info.encode(GetSubtitles.output_encode) a_sub_info = prefix + a_sub_info print(a_sub_info) indexes = range(len(sub_dict.keys())) choice = None while not choice: try: print(prefix) choice = int(input(prefix + ' choose subtitle: ')) except ValueError: print(prefix + ' Error: only numbers accepted') continue if not choice - 1 in indexes: print(prefix + ' Error: numbers not within the range') choice = None chosen_sub = list(sub_dict.keys())[choice - 1] link = sub_dict[chosen_sub]['link'] return chosen_sub, link