Parse the raw DMIdecode output of a single handle
    into a nice dict
427916
def _dmi_data(dmi_raw, clean, fields):
    '''
    
    '''
    dmi_data = {}

    key = None
    key_data = [None, []]
    for line in dmi_raw:
        if re.match(r'\t[^\s]+', line):
            # Finish previous key
            if key is not None:
                # log.debug('Evaluating DMI key {0}: {1}'.format(key, key_data))
                value, vlist = key_data
                if vlist:
                    if value is not None:
                        # On the rare occasion
                        # (I counted 1 on all systems we have)
                        # that there's both a value <and> a list
                        # just insert the value on top of the list
                        vlist.insert(0, value)
                    dmi_data[key] = vlist
                elif value is not None:
                    dmi_data[key] = value

            # Family: Core i5
            # Keyboard Password Status: Not Implemented
            key, val = line.split(':', 1)
            key = key.strip().lower().replace(' ', '_')
            if (clean and key == 'header_and_data') \
                    or (fields and key not in fields):
                key = None
                continue
            else:
                key_data = [_dmi_cast(key, val.strip(), clean), []]
        elif key is None:
            continue
        elif re.match(r'\t\t[^\s]+', line):
            # Installable Languages: 1
            #        en-US
            # Characteristics:
            #        PCI is supported
            #        PNP is supported
            val = _dmi_cast(key, line.strip(), clean)
            if val is not None:
                # log.debug('DMI key %s gained list item %s', key, val)
                key_data[1].append(val)

    return dmi_data
1285052
def fastalite(handle):
    

    Seq = namedtuple('Seq', ['id', 'description', 'seq'])

    header, seq = '', []
    for line in handle:
        if line.startswith('>'):
            if header:
                yield Seq(header.split()[0], header, ''.join(seq))
            header, seq = line[1:].strip(), []
        else:
            seq.append(line.strip())

    if header and seq:
        yield Seq(header.split()[0], header, ''.join(seq))
742095
def _raw_to_der(self, raw_signature):
        """"""
        component_length = self._sig_component_length()
        if len(raw_signature) != int(2 * component_length):
            raise ValueError("Invalid signature")

        r_bytes = raw_signature[:component_length]
        s_bytes = raw_signature[component_length:]
        r = int_from_bytes(r_bytes, "big")
        s = int_from_bytes(s_bytes, "big")
        return encode_dss_signature(r, s)
843367
def parse_ids(self, skiprecover):
        '''
        '''
        idsfile = self.idsfile
        logging.debug("Parse idsfile `{}`".format(idsfile))
        fp = open(idsfile)
        tigs = []
        for row in fp:
            if row[0] == '#':  # Header
                continue
            atoms = row.split()
            tig, size = atoms[:2]
            size = int(size)
            if skiprecover and len(atoms) == 3 and atoms[2] == 'recover':
                continue
            tigs.append((tig, size))

        # Arrange contig names and sizes
        _tigs, _sizes = zip(*tigs)
        self.contigs = set(_tigs)
        self.sizes = np.array(_sizes)
        self.tig_to_size = dict(tigs)

        # Initially all contigs are considered active
        self.active = set(_tigs)
559786
def ParseMultiple(self, result_dicts):
    """"""
    for result_dict in result_dicts:
      args = {"ifname": result_dict["Description"]}
      args["mac_address"] = binascii.unhexlify(
          result_dict["MACAddress"].replace(":", ""))

      self._ConvertIPs([("IPAddress", "addresses"),
                        ("DefaultIPGateway", "ip_gateway_list"),
                        ("DHCPServer", "dhcp_server_list")], result_dict, args)

      if "DHCPLeaseExpires" in result_dict:
        args["dhcp_lease_expires"] = self.WMITimeStrToRDFDatetime(
            result_dict["DHCPLeaseExpires"])

      if "DHCPLeaseObtained" in result_dict:
        args["dhcp_lease_obtained"] = self.WMITimeStrToRDFDatetime(
            result_dict["DHCPLeaseObtained"])

      yield rdf_client_network.Interface(**args)

      yield rdf_client_network.DNSClientConfiguration(
          dns_server=result_dict["DNSServerSearchOrder"],
          dns_suffix=result_dict["DNSDomainSuffixSearchOrder"])
1216690
def dump_opcodes(opmap):
    """"""
    op2name = {}
    for k in opmap.keys():
        op2name[opmap[k]] = k
    for i in sorted(op2name.keys()):
        print("%-3s %s" % (str(i), op2name[i]))
