def test_right(self): expected = 3 zipper = Zipper().insert(1).right with self.assertRaises(ValueError): zipper.value zipper = zipper.insert(expected) self.assertEqual(zipper.value, expected)
def test_left(self): expected = 2 zipper = Zipper().insert(1).left with self.assertRaises(ValueError): zipper.value actual = zipper.insert(expected).value self.assertEqual(actual, expected)
def test_immutability_move_insert(self): z = Zipper().insert(1).right().insert(4).up().left().insert(2).left().insert(3).left() z_2 = z.insert(5) z_3 = z.insert(7) self.assertIs((z.focus is None), True) self.assertEqual(z.up().value(), 3) self.assertNotEqual(z_2.value(), z_3.value())
class Downloader: _name = "" _page = None _path = "" _zipper = None _chapters = None def __init__(self, page, name, chapters=None): self._page = page self._name = self.get_manga_name(name) self._path = path.join(MANGA_DIR, self._name) self._chapters = chapters self._zipper = Zipper(self._path, list(), wait=5) self._zipper.stay_alive(True) self._zipper.daemon = True self._zipper.start() def run(self): mangas = self._page.get_mangas() url = mangas[self._name] chapters = self._page.get_chapters(url) for cid in sorted(chapters): if self._chapters\ and cid not in self._chapters: continue chapter_dir = path.join(self._path, cid) job = Job(self._page, chapters[cid], chapter_dir) print "Downloading Chapter %s" % cid job.run() self._zipper.add_job(cid) self._zipper.stay_alive(False) self._zipper.join() def get_manga_name(self, name): names = self._page.search_manga(name) if len(names) > 1: print "Did you mean..." for i, name in enumerate(names, 1): print "(%i) %s" % (i, name) while True: print ": ", num = raw_input() try: num = int(num) except: print "Not a number!" continue if num < len(names) and num > 0: return names[num-1] return names[0]
class Engine(object): def __init__(self) -> None: self.questions = Zipper( Set("What is your name?", ["a", "b", "c", "d"], "b"), [ Set("Is this the next question?", ["yes", "no", "maybe", "what's it to you?"], "yes") ]) def next_question(self) -> None: self.questions.next() @property def current_question(self): return self.questions.current
def test_set_right_with_subtree(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 6, "left": { "value": 7, "left": None, "right": None }, "right": { "value": 8, "left": None, "right": None }, }, } zipper = Zipper.from_tree(initial) result = zipper.set_right({ "value": 6, "left": { "value": 7, "left": None, "right": None }, "right": { "value": 8, "left": None, "right": None }, }).to_tree() self.assertEqual(result, expected)
def test_set_left_with_leaf(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": {"value": 3, "left": None, "right": None}, }, "right": {"value": 4, "left": None, "right": None}, } expected = { "value": 1, "left": { "value": 2, "left": {"value": 5, "left": None, "right": None}, "right": {"value": 3, "left": None, "right": None}, }, "right": {"value": 4, "left": None, "right": None}, } zipper = Zipper.from_tree(initial) result = ( zipper.left().set_left({"value": 5, "left": None, "right": None}).to_tree() ) self.assertEqual(result, expected)
def test_traversing_up_from_top_reset(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = 1 zipper = Zipper.from_tree(initial) result = zipper.up() self.assertIsNone(result) result = zipper.reset().value() self.assertEqual(result, expected)
def test_dead_end(self): t1, _, _, _ = self.create_trees() zipper = Zipper.from_tree(t1) # I think this shouldn't be None as one might want to insert a node here! # self.assertIsNone(zipper.left().left()) # I think the following makes more sense: with self.assertRaises(AttributeError): zipper.left().left().left()
def handle_new_file(self, file_name): """Take appropriate action on detection of new file.""" if self.action == "store_to_zip": obj = Zipper(self.path, file_name) elif self.action == "filter_zip": obj = ToDecoder(file_name) # self.file_handler_threads.append(obj) obj.daemon = True obj.start() obj.join()
def __init__(self, page, name, chapters=None): self._page = page self._name = self.get_manga_name(name) self._path = path.join(MANGA_DIR, self._name) self._chapters = chapters self._zipper = Zipper(self._path, list(), wait=5) self._zipper.stay_alive(True) self._zipper.daemon = True self._zipper.start()
def test_different_paths_to_same_zipper(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } result = Zipper.from_tree(initial).left().up().right().to_tree() final = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = Zipper.from_tree(final).right().to_tree() self.assertEqual(result, expected)
def test_left_right_and_value(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": {"value": 3, "left": None, "right": None}, }, "right": {"value": 4, "left": None, "right": None}, } zipper = Zipper.from_tree(initial) result = zipper.left().right().value() self.assertEqual(result, 3)
def test_dead_end(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": {"value": 3, "left": None, "right": None}, }, "right": {"value": 4, "left": None, "right": None}, } zipper = Zipper.from_tree(initial) result = zipper.left().left() self.assertIsNone(result)
def test_traversing_up_from_top(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": {"value": 3, "left": None, "right": None}, }, "right": {"value": 4, "left": None, "right": None}, } zipper = Zipper.from_tree(initial) result = zipper.up() self.assertIsNone(result)
def test_tree_from_deep_focus(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } zipper = Zipper.from_tree(initial) result = zipper.left().right().to_tree() self.assertEqual(result, expected)
def test_data_is_retained(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } zipper = Zipper.from_tree(initial) result = zipper.to_tree() self.assertEqual(result, expected)
def test_set_value_after_traversing_up(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } expected = { "value": 1, "left": { "value": 5, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } zipper = Zipper.from_tree(initial) result = zipper.left().right().up().set_value(5).to_tree() self.assertEqual(result, expected)
def test_test_ability_to_descend_multiple_levels_and_return(self): initial = { "value": 1, "left": { "value": 2, "left": None, "right": { "value": 3, "left": None, "right": None }, }, "right": { "value": 4, "left": None, "right": None }, } zipper = Zipper.from_tree(initial) result = zipper.left().right().up().up().value() self.assertEqual(result, 1)
def test_set_value(self): expected = 2 zipper = Zipper().insert(1).right.insert(3).up.left.insert(2) actual = zipper.set_value(zipper.value*expected).value//zipper.value self.assertEqual(actual, expected)
def test_up_non_mutating(self): expected = 3 zipper = Zipper().insert(1).left.insert(expected) zipper.up actual = zipper.value self.assertEqual(actual, expected)
def test_right_up_cancels(self): expected = 2 zipper = Zipper().insert(expected) actual = zipper.right.up.value self.assertEqual(actual, expected)
def test_left_and_right_inserts_non_mutating(self): expected = 1 zipper = Zipper().insert(1) zipper.left.insert(2) zipper.right.insert(3) self.assertEqual(zipper.value, expected)
img = cv2.imread(file_path) # Can unfish only unempty images if (type(img) is np.ndarray): new_img = self.unfish_image(img) cv2.imwrite(file_path, new_img) if __name__ == "__main__": try: archive_name = sys.argv[1] new_archive_name = sys.argv[2] archive_path = os.path.join(os.getcwd(), 'data', archive_name) if not os.path.isfile(archive_path): print('File does not exist') sys.exit() except IndexError: print('Please specify archive name') sys.exit() extracted_path = os.path.join(os.getcwd(), 'data', 'tmp') # Do the stuff unfish = UnfishImages(extracted_path) zipper = Zipper(archive_path, extracted_path) zipper.unzip_images() unfish.transform_images() zipper.zip_back(new_archive_name)
def test_empty(self): zipper = Zipper() with self.assertRaises(ValueError): zipper.value
def test_insert_non_mutating(self): zipper = Zipper() zipper.insert(1) with self.assertRaises(ValueError): zipper.value
def test_set_value_non_mutating(self): expected = 4 zipper = Zipper().insert(expected) zipper.set_value(8) actual = zipper.value self.assertEqual(actual, expected)
def test_root(self): expected = 1 zipper = Zipper().insert(expected).right.insert(3).left.insert(5) actual = zipper.root.value self.assertEqual(actual, expected)
def test_insert_and_get_value(self): expected = 1 actual = Zipper().insert(expected).value self.assertEqual(actual, expected)
def test_root_non_mutating(self): expected = 5 zipper = Zipper().insert(expected).right.insert(3).left.insert(expected) zipper.root actual = zipper.value self.assertEqual(actual, expected)
def test_different_paths_to_same_zipper(self): t1, _, _, _ = create_trees() zipper = Zipper.from_tree(t1) self.assertEqual(zipper.left().up().right().to_tree(), zipper.right().to_tree())
def unzip(self, file_to_extract): """Unzips file_to_extract.""" # Can raise BadZipfile. Zipper().unzip(BASE_DIR_PATH + file_to_extract, BASE_DIR_PATH)
from zipper import Zipper totalPath = str(__file__).rfind("\\") if totalPath == -1: totalPath = str(__file__).rfind("/") path = str(__file__)[:totalPath + 1] print path zip = Zipper() zip.create_zip("readyMan") zip.add_dir(path + "imageZip") zip.close_zip()