def test_ana(): l.debug("Initializing 1") one = A(1) l.debug("Initializing 2") two = A(2) one.make_uuid() l.debug("Copying 1") one_p = pickle.dumps(one) one_copy = pickle.loads(one_p) l.debug("Copying 2") two_p = pickle.dumps(two) two_copy = pickle.loads(two_p) nose.tools.assert_is(one_copy, one) nose.tools.assert_is_not(two_copy, two) nose.tools.assert_equal(str(two_copy), str(two)) nose.tools.assert_is(one, A.ana_load(one.ana_store())) nose.tools.assert_is(two, A.ana_load(two.ana_store())) two_copy2 = pickle.loads(pickle.dumps(two)) nose.tools.assert_equal(str(two_copy2), str(two)) l.debug("Initializing 3") three = A(3) three_str = str(three) l.debug("Storing 3") three_uuid = three.ana_store() l.debug("Deleting 3") del three import gc gc.collect() nose.tools.assert_false(three_uuid in ana.get_dl().uuid_cache) l.debug("Loading 3") three_copy = A.ana_load(three_uuid) nose.tools.assert_equal(three_copy.ana_uuid, three_uuid) #pylint:disable=no-member nose.tools.assert_equal(str(three_copy), three_str) known = set() first_json = three_copy.to_literal(known) nose.tools.assert_true(three_copy.ana_uuid in first_json['objects']) nose.tools.assert_equal( first_json['objects'][three_copy.ana_uuid]['object']['n'], three_copy.n) nose.tools.assert_equal(first_json['value']['ana_uuid'], three_copy.ana_uuid) second_json = three_copy.to_literal(known) nose.tools.assert_false(three_copy.ana_uuid in second_json['objects']) nose.tools.assert_equal(second_json['value']['ana_uuid'], three_copy.ana_uuid)
def make_uuid(self, uuid=None): """ This overrides the default ANA uuid with the hash of the AST. UUID is slow, and we'll soon replace it from ANA itself, and this will go away. :returns: a string representation of the AST hash. """ u = getattr(self, '_ana_uuid', None) if u is None: u = str(self._hash) if uuid is None else uuid ana.get_dl().uuid_cache[u] = self setattr(self, '_ana_uuid', u) return u
def make_uuid(self, uuid=None): ''' This overrides the default ANA uuid with the hash of the AST. UUID is slow, and we'll soon replace it from ANA itself, and this will go away. @returns a string representation of the AST hash. ''' u = getattr(self, '_ana_uuid', None) if u is None: u = str(self._hash) if uuid is None else uuid ana.get_dl().uuid_cache[u] = self setattr(self, '_ana_uuid', u) return u
def test_dict(): ana.set_dl(ana.DictDataLayer()) l.debug("Initializing 1") one = A(1) l.debug("Initializing 2") two = A(2) one.make_uuid() l.debug("Copying 1") one_p = pickle.dumps(one) one_copy = pickle.loads(one_p) l.debug("Copying 2") two_p = pickle.dumps(two) two_copy = pickle.loads(two_p) nose.tools.assert_is(one_copy, one) nose.tools.assert_is_not(two_copy, two) nose.tools.assert_equal(str(two_copy), str(two)) nose.tools.assert_is(one, A.ana_load(one.ana_store())) nose.tools.assert_is(two, A.ana_load(two.ana_store())) two_copy2 = pickle.loads(pickle.dumps(two)) nose.tools.assert_equal(str(two_copy2), str(two)) l.debug("Initializing 3") three = A(3) three_str = str(three) l.debug("Storing 3") three_uuid = three.ana_store() l.debug("Deleting 3") del three gc.collect() nose.tools.assert_false(three_uuid in ana.get_dl().uuid_cache) l.debug("Loading 3") three_copy = A.ana_load(three_uuid) nose.tools.assert_equal(three_copy.ana_uuid, three_uuid) #pylint:disable=no-member nose.tools.assert_equal(str(three_copy), three_str) known = set() first_json = three_copy.to_literal(known) nose.tools.assert_true(three_copy.ana_uuid in first_json['objects']) nose.tools.assert_equal(first_json['objects'][three_copy.ana_uuid]['object']['n'], three_copy.n) nose.tools.assert_equal(first_json['value']['ana_uuid'], three_copy.ana_uuid) second_json = three_copy.to_literal(known) nose.tools.assert_false(three_copy.ana_uuid in second_json['objects']) nose.tools.assert_equal(second_json['value']['ana_uuid'], three_copy.ana_uuid)
def from_file(loc): with open(loc, 'rb') as f: saved = pickle.load(f) ana.get_dl()._state_store = saved['store'] return pickle.loads(saved['pickled'])
def save(self, loc): with open(loc, 'wb') as f: pickled = pickle.dumps(self) store = ana.get_dl()._state_store pickle.dump({'store': store, 'pickled': pickled}, f)