pip_services3_commons.data.StringValueMap module

pip_services3_commons.data.StringValueMap

StringValueMap implementation

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_commons.data.StringValueMap.StringValueMap(map: Any = None)

Bases: dict

Cross-language implementation of a map (dictionary) where all keys and values are strings. The stored values can be converted to different types using variety of accessor methods.

The string map is highly versatile. It can be converted into many formats, stored and sent over the wire.

This class is widely used in Pip.Services as a basis for variety of classes, such as ConfigParams, ConnectionParams, CredentialParams and others.

Example:

value1 = StringValueMap.fromString("key1=1;key2=123.456;key3=2018-01-01")
value1.get_as_boolean("key1")   // Result: true
value1.get_as_integer("key2")   // Result: 123
value1.get_as_float("key2")     // Result: 123.456
value1.get_as_datetime("key3")  // Result: Date 2018,0,1
append(map: Any)

Appends new elements to this map.

Parameters

map – a map with elements to be added.

clear()

Clears this map by removing all its elements.

clone() → Any

Creates a binary clone of this object.

Returns

a clone of this object.

static from_maps(*maps: dict)pip_services3_commons.data.StringValueMap.StringValueMap

Creates a new AnyValueMap by merging two or more maps. Maps defined later in the list override values from previously defined maps.

Parameters

maps – an array of maps to be merged

Returns

a newly created AnyValueMap.

static from_string(line: str)pip_services3_commons.data.StringValueMap.StringValueMap

Parses semicolon-separated key-args pairs and returns them as a StringValueMap.

Parameters

line – semicolon-separated key-args list to initialize StringValueMap.

Returns

a newly created StringValueMap.

static from_tuples(*tuples: Any)pip_services3_commons.data.StringValueMap.StringValueMap

Creates a new StringValueMap from a list of key-args pairs called tuples.

Parameters

tuples – a list of values where odd elements are keys and the following even elements are values

Returns

a newly created StringValueMap.

static from_tuples_array(tuples: Sequence[Any])pip_services3_commons.data.StringValueMap.StringValueMap

Creates a new StringValueMap from a list of key-args pairs called tuples. The method is similar to from_tuples() but tuples are passed as array instead of parameters.

Parameters

tuples – a list of values where odd elements are keys and the following even elements are values

Returns

a newly created StringValueMap.

static from_value(value: Any)pip_services3_commons.data.StringValueMap.StringValueMap

Converts specified args into StringValueMap.

Parameters

value – args to be converted

Returns

a newly created StringValueMap.

get(key: str)str

Gets a map element specified by its key.

Parameters

key – a key of the element to get.

Returns

the args of the map element.

get_as_array(key: str)pip_services3_commons.data.AnyValueArray.AnyValueArray

Converts map element into an AnyValue or returns an empty AnyValue if conversion is not possible.

Parameters

key – a key of element to get.

Returns

AnyValue value of the element or empty AnyValue if conversion is not supported.

get_as_array_with_default(key: str, default_value: pip_services3_commons.data.AnyValueArray.AnyValueArray)pip_services3_commons.data.AnyValueArray.AnyValueArray

Converts map element into an AnyValueArray or returns default value if conversion is not possible.

Parameters
  • key – a key of element to get.

  • default_value – the default value

Returns

AnyValueArray value of the element or default value if conversion is not supported.

get_as_boolean(key: str)bool

Converts map element into a boolean or returns false if conversion is not possible.

Parameters

key – an index of element to get.

Returns

boolean args ot the element or false if conversion is not supported.

get_as_boolean_with_default(key: str, default_value: bool)bool

Converts map element into a boolean or returns default args if conversion is not possible.

Parameters
  • key – an index of element to get.

  • default_value – the default args

Returns

boolean args ot the element or default args if conversion is not supported.

get_as_datetime(key: str)datetime.datetime

Converts map element into a Date or returns the current date if conversion is not possible.

Parameters

key – an index of element to get.

Returns

Date args ot the element or the current date if conversion is not supported.

get_as_datetime_with_default(key: str, default_value: datetime.datetime)datetime.datetime

Converts map element into a Date or returns default args if conversion is not possible.

Parameters
  • key – an index of element to get.

  • default_value – the default args

Returns

Date args ot the element or default args if conversion is not supported.

get_as_double(key: str)float

Converts map element into a double or returns 0 if conversion is not possible.

Parameters

key – an index of element to get.

Returns

double value of the element or 0 if conversion is not supported.

get_as_double_with_default(key: str, default_value: float)float

Converts map element into a double or returns default value if conversion is not possible.

Parameters
  • key – a key of element to get.

  • default_value – the default args

Returns

double value of the element or default value if conversion is not supported.

get_as_float(key: str)float

Converts map element into a float or returns 0 if conversion is not possible.

Parameters

key – an index of element to get.

Returns

float args ot the element or 0 if conversion is not supported.

get_as_float_with_default(key: str, default_value: float)float

Converts map element into a float or returns default args if conversion is not possible.

Parameters
  • key – an index of element to get.

  • default_value – the default args

Returns

float args ot the element or default args if conversion is not supported.

get_as_integer(key: str)int

Converts map element into an integer or returns 0 if conversion is not possible.

Parameters

key – an index of element to get.

Returns

integer args ot the element or 0 if conversion is not supported.

get_as_integer_with_default(key: str, default_value: int)int

Converts map element into an integer or returns default args if conversion is not possible.

Parameters
  • key – an index of element to get.

  • default_value – the default args

Returns

integer args ot the element or default args if conversion is not supported.

