我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ordereddict.OrderedDict()。
def __init__(self, size): self.not_in_cache = not_in_cache = object() cache = _OrderedDict() def get(self, key): return cache.get(key, not_in_cache) def set(self, key, value): cache[key] = value if len(cache) > size: cache.popitem(False) def clear(self): cache.clear() self.get = types.MethodType(get, self) self.set = types.MethodType(set, self) self.clear = types.MethodType(clear, self)
def multiple_policies(self, policies, neighbor): """Creates a new policy that applies list of policies to it. :param policies: list of policies that you want applied to a single policy :param neighbor: the neighbor you are going to apply these policies (used for naming) :type policies: list :type neighbor: str :return: Name of the policy that is created :rtype: str """ policy_name = neighbor.replace('.', '_') policy_name = 'multi_policy_' + policy_name shell = '{"openconfig-routing-policy:routing-policy": {"policy-definitions": {"policy-definition": [{"name": "%s","statements": {"statement": []}}]}}}' % policy_name shell = json.loads(shell, object_pairs_hook=OrderedDict) conditions = shell['openconfig-routing-policy:routing-policy']['policy-definitions']['policy-definition'][0]['statements']['statement'] for policy in policies: policy_nm = 'Policy_' + policy json_policy = '{"name": "%s", "conditions": {"call-policy": "%s"}}' % (policy_nm, policy) json_policy = json.loads(json_policy, object_pairs_hook=OrderedDict) conditions.append(json_policy) multi_policy = json.dumps(shell) print(self.merge_config(multi_policy)) return policy_name
def __addItem(self, entry): dataDefinitionHeader = ESENT_DATA_DEFINITION_HEADER(entry['EntryData']) catalogEntry = ESENT_CATALOG_DATA_DEFINITION_ENTRY(entry['EntryData'][len(dataDefinitionHeader):]) itemName = self.__parseItemName(entry) if catalogEntry['Type'] == CATALOG_TYPE_TABLE: self.__tables[itemName] = OrderedDict() self.__tables[itemName]['TableEntry'] = entry self.__tables[itemName]['Columns'] = OrderedDict() self.__tables[itemName]['Indexes'] = OrderedDict() self.__tables[itemName]['LongValues'] = OrderedDict() self.__currentTable = itemName elif catalogEntry['Type'] == CATALOG_TYPE_COLUMN: self.__tables[self.__currentTable]['Columns'][itemName] = entry self.__tables[self.__currentTable]['Columns'][itemName]['Header'] = dataDefinitionHeader self.__tables[self.__currentTable]['Columns'][itemName]['Record'] = catalogEntry elif catalogEntry['Type'] == CATALOG_TYPE_INDEX: self.__tables[self.__currentTable]['Indexes'][itemName] = entry elif catalogEntry['Type'] == CATALOG_TYPE_LONG_VALUE: self.__addLongValue(entry) else: LOG.error('Unknown type 0x%x' % catalogEntry['Type']) raise
def startElement(self, full_name, attrs): name = self._build_name(full_name) attrs = self._attrs_to_dict(attrs) if attrs and self.namespace_declarations: attrs['xmlns'] = self.namespace_declarations self.namespace_declarations = OrderedDict() self.path.append((name, attrs or None)) if len(self.path) > self.item_depth: self.stack.append((self.item, self.data)) if self.xml_attribs: attr_entries = [] for key, value in attrs.items(): key = self.attr_prefix+self._build_name(key) if self.postprocessor: entry = self.postprocessor(self.path, key, value) else: entry = (key, value) if entry: attr_entries.append(entry) attrs = self.dict_constructor(attr_entries) else: attrs = None self.item = attrs or None self.data = []
def flattened(self): """Returns a flattened version of the parsed whois data""" parsed = self['parsed_whois'] flat = OrderedDict() for key in ('domain', 'created_date', 'updated_date', 'expired_date', 'statuses', 'name_servers'): value = parsed[key] flat[key] = ' | '.join(value) if type(value) in (list, tuple) else value registrar = parsed.get('registrar', {}) for key in ('name', 'abuse_contact_phone', 'abuse_contact_email', 'iana_id', 'url', 'whois_server'): flat['registrar_{0}'.format(key)] = registrar[key] for contact_type in ('registrant', 'admin', 'tech', 'billing'): contact = parsed.get('contacts', {}).get(contact_type, {}) for key in ('name', 'email', 'org', 'street', 'city', 'state', 'postal', 'country', 'phone', 'fax'): value = contact[key] flat['{0}_{1}'.format(contact_type, key)] = ' '.join(value) if type(value) in (list, tuple) else value return flat
def flatten(self, redact=False): """Create a hierarchy of OrderedDicts containing the data from this view, recursively reifying all views to get their represented values. If `redact` is set, then sensitive values are replaced with the string "REDACTED". """ od = OrderedDict() for key, view in self.items(): if redact and view.redact: od[key] = REDACTED_TOMBSTONE else: try: od[key] = view.flatten(redact=redact) except ConfigTypeError: od[key] = view.get() return od
def __init__(self): super(CallbackModule, self).__init__() self._output_dir = os.getenv('JUNIT_OUTPUT_DIR', os.path.expanduser('~/.ansible.log')) self._playbook_path = None self._playbook_name = None self._play_name = None self._task_data = None self.disabled = False if not HAS_JUNIT_XML: self.disabled = True self._display.warning('The `junit_xml` python module is not installed. ' 'Disabling the `junit` callback plugin.') if HAS_ORDERED_DICT: self._task_data = OrderedDict() else: self.disabled = True self._display.warning('The `ordereddict` python module is not installed. ' 'Disabling the `junit` callback plugin.') if not os.path.exists(self._output_dir): os.mkdir(self._output_dir)
def get_completions(self, document, complete_event): cur_text = document.text_before_cursor cur_word = None word_dict = None for regex, method_name in RULES: match = regex.search(cur_text) if match: gen_completions = getattr(self.comp_gen, method_name) completions = gen_completions(self.context, match) word_dict = OrderedDict(completions) groups = match.groups() if len(groups) > 1: cur_word = groups[-1] else: cur_word = document.get_word_before_cursor(WORD=True) break if word_dict: for comp in match_completions(cur_word, word_dict): yield comp