pip_services3_commons.convert package
Submodules
- pip_services3_commons.convert.ArrayConverter module
- pip_services3_commons.convert.BooleanConverter module
- pip_services3_commons.convert.DateTimeConverter module
- pip_services3_commons.convert.DoubleConverter module
- pip_services3_commons.convert.FloatConverter module
- pip_services3_commons.convert.IntegerConverter module
- pip_services3_commons.convert.JsonConverter module
- pip_services3_commons.convert.LongConverter module
- pip_services3_commons.convert.MapConverter module
- pip_services3_commons.convert.RecursiveMapConverter module
- pip_services3_commons.convert.StringConverter module
- pip_services3_commons.convert.TypeCode module
- pip_services3_commons.convert.TypeConverter module
- pip_services3_commons.convert.UTC module
Module contents
pip_services3_commons.convert.__init__
Contains “soft” data converters. Soft data converters differ from the data conversion algorithms found in typical programming language, due to the fact that they support rare conversions between various data types (such as integer to timespan, timespan to string, and so on).
These converters are necessary, due to the fact that data in enterprise systems is represented in various forms and conversion is often necessary – at times in very difficult combinations.
- copyright
Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.
- license
MIT, see LICENSE for more details.
-
class
pip_services3_commons.convert.
ArrayConverter
Bases:
object
Converts arbitrary values into array objects. Example:
value1 = ArrayConverter.to_array([1, 2]) # Result: [1, 2] value2 = ArrayConverter.to_array(1) # Result: [1] value2 = ArrayConverter.list_to_array("1,2,3") # Result: ["1", "2", "3"]
-
static
list_to_array
(value: Any) → List[Any] Converts args into array object with empty array as default. Strings with comma-delimited values are split into array of strings.
- Parameters
value – the list to convert.
- Returns
array object or empty array when args is None
-
static
to_array
(value: Any) → Any Converts args into array object with empty array as default. Single values are converted into arrays with single element.
- Parameters
value – the args to convert.
- Returns
array object or empty array when args is None.
-
static
to_array_with_default
(value: Any, default_value: List[Any]) → List[Any] Converts args into array object with specified default. Single values are converted into arrays with single element.
- Parameters
value – the args to convert.
default_value – default array object.
- Returns
array object or default array when args is None.
-
static
-
class
pip_services3_commons.convert.
BooleanConverter
Bases:
object
Converts arbitrary values to boolean values using extended conversion __rules: - Numbers: <>0 are true, =0 are false - Strings: “true”, “yes”, “T”, “Y”, “1” are true; “false”, “no”, “F”, “N” are false - DateTime: <>0 total milliseconds are true, =0 are false
Example: .. code-block:: python
value1 = BooleanConverter.to_nullable_boolean(true) // true value2 = BooleanConverter.to_nullable_boolean(“yes”) // true value3 = BooleanConverter.to_nullable_boolean(123) // true value4 = BooleanConverter.to_nullable_boolean({}) // None
-
static
to_boolean
(value: Any) → bool Converts args into boolean or returns false when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
boolean args or false when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
DateTimeConverter
Bases:
object
Converts arbitrary values into Date values using extended conversion __rules: - Strings: converted using ISO time format - Numbers: converted using milliseconds since unix epoch
Example: .. code-block:: python
value1 = DateTimeConverter.to_nullable_datetime(“ABC”) // Result: None value2 = DateTimeConverter.to_nullable_datetime(“2018-01-01T11:30:00.0”) // Result: Date(2018,0,1,11,30) value3 = DateTimeConverter.to_nullable_datetime(123) // Result: Date(123)
-
static
to_datetime
(value: Any) → datetime.datetime Converts args into Date or returns current date when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
Date args or current date when conversion is not supported.
-
static
to_datetime_with_default
(value: Any, default_value: datetime.datetime = None) → datetime.datetime Converts args into Date or returns default when conversion is not possible.
- Parameters
value – the args to convert.
default_value – the default args.
- Returns
Date args or default when conversion is not supported.
-
static
to_nullable_datetime
(value: Any) → Optional[datetime.datetime, None] Converts args into Date or returns null when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
Date args or null when conversion is not supported.
-
static
to_utc_datetime
(value: datetime.datetime)
-
static
-
class
pip_services3_commons.convert.
DoubleConverter
Bases:
object
- Converts arbitrary values into double using extended conversion __rules:
Strings are converted to double values
DateTime: total number of milliseconds since unix epoсh
Boolean: 1 for True and 0 for False
Example:
value1 = DoubleConverter.to_nullable_double("ABC") # Result: null value2 = DoubleConverter.to_nullable_double("123.456") # Result: 123.456 value3 = DoubleConverter.to_nullable_double(True) # Result: 1 value4 = DoubleConverter.to_nullable_double(datetime.datetime.now()) # Result: current milliseconds
-
static
to_double
(value: Any) → float Converts args into doubles or returns 0 when conversion is not possible. See
to_double_with_default
- Parameters
value – the args to convert.
- Returns
double args or 0 when conversion is not supported.
-
class
pip_services3_commons.convert.
FloatConverter
Bases:
object
Converts arbitrary values into float using extended conversion __rules: - Strings are converted to float values - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false
Example: .. code-block:: python
value1 = FloatConverter.to_nullable_float(“ABC”) # Result: None value2 = FloatConverter.to_nullable_float(“123.456”) # Result: 123.456 value3 = FloatConverter.to_nullable_float(true) # Result: 1 value4 = FloatConverter.to_nullable_float(datetime.datetime.now()) # Result: current milliseconds
-
static
to_float
(value: Any) → float Converts args into float or returns 0 when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
float args or 0 when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
IntegerConverter
Bases:
object
Converts arbitrary values into integers using extended conversion __rules: - Strings are converted to floats, then to integers - DateTime: total number of milliseconds since unix epoсh - Boolean: 1 for true and 0 for false
Example:
value1 = IntegerConverter.to_nullable_integer("ABC") # Result: None value2 = IntegerConverter.to_nullable_integer("123.456") # Result: 123 value3 = IntegerConverter.to_nullable_integer(true) # Result: 1 value4 = IntegerConverter.to_nullable_integer(datetime.datetime.now()) # Result: current milliseconds
-
static
to_integer
(value: Any) → int Converts args into integer or returns 0 when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
integer args or 0 when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
JsonConverter
Bases:
object
Converts arbitrary values from and to JSON (JavaScript Object Notation) strings.
Example:
value1 = JsonConverter.from_json("{"key":123}") // Result: { key: 123 } value2 = JsonConverter.to_map({ key: 123}) // Result: "{"key":123}"
-
static
from_json
(typ: pip_services3_commons.convert.TypeCode.TypeCode, value: str) → Any Converts JSON string into a args.
- Parameters
typ – the TypeCode for the data type into which ‘args’ is to be converted.
value – the JSON string to convert.
- Returns
converted object args or null when args is None.
-
static
to_json
(value: Any) → Optional[str, None] Converts args into JSON string.
- Parameters
value – the args to convert.
- Returns
JSON string or null when args is None.
-
static
to_map
(value: str) → Any Converts JSON string into map object or returns empty map when conversion is not possible.
- Parameters
value – the JSON string to convert.
- Returns
Map object args or empty object when conversion is not supported.
-
static
to_map_with_default
(value: str, default_value: Any) → Any Converts JSON string into map object or returns default args when conversion is not possible.
- Parameters
value – the JSON string to convert.
default_value – the default args.
- Returns
Map object args or default when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
LongConverter
Bases:
object
- Converts arbitrary values into longs using extended conversion __rules:
Strings are converted to floats, then to longs
DateTime: total number of milliseconds since unix epoсh
Boolean: 1 for true and 0 for false
-
static
to_long
(value: Any) → int Converts value into long or returns 0 when conversion is not possible.
- Parameters
value – the value to convert.
- Returns
long value or 0 when conversion is not supported.
-
class
pip_services3_commons.convert.
MapConverter
Bases:
object
Converts arbitrary values into map objects using extended conversion __rules: - Objects: property names as keys, property values as values - Arrays: element indexes as keys, elements as values
Example:
value1 = MapConverter.to_nullable_map("ABC") // Result: None value2 = MapConverter.to_nullable_map({ key: 123 }) // Result: { key: 123 } value3 = MapConverter.to_nullable_map([1,2,3]) // Result: { "0": 1, "1": 2, "2": 3 }
-
static
to_map
(value: Any) → Any Converts args into map object or returns empty map when conversion is not possible
- Parameters
value – the args to convert.
- Returns
map object or empty map when conversion is not supported.
-
static
to_map_with_default
(value: Any, default_value: Any) → Any Converts args into map object or returns default when conversion is not possible
- Parameters
value – the args to convert.
default_value – the default args.
- Returns
map object or emptu map when conversion is not supported.
-
static
to_nullable_map
(value: Any) → Any Converts args into map object or returns null when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
map object or null when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
RecursiveMapConverter
Bases:
object
Converts arbitrary values into map objects using extended conversion __rules. This class is similar to
MapConverter
, but is recursively converts all values stored in objects and arrays.Example:
value1 = RecursiveMapConverted.to_nullable_map("ABC") # Result: None value2 = RecursiveMapConverted.to_nullable_map({ key: 123 }) # Result: { key: 123 } value3 = RecursiveMapConverted.to_nullable_map([1,[2,3]) # Result: { "0": 1, { "0": 2, "1": 3 } }
-
static
to_map
(value: Any) → Any Converts args into map object or returns empty map when conversion is not possible
- Parameters
value – the args to convert.
- Returns
map object or empty map when conversion is not supported.
-
static
to_map_with_default
(value: Any, default_value: Any) → Any Converts args into map object or returns default when conversion is not possible
- Parameters
value – the args to convert.
default_value – the default args.
- Returns
map object or emptu map when conversion is not supported.
-
static
to_nullable_map
(value: Any) → Any Converts args into map object or returns null when conversion is not possible.
- Parameters
value – the args to convert.
- Returns
map object or null when conversion is not supported.
-
static
-
class
pip_services3_commons.convert.
StringConverter
Bases:
object
Converts arbitrary values into strings using extended conversion __rules: - Numbers: are converted with ‘.’ as decimal point - DateTime: using ISO format - Boolean: “true” for true and “false” for false - Arrays: as comma-separated list - Other objects: using
to_string()
methodExample:
value1 = StringConverter.to_string(123.456) # Result: "123.456" value2 = StringConverter.to_string(true) # Result: "true" value3 = StringConverter.to_string(datetime.datetime(2018,0,1)) # Result: "2018-01-01T00:00:00.00" value4 = StringConverter.to_string([1,2,3]) # Result: "1,2,3"
-
static
to_nullable_string
(value: Any) → Optional[str, None] Converts args into string or returns None when args is None.
- Parameters
value – the args to convert.
- Returns
string args or None when args is None.
-
static
-
class
pip_services3_commons.convert.
TypeCode
(value) Bases:
enum.Enum
Codes for the data types that can be converted using
TypeConverter
.-
Array
= 11
-
Boolean
= 2
-
DateTime
= 7
-
Double
= 6
-
Duration
= 8
-
Enum
= 10
-
Float
= 5
-
Integer
= 3
-
Long
= 4
-
Map
= 12
-
Object
= 9
-
String
= 1
-
Unknown
= 0
-
-
class
pip_services3_commons.convert.
TypeConverter
Bases:
object
Converts arbitrary values into objects specific by TypeCodes. For each TypeCode this class calls corresponding converter which applies extended conversion __rules to convert the values.
Example:
value1 = TypeConverter.to_type(TypeCode.Integer, "123.456") // Result: 123 value2 = TypeConverter.to_type(TypeCode.DateTime, 123) // Result: Date(123) value3 = TypeConverter.to_type(TypeCode.Boolean, "F") // Result: false
-
static
to_nullable_type
(value_type: pip_services3_commons.convert.TypeCode.TypeCode, value: Any) → Any Converts args into an object type specified by Type Code or returns null when conversion is not possible.
- Parameters
value_type – the TypeCode for the data type into which ‘args’ is to be converted.
value – the args to convert.
- Returns
object args of type corresponding to TypeCode, or null when conversion is not supported.
-
static
to_string
(typ: pip_services3_commons.convert.TypeCode.TypeCode) → str Converts a
TypeCode
into its string name.- Parameters
typ – the TypeCode to convert into a string.
- Returns
the name of the TypeCode passed as a string args.
-
static
to_type
(value_type: pip_services3_commons.convert.TypeCode.TypeCode, value: Any) → Any Converts args into an object type specified by Type Code or returns type default when conversion is not possible.
- Parameters
value_type – the TypeCode for the data type into which ‘args’ is to be converted.
value – the args to convert.
- Returns
object args of type corresponding to TypeCode, or type default when conversion is not supported.
-
static
to_type_code
(value: Any) → pip_services3_commons.convert.TypeCode.TypeCode Gets
TypeCode
for specific args.- Parameters
value – args whose TypeCode is to be resolved.
- Returns
the TypeCode that corresponds to the passed object’s type.
-
static
to_type_with_default
(value_type: pip_services3_commons.convert.TypeCode.TypeCode, value: Any, default_value: Any) → Any Converts args into an object type specified by Type Code or returns default args when conversion is not possible.
- Parameters
value_type – the
TypeCode
for the data type into which ‘args’ is to be converted.value – the args to convert.
default_value – the default args to return if conversion is not possible (returns None).
- Returns
object args of type corresponding to
TypeCode
, or default args when conversion is not supported.
-
static