compas
Utility functions
JSON handling
- compas.json_dump(data, fp, pretty=False)[source]
Write a collection of COMPAS object data to a JSON file.
- Parameters
data (any) – Any JSON serializable object. This includes any (combination of) COMPAS object(s).
fp (path string or file-like object) – A writeable file-like object or the path to a file.
pretty (bool, optional) –
True
to format the output with indentation, otherwiseFalse
.
- Returns
None
Examples
>>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> compas.json_dump(data1, 'data.json') >>> data2 = compas.json_load('data.json') >>> data1 == data2 True
- compas.json_dumps(data, pretty=False)[source]
Write a collection of COMPAS objects to a JSON string.
- Parameters
data (any) – Any JSON serializable object. This includes any (combination of) COMPAS object(s).
pretty (bool, optional) –
True
to format the output with indentation, otherwiseFalse
.
- Returns
str
Examples
>>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> s = compas.json_dumps(data1) >>> data2 = compas.json_loads(s) >>> data1 == data2 True
- compas.json_load(fp)[source]
Read COMPAS object data from a JSON file.
- Parameters
fp (path string, file-like object or URL string) – A readable path, a file-like object or a URL pointing to a file.
- Returns
data – The (COMPAS) data contained in the file.
Examples
>>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> compas.json_dump(data1, 'data.json') >>> data2 = compas.json_load('data.json') >>> data1 == data2 True
- compas.json_loads(s)[source]
Read COMPAS object data from a JSON string.
- Parameters
s (str) – A JSON data string.
- Returns
data – The (COMPAS) data contained in the string.
Examples
>>> import compas >>> from compas.geometry import Point, Vector >>> data1 = [Point(0, 0, 0), Vector(0, 0, 0)] >>> s = compas.json_dumps(data1) >>> data2 = compas.json_loads(s) >>> data1 == data2 True
Precision
- compas.set_precision(precision)[source]
Set the precision used by geometric maps.
- Parameters
precision (float) – The precision as a floating point number. For example,
0.0001
.
Notes
This function converts the floating point number to a string formatting specifier and assigns the specifier to
compas.PRECISION
.Examples
>>> compas.set_precision(0.001) >>> compas.PRECISION '3f'
- compas.PRECISION = '3f'
str: The precision used by COMPAS for generation of geometric keys, for the comparison of point locations, for the parsing of geometry files, and for the generation of human-readable representations of geometry objects.
The string is in the format used by the Python string formating mini language for formatting numbers. Float formatting (
'<x>f'
) and integer formatting ('d'
) specifiers are supported.
Execution context
- compas.is_windows()[source]
Check if the operating system is Windows.
- Returns
bool – True if the OS is Windows. False otherwise
- compas.is_linux()[source]
Check if the operating system is Linux.
- Returns
bool – True if the OS is Linux. False otherwise
- compas.is_osx()[source]
- compas.is_mono()[source]
Check if the operating system is running on Mono.
- Returns
bool – True if the OS is running on Mono. False otherwise
- compas.is_ironpython()[source]
Check if the Python implementation is IronPython.
- Returns
bool – True if the implementation is IronPython. False otherwise
- compas.is_rhino()[source]
- compas.is_blender()[source]
- compas.WINDOWS = False
True if the current operating system is Windows, False otherwise.
- Type
- compas.LINUX = True
True if the current operating system is Linux, False otherwise.
- Type
- compas.OSX = False
True if the current operating system is OSX, False otherwise.
- Type
- compas.MONO = False
True if the current operating system is Mono, False otherwise.
- Type
- compas.IPY = False
True if the current Python implementation is IronPython, False otherwise.
- Type
- compas.RHINO = True
True if the current context is Rhino, False otherwise.
- Type
- compas.BLENDER = True
True if the current context is Blender, False otherwise.
- Type