238520
def markdown_dict_string(d):
    
    # sort experimental data from simulated data
    from collections import OrderedDict
    types = OrderedDict()
    for key, value in sorted(d.items()):
        if 'type' in value:
            if value['type'] not in types:
                types[value['type']] = []
            types[value['type']].append(key)
        else:
            print(key, 'does not define data type!')
    # format output
    s = ''
    for type in ['scRNAseq', 'scqPCR', 'bulk', 'simulated']:
        s += '\nExamples using ' + type + ' data.\n'
        for key in types[type]:
            value = d[key]
            s += '* [' + key + '](#' + key + ')'
            if 'ref' in value:
                if 'doi' in value:
                    link = 'http://dx.doi.org/' + value['doi']
                elif 'url' in value:
                    link = value['url']
                s += (' - [' +  value['ref'].replace('et al.','*et al.*')
                             + '](' + link +  ')')
            if 'title' in value:
                s += '   \n*' + value['title'] + '*'
            s += '\n'
    return s
529831
def decode(cls, encoded):
        
        def create_dict(items):
            # Let's guarantee that dictionaries are sorted.
            k_v_pair = zip(*[iter(items)] * 2)
            return OrderedDict(sorted(k_v_pair, key=itemgetter(0)))

        def create_list(items):
            return list(items)

        stack_items = []
        stack_containers = []

        def compress_stack():
            target_container = stack_containers.pop()
            subitems = []

            while True:
                subitem = stack_items.pop()
                subitems.append(subitem)
                if subitem is target_container:
                    break

            container_creator = subitems.pop()
            container = container_creator(reversed(subitems))
            stack_items.append(container)

        def parse_forward(till_char, sequence):
            number = ''
            char_sub_idx = 0

            for char_sub_idx, char_sub in enumerate(sequence):
                char_sub = chr_(char_sub)
                if char_sub == till_char:
                    break

                number += char_sub

            number = int(number or 0)
            char_sub_idx += 1

            return number, char_sub_idx

        while encoded:
            char = encoded[0]
            char = chr_(char)

            if char == 'd':  # Dictionary
                stack_items.append(create_dict)
                stack_containers.append(create_dict)
                encoded = encoded[1:]

            elif char == 'l':  # List
                stack_items.append(create_list)
                stack_containers.append(create_list)
                encoded = encoded[1:]

            elif char == 'i':  # Integer
                number, char_sub_idx = parse_forward('e', encoded[1:])
                char_sub_idx += 1

                stack_items.append(number)
                encoded = encoded[char_sub_idx:]

            elif char.isdigit():  # String
                str_len, char_sub_idx = parse_forward(':', encoded)
                last_char_idx = char_sub_idx + str_len

                string = encoded[char_sub_idx:last_char_idx]
                try:
                    string = string.decode('utf-8')
                except UnicodeDecodeError:
                    # Considered bytestring (e.g. `pieces` hashes concatenation).
                    pass

                stack_items.append(string)
                encoded = encoded[last_char_idx:]

            elif char == 'e':  # End of a dictionary or a list.
                compress_stack()
                encoded = encoded[1:]

            else:
                raise BencodeDecodingError('Unable to interpret `%s` char.' % char)

        if len(stack_items) == 1:
            stack_items = stack_items.pop()

        return stack_items
791698
def _preprocess(project_dict):
        
        handlers = {
            ('archive',): _list_if_none,
            ('on-run-start',): _list_if_none_or_string,
            ('on-run-end',): _list_if_none_or_string,
        }

        for k in ('models', 'seeds'):
            handlers[(k,)] = _dict_if_none
            handlers[(k, 'vars')] = _dict_if_none
            handlers[(k, 'pre-hook')] = _list_if_none_or_string
            handlers[(k, 'post-hook')] = _list_if_none_or_string
        handlers[('seeds', 'column_types')] = _dict_if_none

        def converter(value, keypath):
            if keypath in handlers:
                handler = handlers[keypath]
                return handler(value)
            else:
                return value

        return deep_map(converter, project_dict)
228358
def _get_seqtype_from_ext(handle):
    ''''''
    if isinstance(handle, basestring):
        name = handle
    elif hasattr(handle, 'filename'):
        name = handle.filename
    elif hasattr(handle, 'name'):
        name = handle.name
    else:
        raise ValueError("Unknown datatype for handle!")

    modifier = ''
    dummy, ext = path.splitext(name.lower())
    if ext == ".gz":
        modifier = 'gz-'
        dummy, ext = path.splitext(dummy)

    if not ext:
        ext = "." + dummy

    if ext in (".gbk", ".gb", ".genbank", ".gbff"):
        return modifier + "genbank"
    elif ext in (".embl", ".emb"):
        return modifier + "embl"
    elif ext in (".fa", ".fasta", ".fna", ".faa", ".fas"):
        return modifier + "fasta"
    else:
        raise ValueError("Unknown file format '%s'." % ext)
