def main(): print "test line 5" a1 = Singleton(2) print "test line 7" a2 = Singleton(3) print "test line 9" print a1, a1.x, a1.a print a2, a1.x, a1.a
def main(): s1 = Singleton() s1._instance.first_name = 'William' s1._instance.last_name = 'Murphy' s1._instance.age = 31 s2 = Singleton() print(s1.__dict__) print(s1, s2)
def test_Singleton(self): s = Singleton() self.assertEqual(s.cid, None) s.set_id(1) self.assertEqual(s.cid, 1) s2 = Singleton() self.assertEqual(s2.cid, 1) s2.set_id(2) self.assertEqual(s.cid, 2)
def test_inheritance(self): main_singleton1 = Singleton() main_singleton2 = Singleton() next_singleton1 = SubSingleton1() next_singleton2 = SubSingleton1() last_singleton1 = SubSingleton2() last_singleton2 = SubSingleton2() self.assertTrue(main_singleton1 is not next_singleton1) self.assertTrue(main_singleton1 is not last_singleton1) self.assertTrue(next_singleton1 is not last_singleton1) self.assertTrue(main_singleton1 is main_singleton2) self.assertTrue(next_singleton1 is next_singleton2) self.assertTrue(last_singleton1 is last_singleton2)
def create_singleton(self): print("\nbefore creating singleton") new_singleton = Singleton() time.sleep(1) print("\nafter creating singleton") return new_singleton
def main(): chess = Singleton().chess root = tk.Tk() root.title("Chess") gui = GUI(root, chess) gui.pack() root.mainloop()
def testInherited(self): s = Singleton() ss1 = SubSingleton() ss2 = SubSingleton() self.assertEquals(id(ss1), id(ss2)) self.assertEquals(id(ss1), id(s)) self.assertTrue(ss1 is ss2) self.assertTrue(ss1 is s)
def main(): s = Singleton() print s s = Singleton.get_instance() print s s = Singleton.get_instance() print s
def test_singleton_increment_and_decrement(): """ Test if the value inside the single will be the only one incremented or decremented. """ base_singleton = Singleton() _singleton1 = Singleton() _singleton2 = Singleton() tests = [ (_singleton1.increment, 1), (_singleton2.increment, 2), (_singleton1.decrement, 1), (_singleton2.decrement, 0), (_singleton1.decrement, 0), (_singleton2.increment, 1), (_singleton1.increment, 2), (_singleton2.increment, 2) ] for operation, value in tests: operation() assert base_singleton.value == value
def test_serialization(self): def deserializable(self): with open("object", 'rb') as file: return pickle.load(file) singleton = Singleton() with open("object", 'wb') as file: pickle.dump(singleton, file, pickle.DEFAULT_PROTOCOL) singleton.variable = 2 pool = ThreadPool(2) instances = pool.map(deserializable, range(2)) self.assertTrue(instances[0] is instances[1]) self.assertTrue(singleton.variable == None)
def testThreadSerialization(self): def deserializable(): with open(fileName, 'rb') as f: return pickle.load(f) fileName = 'data.pickle' s1 = Singleton() with open(fileName, 'wb') as f: pickle.dump(s1, f, pickle.HIGHEST_PROTOCOL) t1 = ThreadPool(processes=1) t2 = ThreadPool(processes=1) self.assertTrue( t1.apply_async(deserializable).get() is t2.apply_async( deserializable).get())
def test_singleton(self): first_singleton = Singleton() second_singleton = Singleton() self.assertTrue(first_singleton is second_singleton)
def main(): singleton = Singleton() print('Address =', hex(id(singleton))) instance = Singleton.get_instance() print('Address =', hex(id(instance)))
def to_java(cmd, java_dir, tmpls, output_paths, patterns): ## clean up result directory if os.path.isdir(java_dir): util.clean_dir(java_dir) else: os.makedirs(java_dir) ## interpret synthesis result per demo p2vs = [] logging.info("merging {} template(s)".format(len(tmpls))) for tmpl, output_path in zip(tmpls, output_paths): tmpl.unfreeze() demo = util.pure_base(output_path) _patterns = patterns[:] p2v = {} if cmd == "android": from ..rewrite.android import obs_conf p2v[C.P.OBS] = Observer(output_path, obs_conf) elif cmd == "gui": from ..rewrite.gui import obs_conf p2v[C.P.OBS] = Observer(output_path, obs_conf) if cmd == "android": from ..rewrite.android import acc_conf_uni, acc_conf_map p2v[C.P.ACCU] = AccessorUni(cmd, output_path, acc_conf_uni) p2v[C.P.ACCM] = AccessorMap(cmd, output_path, acc_conf_map) elif cmd == "gui": from ..rewrite.gui import acc_conf_uni, acc_conf_map p2v[C.P.ACCU] = AccessorUni(cmd, output_path, acc_conf_uni) p2v[C.P.ACCM] = AccessorMap(cmd, output_path, acc_conf_map) else: pass if cmd == "gui": p2v[C.P.ADP] = Adapter(output_path) p2v[C.P.SNG] = Singleton(output_path) keys = p2v.keys() if not _patterns: # then try all the patterns _patterns = keys ## filter out unknown pattern names _patterns = util.intersection(_patterns, keys) p2vs.append(p2v) for p in _patterns: if p not in p2v: continue logging.info("decoding {} pattern for {}".format(p, demo)) tmpl.accept(p2v[p]) tmpl.freeze() ## merge multiple synthesis results tmpl, _ = reduce(merge_tmpls, zip(tmpls, p2vs), (None, {})) # final semantic checking logging.info("semantics checking") _visitors = [] _visitors.append(Collection()) _visitors.append(SemanticChecker(cmd)) map(lambda vis: tmpl.accept(vis), _visitors) ## statistics counter = EmptyFinder() tmpl.accept(counter) logging.info("classes: {}".format(counter.cls_count)) logging.info("methods: {} (empty: {})".format(counter.mtd_count, counter.empty_count)) ## framework-specific trimming of models f = globals()["trim_model_" + cmd] f(tmpl) dump(cmd, java_dir, tmpl, "decoding")
from singleton import Singleton if __name__ == "__main__": obj1 = Singleton("dog") obj2 = Singleton("cat") if obj1 == obj2: print("obj1 and obj2 are the same instance.") print("obj1.name={} obj2.name={}".format(obj1.name, obj2.name))
def test_new(self): '''Unit test for SoftJob_Crawler.__new__.''' singleton_one = Singleton() singleton_two = Singleton() self.assertEqual(singleton_one, singleton_two)
#!/usr/bin/env python # coding: utf-8 from singleton import Singleton print("Start.") obj1 = Singleton() obj2 = Singleton() if (id(obj1) == id(obj2)): print("obj1とobj2は同じインスタンスです") else: print("obj1とobj2は同じインスタンスではありません")
def test_singleton(): """ Test a single singleton. """ _singleton1 = Singleton() assert isinstance(_singleton1, Singleton)
from singleton import Singleton if __name__ == '__main__': #Las tres son iguales s = Singleton() print("primera direccion de memoria") print(s) s = Singleton().getInstance() print("segunda direccion de memoria") print(s) s = Singleton().getInstance() print("tercera direccion de memoria") print(s)
def test_singleton_returning_always_the_same_instance(): """ Test if the same stance will always be returned. """ _singleton1 = Singleton() for _ in range(10): assert _singleton1 is Singleton()
def test_singleton_and_child_have_different_identity(self): s1 = Singleton("Sam") c1 = Child("John") self.assertIsNot(s1, c1)
def test_two_singletons_have_same_identity(self): s1 = Singleton("Sam") s2 = Singleton("Tom") self.assertIs(s1, s2)
from singleton import Singleton from not_singleton import NotSingleton a = NotSingleton() b = NotSingleton() print(f"{id(a)} --> {a}") print(f"{id(b)} --> {b}") print(f"is a == b {a is b}") c = Singleton() d = Singleton() print(f"{id(c)} --> {c}") print(f"{id(d)} --> {d}") print(f"is c == d {c is d}")
def test_singleton_and_child_have_different_identity(self): s1 = Singleton('Sam') c1 = Child('John') self.assertIsNot(s1, c1)
from singleton import Singleton if __name__ == "__main__": s1 = Singleton() s2 = Singleton() if id(s1) == id(s2): print("Singleton works, both variables contain the same instance.") else: print("Singleton failed, variables contain different instances.")
def main(): connection_one = Singleton() connection_two = Singleton() print(connection_one is connection_two) # true
def task(): print Singleton()
def visit(cmd, smpls, tmpl, patterns): ## non-trivial, framework-specific rewriting if cmd == "android": from android import R from android.system import System from android.view import View R.generate_R(tmpl) _visitors = [] _visitors.append(System()) _visitors.append(View()) map(lambda vis: tmpl.accept(vis), _visitors) p2v = {} ## structural patterns p2v[C.P.BLD] = Builder(smpls) p2v[C.P.FAC] = Factory(smpls) if cmd == "android": from android import sng_conf p2v[C.P.SNG] = Singleton(smpls, sng_conf) elif cmd == "gui": from gui import sng_conf p2v[C.P.SNG] = Singleton(smpls, sng_conf) else: p2v[C.P.SNG] = Singleton(smpls) if cmd == "android": from android import acc_default, acc_conf_uni, acc_conf_map p2v[C.P.ACCA] = AccessorAdHoc(smpls, acc_default) p2v[C.P.ACCU] = AccessorUni(smpls, acc_default + acc_conf_map.keys(), acc_conf_uni) p2v[C.P.ACCM] = AccessorMap(smpls, acc_default + acc_conf_uni.keys(), acc_conf_map) elif cmd == "gui": from gui import acc_default, acc_conf_uni, acc_conf_map p2v[C.P.ACCA] = AccessorAdHoc(smpls, acc_default) p2v[C.P.ACCU] = AccessorUni(smpls, acc_default + acc_conf_map.keys(), acc_conf_uni) p2v[C.P.ACCM] = AccessorMap(smpls, acc_default + acc_conf_uni.keys(), acc_conf_map) else: p2v[C.P.ACCA] = AccessorAdHoc(smpls) ## behavioral patterns if cmd == "android": from android import obs_conf from android.observer import Observer p2v[C.P.OBS] = Observer(smpls, obs_conf) elif cmd == "gui": from gui import obs_conf from gui.observer import Observer p2v[C.P.OBS] = Observer(smpls, obs_conf) else: from observer import Observer p2v[C.P.OBS] = Observer(smpls) p2v[C.P.PRX] = Proxy(smpls) if cmd == "gui": p2v[C.P.ADP] = Adapter(smpls) p2v[C.P.STA] = State(smpls) keys = p2v.keys() if not patterns: # then try all the patterns patterns = keys # filter out unknown pattern names patterns = util.intersection(patterns, keys) # sort so that visitors can be visited in the order of generation patterns.sort() for p in patterns: logging.info("rewriting {} pattern".format(p)) tmpl.accept(p2v[p]) # final semantic checking logging.info("semantics checking") chker = SemanticChecker(cmd) tmpl.accept(chker)
def test___new__(self): a = Singleton() b = Singleton() self.assertEqual(a, b)
# singleton client from singleton import Singleton if __name__ == '__main__': inst = Singleton() print(inst) inst = Singleton.get_instance() print(inst)