pip_services3_commons.reflect package

Submodules

Module contents

pip_services3_commons.reflect.__init__

Contains classes for data reflection. Reflects objects into parameters, methods. Most programming languages contain reflections, but they are all implemented differently. In the PipService framework, dynamic data types are often used. So as to not rewrite these dynamic data types differently for each language, this cross-language reflection package was written. All dynamic data types that are built on top of this package are portable from one language to another.

copyright

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

license

MIT, see LICENSE for more details.

class pip_services3_commons.reflect.MethodReflector

Bases: object

Helper class to perform method introspection and dynamic invocation. This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity __rules, this MethodReflector treats all method names as case insensitive.

Example:

myObj = new MyObject()

methods = MethodReflector.get_method_names()
MethodReflector.has_method(myObj, "myMethod")
MethodReflector.invoke_method(myObj, "myMethod", 123)
static get_method_names(obj: Any) → List[str]

Gets names of all methods implemented in specified object.

Parameters

obj – an object to introspect.

Returns

a list with method names.

static has_method(obj: Any, name: str)bool

Checks if object has a method with specified name.

Parameters
  • obj – an object to introspect.

  • name – a name of the method to check.

Returns

true if the object has the method and false if it doesn’t.

static invoke_method(obj: Any, name: str, *args: Any) → Any

Invokes an object method by its name with specified parameters.

Parameters
  • obj – an object to invoke.

  • name – a name of the method to invoke.

  • args – a list of method arguments.

Returns

the result of the method invocation or null if method returns void.

class pip_services3_commons.reflect.ObjectReader

Bases: object

Helper class to perform property introspection and dynamic reading.

In contrast to PropertyReflector which only introspects regular objects, this ObjectReader is also able to handle maps and arrays.

For maps properties are key-pairs identified by string keys, For arrays properties are elements identified by integer index.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity __rules, this ObjectReader treats all property names as case insensitive.

Example:

myObj = MyObject()

properties = ObjectReader.get_property_names()

ObjectReader.has_property(myObj, "myProperty")
args = PropertyReflector.get_property(myObj, "myProperty")

myMap = { key1: 123, key2: "ABC" }
ObjectReader.has_property(myMap, "key1")
args = ObjectReader.get_property(myMap, "key1")

myArray = [1, 2, 3]
ObjectReader.has_property(myArrat, "0")
args = ObjectReader.get_property(myArray, "0")
static get_properties(obj: Any) → Any

Get values of all properties in specified object and returns them as a map.

The object can be a user defined object, map or array. Returned properties correspondently are object properties, map key-pairs or array elements with their indexes.

Parameters

obj – an object to get properties from.

Returns

a map, containing the names of the object’s properties and their values.

static get_property(obj: Any, name: str) → Any

Gets args of object property specified by its name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

Parameters
  • obj – an object to read property from.

  • name – a name of the property to get.

Returns

the property args or null if property doesn’t exist or introspection failed.

static get_property_names(obj: Any) → List[str]

Gets names of all properties implemented in specified object.

The object can be a user defined object, map or array. Returned property name correspondently are object properties, map keys or array indexes.

Parameters

obj – an object to introspect.

Returns

a list with property names.

static get_value(obj: Any) → Any

Gets a real object args. If object is a wrapper, it unwraps the args behind it. Otherwise it returns the same object args.

Parameters

obj – an object to unwrap.

Returns

an actual (unwrapped) object args.

static has_property(obj: Any, name: str)bool

Checks if object has a property with specified name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

Parameters
  • obj – an object to introspect.

  • name – a name of the property to check.

Returns

true if the object has the property and false if it doesn’t.

class pip_services3_commons.reflect.ObjectWriter

Bases: object

Helper class to perform property introspection and dynamic writing.

In contrast to PropertyReflector which only introspects regular objects, this ObjectWriter is also able to handle maps and arrays. For maps properties are key-pairs identified by string keys, For arrays properties are elements identified by integer index.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity __rules, this ObjectWriter treats all property names as case insensitive.

Example:

myObj = MyObject()

ObjectWriter.set_property(myObj, "myProperty", 123)
myMap = { key1: 123, key2: "ABC" }
ObjectWriter.set_property(myMap, "key1", "XYZ")

myArray = [1, 2, 3]
ObjectWriter.set_property(myArray, "0", 123)
static set_properties(obj: Any, values: Any)

Sets values of some (all) object properties.

The object can be a user defined object, map or array. Property values correspondently are object properties, map key-pairs or array elements with their indexes.

If some properties do not exist or introspection fails they are just silently skipped and no errors thrown.

