Basic
packtype.utils.basic
clog2(x)
Calculate the ceiling of the base-2 logarithm of x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int
|
The integer to calculate the logarithm of |
required |
Returns:
| Type | Description |
|---|---|
int
|
The ceiling of the base-2 logarithm of x |
copy(pinst)
Safely copy a Packtype instance
This is also more performant than using copy.deepcopy() on the packtype objects.
Use unpack and pack to create a new instance via the packed integer
diff(value_a, value_b, verbose=False, _path=None)
A recursive function to generate a diff between two Packtype instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_a
|
type[Base]
|
Item to be compared |
required |
value_b
|
type[Base]
|
Other item to be compared |
required |
verbose
|
bool
|
Show all fields in complex objects and do not filter matching objects |
False
|
_path
|
list[str] | None
|
A list of strings that can be joined with a '.' to represent the current field path in the recursion |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, list[str | int | Enum]]
|
A dictionary containing fields of: Member name, Value A, Value B and "Diff" |
diff_table(value_a, value_b, verbose=False)
Generate a diff between two Packtype instances. This is a recursive function that walks through the hierarchy and compares all fields, tabulating where differences occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value_a
|
type[Base]
|
First item to be compared |
required |
value_b
|
type[Base]
|
Second item to be compared |
required |
verbose
|
bool
|
Show all fields in complex objects and do not filter matching objects (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A tabulate table containing differing fields. If the objects are the same an empty string is returned. This function should not be used to check the equality of two objects as it is orders of magnitude slower than the eq operation added to all Numeric types |
get_doc(ptype)
Get the docstring of a Packtype definition
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[Base] | Base
|
The Packtype definition to inspect |
required |
Returns:
| Type | Description |
|---|---|
str
|
The docstring of the Packtype definition |
get_name(ptype)
Get the name of a Packtype definition
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[Base] | Base
|
The Packtype definition to inspect |
required |
Returns:
| Type | Description |
|---|---|
str
|
The name of the Packtype definition |
get_package(ptype)
Get the package a Packtype definition is attached to, if the type is not associated to a package then None will be returned
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[Base] | Base
|
The Packtype definition to inspect |
required |
Returns:
| Type | Description |
|---|---|
type[Base] | None
|
The Package to which this type is attached |
get_source(ptype)
Get the name of a Packtype definition
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[Base] | Base
|
The Packtype definition to inspect |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, int]
|
The source file and line number of the Packtype definition |
get_width(ptype)
Get the width of a Packtype definition
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[PackedAssembly | Enum | NumericType | Union] | PackedAssembly | NumericType | Union
|
The Packtype definition to inspect |
required |
Returns:
| Type | Description |
|---|---|
int
|
The width in bits of the Packtype definition |
is_scalar(ptype)
Check if a Packtype definition is a scalar type
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[Base] | Base
|
The Packtype definition to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the definition is a scalar type, False otherwise |
is_signed(ptype)
Check if a Packtype definition is signed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[NumericType] | NumericType
|
The Packtype definition to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the definition is signed, False otherwise |
pack(pinst)
Pack an instance of a Packtype definition into an integer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pinst
|
Base
|
The instance of the Packtype definition to pack |
required |
Returns:
| Type | Description |
|---|---|
int
|
The packed value as an integer |
unpack(ptype, value)
Unpack a value into a Packtype definition
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ptype
|
type[unpack[T]]
|
The Packtype definition to unpack into |
required |
value
|
int
|
The value to unpack |
required |
Returns:
| Type | Description |
|---|---|
unpack[T]
|
An instance of the Packtype definition with the unpacked value |