Special data packing format, used in struct definitions, and .id2 files

    sdk functions: pack_dd etc.
504450
def idaunpack(buf):
    
    buf = bytearray(buf)

    def nextval(o):
        val = buf[o] ; o += 1
        if val == 0xff:  # 32 bit value
            val, = struct.unpack_from(">L", buf, o)
            o += 4
            return val, o
        if val < 0x80:  # 7 bit value
            return val, o
        val <<= 8
        val |= buf[o] ; o += 1
        if val < 0xc000:  # 14 bit value
            return val & 0x3fff, o

        # 29 bit value
        val <<= 8
        val |= buf[o] ; o += 1
        val <<= 8
        val |= buf[o] ; o += 1
        return val & 0x1fffffff, o

    values = []
    o = 0
    while o < len(buf):
        val, o = nextval(o)
        values.append(val)
    return values
747255
def pack(self):
        

        # Possible structs for packet ID.
        #
        try:
            structs_ = get_structs_for_fields([self.fields[0]])
        except (TypeError):
            # TypeError, if self.fields[0] is a wrong argument to `chr()`.
            raise PackError(self)


        # Possible structs for packet ID + subcode
        #
        if structs_ == []:
            try:
                structs_ = get_structs_for_fields([self.fields[0], self.fields[1]])
            except (IndexError, TypeError):
                # IndexError, if no self.fields[1]
                # TypeError, if self.fields[1] is a wrong argument to `chr()`.
                raise PackError(self)


        # Try to pack the packet with any of the possible structs.
        #
        for struct_ in structs_:
            try:
                return struct_.pack(*self.fields)
            except struct.error:
                pass

        # We only get here if the ``return`` inside the``for`` loop
        # above wasn't reached, i.e. none of the `structs_` matched.
        #
        raise PackError(self)
230021
private String emitDataTypes() {
    StringBuilder dtDefs = new StringBuilder();
    while (emittedDatatypes.size() < datatypes.size()) {
      for (String dt : new HashSet<String>(datatypes)) {
        if (!emittedDatatypes.contains(dt)) {
          StructureDefinition sd = context.fetchResource(StructureDefinition.class,
              ProfileUtilities.sdNs(dt, null));
          // TODO: Figure out why the line below doesn't work
          // if (sd != null && !uniq_structures.contains(sd))
          if(sd != null && !uniq_structure_urls.contains(sd.getUrl()))
            dtDefs.append("\n").append(genShapeDefinition(sd, false));
          emittedDatatypes.add(dt);
        }
      }
    }
    return dtDefs.toString();
  }
866561
def __pack_message(operation, data):
    
    request_id = random.randint(-2 ** 31 - 1, 2 ** 31)
    message = struct.pack("<i", 16 + len(data))
    message += struct.pack("<i", request_id)
    message += __ZERO  # responseTo
    message += struct.pack("<i", operation)
    return (request_id, message + data)
504297
def _raw_hex_id(obj):
    """"""
    # interpret as a pointer since that's what really what id returns
    packed = struct.pack('@P', id(obj))
    return ''.join(map(_replacer, packed))
831877
def idat(self, rows, packed=False):
        """"""
        # http://www.w3.org/TR/PNG/#11IDAT
        filt = Filter(self.bitdepth * self.planes,
                      self.interlace, self.height)
        data = bytearray()

        def byteextend(rowbytes):
            """Default extending data with bytes. Applying filter"""
            data.extend(filt.do_filter(self.filter_type, rowbytes))

        # Choose an extend function based on the bitdepth.  The extend
        # function packs/decomposes the pixel values into bytes and
        # stuffs them onto the data array.
        if self.bitdepth == 8 or packed:
            extend = byteextend
        elif self.bitdepth == 16:
            def extend(sl):
                """Decompose into bytes before byteextend"""
                fmt = '!%dH' % len(sl)
                byteextend(bytearray(struct.pack(fmt, *sl)))
        else:
            # Pack into bytes
            assert self.bitdepth < 8
            # samples per byte
            spb = 8 // self.bitdepth

            def extend(sl):
                """Pack into bytes before byteextend"""
                a = bytearray(sl)
                # Adding padding bytes so we can group into a whole
                # number of spb-tuples.
                l = float(len(a))
                extra = math.ceil(l / float(spb)) * spb - l
                a.extend([0] * int(extra))
                # Pack into bytes
                l = group(a, spb)
                l = [reduce(lambda x, y: (x << self.bitdepth) + y, e)
                     for e in l]
                byteextend(l)

        # Build the first row, testing mostly to see if we need to
        # changed the extend function to cope with NumPy integer types
        # (they cause our ordinary definition of extend to fail, so we
        # wrap it).  See
        # http://code.google.com/p/pypng/issues/detail?id=44
        enumrows = enumerate(rows)
        del rows

        # :todo: Certain exceptions in the call to ``.next()`` or the
        # following try would indicate no row data supplied.
        # Should catch.
        i, row = next(enumrows)
        try:
            # If this fails...
            extend(row)
        except:
            # ... try a version that converts the values to int first.
            # Not only does this work for the (slightly broken) NumPy
            # types, there are probably lots of other, unknown, "nearly"
            # int types it works for.
            def wrapmapint(f):
                return lambda sl: f([int(x) for x in sl])
            extend = wrapmapint(extend)
            del wrapmapint
            extend(row)

        for i, row in enumrows:
            extend(row)
            if len(data) > self.chunk_limit:
                yield bytearray_to_bytes(data)
                # Because of our very witty definition of ``extend``,
                # above, we must re-use the same ``data`` object.  Hence
                # we use ``del`` to empty this one, rather than create a
                # fresh one (which would be my natural FP instinct).
                del data[:]
        if len(data):
            yield bytearray_to_bytes(data)
        self.irows = i + 1
