bag.io.common

This module contains some commonly used IO functions.

In particular, this module keeps track of BAG’s system-wide encoding/decoding settings.

Module Contents

Functions

fix_string(obj)

Fix the given potential string object to ensure python 2/3 compatibility.

to_bytes(my_str)

Convert the given string to raw bytes.

set_encoding(new_encoding)

Sets the BAG input/output encoding.

get_encoding()

Returns the BAG input/output encoding.

set_error_policy(new_policy)

Sets the error policy on encoding/decoding errors.

get_error_policy()

Returns the current BAG encoding/decoding error policy.

Attributes

bag_encoding

bag_codec_error

bag.io.common.bag_encoding = 'utf-8'[source]
bag.io.common.bag_codec_error = 'replace'[source]
bag.io.common.fix_string(obj)[source]

Fix the given potential string object to ensure python 2/3 compatibility.

If the given object is raw bytes, decode it into a string using current encoding and return it. Otherwise, just return the given object.

This method is useful for writing python 2/3 compatible code.

Parameters:

obj – any python object.

Returns:

the given object, or a decoded string if the given object is bytes.

Return type:

val

bag.io.common.to_bytes(my_str)[source]

Convert the given string to raw bytes.

Parameters:

my_str (string) – the string to encode to bytes.

Returns:

val – raw bytes of the string.

Return type:

bytes

bag.io.common.set_encoding(new_encoding)[source]

Sets the BAG input/output encoding.

Parameters:

new_encoding (string) – the new encoding name.

bag.io.common.get_encoding()[source]

Returns the BAG input/output encoding.

Returns:

bag_encoding – the encoding name.

Return type:

unicode

bag.io.common.set_error_policy(new_policy)[source]

Sets the error policy on encoding/decoding errors.

Parameters:

new_policy (string) – the new error policy name. See codecs package documentation for more information.

bag.io.common.get_error_policy()[source]

Returns the current BAG encoding/decoding error policy.

Returns:

policy – the current error policy name.

Return type:

unicode