Python types.DictionaryType() Examples

The following are 16 code examples of types.DictionaryType(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module types , or try the search function .
Example #1
Source File: backend_ctypes.py    From kotori with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, **kwargs):
        """
        Ctypes.Structure with integrated default values.
        https://stackoverflow.com/questions/7946519/default-values-in-a-ctypes-structure/25892189#25892189

        :param kwargs: values different to defaults
        :type kwargs: dict
        """

        # sanity checks
        defaults = type(self)._defaults_
        assert type(defaults) is types.DictionaryType

        # use defaults, but override with keyword arguments, if any
        values = defaults.copy()
        for (key, val) in kwargs.items():
            values[key] = val

        # appropriately initialize ctypes.Structure
        #super().__init__(**values)                     # Python 3 syntax
        return Structure.__init__(self, **values)       # Python 2 syntax

    # http://stackoverflow.com/questions/1825715/how-to-pack-and-unpack-using-ctypes-structure-str/1827666#1827666
    # https://wiki.python.org/moin/ctypes 
Example #2
Source File: influx.py    From kotori with GNU Affero General Public License v3.0 6 votes vote down vote up
def process_message(self, topic, payload, *args):

        log.debug('Bus receive: topic={topic}, payload={payload}', topic=topic, payload=payload)

        # TODO: filter by realm/topic

        # decode message
        if type(payload) is types.DictionaryType:
            message = payload.copy()
        elif type(payload) is types.ListType:
            message = OrderedDict(payload)
        else:
            raise TypeError('Unable to handle data type "{}" from bus'.format(type(payload)))

        # compute storage location from topic and message
        storage_location = self.storage_location(message)
        log.debug('Storage location: {storage_location}', storage_location=dict(storage_location))

        # store data
        self.store_message(storage_location, message) 
Example #3
Source File: jsonobject.py    From zstack-utility with Apache License 2.0 6 votes vote down vote up
def _dump(obj):
    if _is_primitive_types(obj): return simplejson.dumps(obj, ensure_ascii=True)
    
    ret = {}
    items = obj.iteritems() if isinstance(obj, types.DictionaryType) else obj.__dict__.iteritems()
    #items = inspect.getmembers(obj)
    for key, val in items:
        if key.startswith('_'): continue
        
        if _is_unsupported_type(obj):
            raise NoneSupportedTypeError('cannot dump %s, type:%s, object dict: %s' % (val, type(val), obj.__dict__))
        
        if _is_primitive_types(val):
            ret[key] = val
        elif isinstance(val, types.DictType):
            ret[key] = val
        elif isinstance(val, types.ListType):
            nlst = _dump_list(val)
            ret[key] = nlst
        elif isinstance(val, types.NoneType):
            pass
        else:
            nmap = _dump(val)
            ret[key] = nmap
    return ret 
Example #4
Source File: common.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_skip_key(self, *args):
        key_parts = []
        for arg in args:
            if isinstance(arg, types.StringTypes):
                key_parts.append(arg)
            elif isinstance(arg, types.DictionaryType):
                key_parts.append(','.join(arg.keys()))
        skip_key = '-'.join(key_parts)
        return skip_key 
Example #5
Source File: influx.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def flatten(l):
    """
    Munge InfluxDB results.

    See also: https://stackoverflow.com/questions/21461140/flatten-an-irregular-list-of-lists-in-python-respecting-pandas-dataframes
    """
    import pandas
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, (basestring, pandas.DataFrame, types.DictionaryType)):
            for sub in flatten(el):
                yield sub
        else:
            yield el 
Example #6
Source File: container.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def unique_sequence(seq):
    # https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order/480227#480227
    seen = set()
    seen_add = seen.add
    unhashable_types = (types.ListType, types.DictionaryType)
    return [x for x in seq if type(x) in unhashable_types or not (x in seen or seen_add(x))] 
Example #7
Source File: normalize.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def normalize_patent(number, as_dict=False, as_string=False, fix_kindcode=False, for_ops=True, provider=None):

    if provider is None and for_ops is True:
        provider = 'ops'

    # 1. handle patent dicts or convert (split) from string
    if isinstance(number, types.DictionaryType):
        patent = number
    else:
        patent = split_patent_number(number)

    # 2.a. normalize patent dict
    patent_normalized = patch_patent(patent, provider=provider)

    # 2.b. apply fixes
    if fix_kindcode:
        fix_patent_kindcode_ops(patent_normalized)

    # 3. result handling

    # 3.a) default mechanism: return what we've got
    if isinstance(number, types.DictionaryType):
        result = patent_normalized
    else:
        result = join_patent(patent_normalized)

    # 3.b) extended mechanism: return what we are requested for
    if as_dict:
        result = patent_normalized
    elif as_string:
        result = join_patent(patent_normalized)

    return result 
