Data¶
Description¶
Module: geocompy.data
The data module provides utility functions and classes for serializing and deserializing data in the serial communication.
Functions¶
parse_stringparse_boolget_enumget_enum_parser
Types¶
AngleByteVectorCoordinate
Definitions¶
-
class Angle(value: float, unit: 'deg' | 'rad' | 'gon' =
'rad', *, normalize: bool =False, positive: bool =False)[source]¶ Type to represent an angular value.
Angles support typical arithmetic operations.
Notes
An Angle can be instantiated from a number of units, and can be converted to any other unit, but internally it is always represented in radians.
- Parameters:
- Raises:
ValueError – If an unknown unit was passed.
-
classmethod parse(string: str, unit: 'deg' | 'rad' | 'gon' =
'rad') Angle[source]¶ Parses string value to float and creates new Angle.
-
asunit(unit: 'deg' | 'rad' | 'gon' =
'rad') float[source]¶ Returns the represented angle in the target unit.
- Parameters:
- unit: 'deg' | 'rad' | 'gon' =
'rad'¶ Target unit, by default ‘rad’
- unit: 'deg' | 'rad' | 'gon' =
- Returns:
Angular value.
- Return type:
- Raises:
ValueError – If an unknown unit was passed
-
normalized(positive: bool =
True) Self[source]¶ Returns a copy of the angle normalized to full angle.
- relative_to(other: SupportsFloat) Self[source]¶
Returns an angle relative to a reference angle in the [-180;+180] deg range.
The calculated relative angle is positive in the clockwise, and negative in the counter clockwise-direction. Value is always between -180 degrees and +180 degrees.
- Parameters:
- other: SupportsFloat¶
Reference angle.
- Returns:
Relative angle.
- Return type:
- class Byte(value: int)[source]¶
Utility type to represent a single byte value.
The main purpose of this class is to help the parsing and formatting of byte values during the handling of serial communication.
Examples
Creating, then “serializing” a Byte:
>>> b = gc.data.Byte(17) >>> print(b) '11'Parsing a Byte from the serialized representation:
>>> value = "'11'" >>> b = gc.data.Byte.parse(value)- Parameters:
- Raises:
ValueError – If the passed value is outside the [0; 255] range.
- class Coordinate(x: float, y: float, z: float)[source]¶
Type to represent a position with 3D cartesian coordinates.
Coordinates support typical arithmetic operations.
Examples
Creating new coordinate and accessing components:
>>> c = gc.data.Coordinate(1, 2, 3) >>> print(c) Coordinate(1.0, 2.0, 3.0) >>> c.x 1.0 >>> c[1] 2.0 >>> x, y, z = c >>> z 3.0- classmethod from_polar(hz: Angle, v: Angle, dist: float) Self[source]¶
Constructs 3D cartesian coordinate from polar survey coordinates.
- to_2d() Self[source]¶
Returns a copy of the coordinate with the vertical component set to zero.
- Returns:
New coordinate with 0 vertical component.
- Return type:
- class Vector(x: float, y: float, z: float)[source]¶
Type to represent a position or direction with 3D cartesian coordinates.
Vectors support typical arithmetic operations.
Examples
Creating new vector and accessing components:
>>> c = gc.data.Vector(1, 2, 3) >>> print(c) Vector(1.0, 2.0, 3.0) >>> c.x 1.0 >>> c[1] 2.0 >>> x, y, z = c >>> z 3.0- length() float[source]¶
Calculates the length of the vector.
- Returns:
Length of the vector.
- Return type:
- normalized() Self[source]¶
Returns a copy of the vector, normalized to unit length.
- Returns:
Normalized vector.
- Return type:
Self
-
swizzle(x: 'x' | 'y' | 'z' | '0', y: 'x' | 'y' | 'z' | '0', z: 'x' | 'y' | 'z' | '0', *, flip_x: bool =
False, flip_y: bool =False, flip_z: bool =False) Self[source]¶ Returns a copy of the vector, with the components rearranged according to the swizzle spec.
- Parameters:
- x: 'x' | 'y' | 'z' | '0'¶
Component to use as X component.
- y: 'x' | 'y' | 'z' | '0'¶
Component to use as Y component.
- z: 'x' | 'y' | 'z' | '0'¶
Component to use as Z component.
- flip_x: bool =
False¶ Negate X component, by default False
- flip_y: bool =
False¶ Negate Y component, by default False
- flip_z: bool =
False¶ Negate Z component, by default False
- Returns:
Vector with swizzled components.
- Return type:
Self
Example
>>> v = Vector(1.0, 2.0, 3.0) >>> v.swizzle('y', 'x', 'z') Vector(2.0, 1.0, 3.0) >>> v.swizzle('x', 'y', '0', flip_x=True) Vector(-1.0, 2.0, 0.0)
- get_enum(e: type[_E], value: _E | str) _E[source]¶
Returns the member of an
Enumwith the given name.If the passed value is already a member instance, the function returns it without modification.
- Parameters:
- Returns:
Enum member instance.
- Return type:
Examples
>>> from enum import Enum >>> >>> class MyEnum(Enum): ... ONE = 1 ... TWO = 2 >>> >>> gc.data.toenum(MyEnum, 'ONE') <MyEnum.ONE: 1> >>> gc.data.toenum(MyEnum, MyEnum.TWO) <MyEnum.TWO: 2>
- get_enum_parser(e: type[_E]) Callable[[str], _E][source]¶
Returns a parser function that can parse the target enum from the serialized enum value.
- Parameters:
- Returns:
Parser function, that takes a string as input, and returns an enum member.
- Return type:
Callable
Examples
>>> from enum import Enum >>> >>> class MyEnum(Enum): ... ONE = 1 ... TWO = 2 >>> >>> parser = gc.data.enumparser(MyEnum) >>> parser('1') <MyEnum.ONE: 1>
- parse_string(value: str) str[source]¶
Returns a string value with the enclosing quote marks (
"...") removed.- Parameters:
- Returns:
String suitable for further processing.
- Return type:
Notes
When a string value is read from an instrument connection, the string is enclosed quotes to indicated the data type. This is a simple convenience function to strip them.