Пример #1
0
def test_write_numeric_with_annotation_events():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-numeric-types
    event = IonEvent(IonEventType.SCALAR,
                     IonType.FLOAT,
                     annotations=(u'abc', ),
                     value=123.0)
    data = BytesIO()
    writer = blocking_writer(text_writer(), data)
    writer.send(event)
    writer.send(ION_STREAM_END_EVENT)
    assert u'abc::123.0e0' == data.getvalue().decode(u'utf-8')
Пример #2
0
def dump(obj,
         fp,
         imports=None,
         binary=True,
         sequence_as_stream=False,
         skipkeys=False,
         ensure_ascii=True,
         check_circular=True,
         allow_nan=True,
         cls=None,
         indent=None,
         separators=None,
         encoding='utf-8',
         default=None,
         use_decimal=True,
         namedtuple_as_object=True,
         tuple_as_array=True,
         bigint_as_string=False,
         sort_keys=False,
         item_sort_key=None,
         for_json=None,
         ignore_nan=False,
         int_as_string_bitcount=None,
         iterable_as_array=False,
         **kw):
    """Serialize ``obj`` as an Ion-formatted stream to ``fp`` (a file-like object), using the following conversion
    table::
        +-------------------+-------------------+
        |  Python           |       Ion         |
        |-------------------+-------------------|
        | None              |    null.null      |
        |-------------------+-------------------|
        | IonPyNull(<type>) |    null.<type>    |
        |-------------------+-------------------|
        | True, False,      |                   |
        | IonPyInt(BOOL),   |     bool          |
        | IonPyBool,        |                   |
        |-------------------+-------------------|
        | int (Python 2, 3) |                   |
        | long (Python 2),  |      int          |
        | IonPyInt(INT)     |                   |
        |-------------------+-------------------|
        | float, IonPyFloat |     float         |
        |-------------------+-------------------|
        | Decimal,          |                   |
        | IonPyDecimal      |     decimal       |
        |-------------------+-------------------|
        | datetime,         |                   |
        | Timestamp,        |    timestamp      |
        | IonPyTimestamp    |                   |
        |-------------------+-------------------|
        | SymbolToken,      |                   |
        | IonPySymbol,      |     symbol        |
        | IonPyText(SYMBOL) |                   |
        |-------------------+-------------------|
        | str (Python 3),   |                   |
        | unicode (Python2),|     string        |
        | IonPyText(STRING) |                   |
        |-------------------+-------------------|
        | IonPyBytes(CLOB)  |     clob          |
        |-------------------+-------------------|
        | str (Python 2),   |                   |
        | bytes (Python 3)  |     blob          |
        | IonPyBytes(BLOB)  |                   |
        |-------------------+-------------------|
        | list, tuple,      |                   |
        | IonPyList(LIST)   |     list          |
        |-------------------+-------------------|
        | IonPyList(SEXP)   |     sexp          |
        |-------------------+-------------------|
        | dict, namedtuple, |                   |
        | IonPyDict         |     struct        |
        +-------------------+-------------------+

    Args:
        obj (Any): A python object to serialize according to the above table. Any Python object which is neither an
            instance of nor inherits from one of the types in the above table will raise TypeError.
        fp (BaseIO): A file-like object.
        imports (Optional[Sequence[SymbolTable]]): A sequence of shared symbol tables to be used by by the writer.
        binary (Optional[True|False]): When True, outputs binary Ion. When false, outputs text Ion.
        sequence_as_stream (Optional[True|False]): When True, if ``obj`` is a sequence, it will be treated as a stream
            of top-level Ion values (i.e. the resulting Ion data will begin with ``obj``'s first element).
            Default: False.
        skipkeys: NOT IMPLEMENTED
        ensure_ascii: NOT IMPLEMENTED
        check_circular: NOT IMPLEMENTED
        allow_nan: NOT IMPLEMENTED
        cls: NOT IMPLEMENTED
        indent (Str): If binary is False and indent is a string, then members of containers will be pretty-printed with
            a newline followed by that string repeated for each level of nesting. None (the default) selects the most
            compact representation without any newlines. Example: to indent with four spaces per level of nesting,
            use ``'    '``.
        item_sort_key: custom callable to sort the items in each dictionary
        separators: NOT IMPLEMENTED
        encoding: NOT IMPLEMENTED
        default: NOT IMPLEMENTED
        use_decimal: NOT IMPLEMENTED
        namedtuple_as_object: NOT IMPLEMENTED
        tuple_as_array: NOT IMPLEMENTED
        bigint_as_string: NOT IMPLEMENTED
        sort_keys: NOT IMPLEMENTED
        for_json: NOT IMPLEMENTED
        ignore_nan: NOT IMPLEMENTED
        int_as_string_bitcount: NOT IMPLEMENTED
        iterable_as_array: NOT IMPLEMENTED
        **kw: NOT IMPLEMENTED

    """

    raw_writer = binary_writer(imports) if binary else text_writer(
        indent=indent)
    writer = blocking_writer(raw_writer, fp)
    writer.send(
        ION_VERSION_MARKER_EVENT
    )  # The IVM is emitted automatically in binary; it's optional in text.
    if sequence_as_stream and isinstance(obj, (list, tuple)):
        # Treat this top-level sequence as a stream; serialize its elements as top-level values, but don't serialize the
        # sequence itself.
        for top_level in obj:
            _dump(top_level, writer, item_sort_key=item_sort_key)
    else:
        _dump(obj, writer, item_sort_key=item_sort_key)
    writer.send(ION_STREAM_END_EVENT)