Example #8
Source File: common.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def decode_patent_number(patent):
    if isinstance(patent, types.StringTypes):
        decoded = split_patent_number(patent)
    elif isinstance(patent, types.DictionaryType):
        decoded = patent
    else:
        raise TypeError(u'Document number "{patent}" of type "{type}" could not be decoded'.format(patent=patent, type=type(patent)))
    return decoded 
Example #9
Source File: export.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def write_numberlist_sheets(self):
        sheets = OrderedDict()
        sheets['rated']     = self.data.get('collections', {}).get('rated')
        sheets['dismissed'] = self.data.get('collections', {}).get('dismissed')
        sheets['seen']      = self.data.get('collections', {}).get('seen')
        for sheet_name, entries in sheets.iteritems():

            #print 'entries:'; pprint(entries)

            if entries:
                first = entries[0]
            else:
                first = {}

            # Create pandas DataFrame
            if type(first) in types.StringTypes:
                df = pandas.DataFrame(entries, columns=['PN'])

            elif isinstance(first, (types.DictionaryType, Bunch)):
                df = pandas.DataFrame(entries, columns=['number', 'score', 'timestamp', 'url'])
                df.rename(columns={'number': 'document', 'url': 'display'}, inplace=True)

            # Export DataFrame to Excel
            df.to_excel(self.writer, sheet_name=sheet_name, index=False)

            # Set column widths
            wks = self.worksheet_set_column_widths(sheet_name, 25, 15, 30, 25, cell_format=self.format_wrap_top)
            wks.set_landscape()
            #wks.set_column('C:C', width=19, cell_format=self.format_small_font)
            self.set_header_footer(wks) 
Example #10
Source File: metacache.py    From plugin.audio.tidal2 with GNU General Public License v3.0 5 votes vote down vote up
def getAlbumJson(self, album_id, checkMasterAlbum=False):
        json_obj = self.fetch('album', '%s' % album_id)
        #if isinstance(json_obj, DictionaryType):
        #    json_obj.update({'_cached': True, '_mqa': False if not checkMasterAlbum else self.isMasterAlbum(album_id)})
        return json_obj 
Example #11
Source File: psLib.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def unpack_item(item):
	tp = type(item.value)
	if tp == types.DictionaryType:
		newitem = {}
		for key, value in item.value.items():
			newitem[key] = unpack_item(value)
	elif tp == types.ListType:
		newitem = [None] * len(item.value)
		for i in range(len(item.value)):
			newitem[i] = unpack_item(item.value[i])
		if item.type == 'proceduretype':
			newitem = tuple(newitem)
	else:
		newitem = item.value
	return newitem 
Example #12
Source File: EFIgyLite_cli.py    From EFIgy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _validate_response(self, response):
        """
        Validate the response that came back from the API, return True if it's good, False if bad

        :param response: API response, Dictionary expected
        :return: True if response if valid, False if not
        """
        # Check for unexpected response - all should be JSON dicts that have
        # already been deserialised
        if not isinstance(response, types.DictionaryType):
            self.message(
                "\t\t[!] ERROR - Unexpected value returned from the API: '%s'" %
                (response))
            return False

        # Check for valid errors
        if "error" in response and "msg" in response:
            self.message(
                "\t\t[!] ERROR - %s (%s)" %
                (response["msg"], response["timestamp"]))
            return False

        # Is this a valid response message
        if "msg" in response:
            return True

        # Catch all...dictionary returned but does not contain expected keys?
        # Who know's what's going on here?!
        else:
            self.message(
                "\t\t[!] ERROR - Unexpected dictionary response returned from the API: '%s'" %
                (response))
            return False 
