def make(global_scope: Scope, builtin_scope: Scope): return State( None, Map({builtin_scope.id: builtin_scope, global_scope.id: global_scope}), (global_scope.id, (builtin_scope.id, None)), Map(), Map(), 0 )
def test_parse_functions(): a_parsed_func = pts(r'/\ x = [y |-> 0]')['x'] s = set([a_parsed_func]) assert a_parsed_func in s assert pts(r'/\ x = [y |-> 0]') == {'x': Map({'y': 0})} assert pts(r'/\ x = [y |-> "abc"]') == {'x': Map({'y': "abc"})} assert pts(r'/\ x = [y |-> "ab c"]') == {'x': Map({'y': "ab c"})} assert pts(r'/\ x = [y |-> 0, z |-> 1]') == {'x': Map({'y': 0, 'z': 1})} assert pts(r'/\ x = []') == {'x': Map({})}
def test_map_eq_2(self): A = HashKey(100, 'A') Er = HashKey(100, 'Er', error_on_eq_to=A) h1 = Map() h1 = h1.set(A, 'a') h2 = Map() h2 = h2.set(Er, 'a') with self.assertRaisesRegex(ValueError, 'cannot compare'): h1 == h2 with self.assertRaisesRegex(ValueError, 'cannot compare'): h1 != h2
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 __new__(mcs, name, bases, namespace, **kwargs): if name != 'Model': table_name = namespace.get('__table_name__', name) namespace['__table_name__'] = camel_case_to_snake_case(table_name).replace(' ', '_') fields = Map() property_fields = {} MetaModel._set_id(namespace) annotations = {} for attr, field in namespace.items(): if isinstance(field, Field): if not attr.startswith('_'): raise PrivateField('Fields name must be private') fields = fields.set(attr, field) property_name = attr.replace('_', '', 1) if field.name is None: field.name = property_name property_fields[property_name] = MetaModel._resolve_getters_and_setters( attr, namespace, property_fields, field ) annotations[property_name] = field.internal_type MetaModel._set_foreignkey_field(annotations, attr, property_fields, property_name, field) namespace['_fields'] = fields namespace.update(property_fields) namespace['__annotations__'] = annotations return super().__new__(mcs, name, bases, namespace)
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 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
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 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_repr_1(self): h = Map() self.assertTrue(repr(h).startswith('<immutables.Map({}) at 0x')) h = h.set(1, 2).set(2, 3).set(3, 4) self.assertTrue( repr(h).startswith('<immutables.Map({1: 2, 2: 3, 3: 4}) at 0x'))
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"]
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 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 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_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)
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 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 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_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 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 test_map_basics_4(self): h = Map() h1 = h.set('key', []) h2 = h1.set('key', []) self.assertIsNot(h1, h2) self.assertEqual(len(h1), 1) self.assertEqual(len(h2), 1) self.assertIsNot(h1.get('key'), h2.get('key'))
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 test_map_eq_1(self): A = HashKey(100, 'A') B = HashKey(101, 'B') C = HashKey(100100, 'C') D = HashKey(100100, 'D') E = HashKey(120, 'E') h1 = Map() h1 = h1.set(A, 'a') h1 = h1.set(B, 'b') h1 = h1.set(C, 'c') h1 = h1.set(D, 'd') h2 = Map() h2 = h2.set(A, 'a') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.set(B, 'b') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.set(C, 'c') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.set(D, 'd2') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.set(D, 'd') self.assertTrue(h1 == h2) self.assertFalse(h1 != h2) h2 = h2.set(E, 'e') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.delete(D) self.assertFalse(h1 == h2) self.assertTrue(h1 != h2) h2 = h2.set(E, 'd') self.assertFalse(h1 == h2) self.assertTrue(h1 != h2)
def Mapit_mutate(): a = Map({"key_one": ("val_one", )}) b = a.set("key_one", ("sweets", "great")) with a.mutate() as mm: mm['key_one'] = ("sweets", "great") b = mm.finish() return b
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_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 __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