sys.hexversion

Here are the examples of the python api sys.hexversion taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

106 Examples 7

5 Source : testlex.py
with Apache License 2.0
from gethue

def make_pymodule_path(filename, optimization=None):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3050000:
        fullpath = cache_from_source(filename, optimization=optimization)
    elif sys.hexversion >= 0x3040000:
        fullpath = cache_from_source(filename, ext=='.pyc')
    elif sys.hexversion >= 0x3020000:
        import imp
        modname = mod+"."+imp.get_tag()+ext
        fullpath = os.path.join(path,'__pycache__',modname)
    else:
        fullpath = filename
    return fullpath

def pymodule_out_exists(filename, optimization=None):

5 Source : pydev_versioncheck.py
with GNU General Public License v3.0
from xbaysal11

def versionok_for_gui():
    ''' Return True if running Python is suitable for GUI Event Integration and deeper IPython integration '''
    # We require Python 2.6+ ...
    if sys.hexversion   <   0x02060000:
        return False
    # Or Python 3.2+
    if sys.hexversion >= 0x03000000 and sys.hexversion  <  0x03020000:
        return False
    # Not supported under Jython nor IronPython
    if sys.platform.startswith("java") or sys.platform.startswith('cli'):
        return False

    return True

3 Source : driver.py
with MIT License
from AgenteDog

    def _generate(self):
        """ Generate the Python code. """

        if sys.hexversion >= 0x03000000:
            if self._opts.output == '-':
                from io import TextIOWrapper

                pyfile = TextIOWrapper(sys.stdout.buffer, encoding='utf8')
            else:
                pyfile = open(self._opts.output, 'wt', encoding='utf8')
        else:
            if self._opts.output == '-':
                pyfile = sys.stdout
            else:
                pyfile = open(self._opts.output, 'wt')

        compileUi(self._ui_file, pyfile, self._opts.execute, self._opts.indent, self._opts.from_imports)

    def on_IOError(self, e):

3 Source : py37compat.py
with MIT License
from Air-999

def _pythonlib_compat():
    """
    On Python 3.7 and earlier, distutils would include the Python
    library. See pypa/distutils#9.
    """
    from distutils import sysconfig
    if not sysconfig.get_config_var('Py_ENABLED_SHARED'):
        return

    yield 'python{}.{}{}'.format(
        sys.hexversion >> 24,
        (sys.hexversion >> 16) & 0xff,
        sysconfig.get_config_var('ABIFLAGS'),
    )


def compose(f1, f2):

3 Source : Bake.py
with GNU General Public License v2.0
from CN-TU

    def checkPythonVersion(self):
        """ Checks the version  of the user's machine python. python-2.6 or
            2.7 supported"""

        if sys.hexversion   <   0x02060000:
            print(">>> Old Python version detected, please use python2 >= version 2.6")
            sys.exit(1)

    def main(self, argv):

3 Source : Marshallers.py
with Apache License 2.0
from Enigma644

    def __init__(self):
        NumpyScalarArrayMarshaller.__init__(self)
        # In Python 3, the unicode and bare bytes type strings are str
        # and bytes, but before Python 3, they were unicode and str
        # respectively. The Python 3 python_type_strings will be used,
        # though.
        if sys.hexversion >= 0x03000000:
            self.types = [str, bytes, bytearray]
        else:
            self.types = [unicode, str, bytearray]
        self.python_type_strings = ['str', 'bytes', 'bytearray']
        # As the parent class already has MATLAB strings handled, there
        # are no MATLAB classes that this marshaller should be used for.
        self.matlab_classes = []

        # Update the type lookups.
        self.update_type_lookups()

    def write(self, f, grp, name, data, type_string, options):

3 Source : Marshallers.py
with Apache License 2.0
from Enigma644

    def write(self, f, grp, name, data, type_string, options):
        # data just needs to be converted to a numpy string of the
        # appropriate type (str to np.str_ and the others to np.bytes_).
        if (sys.hexversion >= 0x03000000 and isinstance(data, str)) \
                or (sys.hexversion   <   0x03000000 \
                and isinstance(data, unicode)):
            cdata = np.unicode_(data)
        else:
            cdata = np.bytes_(data)

        # Now pass it to the parent version of this function to write
        # it. The proper type_string needs to be grabbed now as the
        # parent function will have a modified form of data to guess
        # from if not given the right one explicitly.
        NumpyScalarArrayMarshaller.write(self, f, grp, name, cdata,
                                         self.get_type_string(data,
                                         type_string), options)

    def read(self, f, dsetgrp, attributes, options):

3 Source : make_randoms.py
with Apache License 2.0
from Enigma644

def random_str_ascii_letters(length):
    # Makes a random ASCII str of the specified length.
    if sys.hexversion >= 0x03000000:
        ltrs = string.ascii_letters
        return ''.join([random.choice(ltrs) for i in
                       range(0, length)])
    else:
        ltrs = unicode(string.ascii_letters)
        return unicode('').join([random.choice(ltrs) for i in
                                 range(0, length)])


def random_str_ascii(length):

3 Source : make_randoms.py
with Apache License 2.0
from Enigma644

def random_str_ascii(length):
    # Makes a random ASCII str of the specified length.
    if sys.hexversion >= 0x03000000:
        ltrs = string.ascii_letters + string.digits
        return ''.join([random.choice(ltrs) for i in
                       range(0, length)])
    else:
        ltrs = unicode(string.ascii_letters + string.digits)
        return unicode('').join([random.choice(ltrs) for i in
                                 range(0, length)])


def random_str_some_unicode(length):

3 Source : make_randoms.py
with Apache License 2.0
from Enigma644