get_as_long(key: str)float
get_as_long_with_default(key: str, default_value: float)float
get_as_map(key: Any)pip_services3_commons.data.AnyValueMap.AnyValueMap

Converts map element into an AnyValueMap or returns empty AnyValueMap if conversion is not possible.

Parameters

key – a key of element to get.

Returns

AnyValueMap args of the element or empty AnyValueMap if conversion is not supported.

get_as_map_with_default(key: str, default_value: pip_services3_commons.data.AnyValueMap.AnyValueMap)pip_services3_commons.data.AnyValueMap.AnyValueMap

Converts map element into an AnyValueMap or returns default args if conversion is not possible.

Parameters
  • key – a key of element to get.

  • default_value – the default args

Returns

AnyValueMap args of the element or default args if conversion is not supported.

get_as_nullable_array(key: str) → Optional[pip_services3_commons.data.AnyValueArray.AnyValueArray, None]

Converts map element into an AnyValueArray or returns null if conversion is not possible.

Parameters

key – a key of element to get.

Returns

AnyValueArray value of the element or null if conversion is not supported.

get_as_nullable_boolean(key: str) → Optional[bool, None]

Converts map element into a boolean or returns None if conversion is not possible

Parameters

key – an index of element to get.

Returns

boolean args of the element or None if conversion is not supported.

get_as_nullable_datetime(key: str) → Optional[datetime.datetime, None]

Converts map element into a Date or returns None if conversion is not possible.

Parameters

key – an index of element to get.

Returns

Date args of the element or None if conversion is not supported.

get_as_nullable_double(key: str) → Optional[float, None]

Converts map element into a double or returns null if conversion is not possible.

Parameters

key – an index of element to get.

Returns

double value of the element or null if conversion is not supported.

get_as_nullable_float(key: str) → Optional[float, None]

Converts map element into a float or returns None if conversion is not possible.

Parameters

key – an index of element to get.

Returns

float args of the element or None if conversion is not supported.

get_as_nullable_integer(key: str) → Optional[int, None]

Converts map element into an integer or returns None if conversion is not possible.

Parameters

key – an index of element to get.

Returns

integer args of the element or None if conversion is not supported.

get_as_nullable_long(key: str) → Optional[float, None]
get_as_nullable_map(key: str)pip_services3_commons.data.AnyValueMap.AnyValueMap

Converts map element into an AnyValueMap or returns None if conversion is not possible.

Parameters

key – a key of element to get.

Returns

AnyValueMap args of the element or None if conversion is not supported.

get_as_nullable_string(key: str) → Optional[str, None]

Converts map element into a string or returns None if conversion is not possible.

Parameters

key – an index of element to get.

Returns

string args of the element or None if conversion is not supported.

get_as_nullable_type(value_type: pip_services3_commons.convert.TypeCode.TypeCode, key: str) → Optional[Any, None]

Converts map element into a args defined by specied typecode. If conversion is not possible it returns None.

Parameters
  • value_type – the TypeCode that defined the type of the result

  • key – an index of element to get.

Returns

element args defined by the typecode or None if conversion is not supported.

get_as_object(key: str = None) → Any

Gets the args stored in map element without any conversions. When element key is not defined it returns the entire map args.

Parameters

key – (optional) a key of the element to get

Returns

the element args or args of the map when index is not defined.

get_as_string(key: str)str

Converts map element into a string or returns “” if conversion is not possible.

Parameters

key – an index of element to get.

Returns

string args ot the element or “” if conversion is not supported.

get_as_string_with_default(key: str, default_value: str)str

Converts map element into a string or returns default args if conversion is not possible.

Parameters
  • key – an index of element to get.

  • default_value – the default args

Returns

string args ot the element or default args if conversion is not supported.

get_as_type(value_type: pip_services3_commons.convert.TypeCode.TypeCode, key: str) → Any

Converts map element into a args defined by specied typecode. If conversion is not possible it returns default args for the specified type.

Parameters
  • value_type – the TypeCode that defined the type of the result

  • key – an index of element to get.

Returns

element args defined by the typecode or default if conversion is not supported.

get_as_type_with_default(value_type: pip_services3_commons.convert.TypeCode.TypeCode, key: str, default_value: Any) → Any

Converts map element into a args defined by specied typecode. If conversion is not possible it returns default args.

Parameters
  • value_type – the TypeCode that defined the type of the result

  • key – an index of element to get.

  • default_value – the default args

Returns

element args defined by the typecode or default args if conversion is not supported.

get_as_value(key: str)pip_services3_commons.data.AnyValue.AnyValue

Converts map element into an AnyValue or returns an empty AnyValue if conversion is not possible.

Parameters

key – a key of element to get.

Returns

AnyValue value of the element or empty AnyValue if conversion is not supported.

get_keys() → List[str]

Gets keys of all elements stored in this map.

Returns

a list with all map keys.

length()int

Gets a number of elements stored in this map.

Returns

the number of elements in this map.

put(key: str, value: Any)

Puts a new args into map element specified by its key.

Parameters
  • key – a key of the element to put.

  • value – a new args for map element.

remove(key: str)

Removes a map element specified by its key

Parameters

key – a key of the element to remove.

set_as_map(values: dict)

Sets values to map

Parameters

values – values to set

set_as_object(key: Any, value: Any = None)

Sets a new args to map element specified by its index. When the index is not defined, it resets the entire map args. This method has double purpose because method overrides are not supported in JavaScript.

Parameters
  • key – (optional) a key of the element to set

  • value – a new element or map value.

to_string()

Gets a string representation of the object. The result is a semicolon-separated list of key-args pairs as “key1=value1;key2=value2;key=value3”

Returns

a string representation of the object.