def test_map_delete_3(self): A = HashKey(100, 'A') B = HashKey(101, 'B') C = HashKey(100100, 'C') D = HashKey(100100, 'D') E = HashKey(104, 'E') h = Map() h = h.set(A, 'a') h = h.set(B, 'b') h = h.set(C, 'c') h = h.set(D, 'd') h = h.set(E, 'e') orig_len = len(h) # BitmapNode(size=6 bitmap=0b100110000): # NULL: # BitmapNode(size=4 bitmap=0b1000000000000000000001000): # <Key name:A hash:100>: 'a' # NULL: # CollisionNode(size=4 id=0x108572410): # <Key name:C hash:100100>: 'c' # <Key name:D hash:100100>: 'd' # <Key name:B hash:101>: 'b' # <Key name:E hash:104>: 'e' h = h.delete(A) self.assertEqual(len(h), orig_len - 1) h = h.delete(E) self.assertEqual(len(h), orig_len - 2) self.assertEqual(h.get(C), 'c') self.assertEqual(h.get(B), 'b')
def test_map_delete_5(self): h = Map() keys = [] for i in range(17): key = HashKey(i, str(i)) keys.append(key) h = h.set(key, 'val-{}'.format(i)) collision_key16 = HashKey(16, '18') h = h.set(collision_key16, 'collision') # ArrayNode(id=0x10f8b9318): # 0:: # BitmapNode(size=2 count=1 bitmap=0b1): # <Key name:0 hash:0>: 'val-0' # # ... 14 more BitmapNodes ... # # 15:: # BitmapNode(size=2 count=1 bitmap=0b1): # <Key name:15 hash:15>: 'val-15' # # 16:: # BitmapNode(size=2 count=1 bitmap=0b1): # NULL: # CollisionNode(size=4 id=0x10f2f5af8): # <Key name:16 hash:16>: 'val-16' # <Key name:18 hash:16>: 'collision' self.assertEqual(len(h), 18) h = h.delete(keys[2]) self.assertEqual(len(h), 17) h = h.delete(collision_key16) self.assertEqual(len(h), 16) h = h.delete(keys[16]) self.assertEqual(len(h), 15) h = h.delete(keys[1]) self.assertEqual(len(h), 14) h = h.delete(keys[1]) self.assertEqual(len(h), 14) for key in keys: h = h.delete(key) self.assertEqual(len(h), 0)
def test_nested_frozendict_unfreeze(): nested_test = Map({ 'first_key': 'string_value', 'dictionary_property': Map({ 'some_key': 'some_value', 'another_key': tuple([Map({'nested_key': 'nested_value'})]) }) }) res = unfreeze(nested_test) assert isinstance(res, dict) assert isinstance(res['dictionary_property'], dict) assert isinstance(res['dictionary_property']['another_key'], list) assert isinstance(res['dictionary_property']['another_key'][0], dict)
class BugdexJiraFields: """Bugdex's internal representation of Jira Fields recommended components """ summary: str = attr.ib() description: str = attr.ib() components: AbstractSet[Union[Component, Mapping]] = attr.ib(factory=set) labels: AbstractSet[str] = attr.ib(factory=set) issuetype: Mapping[str, str] = attr.ib(default=Map({'name': 'Bug'})) def to_jira_update_args(self) -> Mapping[str, Any]: from .serializing import CustomEncoder def raw(component): if isinstance(component, Component): return component.raw else: return component raw_components = { Map(toolz.keyfilter(lambda key: key in {'name', 'id'}, raw(component))) for component in self.components} raw_fields = toolz.assoc(attr.asdict(self), 'components', raw_components) return CustomEncoder().deep_represent(raw_fields)
def load_config(): conf = DEFAULT_CONFIG try: with open("server.yml", "r") as f: conf = yaml.safe_load(f.read()) except FileNotFoundError: with open("server.yml", "w+") as f: f.write(yaml.dump(DEFAULT_CONFIG)) # Check for missing if any([(key not in conf) for key in DEFAULT_CONFIG.keys()]): conf = {**DEFAULT_CONFIG, **conf} with open("server.yml", "w") as f: f.write(yaml.dump(conf)) if isinstance(conf["seed"], str): # seed is str, we need int conf["seed"] = string_hash_code(conf["seed"][:20]) if conf["seed"] > 2 ** 64: # seed is too big conf["seed"] = gen_seed() with open("server.yml", "w") as f: f.write(yaml.dump(conf)) return Map(conf)
def test_serialization_frozen_params( allowed_intents, target_dialogue_state, time_zone, timestamp, language, locale, dynamic_resource, ): params = Params() params.allowed_intents = allowed_intents params.target_dialogue_state = target_dialogue_state params.time_zone = time_zone params.timestamp = timestamp params.language = language params.locale = locale params.dynamic_resource = Map(dynamic_resource) frozen_params = freeze_params(params) dict_result = dict(frozen_params) assert allowed_intents == dict_result["allowed_intents"] assert target_dialogue_state == dict_result["target_dialogue_state"] assert time_zone == dict_result["time_zone"] assert timestamp == dict_result["timestamp"] assert language == dict_result["language"] assert locale == dict_result["locale"] assert dynamic_resource == dict_result["dynamic_resource"]
class RawStorage(BaseStorage): """ Saves metrics as is """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._metrics = Map() self._storage_class = list def __iter__(self) -> AsyncIterable[Metric]: current_time = int(time.time()) for name, metrics in self._metrics.items(): # type: Counter returning = list() for ts, value in metrics: if ts >= current_time: returning.append((ts, value)) continue yield Metric(name=name, timestamp=ts, value=value) metrics.clear() if not returning: continue metrics.extend(returning) log.info( "%d metric(s) of %r weren't sent yet because they're " "newer than the current timestamp %d", len(returning), name, current_time) def add(self, metric: Metric, operation=None): self._get_metric(metric.name).append((metric.timestamp, metric.value))
def load_properties(): properties = SERVER_PROPERTIES_DEFAULT try: with open("server.yml", "r") as f: properties = yaml.safe_load(f.read()) except FileNotFoundError: with open("server.yml", "w+") as f: f.write(yaml.dump(dict(SERVER_PROPERTIES_DEFAULT))) # Check for missing if any([(key not in properties) for key in SERVER_PROPERTIES_DEFAULT.keys()]): properties = {**SERVER_PROPERTIES_DEFAULT, **properties} with open("server.yml", "w") as f: f.write(yaml.dump(properties)) if isinstance(properties["seed"], str): # seed is str, we need int properties["seed"] = string_hash_code(properties["seed"][:20]) if properties["seed"] > 2**64: # seed is too big properties["seed"] = gen_seed() with open("server.yml", "w") as f: f.write(yaml.dump(properties)) return Map(properties)
def test_existing(self): item = StringItem.get_item('TestString') assert item.tags == frozenset(['TestTag']) assert item.groups == frozenset(['TestGroup']) assert list(item.metadata.keys()) == ['meta1'] assert item.metadata['meta1'].value == 'test' assert item.metadata['meta1'].config == Map({'key': 'value'})
def FROM(self, tr, *pattern, seed=Map()): # seed is immutable """Yields bindings that match PATTERN""" assert len(pattern) == len(self._items), "invalid item count" variable = tuple(isinstance(x, Variable) for x in pattern) # find the first index suitable for the query combination = tuple(x for x in range(len(self._items)) if not variable[x]) for subspace, index in enumerate(self._indices): if is_permutation_prefix(combination, index): break else: raise NStoreException("Oops!") # `index` variable holds the permutation suitable for the # query. `subspace` is the "prefix" of that index. prefix = tuple(pattern[i] for i in index if not isinstance(pattern[i], Variable)) prefix = (self._prefix, subspace, prefix) for key, _ in tr.get_range_startswith(fdb.tuple.pack(prefix)[:-1]): key = fdb.tuple.unpack(key) items = key[2] # re-order the items items = tuple(items[index.index(i)] for i in range(len(self._items))) # seed is immutable bindings = seed for i, item in enumerate(pattern): if isinstance(item, Variable): bindings = bindings.set(item.name, items[i]) yield bindings
def FROM(self, transaction, *pattern, seed=Map()): # seed is immutable """Yields bindings that match PATTERN""" assert len(pattern) == len(self._items), "invalid item count" variable = tuple(isinstance(x, Variable) for x in pattern) # find the first index suitable for the query combination = tuple(x for x in range(len(self._items)) if not variable[x]) for subspace, index in enumerate(self._indices): if is_permutation_prefix(combination, index): break else: raise HoplyException("oops!") # `index` variable holds the permutation suitable for the # query. `subspace` is the "prefix" of that index. prefix = list(pattern[i] for i in index if not isinstance(pattern[i], Variable)) prefix = self._prefix + [subspace] + prefix for key, _ in transaction.prefix(pack(prefix)): items = unpack(key)[len(self._prefix) + 1:] # re-order the items items = tuple(items[index.index(i)] for i in range(len(self._items))) bindings = seed for i, item in enumerate(pattern): if isinstance(item, Variable): bindings = bindings.set(item.name, items[i]) yield bindings
def test_unfreeze_2(): '''Frozensets can't properly be converted if they contain items that will be unhashable.''' map_object = frozenset({ Map({ 'testing_facility': (frozenset({('lithium', 'm')}), frozenset({('hydrogen', 'm'), ('hydrogen', 'g')}), frozenset({('lithium', 'g')}), frozenset()), 'elevator': 1 }) }) result = unfreeze(map_object) expected_result = [ { 'testing_facility': [ [['lithium', 'm']], [['hydrogen', 'g'], ['hydrogen', 'm']], [['lithium', 'g']], [], ], 'elevator': 1, }, ] assert result == expected_result
def __init__(self, name: str, initial_value=None, tags: FrozenSet[str] = frozenset(), groups: FrozenSet[str] = frozenset(), metadata: Mapping[str, MetaData] = Map()): super().__init__(name, initial_value, tags, groups, metadata) # this item is unique because we also save the image type and thus have two states self.image_type: Optional[str] = None
def FROM(self, tr, *pattern, seed=Map()): # seed is immutable """Yields bindings that match PATTERN""" assert len(pattern) == len(self._items), "invalid item count" # TODO: validate that pattern does not have variables named # `alive?` or `changeid`. def bind(pattern, binding): for item in pattern: if isinstance(item, Variable): yield binding[item.name] else: yield item # The complexity really depends on the pattern. A pattern # only made of variables will scan the whole database. In # practice, the user will seldom do time traveling queries, so # it should rarely hit this code path. pattern = list(pattern) + [ nstore.var('alive?'), nstore.var('changeid') ] bindings = self._tuples.FROM(tr, *pattern, seed=seed) for binding in bindings: if not binding['alive?']: # The associated tuple is dead, so the bindings are # not valid in all cases. continue elif self.ask(self, *bind(pattern, binding)): # The bound pattern exist, so the bindings are valid binding = binding.delete('alive?') binding = binding.delete('changeid') yield binding else: continue
def load_config( ): # FIXME Write directly to file with yaml lib instead of like this(can wait until we switch to new lib) conf = DEFAULT_CONFIG try: with open("server.yml", "r", encoding="utf8") as f: conf = yaml.safe_load(f.read()) except FileNotFoundError: with open("server.yml", "w+") as f: f.write(yaml.dump(DEFAULT_CONFIG, default_style='"')) # Check for missing if any([(key not in conf) for key in DEFAULT_CONFIG.keys()]): conf = {**DEFAULT_CONFIG, **conf} with open("server.yml", "w") as f: f.write(yaml.dump(conf)) if isinstance(conf["seed"], str): # seed is str, we need int conf["seed"] = java_string_hash(conf["seed"][:20]) if conf["seed"] > 2**64: # seed is too big conf["seed"] = gen_seed() with open("server.yml", "w") as f: f.write(yaml.dump(conf)) return Map(conf)
def test_function_prepend_syntax(): assert pts(r'/\ x = ([y |-> 0] @@ [z |-> 1])') == { 'x': Map({ 'y': 0, 'z': 1 }) } assert pts(r'/\ x = [y |-> 0] @@ [z |-> 1]') == { 'x': Map({ 'y': 0, 'z': 1 }) } assert pts(r'/\ x = (y :> 2 @@ z :> 3)') == {'x': Map({'y': 2, 'z': 3})} assert pts(r'/\ x = (y :> 4 @@ y :> 5)') == {'x': Map({'y': 4})}
def __init__(self, name: str, h=0.0, s=0.0, b=0.0, tags: FrozenSet[str] = frozenset(), groups: FrozenSet[str] = frozenset(), metadata: Mapping[str, MetaData] = Map()): super().__init__(name=name, initial_value=(h, s, b), tags=tags, groups=groups, metadata=metadata) self.hue: float = min(max(0.0, h), HUE_FACTOR) self.saturation: float = min(max(0.0, s), PERCENT_FACTOR) self.brightness: float = min(max(0.0, b), PERCENT_FACTOR)
def make_scope(self, parent_id: int, new_id: int, persistent: bool = False) -> "State": assert parent_id in self.scopes assert new_id not in self.scopes new_scope = Scope(parent_id, new_id, Map(), persistent=persistent) return dataclass_replace( self.set_scope(new_id, new_scope), scope_stack=(new_id, self.scope_stack) )
def test_frozendict_with_tuple_keys(): nested_test = Map({ (0, 1): 'string_value', (0, 2): 'string_value_2', }) res = unfreeze(nested_test) assert isinstance(res, dict)
def make_immutable_check_config(obj): if isinstance(obj, Sequence) and not isinstance(obj, str): return tuple(make_immutable_check_config(item) for item in obj) elif isinstance(obj, Mapping): # There are no ordering guarantees, see https://github.com/MagicStack/immutables/issues/57 return Map((k, make_immutable_check_config(v)) for k, v in obj.items()) return obj
def test_map_basics_2(self): h = Map() self.assertEqual(len(h), 0) h2 = h.set('a', 'b') self.assertIsNot(h, h2) self.assertEqual(len(h), 0) self.assertEqual(len(h2), 1) self.assertIsNone(h.get('a')) self.assertEqual(h.get('a', 42), 42) self.assertEqual(h2.get('a'), 'b') h3 = h2.set('b', 10) self.assertIsNot(h2, h3) self.assertEqual(len(h), 0) self.assertEqual(len(h2), 1) self.assertEqual(len(h3), 2) self.assertEqual(h3.get('a'), 'b') self.assertEqual(h3.get('b'), 10) self.assertIsNone(h.get('b')) self.assertIsNone(h2.get('b')) self.assertIsNone(h.get('a')) self.assertEqual(h2.get('a'), 'b') h = h2 = h3 = None
def test_map_gc_2(self): A = HashKey(100, 'A') h = Map() h = h.set(A, 'a') h = h.set(A, h) ref = weakref.ref(h) hi = h.items() next(hi) del h, hi gc.collect() gc.collect() gc.collect() self.assertIsNone(ref())
def __init__( self, d: Union["Map[Variable, Term]", Dict[Variable, Term]], o: List[OrderCondition], triples: AbstractSet[Triple], ): self._d = d if isinstance(d, Map) else Map(d) self._o = o self._triples = triples
def test_repr_3(self): class Key: def __init__(self): self.val = None def __hash__(self): return 123 def __repr__(self): return repr(self.val) h = Map() k = Key() h = h.set(k, 1) k.val = h self.assertTrue( repr(h).startswith('<immutables.Map({{...}: 1}) at 0x'))
def __init__(self, name: str, initial_value=None, tags: FrozenSet[str] = frozenset(), groups: FrozenSet[str] = frozenset(), metadata: Mapping[str, MetaData] = Map()): super().__init__(name, initial_value) self.tags: FrozenSet[str] = tags self.groups: FrozenSet[str] = groups self.metadata: Mapping[str, MetaData] = metadata
def test_embedded_dict(): tla = r''' /\ x = [MsgSteps |-> { [ sender |-> "client1", receiver |-> "client2"] }, NextMsgId |-> 0 ] ''' assert pts(tla) == { 'x': Map({ 'MsgSteps': set([Map({ 'sender': "client1", 'receiver': "client2" })]), 'NextMsgId': 0 }) }
def test_map_in_1(self): A = HashKey(100, 'A') AA = HashKey(100, 'A') B = HashKey(101, 'B') h = Map() h = h.set(A, 1) self.assertTrue(A in h) self.assertFalse(B in h) with self.assertRaises(EqError): with HaskKeyCrasher(error_on_eq=True): AA in h with self.assertRaises(HashingError): with HaskKeyCrasher(error_on_hash=True): AA in h
def override_context(self, context: immutables.Map) -> Iterator[None]: main_context = self._context main_level = self.level try: self._context = context self.level = context.get("log_level", INFO) yield finally: self._context = main_context self.level = main_level
def test_metadata(): make_number = partial(map_item, 'test', 'Number', None, frozenset(), frozenset()) item = make_number({'ns1': {'value': 'v1'}}) assert isinstance(item.metadata, Map) assert item.metadata['ns1'].value == 'v1' assert isinstance(item.metadata['ns1'].config, Map) assert item.metadata['ns1'].config == Map() item = make_number({'ns1': {'value': 'v1', 'config': {'c': 1}}}) assert item.metadata['ns1'].value == 'v1' assert isinstance(item.metadata['ns1'].config, Map) assert item.metadata['ns1'].config == Map({'c': 1}) item = make_number({'ns1': {'value': 'v1'}, 'ns2': {'value': 12}}) assert item.metadata['ns1'].value == 'v1' assert item.metadata['ns1'].config == Map() assert item.metadata['ns2'].value == 12 assert item.metadata['ns2'].config == Map()
def test_parse_multiple_variables(): two_vars = r''' /\ x = 2 /\ y = 3 ''' assert pts(two_vars) == {'x': 2, 'y': 3} four_vars = r''' /\ x = 2 /\ y = 3 /\ z = [ a |-> "b" ] /\ w = [ a |-> <<1, 2>> ] ''' assert pts(four_vars) == { 'x': 2, 'y': 3, 'z': Map({'a': 'b'}), 'w': Map({'a': (1, 2)}) }