Example #13
Source File: reflect.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def objgrep(start, goal, eq=isLike, path='', paths=None, seen=None, showUnknowns=0, maxDepth=None):
    '''An insanely CPU-intensive process for finding stuff.
    '''
    if paths is None:
        paths = []
    if seen is None:
        seen = {}
    if eq(start, goal):
        paths.append(path)
    if id(start) in seen:
        if seen[id(start)] is start:
            return
    if maxDepth is not None:
        if maxDepth == 0:
            return
        maxDepth -= 1
    seen[id(start)] = start
    if isinstance(start, types.DictionaryType):
        for k, v in start.items():
            objgrep(k, goal, eq, path+'{'+repr(v)+'}', paths, seen, showUnknowns, maxDepth)
            objgrep(v, goal, eq, path+'['+repr(k)+']', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, (list, tuple, deque)):
        for idx in xrange(len(start)):
            objgrep(start[idx], goal, eq, path+'['+str(idx)+']', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, types.MethodType):
        objgrep(start.im_self, goal, eq, path+'.im_self', paths, seen, showUnknowns, maxDepth)
        objgrep(start.im_func, goal, eq, path+'.im_func', paths, seen, showUnknowns, maxDepth)
        objgrep(start.im_class, goal, eq, path+'.im_class', paths, seen, showUnknowns, maxDepth)
    elif hasattr(start, '__dict__'):
        for k, v in start.__dict__.items():
            objgrep(v, goal, eq, path+'.'+k, paths, seen, showUnknowns, maxDepth)
        if isinstance(start, types.InstanceType):
            objgrep(start.__class__, goal, eq, path+'.__class__', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, weakref.ReferenceType):
        objgrep(start(), goal, eq, path+'()', paths, seen, showUnknowns, maxDepth)
    elif (isinstance(start, types.StringTypes+
                    (types.IntType, types.FunctionType,
                     types.BuiltinMethodType, RegexType, types.FloatType,
                     types.NoneType, types.FileType)) or
          type(start).__name__ in ('wrapper_descriptor', 'method_descriptor',
                                   'member_descriptor', 'getset_descriptor')):
        pass
    elif showUnknowns:
        print 'unknown type', type(start), start
    return paths 
Example #14
Source File: client.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def add_contact(self, new_contact, insert_uri=None, auth_token=None,
                  billing_information=None, birthday=None, calendar_link=None, **kwargs):
    """Adds an new contact to Google Contacts.

    Args:
      new_contact: atom.Entry or subclass A new contact which is to be added to
                Google Contacts.
      insert_uri: the URL to post new contacts to the feed
      url_params: dict (optional) Additional URL parameters to be included
                  in the insertion request.
      escape_params: boolean (optional) If true, the url_parameters will be
                     escaped before they are included in the request.

    Returns:
      On successful insert,  an entry containing the contact created
      On failure, a RequestError is raised of the form:
        {'status': HTTP status code from server,
         'reason': HTTP reason from the server,
         'body': HTTP body of the server's response}
    """

    contact = gdata.contacts.data.ContactEntry()

    if billing_information is not None:
      if not isinstance(billing_information, gdata.contacts.data.BillingInformation):
        billing_information = gdata.contacts.data.BillingInformation(text=billing_information)

      contact.billing_information = billing_information

    if birthday is not None:
      if not isinstance(birthday, gdata.contacts.data.Birthday):
        birthday = gdata.contacts.data.Birthday(when=birthday)

      contact.birthday = birthday

    if calendar_link is not None:
      if type(calendar_link) is not ListType:
        calendar_link = [calendar_link]

      for link in calendar_link:
        if not isinstance(link, gdata.contacts.data.CalendarLink):
          if type(link) is not DictionaryType:
            raise TypeError("calendar_link Requires dictionary not %s" % type(link))

          link = gdata.contacts.data.CalendarLink(
                                                  rel=link.get("rel", None),
                                                  label=link.get("label", None),
                                                  primary=link.get("primary", None),
                                                  href=link.get("href", None),
                                                  )

        contact.calendar_link.append(link)

    insert_uri = insert_uri or self.GetFeedUri()
    return self.Post(contact, insert_uri,
                     auth_token=auth_token,  **kwargs) 