def random_str_some_unicode(length):
    # Makes a random ASCII+limited unicode str of the specified
    # length.
    ltrs = random_str_ascii(10)
    if sys.hexversion >= 0x03000000:
        ltrs += 'αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩς'
        c = ''
    else:
        ltrs += unicode('αβγδεζηθικλμνξοπρστυφχψω'
                        + 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩς', 'utf-8')
        c = unicode('')
    return c.join([random.choice(ltrs) for i in range(0, length)])


def random_bytes(length):

3 Source : test_string_utf16_conversion.py
with Apache License 2.0
from Enigma644

def test_conv_utf16():
    if sys.hexversion   <   0x3000000:
        tps = (unicode, np.unicode_)
    else:
        tps = (str, np.unicode_)
    for tp in tps:
        yield check_conv_utf16, tp

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def check_dict_like_key_null_character(self, tp):
        data = random_dict(tp)
        if sys.hexversion >= 0x03000000:
            ch = '\x00'
        else:
            ch = unicode('\x00')
        key = ch.join([random_str_ascii(max_dict_key_length)
                      for i in range(2)])
        data[key] = random_int()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def check_dict_like_key_forward_slash(self, tp):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def check_dict_like_key_forward_slash(self, tp):
        data = random_dict(tp)
        if sys.hexversion >= 0x03000000:
            ch = '/'
        else:
            ch = unicode('/')
        key = ch.join([random_str_ascii(max_dict_key_length)
                      for i in range(2)])
        data[key] = random_int()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def check_dict_like_key_back_slash(self, tp):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def check_dict_like_key_back_slash(self, tp):
        data = random_dict(tp)
        if sys.hexversion >= 0x03000000:
            ch = '\\'
        else:
            ch = unicode('\\')
        key = ch.join([random_str_ascii(max_dict_key_length)
                      for i in range(2)])
        data[key] = random_int()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def check_dict_like_key_leading_periods(self, tp):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def check_dict_like_key_leading_periods(self, tp):
        data = random_dict(tp)
        if sys.hexversion >= 0x03000000:
            prefix = '.' * random.randint(1, 10)
        else:
            prefix = unicode('.') * random.randint(1, 10)
        key = prefix + random_str_ascii(max_dict_key_length)
        data[key] = random_int()
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def test_None(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_long_needs_32_bits(self):
        if sys.hexversion   <   0x03000000:
            data = long(random_int())
            out = self.write_readback(data, random_name(),
                                      self.options)
            self.assert_equal(out, data)

    # Only relevant in Python 2.x.
    def test_long_needs_64_bits(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_long_needs_64_bits(self):
        if sys.hexversion   <   0x03000000:
            data = long(2)**32 * long(random_int())
            out = self.write_readback(data, random_name(),
                                      self.options)
            self.assert_equal(out, data)

    def test_int_or_long_too_big(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_int_or_long_too_big(self):
        if sys.hexversion >= 0x03000000:
            data = 2**64 * random_int()
        else:
            data = long(2)**64 * long(random_int())
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def test_float(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_str_ascii_encoded_utf8(self):
        ltrs = string.ascii_letters + string.digits
        data = 'a'
        if sys.hexversion   <   0x03000000:
            data = unicode(data)
            ltrs = unicode(ltrs)
        while all([(c in ltrs) for c in data]):
            data = random_str_some_unicode(random.randint(1, \
                max_string_length))
        data = data.encode('utf-8')
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def test_str_with_null(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_str_with_null(self):
        strs = [random_str_ascii(
                random.randint(1, max_string_length))
                for i in range(2)]
        if sys.hexversion   <   0x03000000:
            data = unicode('\x00').join(strs)
        else:
            data = '\x00'.join(strs)
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def test_str_unicode(self):

3 Source : test_write_readback.py
with Apache License 2.0
from Enigma644

    def test_bytes_with_null(self):
        strs = [random_bytes(
                random.randint(1, max_string_length))
                for i in range(2)]
        if sys.hexversion   <   0x03000000:
            data = '\x00'.join(strs)
        else:
            data = b'\x00'.join(strs)
        out = self.write_readback(data, random_name(),
                                  self.options)
        self.assert_equal(out, data)

    def test_bytearray(self):

3 Source : testyacc.py
with Apache License 2.0
from gethue

def make_pymodule_path(filename):
    path = os.path.dirname(filename)
    file = os.path.basename(filename)
    mod, ext = os.path.splitext(file)

    if sys.hexversion >= 0x3040000:
        import importlib.util
        fullpath = importlib.util.cache_from_source(filename, ext=='.pyc')
    elif sys.hexversion >= 0x3020000:
        import imp
        modname = mod+"."+imp.get_tag()+ext
        fullpath = os.path.join(path,'__pycache__',modname)
    else:
        fullpath = filename
    return fullpath

def pymodule_out_exists(filename):

3 Source : test_sslsocket.py
with Apache License 2.0
from gethue

    def _assert_raises(self, exc):
        if sys.hexversion >= 0x020700F0:
            return self.assertRaises(exc)
        else:
            return AssertRaises(exc)

    def _assert_connection_success(self, server, path=None, **client_args):

3 Source : file_write.py
with Apache License 2.0
from hanranCode

def decode_strs(strs):
    # py2 
    if sys.hexversion   <   0x3000000:
        strs = strs.decode('utf-8')
    return strs


def writeExcel(write_infos_mul, excel_path, sheetNames=None, set_color=False):

3 Source : pybass.py
with GNU General Public License v3.0
from HsOjo

def string_for_print(value, codec='cp1251'):
    if sys.hexversion   <   0x03000000:
        return value
    else:
        return str(value, codec)


def seconds_to_string(value):

3 Source : pybass.py
with MIT License
from J-CITY

def string_for_print(value, codec = 'cp1251'):
	if sys.hexversion   <   0x03000000:
		return value
	else:
		return str(value, codec)

def seconds_to_string(value):

3 Source : test_init.py
with MIT License
from jest-community

def test_setup_without_tls_config_uses_tlsv1_under_python36(hass):
    """Test setup defaults to TLSv1 under python3.6."""
    test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker'}}

    with mock.patch('homeassistant.components.mqtt.MQTT') as mock_MQTT:
        yield from async_setup_component(hass, mqtt.DOMAIN, test_broker_cfg)

    assert mock_MQTT.called

    import sys
    if sys.hexversion >= 0x03060000:
        expectedTlsVersion = ssl.PROTOCOL_TLS  # pylint: disable=no-member
    else:
        expectedTlsVersion = ssl.PROTOCOL_TLSv1

    assert mock_MQTT.mock_calls[0][1][14] == expectedTlsVersion


@asyncio.coroutine

3 Source : sr_config.py
with GNU General Public License v2.0
from MetPX

    def xcl( self, x ):
        if sys.hexversion > 0x03030000 :
            return x.__qualname__.split('.')[0] + ' '
        else:
            return x.__name__ + ' '

    def log_settings(self):

3 Source : base.py
with GNU General Public License v3.0
from Mulab11

def get_tool_conf():
	def get_sys_env():
		return '$'.join([
			os.environ[key]
			for key in ['OS', 'SESSIONNAME', 'USERNAME', 'COMPUTERNAME', 'USERDOMAIN', 'USER', 'SHELL', 'SESSION'] \
			if key in os.environ
		] + ['py%x' % sys.hexversion])
	global env_conf
	try:
		tool_conf = json.loads(open(pjoin(tool_path, 'conf.json'), 'rb').read().decode('utf-8'))
	except:
		tool_conf = {}
	sys_env = get_sys_env()
	if sys_env not in tool_conf:
		tool_conf[sys_env] = {}
	env_conf = tool_conf[sys_env]
	return tool_conf

def check_install(pack):

3 Source : checkData.py
with BSD 2-Clause "Simplified" License
from palexandremello

def pythonVersion():
## The function is necessary to check yout python version 
# IF the your python is   <   or equal 2.7 so put a True bool
## Else (python > 2.7) put a False bool 
    import sys
    if (sys.hexversion  < = 34017264):
        return True
    else:
        return False

def setHome():

3 Source : dictmanager.py
with GNU General Public License v3.0
from sth2018

    def on_edit(self, path):
        '''edit dictionary file'''
        d = QDialog(self)
        frm = Ui_Dialog()
        frm.setupUi(d)
        d.setWindowTitle(os.path.basename(path))
        # 2x3 compatible
        if sys.hexversion >= 0x03000000:
            frm.text.setPlainText(open(path, 'r', encoding="utf-8").read())
        else:
            frm.text.setPlainText(unicode(open(path).read(), "utf8"))
        d.accepted.connect(lambda: self.on_accept_edit(path, frm))
        d.exec_()

    def on_accept_edit(self, path, frm):

3 Source : dictmanager.py
with GNU General Public License v3.0
from sth2018

    def on_accept_edit(self, path, frm):
        '''save dictionary file'''
        # 2x3 compatible
        if sys.hexversion >= 0x03000000:
            open(path, "w", encoding='utf-8').write(frm.text.toPlainText())
        else:
            open(path, "w").write(frm.text.toPlainText().encode("utf8"))

    def accept(self):

3 Source : options.py
with GNU General Public License v3.0
from sth2018

    def changedTab(self, i):
        if not isMac or sys.hexversion >= 0x03000000:
            # restore
            for k in range(0, len(self.tabs)):
                self.tab_widget.setTabIcon(k, self._NULL_ICON)
            # add flag
            self.tab_widget.setTabIcon(i, self._OK_ICON)
        self.tabs[i].build_layout()

    def show_models(self):

3 Source : test_python.py
with MIT License
from tr11

def test_load_from_module():  # type: ignore
    from . import python_config

    cfg = config_from_python(python_config, prefix="CONFIG", lowercase_keys=True)
    assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
    assert cfg["sys.version"] == sys.hexversion


def test_load_from_path():  # type: ignore

3 Source : test_python.py
with MIT License
from tr11

def test_load_from_path():  # type: ignore
    path = os.path.join(os.path.dirname(__file__), "python_config.py")
    cfg = config_from_python(path, prefix="CONFIG", lowercase_keys=True)
    assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
    assert cfg["sys.version"] == sys.hexversion


def test_load_from_module_string():  # type: ignore

3 Source : test_python.py
with MIT License
from tr11

def test_load_from_module_string():  # type: ignore
    path = "tests.python_config"
    cfg = config_from_python(path, prefix="CONFIG", lowercase_keys=True)
    assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1}
    assert cfg["sys.version"] == sys.hexversion


def test_equality():  # type: ignore

3 Source : tcpros_service.py
with GNU General Public License v3.0
from Wei1234c

    def _write_service_error(self, transport, err_msg):
        """
        Send error message to client
        @param transport: transport connection to client 
        @type  transport: Transport
        @param err_msg: error message to send to client
        @type  err_msg: str
        """
        if sys.hexversion > 0x03000000: #Python3
            err_msg = bytes(err_msg, 'utf-8')
        transport.write_data(struct.pack('  <  BI%ss'%len(err_msg), 0, len(err_msg), err_msg))

    def _handle_request(self, transport, request):

3 Source : utils.py
with BSD 3-Clause "New" or "Revised" License
from yannvgn

    def __enter__(self):
        #pylint: disable=import-outside-toplevel
        import sys

        if self.original_sre_parse_uniq is None and (
                0x03070000   <  = sys.hexversion  < = 0x030704f0
                or 0x03080000  < = sys.hexversion  < = 0x030800b3):
            try:
                import sre_parse
                self.sre_parse = sre_parse
                #pylint: disable=protected-access
                self.original_sre_parse_uniq = sre_parse._uniq
                sre_parse._uniq = lambda x: list(dict.fromkeys(x))
            except (ImportError, AttributeError):
                self.sre_parse = None
                self.original_sre_parse_uniq = None

    def __exit__(self, type_, value, traceback):

3 Source : varobf.py
with MIT License
from zevtyardt

    def handle_argument(self, elements, symtabs):
        if len(elements) >= 4:
            if sys.hexversion >= 0x2040000:
                keyword = elements[1][1][1][1][1][1][1][1][1][1][1][1][1][1][1]
            else:
                keyword = elements[1][1][1][1][1][1][1][1][1][1][1][1][1][1]
            assert keyword[0] == token.NAME
            keyword_id = keyword[1]
            keyword_line = keyword[2]
            self.addToNames(keyword_line, keyword_id, False)
            elements = elements[3]
        for node in elements:
            self.walk(node, symtabs)

    def handle_lambdef(self, elements, symtabs):

2 Source : Marshallers.py
with Apache License 2.0
from Enigma644

    def read(self, f, dsetgrp, attributes, options):
        dset = dsetgrp

        # Get the different attributes this marshaller uses.

        type_string = convert_attribute_to_string( \
            attributes['Python.Type'])
        underlying_type = convert_attribute_to_string( \
            attributes['Python.numpy.UnderlyingType'])
        shape = attributes['Python.Shape']
        container = convert_attribute_to_string( \
            attributes['Python.numpy.Container'])
        python_empty = attributes['Python.Empty']
        python_fields = convert_attribute_to_string_array( \
            attributes['Python.Fields'])

        matlab_class = convert_attribute_to_string( \
            attributes['MATLAB_class'])
        matlab_empty = attributes['MATLAB_empty']

        # We can actually read the MATLAB_fields Attribute if it is
        # present.
        matlab_fields = attributes['MATLAB_fields']

        # If it is a Dataset, it can simply be read and then acted upon
        # (if it is an HDF5 Reference array, it will need to be read
        # recursively). If it is a Group, then it is a structured
        # ndarray like object that needs to be read field wise and
        # constructed.
        if isinstance(dset, h5py.Dataset):
            # Read the data.
            data = dset[...]

            # If it is a reference type, then we need to make an object
            # array that is its replicate, but with the objects they are
            # pointing to in their elements instead of just the
            # references.
            if h5py.check_dtype(ref=dset.dtype) is not None:
                data = read_object_array(f, data, options)
        else:
            # Starting with an empty dict, all that has to be done is
            # iterate through all the Datasets and Groups in dset
            # and add them to a dict with their name as the key. Since
            # we don't want an exception thrown by reading an element to
            # stop the whole reading process, the reading is wrapped in
            # a try block that just catches exceptions and then does
            # nothing about them (nothing needs to be done). We also
            # need to keep track of whether any of the fields are
            # Groups, aren't Reference arrays, or have attributes other
            # than H5PATH since that means that the fields are the
            # values (single element structured ndarray), as opposed to
            # Reference arrays to all the values (multi-element structed
            # ndarray). In Python 2, the field names need to be
            # converted to str from unicode when storing the fields in
            # struct_data.
            struct_data = dict()
            is_multi_element = True
            for k in dset:
                # Unescape the name.
                unescaped_k = unescape_path(k)
                # We must exclude group_for_references
                if dset[k].name == options.group_for_references:
                    continue
                fld = dset[k]
                if isinstance(fld, h5py.Group) \
                        or h5py.check_dtype(ref=fld.dtype) is None \
                        or len(set(fld.attrs) \
                        & ((set(self.python_attributes) \
                        | set(self.matlab_attributes))
                        - set(['H5PATH', 'MATLAB_empty',
                        'Python.Empty']))) != 0:
                    is_multi_element = False
                try:
                    struct_data[unescaped_k] = read_data(f, dset,
                                                         k, options)
                except:
                    pass

            # If it isn't multi element, we need to pack all the values
            # in struct_array inside of numpy.object_'s so that the code
            # after this that depends on this will work.
            if not is_multi_element:
                for k, v in struct_data.items():
                    obj = np.zeros((1,), dtype='object')
                    obj[0] = v
                    struct_data[k] = obj

            # The dtype for the structured ndarray needs to be
            # composed. This is done by going through each field (in the
            # proper order, if the fields were given, or any order if
            # not) and determine the dtype and shape of that field to
            # put in the list.

            if python_fields is not None or matlab_fields is not None:
                if python_fields is not None:
                    fields = [unescape_path(k) for k in python_fields]
                else:
                    fields = [unescape_path(k.tostring().decode())
                              for k in matlab_fields]
                # Now, there may be fields available that were not
                # given, but still should be read. Keys that are not in
                # python_fields need to be added to the list.
                extra_fields = list(set(struct_data)
                                    - set(fields))
                fields.extend(sorted(extra_fields))
            else:
                fields = sorted(struct_data)

            dt_whole = []
            for k in fields:
                # In Python 2, the field names for a structured ndarray
                # must be str as opposed to unicode, so k needs to be
                # converted in the Python 2 case.
                if sys.hexversion >= 0x03000000:
                    k_name = k
                else:
                    k_name = k.encode('UTF-8')

                # Read the value.
                v = struct_data[k]

                # If any of the elements are not Numpy types or if they
                # don't all have the exact same dtype and shape, then
                # this field will just be an object field.
                if v.size == 0 or type(v.flat[0]) \
                        not in self._numpy_types:
                    dt_whole.append((k_name, 'object'))
                    continue

                first = v.flat[0]
                dt = first.dtype
                sp = first.shape
                all_same = True
                try:
                    for x in v.flat:
                        if dt != x.dtype or sp != x.shape:
                            all_same = False
                            break
                except:
                    all_same = False

                # If they are all the same, then dt and shape should be
                # used. Otherwise, it has to be object.
                if all_same:
                    dt_whole.append((k_name, dt, sp))
                else:
                    dt_whole.append((k_name, 'object'))

            # Make the structured ndarray with the constructed
            # dtype. The shape is simply the shape of the object arrays
            # of its fields, so we might as well use the shape of
            # v. Then, all the elements of every field need to be
            # assigned. Now, if dtype's itemsize is 0, a TypeError will
            # be thrown by numpy due to a bug in numpy. np.zeros (as
            # well as ones and empty) does not like to make arrays with
            # no bytes. A workaround is to make an empty array of some
            # other type and convert its dtype. The smallest one we can
            # make is an np.int8([]). Yes, one byte will be wasted, but
            # at least no errors will happen.
            dtwhole = np.dtype(dt_whole)
            if dtwhole.itemsize == 0:
                data = np.zeros(shape=v.shape,
                                dtype='int8').astype(dtwhole)
            else:
                data = np.zeros(shape=v.shape, dtype=dtwhole)

            for k, v in struct_data.items():
                if sys.hexversion   <   0x03000000:
                    k = k.encode('UTF-8')
                # There is no sense iterating through the elements if
                # the shape is an empty shape.
                if all(data.shape) and all(v.shape):
                    for index, x in np.ndenumerate(v):
                        data[k][index] = x

        # If metadata is present, that can be used to do convert to the
        # desired/closest Python data types. If none is present, or not
        # enough of it, then no conversions can be done.
        if type_string is not None and underlying_type is not None and \
                shape is not None:
            # If the Attributes 'Python.Fields' and/or 'MATLAB_fields'
            # are present, the underlying type needs to be changed to
            # the proper dtype for the structure.
            if python_fields is not None or matlab_fields is not None:
                if python_fields is not None:
                    fields = [unescape_path(k) for k in python_fields]
                else:
                    fields = [k.tostring().decode()
                              for k in matlab_fields]
                struct_dtype = list()
                for k in fields:
                    if sys.hexversion >= 0x03000000:
                        struct_dtype.append((k, 'object'))
                    else:
                        struct_dtype.append((k.encode('UTF-8'),
                                            'object'))
            else:
                struct_dtype = None

            # If it is empty ('Python.Empty' set to 1), then the shape
            # information is stored in data and we need to set data to
            # the empty array of the proper type (in underlying_type)
            # and the given shape. If we are going to transpose it
            # later, we need to transpose it now so that it still keeps
            # the right shape. Also, if it is a structure that we just
            # figured out the dtype for, that needs to be used.
            if python_empty == 1:
                if underlying_type.startswith('bytes'):
                    if underlying_type == 'bytes':
                        nchars = 1
                    else:
                        nchars = int(int(
                                     underlying_type[len('bytes'):])
                                     / 8)
                    data = np.zeros(tuple(shape),
                                    dtype='S' + str(nchars))
                elif underlying_type.startswith('str'):
                    if underlying_type == 'str':
                        nchars = 1
                    else:
                        nchars = int(int(
                                     underlying_type[len('str'):])
                                     / 32)
                    data = np.zeros(tuple(shape),
                                    dtype='U' + str(nchars))
                elif struct_dtype is not None:
                    data = np.zeros(tuple(shape),
                                    dtype=struct_dtype)
                else:
                    data = np.zeros(tuple(shape),
                                    dtype=underlying_type)
                if matlab_class is not None or \
                        options.reverse_dimension_order:
                    data = data.T

            # If it is a complex type, then it needs to be decoded
            # properly.
            if underlying_type.startswith('complex'):
                data = decode_complex(data)

            # If its underlying type is 'bool' but it is something else,
            # then it needs to be converted (means it was written with
            # the convert_bools_to_uint8 option).
            if underlying_type == 'bool' and data.dtype.name != 'bool':
                data = np.bool_(data)

            # If MATLAB attributes are present or the reverse dimension
            # order option was given, the dimension order needs to be
            # reversed. This needs to be done before any reshaping as
            # the shape was stored before any dimensional reordering.
            if matlab_class is not None or \
                    options.reverse_dimension_order:
                data = data.T

            # String types might have to be decoded depending on the
            # underlying type, and MATLAB class if given. They also need
            # to be properly decoded into strings of the right length if
            # it originally represented an array of strings (turned into
            # uints of some sort). The length in bits is contained in
            # the dtype name, which is the underlying_type.
            if underlying_type.startswith('bytes'):
                if underlying_type == 'bytes':
                    data = np.bytes_(b'')
                else:
                    data = convert_to_numpy_bytes(data, \
                        length=int(underlying_type[5:])//8)
            elif underlying_type.startswith('str') \
                    or matlab_class == 'char':
                if underlying_type == 'str':
                    data = np.unicode_('')
                elif underlying_type.startswith('str'):
                    data = convert_to_numpy_str(data, \
                        length=int(underlying_type[3:])//32)
                else:
                    data = convert_to_numpy_str(data)

            # If the shape of data and the shape attribute are
            # different but give the same number of elements, then data
            # needs to be reshaped.
            if tuple(shape) != data.shape \
                    and np.prod(shape) == np.prod(data.shape):
                data.shape = tuple(shape)

            # If data is a structured ndarray and the type string says
            # it is a recarray, then turn it into one.
            if type_string == 'numpy.recarray':
                data = data.view(np.core.records.recarray)

            # Convert to scalar, matrix, chararray, or ndarray depending
            # on the container type. For an empty scalar string, it
            # needs to be manually set to '' and b'' or there will be
            # problems.
            if container == 'scalar':
                if underlying_type.startswith('bytes'):
                    if python_empty == 1:
                        data = np.bytes_(b'')
                    elif isinstance(data, np.ndarray):
                        data = data.flat[0]
                elif underlying_type.startswith('str'):
                    if python_empty == 1:
                        data = np.unicode_('')
                    elif isinstance(data, np.ndarray):
                        data = data.flat[0]
                else:
                    data = data.flat[0]
            elif container == 'matrix':
                data = np.asmatrix(data)
            elif container == 'chararray':
                data = data.view(np.chararray)
            elif container == 'ndarray':
                data = np.asarray(data)

        elif matlab_class in self.__MATLAB_classes_reverse:
            # MATLAB formatting information was given. The extraction
            # did most of the work except handling empties, array
            # dimension order, and string conversion.

            # If it is empty ('MATLAB_empty' set to 1), then the shape
            # information is stored in data and we need to set data to
            # the empty array of the proper type. If it is a MATLAB
            # struct, then the proper dtype has to be constructed from
            # the field names if present (the dtype of each individual
            # field is set to object).
            if matlab_empty == 1:
                if matlab_fields is None:
                    data = np.zeros(tuple(np.uint64(data)), \
                        dtype=self.__MATLAB_classes_reverse[ \
                        matlab_class])
                else:
                    dt_whole = list()
                    for k in matlab_fields:
                        uk = unescape_path(k.tostring())
                        if sys.hexversion >= 0x03000000:
                            dt_whole.append((uk, 'object'))
                        else:
                            dt_whole.append((uk.encode('utf-8'),
                                             'object'))
                    data = np.zeros(shape=tuple(np.uint64(data)),
                                    dtype=dt_whole)

            # The order of the dimensions must be switched from Fortran
            # order which MATLAB uses to C order which Python uses.
            data = data.T

            # Now, if the matlab class is 'single' or 'double', data
            # could possibly be a complex type which needs to be
            # properly decoded.
            if matlab_class in ['single', 'double']:
                data = decode_complex(data)

            # If it is a logical, then it must be converted to
            # numpy.bool8.
            if matlab_class == 'logical':
                data = np.bool_(data)

            # If it is a 'char' type, the proper conversion to
            # numpy.unicode needs to be done unless it is currently a
            # numpy.bytes_ in which case it will be preserved.
            if matlab_class == 'char':
                if data.dtype.type != np.bytes_:
                    data = convert_to_numpy_str(data)

        # Done adjusting data, so it can be returned.
        return data


class PythonScalarMarshaller(NumpyScalarArrayMarshaller):

2 Source : utilities.py
with Apache License 2.0
from Enigma644

def process_path(pth):
    """ Processes paths.

    Processes the provided path and breaks it into it Group part
    (`groupname`) and target part (`targetname`). ``bytes`` paths are
    converted to ``str``. Separated paths are given as an iterable of
    ``str`` and ``bytes``. Each part of a separated path is escaped
    using ``escape_path``. Otherwise, the path is assumed to be already
    escaped. Escaping is done so that targets with a part that starts
    with one or more periods, contain slashes, and/or contain nulls can
    be used without causing the wrong Group to be looked in or the wrong
    target to be looked at. It essentially allows one to make a Dataset
    named ``'..'`` or ``'a/a'`` instead of moving around in the Dataset
    hierarchy.

    All paths are POSIX style.

    .. versionadded:: 0.2

    Parameters
    ----------
    pth : str or bytes or iterable of str or bytes
        The POSIX style path as a ``str`` or ``bytes`` or the
        separated path in an iterable with the elements being ``str``
        and ``bytes``. For separated paths, escaping will be done
        on each part.

    Returns
    -------
    groupname : str
        The path to the Group containing the target `pth` was pointing
        to.
    targetname : str
        The name of the target pointed to by `pth` in the Group
        `groupname`.

    Raises
    ------
    TypeError
        If `pth` is not of the right type.

    See Also
    --------
    escape_path

    """
    # Do conversions and possibly escapes.
    if isinstance(pth, bytes):
        p = pth.decode('utf-8')
    elif (sys.hexversion >= 0x03000000 and isinstance(pth, str)) \
            or (sys.hexversion   <   0x03000000 \
            and isinstance(pth, unicode)):
        p = pth
    elif not isinstance(pth, collections.Iterable):
        raise TypeError('p must be str, bytes, or an iterable '
                        + 'solely of one of those two.')
    else:
        # Check that all elements are unicode or bytes.
        if sys.hexversion >= 0x03000000:
            if not all([isinstance(s, (bytes, str)) for s in pth]):
                raise TypeError('Elements of p must be str or bytes.')
        else:
            if not all([isinstance(s, (str, unicode)) for s in pth]):
                raise TypeError('Elements of p must be str or '
                                + 'unicode.')

        # Escape (and possibly convert to unicode) each element and then
        # join them all together.
        parts = [None] * len(pth)
        for i, s in enumerate(pth):
            if isinstance(s, bytes):
                s = s.decode('utf-8')
            parts[i] = escape_path(s)
        parts = tuple(parts)
        p = posixpath.join(*parts)

    # Remove double slashes and a non-root trailing slash.
    path = posixpath.normpath(p)

    # Extract the group name and the target name (will be a dataset if
    # data can be mapped to it, but will end up being made into a group
    # otherwise. As HDF5 files use posix path, conventions, posixpath
    # will do everything.
    groupname = posixpath.dirname(path)
    targetname = posixpath.basename(path)

    # If groupname got turned into blank, then it is just root.
    if len(groupname) == 0:
        groupname = b'/'.decode('ascii')

    # If targetname got turned blank, then it is the current directory.
    if len(targetname) == 0:
        targetname = b'.'.decode('ascii')

    return groupname, targetname


def does_dtype_have_a_zero_shape(dt):

2 Source : asserts.py
with Apache License 2.0
from Enigma644

def assert_equal_none_format(a, b, options=None):
    # Compares a and b for equality. b is always the original. If they
    # are dictionaries, a must be a structured ndarray and they must
    # have the same set of keys, after which they values must all be
    # compared. If they are a collection type (list, tuple, set,
    # frozenset, or deque), then the compairison must be made with b
    # converted to an object array. If the original is not a numpy type
    # (isn't or doesn't inherit from np.generic or np.ndarray), then it
    # is a matter of converting it to the appropriate numpy
    # type. Otherwise, both are supposed to be numpy types. For object
    # arrays, each element must be iterated over to be compared. Then,
    # if it isn't a string type, then they must have the same dtype,
    # shape, and all elements. If it is an empty string, then it would
    # have been stored as just a null byte (recurse to do that
    # comparison). If it is a bytes_ type, the dtype, shape, and
    # elements must all be the same. If it is string_ type, we must
    # convert to uint32 and then everything can be compared. Big longs
    # and ints get written as numpy.bytes_.
    if type(b) == dict or (sys.hexversion >= 0x2070000
                           and type(b) == collections.OrderedDict):
        assert_equal_nose(type(a), np.ndarray)
        assert a.dtype.names is not None

        # Determine if any of the keys could not be stored as str. If
        # they all can be, then the dtype field names should be the
        # keys. Otherwise, they should be 'keys' and 'values'.
        all_str_keys = True
        if sys.hexversion >= 0x03000000:
            tp_str = str
            tp_bytes = bytes
            converters = {tp_str: lambda x: x,
                          tp_bytes: lambda x: x.decode('UTF-8'),
                          np.bytes_:
                          lambda x: bytes(x).decode('UTF-8'),
                          np.unicode_: lambda x: str(x)}
            tp_conv = lambda x: converters[type(x)](x)
            tp_conv_str = lambda x: tp_conv(x)
        else:
            tp_str = unicode
            tp_bytes = str
            converters = {tp_str: lambda x: x,
                          tp_bytes: lambda x: x.decode('UTF-8'),
                          np.bytes_:
                          lambda x: bytes(x).decode('UTF-8'),
                          np.unicode_: lambda x: unicode(x)}
            tp_conv = lambda x: converters[type(x)](x)
            tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
        tps = tuple(converters.keys())
        for k in b.keys():
            if type(k) not in tps:
                all_str_keys = False
                break
            try:
                k_str = tp_conv(k)
            except:
                all_str_keys = False
                break
        if all_str_keys:
            assert_equal_nose(set(a.dtype.names),
                              set([tp_conv_str(k) for k in b.keys()]))
            for k in b:
                assert_equal_none_format(a[tp_conv_str(k)][0],
                                         b[k], options)
        else:
            names = (options.dict_like_keys_name,
                     options.dict_like_values_name)
            assert set(a.dtype.names) == set(names)
            keys = a[names[0]]
            values = a[names[1]]
            assert_equal_none_format(keys, tuple(b.keys()), options)
            assert_equal_none_format(values, tuple(b.values()), options)
    elif type(b) in (list, tuple, set, frozenset, collections.deque):
        b_conv = np.zeros(dtype='object', shape=(len(b), ))
        for i, v in enumerate(b):
            b_conv[i] = v
        assert_equal_none_format(a, b_conv, options)
    elif not isinstance(b, (np.generic, np.ndarray)):
        if b is None:
            # It should be np.float64([])
            assert_equal_nose(type(a), np.ndarray)
            assert_equal_nose(a.dtype, np.float64([]).dtype)
            assert_equal_nose(a.shape, (0, ))
        elif (sys.hexversion >= 0x03000000 \
                and isinstance(b, (bytes, bytearray))) \
                or (sys.hexversion   <   0x03000000 \
                and isinstance(b, (bytes, bytearray))):
            assert_equal_nose(a, np.bytes_(b))
        elif (sys.hexversion >= 0x03000000 \
                and isinstance(b, str)) \
                or (sys.hexversion  <  0x03000000 \
                and isinstance(b, unicode)):
            assert_equal_none_format(a, np.unicode_(b), options)
        elif (sys.hexversion >= 0x03000000 \
                and type(b) == int) \
                or (sys.hexversion  <  0x03000000 \
                and type(b) == long):
            if b > 2**63 or b  <  -(2**63 - 1):
                assert_equal_none_format(a, np.bytes_(b), options)
            else:
                assert_equal_none_format(a, np.int64(b), options)
        else:
            assert_equal_none_format(a, np.array(b)[()], options)
    elif isinstance(b, np.recarray):
        assert_equal_none_format(a, b.view(np.ndarray),
                                 options)
    else:
        if b.dtype.name != 'object':
            if b.dtype.char in ('U', 'S'):
                if b.dtype.char == 'S' and b.shape == tuple() \
                        and len(b) == 0:
                    assert_equal(a, \
                        np.zeros(shape=tuple(), dtype=b.dtype.char), \
                        options)
                elif b.dtype.char == 'U':
                    if b.shape == tuple() and len(b) == 0:
                        c = np.uint32(())
                    else:
                        c = np.atleast_1d(b).view(np.uint32)
                    assert_equal_nose(a.dtype, c.dtype)
                    assert_equal_nose(a.shape, c.shape)
                    npt.assert_equal(a, c)
                else:
                    assert_equal_nose(a.dtype, b.dtype)
                    assert_equal_nose(a.shape, b.shape)
                    npt.assert_equal(a, b)
            else:
                # Check that the dtype's shape matches.
                assert_equal_nose(a.dtype.shape, b.dtype.shape)

                # Now, if b.shape is just all ones, then a.shape will
                # just be (1,). Otherwise, we need to compare the shapes
                # directly. Also, dimensions need to be squeezed before
                # comparison in this case.
                assert_equal_nose(np.prod(a.shape), np.prod(b.shape))
                if a.shape != b.shape:
                    assert_equal_nose(np.prod(b.shape), 1)
                    assert_equal_nose(a.shape, (1, ))
                if np.prod(a.shape) == 1:
                    a = np.squeeze(a)
                    b = np.squeeze(b)
                # If there was a null in the dtype or the dtype of one
                # of its fields (or subfields) has a 0 in its shape,
                # then it was written as a Group so the field order
                # could have changed.
                has_zero_shape = False
                if b.dtype.names is not None:
                    parts = [b.dtype]
                    while 0 != len(parts):
                        part = parts.pop()
                        if 0 in part.shape:
                            has_zero_shape = True
                        if part.names is not None:
                            parts.extend([v[0] for v
                                          in part.fields.values()])
                        if part.base != part:
                            parts.append(part.base)
                if b.dtype.names is not None \
                        and ('\\x00' in str(b.dtype) \
                        or has_zero_shape):
                    assert_equal_nose(a.shape, b.shape)
                    assert_equal_nose(set(a.dtype.names),
                                      set(b.dtype.names))
                    for n in b.dtype.names:
                        assert_equal_none_format(a[n], b[n], options)
                else:
                    assert_equal_nose(a.dtype, b.dtype)
                    with warnings.catch_warnings():
                        warnings.simplefilter('ignore', RuntimeWarning)
                        npt.assert_equal(a, b)
        else:
            # If the original is structued, it is possible that the
            # fields got out of order, in which case the dtype won't
            # quite match. It will need to be checked just to make sure
            # all pieces are there. Otherwise, the dtypes can be
            # directly compared.
            if b.dtype.fields is None:
                assert_equal_nose(a.dtype, b.dtype)
            else:
                assert_equal_nose(dict(a.dtype.fields),
                                  dict(b.dtype.fields))
            assert_equal_nose(a.shape, b.shape)
            for index, x in np.ndenumerate(a):
                assert_equal_none_format(a[index], b[index], options)


def assert_equal_matlab_format(a, b, options=None):

2 Source : asserts.py
with Apache License 2.0
from Enigma644

def assert_equal_matlab_format(a, b, options=None):
    # Compares a and b for equality. b is always the original. If they
    # are dictionaries, a must be a structured ndarray and they must
    # have the same set of keys, after which they values must all be
    # compared. If they are a collection type (list, tuple, set,
    # frozenset, or deque), then the compairison must be made with b
    # converted to an object array. If the original is not a numpy type
    # (isn't or doesn't inherit from np.generic or np.ndarray), then it
    # is a matter of converting it to the appropriate numpy
    # type. Otherwise, both are supposed to be numpy types. For object
    # arrays, each element must be iterated over to be compared. Then,
    # if it isn't a string type, then they must have the same dtype,
    # shape, and all elements. All strings are converted to numpy.str_
    # on read unless they were stored as a numpy.bytes_ due to having
    # non-ASCII characters. If it is empty, it has shape (1, 0). A
    # numpy.str_ has all of its strings per row compacted together. A
    # numpy.bytes_ string has to have the same thing done, but then it
    # needs to be converted up to UTF-32 and to numpy.str_ through
    # uint32. Big longs and ints end up getting converted to UTF-16
    # uint16's when written and read back as UTF-32 numpy.unicode_.
    #
    # In all cases, we expect things to be at least two dimensional
    # arrays.
    if type(b) == dict or (sys.hexversion >= 0x2070000
                           and type(b) == collections.OrderedDict):
        assert_equal_nose(type(a), np.ndarray)
        assert a.dtype.names is not None

        # Determine if any of the keys could not be stored as str. If
        # they all can be, then the dtype field names should be the
        # keys. Otherwise, they should be 'keys' and 'values'.
        all_str_keys = True
        if sys.hexversion >= 0x03000000:
            tp_str = str
            tp_bytes = bytes
            converters = {tp_str: lambda x: x,
                          tp_bytes: lambda x: x.decode('UTF-8'),
                          np.bytes_:
                          lambda x: bytes(x).decode('UTF-8'),
                          np.unicode_: lambda x: str(x)}
            tp_conv = lambda x: converters[type(x)](x)
            tp_conv_str = lambda x: tp_conv(x)
        else:
            tp_str = unicode
            tp_bytes = str
            converters = {tp_str: lambda x: x,
                          tp_bytes: lambda x: x.decode('UTF-8'),
                          np.bytes_:
                          lambda x: bytes(x).decode('UTF-8'),
                          np.unicode_: lambda x: unicode(x)}
            tp_conv = lambda x: converters[type(x)](x)
            tp_conv_str = lambda x: tp_conv(x).encode('UTF-8')
        tps = tuple(converters.keys())
        for k in b.keys():
            if type(k) not in tps:
                all_str_keys = False
                break
            try:
                k_str = tp_conv(k)
            except:
                all_str_keys = False
                break
        if all_str_keys:
            assert_equal_nose(set(a.dtype.names),
                              set([tp_conv_str(k)
                                   for k in b.keys()]))
            for k in b:
                assert_equal_matlab_format(a[tp_conv_str(k)][0],
                                           b[k], options)
        else:
            names = (options.dict_like_keys_name,
                     options.dict_like_values_name)
            assert_equal_nose(set(a.dtype.names), set(names))
            keys = a[names[0]][0]
            values = a[names[1]][0]
            assert_equal_matlab_format(keys, tuple(b.keys()), options)
            assert_equal_matlab_format(values, tuple(b.values()),
                                       options)
    elif type(b) in (list, tuple, set, frozenset, collections.deque):
        b_conv = np.zeros(dtype='object', shape=(len(b), ))
        for i, v in enumerate(b):
            b_conv[i] = v
        assert_equal_matlab_format(a, b_conv, options)
    elif not isinstance(b, (np.generic, np.ndarray)):
        if b is None:
            # It should be np.zeros(shape=(0, 1), dtype='float64'))
            assert_equal_nose(type(a), np.ndarray)
            assert_equal_nose(a.dtype, np.dtype('float64'))
            assert_equal_nose(a.shape, (1, 0))
        elif (sys.hexversion >= 0x03000000 \
                and isinstance(b, (bytes, str, bytearray))) \
                or (sys.hexversion   <   0x03000000 \
                and isinstance(b, (bytes, unicode, bytearray))):
            if len(b) == 0:
                assert_equal(a, np.zeros(shape=(1, 0), dtype='U'),
                             options)
            elif isinstance(b, (bytes, bytearray)):
                try:
                    c = np.unicode_(b.decode('ASCII'))
                except:
                    c = np.bytes_(b)
                assert_equal(a, np.atleast_2d(c), options)
            else:
                assert_equal(a, np.atleast_2d(np.unicode_(b)), options)
        elif (sys.hexversion >= 0x03000000 \
                and type(b) == int) \
                or (sys.hexversion  <  0x03000000 \
                and type(b) == long):
            if b > 2**63 or b  <  -(2**63 - 1):
                assert_equal(a, np.atleast_2d(np.unicode_(b)), options)
            else:
                assert_equal(a, np.atleast_2d(np.int64(b)), options)
        else:
            assert_equal(a, np.atleast_2d(np.array(b)), options)
    else:
        if b.dtype.name != 'object':
            if b.dtype.char in ('U', 'S'):
                if len(b) == 0 and (b.shape == tuple() \
                        or b.shape == (0, )):
                    assert_equal(a, np.zeros(shape=(1, 0),
                                             dtype='U'), options)
                elif b.dtype.char == 'U':
                    c = np.atleast_1d(b)
                    c = np.atleast_2d(c.view(np.dtype('U' \
                        + str(c.shape[-1]*c.dtype.itemsize//4))))
                    assert_equal_nose(a.dtype, c.dtype)
                    assert_equal_nose(a.shape, c.shape)
                    npt.assert_equal(a, c)
                elif b.dtype.char == 'S':
                    c = np.atleast_1d(b).view(np.ndarray)
                    if np.all(c.view(np.uint8)  <  128):
                        c = c.view(np.dtype('S' \
                            + str(c.shape[-1]*c.dtype.itemsize)))
                        c = c.view(np.dtype('uint8'))
                        c = np.uint32(c.view(np.dtype('uint8')))
                        c = c.view(np.dtype('U' + str(c.shape[-1])))
                    c = np.atleast_2d(c)
                    assert_equal_nose(a.dtype, c.dtype)
                    assert_equal_nose(a.shape, c.shape)
                    npt.assert_equal(a, c)
                    pass
                else:
                    c = np.atleast_2d(b)
                    assert_equal_nose(a.dtype, c.dtype)
                    assert_equal_nose(a.shape, c.shape)
                    with warnings.catch_warnings():
                        warnings.simplefilter('ignore', RuntimeWarning)
                        npt.assert_equal(a, c)
            else:
                c = np.atleast_2d(b)
                # An empty complex number gets turned into a real
                # number when it is stored.
                if np.prod(c.shape) == 0 \
                        and b.dtype.name.startswith('complex'):
                    c = np.real(c)
                # If it is structured, check that the field names are
                # the same, in the same order, and then go through them
                # one by one. Otherwise, make sure the dtypes and shapes
                # are the same before comparing all values.
                if b.dtype.names is None and a.dtype.names is None:
                    assert_equal_nose(a.dtype, c.dtype)
                    assert_equal_nose(a.shape, c.shape)
                    with warnings.catch_warnings():
                        warnings.simplefilter('ignore', RuntimeWarning)
                        npt.assert_equal(a, c)
                else:
                    assert a.dtype.names is not None
                    assert b.dtype.names is not None
                    assert_equal_nose(set(a.dtype.names),
                                      set(b.dtype.names))
                    # The ordering of fields must be preserved if the
                    # MATLAB_fields attribute could be used, which can
                    # only be done if there are no non-ascii characters
                    # in any of the field names.
                    if sys.hexversion >= 0x03000000:
                        allfields = ''.join(b.dtype.names)
                    else:
                        allfields = unicode('').join( \
                            [nm.decode('UTF-8') \
                            for nm in b.dtype.names])
                    if np.all(np.array([ord(ch)  <  128 \
                            for ch in allfields])):
                        assert_equal_nose(a.dtype.names, b.dtype.names)
                    a = a.flatten()
                    b = b.flatten()
                    for k in b.dtype.names:
                        for index, x in np.ndenumerate(a):
                            assert_equal_from_matlab(a[k][index],
                                                     b[k][index],
                                                     options)
        else:
            c = np.atleast_2d(b)
            assert_equal_nose(a.dtype, c.dtype)
            assert_equal_nose(a.shape, c.shape)
            for index, x in np.ndenumerate(a):
                assert_equal_matlab_format(a[index], c[index], options)


def assert_equal_from_matlab(a, b, options=None):

0 Source : sslcompat.py
with GNU General Public License v3.0
from AbiPutrallg

def _optional_dependencies():
    try:
        import ipaddress  # noqa
        logger.debug('ipaddress module is available')
        ipaddr = True
    except ImportError:
        logger.warn('ipaddress module is unavailable')
        ipaddr = False

    if sys.hexversion   <   0x030500F0:
        try:
            from backports.ssl_match_hostname import match_hostname, __version__ as ver
            ver = list(map(int, ver.split('.')))
            logger.debug('backports.ssl_match_hostname module is available')
            match = match_hostname
            if ver[0] * 10 + ver[1] >= 35:
                return ipaddr, match
            else:
                logger.warn('backports.ssl_match_hostname module is too old')
                ipaddr = False
        except ImportError:
            logger.warn('backports.ssl_match_hostname is unavailable')
            ipaddr = False
    try:
        from ssl import match_hostname
        logger.debug('ssl.match_hostname is available')
        match = match_hostname
    except ImportError:
        logger.warn('using legacy validation callback')
        match = legacy_validate_callback
    return ipaddr, match

_match_has_ipaddress, _match_hostname = _optional_dependencies()

0 Source : sdat2img.py
with GNU General Public License v3.0
from AEnjoy

def main(TRANSFER_LIST_FILE, NEW_DATA_FILE, OUTPUT_IMAGE_FILE,quiet=1):#quiet:0:enable 1:disable
    __version__ = '1.2'

    if sys.hexversion   <   0x02070000:
        print >> sys.stderr, "需要Python 2.7或更新的版本."
        try:
            input = raw_input
        except NameError: pass
        input('按 ENTER 退出...')
        sys.exit(1)
    else:
        if quiet==1:
            print('sdat2img Version: {}\n'.format(__version__))

    def rangeset(src):
        src_set = src.split(',')
        num_set =  [int(item) for item in src_set]
        if len(num_set) != num_set[0]+1:
            print('以下数据分析到RangeSet时出错:\n {}'.format(src), file=sys.stderr)
            sys.exit(1)

        return tuple ([ (num_set[i], num_set[i+1]) for i in range(1, len(num_set), 2) ])

    def parse_transfer_list_file(path):
        trans_list = open(TRANSFER_LIST_FILE, 'r')

        # First line in transfer list is the version number
        version = int(trans_list.readline())

        # Second line in transfer list is the total number of blocks we expect to write
        new_blocks = int(trans_list.readline())

        if version >= 2:
            # Third line is how many stash entries are needed simultaneously
            trans_list.readline()
            # Fourth line is the maximum number of blocks that will be stashed simultaneously
            trans_list.readline()

        # Subsequent lines are all individual transfer commands
        commands = []
        for line in trans_list:
            line = line.split(' ')
            cmd = line[0]
            if cmd in ['erase', 'new', 'zero']:
                commands.append([cmd, rangeset(line[1])])
            else:
                # Skip lines starting with numbers, they are not commands anyway
                if not cmd[0].isdigit():
                    print('命令 "{}" 无效.'.format(cmd), file=sys.stderr)
                    trans_list.close()
                    sys.exit(1)

        trans_list.close()
        return version, new_blocks, commands

    BLOCK_SIZE = 4096
    
    version, new_blocks, commands = parse_transfer_list_file(TRANSFER_LIST_FILE)
    if quiet==1:
        if version == 1:
            print('Android Lollipop 5.0 检测到!\n')
        elif version == 2:
            print('Android Lollipop 5.1 检测到!\n')
        elif version == 3:
            print('Android Marshmallow 6.x 检测到!\n')
        elif version == 4:
            print('Android Nougat 7.x / Oreo 8.x 检测到!\n')
        else:
            print('Unknown Android version!\n')

    # Don't clobber existing files to avoid accidental data loss
    try:
        output_img = open(OUTPUT_IMAGE_FILE, 'wb')
    except IOError as e:
        if e.errno == errno.EEXIST:
            print('错误: 输出的文件 "{}" 已经存在'.format(e.filename), file=sys.stderr)
            print('移动它, 重命名它, 或选择一个不同的文件名.', file=sys.stderr)
            sys.exit(e.errno)
        else:
            raise

    new_data_file = open(NEW_DATA_FILE, 'rb')
    all_block_sets = [i for command in commands for i in command[1]]
    max_file_size = max(pair[1] for pair in all_block_sets)*BLOCK_SIZE

    for command in commands:
        if command[0] == 'new':
            for block in command[1]:
                begin = block[0]
                end = block[1]
                block_count = end - begin
                if quiet==1:
                    print('复制 {} 区段到位置 {}...'.format(block_count, begin))
                # Position output file
                output_img.seek(begin*BLOCK_SIZE)
                
                # Copy one block at a time
                while(block_count > 0):
                    output_img.write(new_data_file.read(BLOCK_SIZE))
                    block_count -= 1
        else:
            if quiet==1:
                print('跳过命令 {}...'.format(command[0]))

    # Make file larger if necessary
    if(output_img.tell()  <  max_file_size):
        output_img.truncate(max_file_size)

    output_img.close()
    new_data_file.close()
    print('完成! 输出的镜像文件:  {}'.format(os.path.realpath(output_img.name)))

if __name__ == '__main__':

0 Source : driver.py
with MIT License
from AgenteDog

    def _generate(self):
        """ Generate the Python code. """

        needs_close = False

        if sys.hexversion >= 0x03000000:
            if self._opts.output == '-':
                from io import TextIOWrapper

                pyfile = TextIOWrapper(sys.stdout.buffer, encoding='utf8')
            else:
                pyfile = open(self._opts.output, 'wt', encoding='utf8')
                needs_close = True
        else:
            if self._opts.output == '-':
                pyfile = sys.stdout
            else:
                pyfile = open(self._opts.output, 'wt')
                needs_close = True

        import_from = self._opts.import_from

        if import_from:
            from_imports = True
        elif self._opts.from_imports:
            from_imports = True
            import_from = '.'
        else:
            from_imports = False

        compileUi(self._ui_file, pyfile, self._opts.execute, self._opts.indent,
                from_imports, self._opts.resource_suffix, import_from)

        if needs_close:
            pyfile.close()

    def on_IOError(self, e):

0 Source : __init__.py
with MIT License
from AgenteDog

def loadUiType(uifile, from_imports=False, resource_suffix='_rc', import_from='.'):
    """loadUiType(uifile, from_imports=False, resource_suffix='_rc', import_from='.') -> (form class, base class)

    Load a Qt Designer .ui file and return the generated form class and the Qt
    base class.

    uifile is a file name or file-like object containing the .ui file.
    from_imports is optionally set to generate relative import statements.  At
    the moment this only applies to the import of resource modules.
    resource_suffix is the suffix appended to the basename of any resource file
    specified in the .ui file to create the name of the Python module generated
    from the resource file by pyrcc4.  The default is '_rc', i.e. if the .ui
    file specified a resource file called foo.qrc then the corresponding Python
    module is foo_rc.
    import_from is optionally set to the package used for relative import
    statements.  The default is ``'.'``.
    """

    import sys

    from PyQt5 import QtWidgets

    if sys.hexversion >= 0x03000000:
        from .port_v3.string_io import StringIO
    else:
        from .port_v2.string_io import StringIO

    code_string = StringIO()
    winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports,
            resource_suffix, import_from)

    ui_globals = {}
    exec(code_string.getvalue(), ui_globals)

    uiclass = winfo["uiclass"]
    baseclass = winfo["baseclass"]

    # Assume that the base class is a custom class exposed in the globals.
    ui_base = ui_globals.get(baseclass)
    if ui_base is None:
        # Otherwise assume it is in the QtWidgets module.
        ui_base = getattr(QtWidgets, baseclass)

    return (ui_globals[uiclass], ui_base)


def loadUi(uifile, baseinstance=None, package='', resource_suffix='_rc'):

0 Source : uic.py
with MIT License
from AgenteDog

def main():
    if sys.hexversion >= 0x03000000:
        from pyside2uic.port_v3.invoke import invoke
    else:
        from pyside2uic.port_v2.invoke import invoke

    parser = optparse.OptionParser(usage="pyside2-uic [options]   <  ui-file>",
            version=Version)
    parser.add_option("-p", "--preview", dest="preview", action="store_true",
            default=False,
            help="show a preview of the UI instead of generating code")
    parser.add_option("-o", "--output", dest="output", default="-", metavar="FILE",
            help="write generated code to FILE instead of stdout")
    parser.add_option("-x", "--execute", dest="execute", action="store_true",
            default=False,
            help="generate extra code to test and display the class")
    parser.add_option("-d", "--debug", dest="debug", action="store_true",
            default=False, help="show debug output")
    parser.add_option("-i", "--indent", dest="indent", action="store", type="int",
            default=4, metavar="N",
            help="set indent width to N spaces, tab if N is 0 (default: 4)")

    g = optparse.OptionGroup(parser, title="Code generation options")
    g.add_option("--from-imports", dest="from_imports", action="store_true",
            default=False, help="generate imports relative to '.'")
    parser.add_option_group(g)

    opts, args = parser.parse_args()

    if len(args) != 1:
        sys.stderr.write("Error: one input ui-file must be specified\n")
        sys.exit(1)

    sys.exit(invoke(Driver(opts, args[0])))

if __name__ == "__main__":

0 Source : server.py
with GNU General Public License v3.0
from ansible-collections

    def start(self):
        self.loop = asyncio.get_event_loop()
        self.loop.add_signal_handler(signal.SIGTERM, self.stop)
        self.loop.set_exception_handler(self.handle_exception)
        self._watcher = self.loop.create_task(self.ghost_killer())

        import sys

        if sys.hexversion >= 0x30A00B1:
            # py3.10 drops the loop argument of create_task.
            self.loop.create_task(
                asyncio.start_unix_server(self.handle, path=self.socket_path)
            )
        else:
            self.loop.create_task(
                asyncio.start_unix_server(
                    self.handle, path=self.socket_path, loop=self.loop
                )
            )
        self.loop.run_forever()

    def stop(self):

0 Source : pdf-parser.py
with GNU General Public License v3.0
from AUCyberClub

    def __init__(self, file):
        self.file = file
        if type(file) != str:
        	  self.infile = file
        elif file.lower().startswith('http://') or file.lower().startswith('https://'):
            try:
                if sys.hexversion >= 0x020601F0:
                    self.infile = urllib23.urlopen(file, timeout=5)
                else:
                    self.infile = urllib23.urlopen(file)
            except urllib23.HTTPError:
                print('Error accessing URL %s' % file)
                print(sys.exc_info()[1])
                sys.exit()
        elif file.lower().endswith('.zip'):
            try:
                self.zipfile = zipfile.ZipFile(file, 'r')
                self.infile = self.zipfile.open(self.zipfile.infolist()[0], 'r', C2BIP3('infected'))
            except:
                print('Error opening file %s' % file)
                print(sys.exc_info()[1])
                sys.exit()
        else:
            try:
                self.infile = open(file, 'rb')
            except:
                print('Error opening file %s' % file)
                print(sys.exc_info()[1])
                sys.exit()
        self.ungetted = []
        self.position = -1

    def byte(self):

See More Examples