Python io.py() Examples

The following are 7 code examples for showing how to use io.py(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module io , or try the search function .

Example 1
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_hierarchy():
    files = """
        relimp:
            - __init__.py
            - a:
                - __init__.py
                - amodule.py: |
                    from ..b import bmodule
            - b:
                - __init__.py
                - bmodule.py

    """
    with create_files(files, cleanup=True) as workdir:
        os.system("tree " + workdir)
        deps = simpledeps('relimp')
        assert 'relimp.b -> relimp.a.amodule' in deps
        assert 'relimp.b.bmodule -> relimp.a.amodule' in deps 
Example 2
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_relative_imports():
    files = """
        relimp:
            - __init__.py
            - a.py: |
                from . import b
            - b.py
    """
    with create_files(files) as workdir:
        assert simpledeps('relimp') == {'relimp.b -> relimp.a'} 
Example 3
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_relative_imports2():
    files = """
        relimp:
            - __init__.py
            - a.py: |
                from . import b
            - b.py: |
                from . import c
            - c.py
    """
    with create_files(files) as workdir:
        deps = simpledeps('relimp')
        assert 'relimp.c -> relimp.b' in deps
        assert 'relimp.b -> relimp.a' in deps 
Example 4
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_relative_imports3():
    files = """
        relimp:
            - __init__.py
            - a.py: |
                from .b import c
            - b.py
    """
    with create_files(files) as workdir:
        assert simpledeps('relimp') == {'relimp.b -> relimp.a'} 
Example 5
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_relative_imports_same_name_with_std():
    files = """
        relimp:
            - __init__.py
            - io.py: |
                import io
    """
    with create_files(files) as workdir:
        if sys.version_info < (3,):                # pragma: nocover
            deps = {'relimp.io -> relimp.io'}
        else:                                      # pragma: nocover
            deps = {'io -> relimp.io'}
        assert simpledeps('relimp', '--pylib') == deps 
Example 6
Project: pydeps   Author: thebjorn   File: test_relative_imports.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_relative_imports_same_name_with_std_future():
    files = """
        relimp:
            - __init__.py
            - io.py: |
                from __future__ import absolute_import
                import io
    """
    with create_files(files) as workdir:
        deps = {
            '__future__ -> relimp.io',
            'io -> relimp.io'
        }
        assert simpledeps('relimp', '--pylib') == deps 
Example 7
Project: confluent-kafka-python   Author: confluentinc   File: message_serializer.py    License: Apache License 2.0 4 votes vote down vote up
def _get_decoder_func(self, schema_id, payload, is_key=False):
        if schema_id in self.id_to_decoder_func:
            return self.id_to_decoder_func[schema_id]

        # fetch writer schema from schema reg
        try:
            writer_schema_obj = self.registry_client.get_by_id(schema_id)
        except ClientError as e:
            raise SerializerError("unable to fetch schema with id %d: %s" % (schema_id, str(e)))

        if writer_schema_obj is None:
            raise SerializerError("unable to fetch schema with id %d" % (schema_id))

        curr_pos = payload.tell()

        reader_schema_obj = self.reader_key_schema if is_key else self.reader_value_schema

        if HAS_FAST:
            # try to use fast avro
            try:
                fast_avro_writer_schema = parse_schema(writer_schema_obj.to_json())
                fast_avro_reader_schema = parse_schema(reader_schema_obj.to_json())
                schemaless_reader(payload, fast_avro_writer_schema)

                # If we reach this point, this means we have fastavro and it can
                # do this deserialization. Rewind since this method just determines
                # the reader function and we need to deserialize again along the
                # normal path.
                payload.seek(curr_pos)

                self.id_to_decoder_func[schema_id] = lambda p: schemaless_reader(
                    p, fast_avro_writer_schema, fast_avro_reader_schema)
                return self.id_to_decoder_func[schema_id]
            except Exception:
                # Fast avro failed, fall thru to standard avro below.
                pass

        # here means we should just delegate to slow avro
        # rewind
        payload.seek(curr_pos)
        # Avro DatumReader py2/py3 inconsistency, hence no param keywords
        # should be revisited later
        # https://github.com/apache/avro/blob/master/lang/py3/avro/io.py#L459
        # https://github.com/apache/avro/blob/master/lang/py/src/avro/io.py#L423
        # def __init__(self, writers_schema=None, readers_schema=None)
        # def __init__(self, writer_schema=None, reader_schema=None)
        avro_reader = avro.io.DatumReader(writer_schema_obj, reader_schema_obj)

        def decoder(p):
            bin_decoder = avro.io.BinaryDecoder(p)
            return avro_reader.read(bin_decoder)

        self.id_to_decoder_func[schema_id] = decoder
        return self.id_to_decoder_func[schema_id]