957260
def _extract_text_dict(self, raw_text):
        """"""
        delimiter = raw_text[0]

        if raw_text[-1] != delimiter:
            raw_text = raw_text.strip()
            if raw_text[-1] != delimiter:
                msg = (u'The first two characters were:\n {}. The last two characters were: {}\n'
                       u'Parser expects the same delimiter character in beginning '
                       u'and end of TEXT segment'.format(raw_text[:2], raw_text[-2:]))
                raise ParserFeatureNotImplementedError(msg)

        # The delimiter is escaped by being repeated (two consecutive delimiters). This code splits
        # on the escaped delimiter first, so there is no need for extra logic to distinguish
        # actual delimiters from escaped delimiters.
        nested_split_list = [x.split(delimiter) for x in raw_text[1:-1].split(delimiter * 2)]
        # 1:-1 above removes the first and last characters which are reserved for the delimiter.

        # Flatten the nested list to a list of elements (alternating keys and values)
        raw_text_elements = nested_split_list[0]
        for partial_element_list in nested_split_list[1:]:
            # Rejoin two parts of an element that was split by an escaped delimiter (the end and
            # start of two successive sub-lists in nested_split_list)
            raw_text_elements[-1] += (delimiter + partial_element_list[0])
            raw_text_elements.extend(partial_element_list[1:])

        keys, values = raw_text_elements[0::2], raw_text_elements[1::2]
        return dict(zip(keys, values))
1294130
def _handle_dumps(self, handler, **kwargs):
    

    return handler.dumps(self.__class__, to_dict(self), **kwargs)
1237671
def write_pretty_dict_str(out, obj, indent=2):
    """"""
    json.dump(obj,
              out,
              indent=indent,
              sort_keys=True,
              separators=(',', ': '),
              ensure_ascii=False,
              encoding="utf-8")
470831
def _dmi_parse(data, clean=True, fields=None):
    '''
    
    '''
    dmi = []

    # Detect & split Handle records
    dmi_split = re.compile('(handle [0-9]x[0-9a-f]+[^\n]+)\n', re.MULTILINE+re.IGNORECASE)
    dmi_raw = iter(re.split(dmi_split, data)[1:])
    for handle, dmi_raw in zip(dmi_raw, dmi_raw):
        handle, htype = [hline.split()[-1] for hline in handle.split(',')][0:2]
        dmi_raw = dmi_raw.split('\n')
        # log.debug('%s record contains %s', handle, dmi_raw)
        log.debug('Parsing handle %s', handle)

        # The first line of a handle is a description of the type
        record = {
            'handle':      handle,
            'description': dmi_raw.pop(0).strip(),
            'type':        int(htype)
        }

        if not dmi_raw:
            # empty record
            if not clean:
                dmi.append(record)
            continue

        # log.debug('%s record contains %s', record, dmi_raw)
        dmi_data = _dmi_data(dmi_raw, clean, fields)
        if dmi_data:
            record['data'] = dmi_data
            dmi.append(record)
        elif not clean:
            dmi.append(record)

    return dmi
327785
def decode(raw):
    """"""
    return raw \
        .replace(bytes([SLIP_ESC, SLIP_ESC_END]), bytes([SLIP_END])) \
        .replace(bytes([SLIP_ESC, SLIP_ESC_ESC]), bytes([SLIP_ESC]))
511866
def _dict_to_obj(self, d):
    """"""
    if JsonEncoder.TYPE_ID not in d:
      return d

    type_name = d.pop(JsonEncoder.TYPE_ID)
    if type_name in _TYPE_NAME_TO_DECODER:
      decoder = _TYPE_NAME_TO_DECODER[type_name]
      return decoder(d)
    else:
      raise TypeError("Invalid type %s.", type_name)
323155
def dmp(self, thing):
        """"""

        def safe_getattr(key):
            """Avoid crash on getattr"""
            try:
                return getattr(thing, key)
            except Exception as e:
                return 'Error getting attr "%s" from "%s" (%s: %s)' % (
                    key, thing, type(e).__name__, e
                )

        return dict((
            escape(key), {
                'val': self.safe_better_repr(safe_getattr(key)),
                'type': type(safe_getattr(key)).__name__
            }
        ) for key in dir(thing))