def testMultipleCraftersUseSameCreases(self): counter, b_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.other_id) @pattern(crafter=self.id) class Foo(object): folds = { self.id: 'a=uint:8, b=uint:4', self.other_id: 'b=uint:4, c=uint:7' } creases = {'b': b_creases} __init__ = init(*list('abc')) __eq__ = equals(*list('abc')) original_foo = Foo(129, '12', 100) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert foo.a == original_foo.a assert foo.b == original_foo.b assert other_foo.b == original_foo.b assert other_foo.c == original_foo.c assert counter['fold'] == counter['unfold'] == 2
def main(): with open("input.txt", "r") as file: inputs = file.readlines() inputs = [input.replace("\n", "") for input in inputs] dots, fold_lines = og.parse_inputs(inputs) # part 1 dots = og.fold(dots, fold_lines[0]) print(len(dots)) # part 2 for line in fold_lines[1:]: dots = og.fold(dots, line) print(og.dots_to_code(dots))
def testMultipleCraftersSameFolds(self): @pattern(crafter=self.id) @pattern(crafter=self.other_id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') original_foo = Foo(129, 1) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert original_foo == foo assert foo == other_foo
def testPatternInvalidInit(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8' def __init__(self, a): self.a = a foo = Foo(10) data = fold(foo, crafter=self.id) with pytest.raises(OrigamiException): unfold(data, Foo, crafter=self.id)
def testUnfoldByString(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo(129, 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, "Foo", crafter=self.id) assert foo == other_foo
def testMultipleCraftersDifferentFolds(self): @pattern(crafter=self.id) @pattern(crafter=self.other_id) class Foo(object): folds = { self.id: 'a=uint:8, b=uint:1', self.other_id: 'b=uint:1, c=uint:7' } __init__ = init(*list('abc')) __eq__ = equals(*list('abc')) original_foo = Foo(129, 1, 100) data = fold(original_foo, crafter=self.id) other_data = fold(original_foo, crafter=self.other_id) foo = unfold(data, Foo, crafter=self.id) other_foo = unfold(other_data, Foo, crafter=self.other_id) assert foo.a == original_foo.a assert foo.b == original_foo.b assert other_foo.b == original_foo.b assert other_foo.c == original_foo.c
def testUnfoldIntoInstance(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo(129, 1) other_foo = Foo() data = fold(foo, crafter=self.id) third_foo = unfold(data, other_foo, crafter=self.id) assert foo == third_foo assert other_foo is third_foo
def testPatternWithSlots(self): @pattern(crafter=self.id) class Foo(object): __slots__ = ['a'] folds = 'a=uint:8' def __init__(self, a=10): self.a = a __eq__ = equals('a') foo = Foo(120) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo
def testFormatCrease(self): counter, uint8_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' creases = {'uint:8': uint8_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert counter['fold'] == counter['unfold'] == 1
def testFormatCreaseWithCustomFmt(self): counter, my_int_creases = count_creases(fold=int, unfold=str) my_int_creases['fmt'] = 'uint:8' @pattern(crafter=self.id) class Foo(object): folds = 'a=my_int, b=uint:1' creases = {'my_int': my_int_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert counter['fold'] == counter['unfold'] == 1
def testNameCreaseHasPriority(self): name_counter, name_creases = count_creases(fold=int, unfold=str) fmt_counter, fmt_creases = count_creases(fold=int, unfold=str) @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8, b=uint:1' creases = {'a': name_creases, 'uint:8': fmt_creases} __init__ = init('a', 'b') __eq__ = equals('a', 'b') foo = Foo('129', 1) data = fold(foo, crafter=self.id) other_foo = unfold(data, Foo, crafter=self.id) assert foo == other_foo assert name_counter['fold'] == name_counter['unfold'] == 1 assert fmt_counter['fold'] == fmt_counter['unfold'] == 0
def testFoldLearnedPattern(self): @pattern(crafter=self.id) class Foo(object): folds = 'a=uint:8' __init__ = init('a') __eq__ = equals('a') @pattern(crafter=self.id) class Bar(object): folds = 'a=Foo' __init__ = init('a') __eq__ = equals('a') foo = Foo(129) bar = Bar(foo) data = fold(bar, crafter=self.id) other_bar = unfold(data, Bar, crafter=self.id) assert bar == other_bar
def fold_(value): counter['fold'] += 1 return fold(value)