444843
def byteswap(s):
    
    assert len(s) % 2 == 0
    parts = []
    for i in range(0, len(s), 2):
        chunk = s[i:i + 2]
        newchunk = struct.pack('<h', *struct.unpack('>h', chunk))
        parts.append(newchunk)
    return b''.join(parts)
355525
def pack_ihex(type_, address, size, data):
    

    line = '{:02X}{:04X}{:02X}'.format(size, address, type_)

    if data:
        line += binascii.hexlify(data).decode('ascii').upper()

    return ':{}{:02X}'.format(line, crc_ihex(line))
200580
def smartDumpDictHdf5(RV,o):
    
    for key in list(RV.keys()):
        if type(RV[key])==dict:
            g = o.create_group(key)
            smartDumpDictHdf5(RV[key],g)
        else:
            o.create_dataset(name=key,data=SP.array(RV[key]),chunks=True,compression='gzip')
1106129
def standard_dm_read(datatype_num, desc):
    
    nicename, structchar, types = desc

    def dm_read_x(f, outdata=None):
        """Reads (or write if outdata is given) a simple data type.
        returns the data if reading and the number of bytes of header
        """
        if outdata is not None:  # this means we're WRITING to the file
            if verbose:
                print("dm_write start", structchar, outdata, "at", f.tell())
            put_into_file(f, "<" + structchar, outdata)
            if verbose:
                print("dm_write end", f.tell())
            return 0
        else:
            if verbose:
                print("dm_read start", structchar, "at", f.tell())
            result = get_from_file(f, "<" + structchar)
            if verbose:
                print("dm_read end", f.tell())
            return result, 0

    return dm_read_x
1085957
def _pack(self, seq='SEQUNSET'):
        '''
        
        '''

        return 'AT*{clsname}={seq}{argl_wc}\r'.format(
            clsname=type(self).__name__,
            seq=seq,
            argl_wc=b''.join(self._iter_packed_with_comma()).decode()
        ).encode()
134065
def pack(self):
        
        data = []
        for field in self.__fields__:
            (vtype, vlen) = self.__fields_types__[field]
            if vtype == 'char': # string
                data.append(getattr(self, field))
            elif isinstance(vtype, CStructMeta):
                num = int(vlen / vtype.size)
                if num == 1: # single struct
                    v = getattr(self, field, vtype())
                    v = v.pack()
                    if sys.version_info >= (3, 0):
                        v = ([bytes([x]) for x in v])
                    data.extend(v)
                else: # multiple struct
                    values = getattr(self, field, [])
                    for j in range(0, num):
                        try:
                            v = values[j]
                        except:
                            v = vtype()
                        v = v.pack()
                        if sys.version_info >= (3, 0):
                            v = ([bytes([x]) for x in v])
                        data.extend(v)
            elif vlen == 1:
                data.append(getattr(self, field))
            else:
                v = getattr(self, field)
                v = v[:vlen] + [0] * (vlen - len(v))
                data.extend(v)
        return struct.pack(self.__fmt__, *data)
732065
def _formatinfo(format):
    
    size = struct.calcsize(format)
    return size, len(struct.unpack(format, B('\x00') * size))