Parameters
  • obj – an object to write properties to.

  • values – a map, containing property names and their values.

static set_property(obj: Any, name: str, value: Any)

ets args of object property specified by its name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

If the property does not exist or introspection fails this method doesn’t do anything and doesn’t any throw errors.

Parameters
  • obj – an object to write property to.

  • name – a name of the property to set.

  • value – a new args for the property to set.

class pip_services3_commons.reflect.PropertyReflector

Bases: object

Helper class to perform property introspection and dynamic reading and writing.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity __rules, this PropertyReflector treats all property names as case insensitive.

Example:

myObj = MyObject()

properties = PropertyReflector.get_property_names()
PropertyReflector.has_property(myObj, "myProperty")

args = PropertyReflector.get_property(myObj, "myProperty")
PropertyReflector.set_property(myObj, "myProperty", 123)
static get_properties(obj: Any) → Any

Get values of all properties in specified object and returns them as a map.

Parameters

obj – an object to get properties from.

Returns

a map, containing the names of the object’s properties and their values.

static get_property(obj: Any, name: str) → Any

Gets args of object property specified by its name.

Parameters
  • obj – an object to read property from.

  • name – a name of the property to get.

Returns

the property args or null if property doesn’t exist or introspection failed.

static get_property_names(obj: Any) → List[str]

Gets names of all properties implemented in specified object.

Parameters

obj – an objec to introspect.

Returns

a list with property names.

static has_property(obj: Any, name: str)bool

Checks if object has a property with specified name.

Parameters
  • obj – an object to introspect.

  • name – a name of the property to check.

Returns

true if the object has the property and false if it doesn’t.

static set_properties(obj: Any, values: Any)

Sets values of some (all) object properties.

If some properties do not exist or introspection fails they are just silently skipped and no errors thrown.

Parameters
  • obj – an object to write properties to.

  • values – a map, containing property names and their values.

static set_property(obj: Any, name: str, value: Any)

Sets args of object property specified by its name.

If the property does not exist or introspection fails this method doesn’t do anything and doesn’t any throw errors.

Parameters
  • obj – an object to write property to.

  • name – a name of the property to set.

  • value – a new args for the property to set.

class pip_services3_commons.reflect.RecursiveObjectReader

Bases: object

Helper class to perform property introspection and dynamic reading.

It is similar to ObjectReader but reads properties recursively through the entire object graph. Nested property names are defined using dot notation as “object.subobject.property”

static get_properties(obj: Any) → Any

Get values of all properties in specified object and its subobjects and returns them as a map.

The object can be a user defined object, map or array. Returned properties correspondently are object properties, map key-pairs or array elements with their indexes.

Parameters

obj – an object to get properties from.

Returns

a map, containing the names of the object’s properties and their values.

static get_property(obj: Any, name: str) → Any

Recursively gets args of object or its subobjects property specified by its name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

Parameters
  • obj – an object to read property from.

  • name – a name of the property to get.

Returns

the property args or null if property doesn’t exist or introspection failed.

static get_property_names(obj: Any) → List[str]

Recursively gets names of all properties implemented in specified object and its subobjects.

The object can be a user defined object, map or array. Returned property name correspondently are object properties, map keys or array indexes.

Parameters

obj – an object to introspect.

Returns

a list with property names.

static has_property(obj: Any, name: str)bool

Checks recursively if object or its subobjects has a property with specified name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

Parameters
  • obj – an object to introspect.

  • name – a name of the property to check.

Returns

true if the object has the property and false if it doesn’t.

class pip_services3_commons.reflect.RecursiveObjectWriter

Bases: object

Helper class to perform property introspection and dynamic writing.

It is similar to ObjectWriter but writes properties recursively through the entire object graph. Nested property names are defined using dot notation as “object.subobject.property”

static copy_properties(dest: Any, src: Any)

Copies content of one object to another object by recursively reading all properties from source object and then recursively writing them to destination object.

Parameters
  • dest – a destination object to write properties to.

  • src – a source object to read properties from

static set_properties(obj: Any, values: Any)

Recursively sets values of some (all) object and its subobjects properties.

The object can be a user defined object, map or array. Property values correspondently are object properties, map key-pairs or array elements with their indexes.

If some properties do not exist or introspection fails they are just silently skipped and no errors thrown.

Parameters
  • obj – an object to write properties to.

  • values – a map, containing property names and their values.

static set_property(obj: Any, name: str, value: Any)

Recursively sets args of object and its subobjects property specified by its name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

If the property does not exist or introspection fails this method doesn’t do anything and doesn’t any throw errors.