Пример #3
0
def dump(obj, fp, imports=None, binary=True, sequence_as_stream=False, skipkeys=False, ensure_ascii=True,
         check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None,
         use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, bigint_as_string=False, sort_keys=False,
         item_sort_key=None, for_json=None, ignore_nan=False, int_as_string_bitcount=None, iterable_as_array=False,
         **kw):
    """Serialize ``obj`` as an Ion-formatted stream to ``fp`` (a file-like object), using the following conversion
    table::
        +-------------------+-------------------+
        |  Python           |       Ion         |
        |-------------------+-------------------|
        | None              |    null.null      |
        |-------------------+-------------------|
        | IonPyNull(<type>) |    null.<type>    |
        |-------------------+-------------------|
        | True, False,      |                   |
        | IonPyInt(BOOL),   |     bool          |
        | IonPyBool,        |                   |
        |-------------------+-------------------|
        | int (Python 2, 3) |                   |
        | long (Python 2),  |      int          |
        | IonPyInt(INT)     |                   |
        |-------------------+-------------------|
        | float, IonPyFloat |     float         |
        |-------------------+-------------------|
        | Decimal,          |                   |
        | IonPyDecimal      |     decimal       |
        |-------------------+-------------------|
        | datetime,         |                   |
        | Timestamp,        |    timestamp      |
        | IonPyTimestamp    |                   |
        |-------------------+-------------------|
        | SymbolToken,      |                   |
        | IonPySymbol,      |     symbol        |
        | IonPyText(SYMBOL) |                   |
        |-------------------+-------------------|
        | str (Python 3),   |                   |
        | unicode (Python2),|     string        |
        | IonPyText(STRING) |                   |
        |-------------------+-------------------|
        | IonPyBytes(CLOB)  |     clob          |
        |-------------------+-------------------|
        | str (Python 2),   |                   |
        | bytes (Python 3)  |     blob          |
        | IonPyBytes(BLOB)  |                   |
        |-------------------+-------------------|
        | list, tuple,      |                   |
        | IonPyList(LIST)   |     list          |
        |-------------------+-------------------|
        | IonPyList(SEXP)   |     sexp          |
        |-------------------+-------------------|
        | dict, namedtuple, |                   |
        | IonPyDict         |     struct        |
        +-------------------+-------------------+

    Args:
        obj (Any): A python object to serialize according to the above table. Any Python object which is neither an
            instance of nor inherits from one of the types in the above table will raise TypeError.
        fp (BaseIO): A file-like object.
        imports (Optional[Sequence[SymbolTable]]): A sequence of shared symbol tables to be used by by the writer.
        binary (Optional[True|False]): When True, outputs binary Ion. When false, outputs text Ion.
        sequence_as_stream (Optional[True|False]): When True, if ``obj`` is a sequence, it will be treated as a stream
            of top-level Ion values (i.e. the resulting Ion data will begin with ``obj``'s first element).
            Default: False.
        skipkeys: NOT IMPLEMENTED
        ensure_ascii: NOT IMPLEMENTED
        check_circular: NOT IMPLEMENTED
        allow_nan: NOT IMPLEMENTED
        cls: NOT IMPLEMENTED
        indent: NOT IMPLEMENTED
        separators: NOT IMPLEMENTED
        encoding: NOT IMPLEMENTED
        default: NOT IMPLEMENTED
        use_decimal: NOT IMPLEMENTED
        namedtuple_as_object: NOT IMPLEMENTED
        tuple_as_array: NOT IMPLEMENTED
        bigint_as_string: NOT IMPLEMENTED
        sort_keys: NOT IMPLEMENTED
        item_sort_key: NOT IMPLEMENTED
        for_json: NOT IMPLEMENTED
        ignore_nan: NOT IMPLEMENTED
        int_as_string_bitcount: NOT IMPLEMENTED
        iterable_as_array: NOT IMPLEMENTED
        **kw: NOT IMPLEMENTED

    """
    raw_writer = binary_writer(imports) if binary else text_writer()
    writer = blocking_writer(raw_writer, fp)
    writer.send(ION_VERSION_MARKER_EVENT)  # The IVM is emitted automatically in binary; it's optional in text.
    if sequence_as_stream and isinstance(obj, (list, tuple)):
        # Treat this top-level sequence as a stream; serialize its elements as top-level values, but don't serialize the
        # sequence itself.
        for top_level in obj:
            _dump(top_level, writer)
    else:
        _dump(obj, writer)
    writer.send(ION_STREAM_END_EVENT)
Пример #4
0
def test_pretty_print_events():
    # http://amzn.github.io/ion-docs/guides/cookbook.html#pretty-printing
    pretty = BytesIO()
    writer = blocking_writer(text_writer(indent=u'  '), pretty)
    writer.send(ION_STREAM_END_EVENT)