def test_reading_events_non_blocking(): # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data reader = managed_reader(text_reader()) event = reader.send(NEXT_EVENT) # No data has been provided, so the reader is at STREAM_END # and will wait for data. assert event.event_type == IonEventType.STREAM_END # Send an incomplete Ion value. event = reader.send(read_data_event(b'{hello:')) # Enough data was available for the reader to determine that # the start of a struct value has been encountered. assert event.event_type == IonEventType.CONTAINER_START assert event.ion_type == IonType.STRUCT # Advancing the reader causes it to step into the struct. event = reader.send(NEXT_EVENT) # The reader reached the end of the data before completing # a value. Therefore, an INCOMPLETE event is returned. assert event.event_type == IonEventType.INCOMPLETE # Send the rest of the value. event = reader.send(read_data_event(b'"world"}')) # The reader now finishes parsing the value within the struct. assert event.event_type == IonEventType.SCALAR assert event.ion_type == IonType.STRING hello = event.field_name.text world = event.value # Advance the reader past the string value. event = reader.send(NEXT_EVENT) # The reader has reached the end of the struct. assert event.event_type == IonEventType.CONTAINER_END # Advancing the reader causes it to step out of the struct. event = reader.send(NEXT_EVENT) # There is no more data and a value has been completed. # Therefore, the reader conveys STREAM_END. assert event.event_type == IonEventType.STREAM_END assert u'hello world' == u'%s %s' % (hello, world)
def test_read_numerics_events(): # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-numeric-types data = BytesIO(b'1.23456 1.2345e6 123456 12345678901234567890') reader = blocking_reader(managed_reader(text_reader()), data) event = reader.send(NEXT_EVENT) assert event.ion_type == IonType.DECIMAL assert isinstance(event.value, Decimal) event = reader.send(NEXT_EVENT) assert event.ion_type == IonType.FLOAT assert isinstance(event.value, float) event = reader.send(NEXT_EVENT) assert event.ion_type == IonType.INT assert isinstance(event.value, int) event = reader.send(NEXT_EVENT) assert event.ion_type == IonType.INT assert isinstance(event.value, int)
def test_reading_events_blocking(): # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data data = BytesIO(b'{hello: "world"}') reader = blocking_reader(managed_reader(text_reader()), data) event = reader.send(NEXT_EVENT) assert event.event_type == IonEventType.CONTAINER_START assert event.ion_type == IonType.STRUCT # Advancing the reader causes it to step into the struct. event = reader.send(NEXT_EVENT) assert event.event_type == IonEventType.SCALAR assert event.ion_type == IonType.STRING hello = event.field_name.text world = event.value # Advance the reader past the string value. event = reader.send(NEXT_EVENT) # The reader has reached the end of the struct. assert event.event_type == IonEventType.CONTAINER_END # Advancing the reader causes it to step out of the struct. event = reader.send(NEXT_EVENT) # There is no more data and a value has been completed. # Therefore, the reader conveys STREAM_END. assert event.event_type == IonEventType.STREAM_END assert u'hello world' == u'%s %s' % (hello, world)
def load(fp, catalog=None, single_value=True, encoding='utf-8', cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, use_decimal=None, **kw): """Deserialize ``fp`` (a file-like object), which contains a text or binary Ion stream, to a Python object using the following conversion table:: +-------------------+-------------------+ | Ion | Python | |-------------------+-------------------| | null.<type> | IonPyNull(<type>) | |-------------------+-------------------| | bool | IonPyBool | |-------------------+-------------------| | int | IonPyInt | |-------------------+-------------------| | float | IonPyFloat | |-------------------+-------------------| | decimal | IonPyDecimal | |-------------------+-------------------| | timestamp | IonPyTimestamp | |-------------------+-------------------| | symbol | IonPySymbol | |-------------------+-------------------| | string | IonPyText(STRING) | |-------------------+-------------------| | clob | IonPyBytes(CLOB) | |-------------------+-------------------| | blob | IonPyBytes(BLOB) | |-------------------+-------------------| | list | IonPyList(LIST) | |-------------------+-------------------| | sexp | IonPyList(SEXP) | |-------------------+-------------------| | struct | IonPyDict | +-------------------+-------------------+ Args: fp (BaseIO): A file-like object containing Ion data. catalog (Optional[SymbolTableCatalog]): The catalog to use for resolving symbol table imports. single_value (Optional[True|False]): When True, the data in ``obj`` is interpreted as a single Ion value, and will be returned without an enclosing container. If True and there are multiple top-level values in the Ion stream, IonException will be raised. NOTE: this means that when data is dumped using ``sequence_as_stream=True``, it must be loaded using ``single_value=False``. Default: True. encoding: NOT IMPLEMENTED cls: NOT IMPLEMENTED object_hook: NOT IMPLEMENTED parse_float: NOT IMPLEMENTED parse_int: NOT IMPLEMENTED parse_constant: NOT IMPLEMENTED object_pairs_hook: NOT IMPLEMENTED use_decimal: NOT IMPLEMENTED **kw: NOT IMPLEMENTED Returns (Any): if single_value is True: A Python object representing a single Ion value. else: A sequence of Python objects representing a stream of Ion values. """ if isinstance(fp, _TEXT_TYPES): raw_reader = text_reader(is_unicode=True) else: maybe_ivm = fp.read(4) fp.seek(0) if maybe_ivm == _IVM: raw_reader = binary_reader() else: raw_reader = text_reader() reader = blocking_reader(managed_reader(raw_reader, catalog), fp) out = [] # top-level _load(out, reader) if single_value: if len(out) != 1: raise IonException( 'Stream contained %d values; expected a single value.' % (len(out), )) return out[0] return out
def load(fp, catalog=None, single_value=True, encoding='utf-8', cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, use_decimal=None, **kw): """Deserialize ``fp`` (a file-like object), which contains a text or binary Ion stream, to a Python object using the following conversion table:: +-------------------+-------------------+ | Ion | Python | |-------------------+-------------------| | null.<type> | IonPyNull(<type>) | |-------------------+-------------------| | bool | IonPyBool | |-------------------+-------------------| | int | IonPyInt | |-------------------+-------------------| | float | IonPyFloat | |-------------------+-------------------| | decimal | IonPyDecimal | |-------------------+-------------------| | timestamp | IonPyTimestamp | |-------------------+-------------------| | symbol | IonPySymbol | |-------------------+-------------------| | string | IonPyText(STRING) | |-------------------+-------------------| | clob | IonPyBytes(CLOB) | |-------------------+-------------------| | blob | IonPyBytes(BLOB) | |-------------------+-------------------| | list | IonPyList(LIST) | |-------------------+-------------------| | sexp | IonPyList(SEXP) | |-------------------+-------------------| | struct | IonPyDict | +-------------------+-------------------+ Args: fp (BaseIO): A file-like object containing Ion data. catalog (Optional[SymbolTableCatalog]): The catalog to use for resolving symbol table imports. single_value (Optional[True|False]): When True, the data in ``obj`` is interpreted as a single Ion value, and will be returned without an enclosing container. If True and there are multiple top-level values in the Ion stream, IonException will be raised. NOTE: this means that when data is dumped using ``sequence_as_stream=True``, it must be loaded using ``single_value=False``. Default: True. encoding: NOT IMPLEMENTED cls: NOT IMPLEMENTED object_hook: NOT IMPLEMENTED parse_float: NOT IMPLEMENTED parse_int: NOT IMPLEMENTED parse_constant: NOT IMPLEMENTED object_pairs_hook: NOT IMPLEMENTED use_decimal: NOT IMPLEMENTED **kw: NOT IMPLEMENTED Returns (Any): if single_value is True: A Python object representing a single Ion value. else: A sequence of Python objects representing a stream of Ion values. """ if isinstance(fp, _TEXT_TYPES): raw_reader = text_reader(is_unicode=True) else: maybe_ivm = fp.read(4) fp.seek(0) if maybe_ivm == _IVM: raw_reader = binary_reader() else: raw_reader = text_reader() reader = blocking_reader(managed_reader(raw_reader, catalog), fp) out = [] # top-level _load(out, reader) if single_value: if len(out) != 1: raise IonException('Stream contained %d values; expected a single value.' % (len(out),)) return out[0] return out
def _f(): if type == "binary": return binary_reader() elif type == "text": return text_reader(is_unicode=True)