Code References

Read this page to know how to use primitive-type library.

At present, all methods, type aliases could be imported from path primitive_type immediately.

Type Aliases

Type aliases for primitive types.

Note

Real API path: primitive_type.types

type Primitive = str | int | float | bool | None

Type alias for primitive values.

Including: str, int, float, bool, and None.

type PrimitiveMap = dict[str, Primitive]

Type alias for a mapping of string keys to primitive values.

This dictionary represents a collection of metadata or attributes where each value is guaranteed to be a Primitive type, ensuring type safety and ease of serialization.

Type Checker

Utils for checking objects.

Note

Real API path: primitive_type.checker

is_primitive(val: object) bool[source]

Check an object if it’s a primitive object.

Parameters:

val – Any instance for checking.

Returns:

  • True – If the object is primitive

  • False – Otherwise.

is_nested_dict(obj: dict) bool[source]

Check a dict if it has nested structures.

Parameters:

obj – Any dict object for checking.

Returns:

  • True – If the dict has nested structures.

  • False – Otherwise.

Type Converter

A quick and simple converter to convert the type of an object.

Note

Real API path: primitive_type.converter

class ConvertError[source]

An exception should happens when conversion failed.

get_primitive_object(val: Primitive, obj_type: type[Primitive] = None) Primitive[source]

Get a primitive object from a given value.

Parameters:
  • val – The given value wants to be converted.

  • obj_type – The target type of object that finally converted out. Default to None as disabled.

Returns:

  • str – If given value is a normal string, or convert to if type specified.

  • int – The origin object or convert to.

  • float – The origin object or convert to.

  • bool – The origin object or convert to.

  • None – Only when given value is None.

Note

Methods get_str_object, get_int_object, get_float_object and get_bool_object are simplified method for get_primitive_object, they return get_primitive_object(val, <type>) and confirm the return type is specified type.

get_str_object(val: Primitive) str[source]

Get a string object from a given value.

Parameters:

val – The given value wants to be converted.

Returns:

A string object.

get_int_object(val: Primitive) int[source]

Get an integer object from a given value.

Parameters:

val – The given value wants to be converted.

Returns:

An integer object.

get_float_object(val: Primitive) float[source]

Get a float object from a given value.

Parameters:

val – The given value wants to be converted.

Returns:

A float object.

get_bool_object(val: Primitive) bool[source]

Get a boolean object from a given value.

Parameters:

val – The given value wants to be converted.

Returns:

A bool object.