331827
def format_sdist_header_metadata(data, filename):
    
    description = get_header_description(data)
    config_items = python_version_check(data)
    attrs = dict(config_items)
    name = pop_key(attrs, 'Name', None)

    basename = path.basename(filename)
    if name is None:
        name = basename.split('-')[0]

    package_data = {
        'name': name,
        'summary': pop_key(attrs, 'Summary', None),
        'license': pop_key(attrs, 'License', None),
    }

    release_data = {
        'version': pop_key(attrs, 'Version'),
        'description': pop_key(attrs, 'Description', description),
        'home_page': pop_key(attrs, 'Home-page', None),
    }

    file_data = {
        'basename': basename,
        'attrs': {
            'packagetype': 'sdist',
            'python_version': 'source',
        }
    }

    # Parse multiple keys
    deps = []
    exts = {}
    environments = {}
    for key, val in config_items:
        if key in ['Requires-Dist', 'Requires']:
            name, extras, const, marker, url = parse_specification(val)
            name = norm_package_name(name)
            specs = const.split(',')
            new_specs = []
            for spec in specs:
                pos = [i for i, c in enumerate(spec) if c in '0123456789']
                if pos:
                    pos = pos[0]
                    comp, spec_ = spec[:pos].strip(), spec[pos:].strip()
                    new_specs.append((comp, spec_))

            # TODO: All this is to preserve the format used originally
            # but is this really needed?
            if marker:
                if marker.startswith('extra'):
                    marker = marker.replace('extra', '')
                    marker = marker.replace('==', '').strip()
                    ext = marker.rsplit(' ')[-1]
                    if '"' in ext or "'" in ext:
                        ext = ext[1:-1]

                    if ext not in exts:
                        exts[ext] = [{'name': name, 'specs': new_specs}]
                    else:
                        exts[ext].append({'name': name, 'specs': new_specs})
                else:
                    if marker not in environments:
                        environments[marker] = [{'name': name, 'specs': new_specs}]
                    else:
                        environments[marker].append({'name': name, 'specs': new_specs})
            else:
                deps.append({
                    'name': name,
                    'specs': new_specs,
                })

    deps.sort(key=lambda o: o['name'])

    new_exts = []
    for key, values in exts.items():
        new_exts.append({'name': key, 'depends': values})

    new_environments = []
    for key, values in environments.items():
        new_environments.append({'name': key, 'depends': values})

    file_data.update(dependencies={
        'has_dep_errors': False,
        'depends': deps,
        'extras': new_exts,
        'environments': new_environments,
    })

    return package_data, release_data, file_data
136875
def makeReadPacket(ID, reg, values=None):
	
	pkt = makePacket(ID, xl320.XL320_READ, reg, values)
	return pkt
696141
def _StructPackEncoder(wire_type, format):
  

  value_size = struct.calcsize(format)

  def SpecificEncoder(field_number, is_repeated, is_packed):
    local_struct_pack = struct.pack
    if is_packed:
      tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
      local_EncodeVarint = _EncodeVarint
      def EncodePackedField(write, value):
        write(tag_bytes)
        local_EncodeVarint(write, len(value) * value_size)
        for element in value:
          write(local_struct_pack(format, element))
      return EncodePackedField
    elif is_repeated:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeRepeatedField(write, value):
        for element in value:
          write(tag_bytes)
          write(local_struct_pack(format, element))
      return EncodeRepeatedField
    else:
      tag_bytes = TagBytes(field_number, wire_type)
      def EncodeField(write, value):
        write(tag_bytes)
        return write(local_struct_pack(format, value))
      return EncodeField

  return SpecificEncoder
907906
def _write_dihedral_information(gsd_file, structure):
    

    gsd_file.dihedrals.N = len(structure.rb_torsions)

    unique_dihedral_types = set()
    for dihedral in structure.rb_torsions:
        t1, t2 = dihedral.atom1.type, dihedral.atom2.type
        t3, t4 = dihedral.atom3.type, dihedral.atom4.type
        if [t2, t3] == sorted([t2, t3], key=natural_sort):
            dihedral_type = ('-'.join((t1, t2, t3, t4)))
        else:
            dihedral_type = ('-'.join((t4, t3, t2, t1)))
        unique_dihedral_types.add(dihedral_type)
    unique_dihedral_types = sorted(list(unique_dihedral_types), key=natural_sort)
    gsd_file.dihedrals.types = unique_dihedral_types

    dihedral_typeids = []
    dihedral_groups = []
    for dihedral in structure.rb_torsions:
        t1, t2 = dihedral.atom1.type, dihedral.atom2.type
        t3, t4 = dihedral.atom3.type, dihedral.atom4.type
        if [t2, t3] == sorted([t2, t3], key=natural_sort):
            dihedral_type = ('-'.join((t1, t2, t3, t4)))
        else:
            dihedral_type = ('-'.join((t4, t3, t2, t1)))
        dihedral_typeids.append(unique_dihedral_types.index(dihedral_type))
        dihedral_groups.append((dihedral.atom1.idx, dihedral.atom2.idx,
                                dihedral.atom3.idx, dihedral.atom4.idx))

    gsd_file.dihedrals.typeid = dihedral_typeids
    gsd_file.dihedrals.group = dihedral_groups