Example #15
Source File: reflect.py    From BitTorrent with GNU General Public License v3.0 4 votes vote down vote up
def objgrep(start, goal, eq=isLike, path='', paths=None, seen=None, showUnknowns=0, maxDepth=None):
    '''An insanely CPU-intensive process for finding stuff.
    '''
    if paths is None:
        paths = []
    if seen is None:
        seen = {}
    if eq(start, goal):
        paths.append(path)
    if seen.has_key(id(start)):
        if seen[id(start)] is start:
            return
    if maxDepth is not None:
        if maxDepth == 0:
            return
        maxDepth -= 1
    seen[id(start)] = start
    if isinstance(start, types.DictionaryType):
        r = []
        for k, v in start.items():
            objgrep(k, goal, eq, path+'{'+repr(v)+'}', paths, seen, showUnknowns, maxDepth)
            objgrep(v, goal, eq, path+'['+repr(k)+']', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, types.ListType) or isinstance(start, types.TupleType):
        for idx in xrange(len(start)):
            objgrep(start[idx], goal, eq, path+'['+str(idx)+']', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, types.MethodType):
        objgrep(start.im_self, goal, eq, path+'.im_self', paths, seen, showUnknowns, maxDepth)
        objgrep(start.im_func, goal, eq, path+'.im_func', paths, seen, showUnknowns, maxDepth)
        objgrep(start.im_class, goal, eq, path+'.im_class', paths, seen, showUnknowns, maxDepth)
    elif hasattr(start, '__dict__'):
        for k, v in start.__dict__.items():
            objgrep(v, goal, eq, path+'.'+k, paths, seen, showUnknowns, maxDepth)
        if isinstance(start, types.InstanceType):
            objgrep(start.__class__, goal, eq, path+'.__class__', paths, seen, showUnknowns, maxDepth)
    elif isinstance(start, weakref.ReferenceType):
        objgrep(start(), goal, eq, path+'()', paths, seen, showUnknowns, maxDepth)
    elif (isinstance(start, types.StringTypes+
                    (types.IntType, types.FunctionType,
                     types.BuiltinMethodType, RegexType, types.FloatType,
                     types.NoneType, types.FileType)) or
          type(start).__name__ in ('wrapper_descriptor', 'method_descriptor',
                                   'member_descriptor', 'getset_descriptor')):
        pass
    elif showUnknowns:
        print 'unknown type', type(start), start
    return paths 
Example #16
Source File: EFIgyLite_cli.py    From EFIgy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _validate_json(self):
        """
        Validate the supplied json file to make sure it is json in the expected format
        :return:
        """
        # Do we find valid json?
        try:
            with open(self.batch_json_path, "rb") as fd:
                batch_json = json.loads(fd.read())

        except Exception as err:
            raise
            self.message(
                "[-] Error reading JSON batch file '%s' : '%s'" %
                (self.batch_json_path, err))
            return False

        # Does the json represent a dictionary of the expected form?
        if not isinstance(batch_json, types.DictionaryType):
            self.message(
                "[-] JSON batch file '%s' deserialises to unexpected object type '%s'" %
                (self.batch_json_path, type(batch_json)))
            return False

        # If it is a dictionary does it have the expected characteristics?
        for endpoint, sys_info in batch_json.items():

            # Endpoint should be a hostname, IP or some other string
            # identifier, difficult to validate much beyond 'string'
            if type(endpoint) not in [types.StringType, types.UnicodeType]:
                self.message(
                    "[-] Element within JSON batch file '%s' conatins unexpected object type for an endpoint element '%s'. %s : %s" %
                    (self.batch_json_path, type(endpoint), endpoint, sys_info))
                return False

            # Does the sys_info dict contain the expected keys?
            if set(sys_info.keys()).symmetric_difference(
                    set(self.json_batch_template)):
                self.message(
                    "[-] Unexpected sys_info structure within JSON batch file %s, expected keys '%s' %s : %s" %
                    (self.batch_json_path, self.json_batch_template, endpoint, sys_info))
                return False

            # Create a psuedononymised hash of the uuid using MAC addr as salt
            mac_repr = "0x" + sys_info["mac_addr"].lower().replace(":", "")
            sys_info["hashed_uuid"] = hashlib.sha256(
                mac_repr + sys_info["sys_uuid"]).hexdigest()

            # Remove both the real sys_uuid and the mac_addr from the structure so they do not get submitted to the API
            # and remain confidential to the submitter
            del sys_info["sys_uuid"]
            del sys_info["mac_addr"]

        # Set the read in json structure as the structure of system data to
        # walk and send to the API
        self.endpoints_to_check = batch_json

        self.message("[+] Batch JSON file validated")
        return True