Parameters
  • obj – an object to write property to.

  • name – a name of the property to set.

  • value – a new args for the property to set.

class pip_services3_commons.reflect.TypeDescriptor(name: str, library: Optional[str, None])

Bases: object

Descriptor that points to specific object type by it’s name and optional library (or module) where this type is defined.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

equals(other: Any)bool

Compares this descriptor to a args. If the args is also a TypeDescriptor it compares their name and library fields. Otherwise this method returns false.

Parameters

other – a args to compare.

Returns

true if args is identical TypeDescriptor and false otherwise.

static from_string(value: str) → Optional[pip_services3_commons.reflect.TypeDescriptor.TypeDescriptor, None]

Parses a string to get descriptor fields and returns them as a Descriptor. The string must have format name[,library]

Parameters

value – a string to parse.

Returns

a newly created Descriptor.

get_library()str

Gets the name of the library or module where the object type is defined.

Returns

the name of the library or module.

get_name()str

Get the name of the object type.

Returns

the name of the object type.

to_string()

Gets a string representation of the object. The result has format name[,library]

Returns

a string representation of the object.

class pip_services3_commons.reflect.TypeMatcher

Bases: object

Helper class matches args types for equality.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

static match_type(expected_type: Any, actual_type: pip_services3_commons.convert.TypeCode.TypeCode, actual_value: Any = None)bool

Matches expected type to an actual type. The types can be specified as types, type names or TypeCode.

Parameters
  • expected_type – an expected type to match.

  • actual_type – an actual type to match.

  • actual_value – an optional args to match its type to the expected one.

Returns

True if types are matching and False if they don’t.

static match_type_by_name(expected_type: str, actual_type: pip_services3_commons.convert.TypeCode.TypeCode, actual_value: Any = None)bool

Matches expected type to an actual type.

Parameters
  • expected_type – an expected type name to match.

  • actual_type – an actual type to match defined by type code.

  • actual_value – an optional args to match its type to the expected one.

Returns

true if types are matching and false if they don’t.

static match_value_type(expected_type: Any, actual_value: Any)bool

Matches expected type to a type of a args. The expected type can be specified by a type, type name or TypeCode.

Parameters
  • expected_type – an expected type to match.

  • actual_value – a args to match its type to the expected one.

Returns

True if types are matching and False if they don’t.

static match_value_type_by_name(expected_type: str, actual_value: Any)bool

Matches expected type to a type of a args.

Parameters
  • expected_type – an expected type name to match.

  • actual_value – a args to match its type to the expected one.

Returns

True if types are matching and False if they don’t.

class pip_services3_commons.reflect.TypeReflector

Bases: object

Helper class to perform object type introspection and object instantiation.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity __rules, this TypeReflector treats all type names as case insensitive.

Example:

descriptor = TypeDescriptor("MyObject", "mylibrary")
Typeeflector.get_type_by_descriptor(descriptor)
myObj = TypeReflector.create_instance_by_descriptor(descriptor)
TypeDescriptor.is_primitive(myObject)           # Result: false
TypeDescriptor.is_primitive(123)                # Result: true
static create_instance(name: str, library: str, *args: Any) → Any

Creates an instance of an object type specified by its name and library where it is defined.

Parameters
  • name – an object type (factory function) to create.

  • library – a library (module) where object type is defined.

  • args – arguments for the object constructor.

Returns

the created object instance.

static create_instance_by_descriptor(descriptor: pip_services3_commons.reflect.TypeDescriptor.TypeDescriptor, *args: Any) → Any

Creates an instance of an object type specified by type descriptor.

Parameters
  • descriptor – a type descriptor that points to an object type

  • args – arguments for the object constructor.

Returns

the created object instance.

static create_instance_by_type(obj_type: Any, *args: Any) → Any

Creates an instance of an object type.

Parameters
  • obj_type – an object type (factory function) to create.

  • args – arguments for the object constructor.

Returns

the created object instance.

static get_type(name: str, library: str) → Any

Gets object type by its name and library where it is defined.

Parameters
  • name – an object type name.

  • library – a library where the type is defined

Returns

the object type or null is the type wasn’t found.

static get_type_by_descriptor(descriptor: pip_services3_commons.reflect.TypeDescriptor.TypeDescriptor) → Any

Gets object type by type descriptor.

Parameters

descriptor – a type descriptor that points to an object type

Returns

the object type or null is the type wasn’t found.

static is_primitive(value: Any)bool

Checks if args has primitive type.

Primitive types are: numbers, strings, booleans, date and time. Complex (non-primitive types are): objects, maps and arrays

Parameters

value – a args to check

Returns

true if the args has primitive type and false if args type is complex.