Skip to content

Commit

Permalink
Merge pull request #247 from Dessia-tech/testing
Browse files Browse the repository at this point in the history
Testing to master: towards v0.8.0
  • Loading branch information
masfaraud committed May 6, 2022
2 parents fcc4392 + 86e0573 commit 1e8d5e2
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 271 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,8 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.8.0

### Added
- performance analysis function

### Fixed
- babylon display fix
- Any typing does not trigger error with subclass anymore
- workflow: imposed variable values fixes

### Performance
- types: caching type from calling import_module

## v0.7.0

Expand Down
6 changes: 3 additions & 3 deletions code_pylint.py
Expand Up @@ -21,7 +21,7 @@
'protected-access': 27,
'invalid-name': 8,
'consider-using-f-string': 10,
'no-else-return': 4,
'no-else-return': 0,
'arguments-differ': 12,
'no-member': 1,
'too-many-locals': 14,
Expand All @@ -40,7 +40,7 @@
'unnecessary-comprehension': 5,
'no-value-for-parameter': 2,
'too-many-return-statements': 8,
'raise-missing-from': 6,
'raise-missing-from': 0,
'consider-merging-isinstance': 6,
'abstract-method': 6,
'import-outside-toplevel': 7,
Expand All @@ -52,7 +52,6 @@
'consider-using-get': 2,
'undefined-loop-variable': 2,
'consider-using-with': 2,
'eval-used': 2,
'too-many-nested-blocks': 2,
'bad-staticmethod-argument': 1,
'too-many-public-methods': 2, # Try to lower by splitting DessiaObject and Workflow
Expand All @@ -63,6 +62,7 @@
'use-maxsplit-arg': 1,
'duplicate-code': 1,
# No tolerance errors
'eval-used': 0,
'redefined-builtin': 0,
'arguments-renamed': 0,
'ungrouped-imports': 0,
Expand Down
14 changes: 11 additions & 3 deletions dessia_common/breakdown.py
Expand Up @@ -41,7 +41,7 @@ def extract_from_object(object_, segment):

try:
return object_[int(segment)]
except ValueError:
except ValueError as error:
# should be a tuple
if segment.startswith('(') and segment.endswith(')') and ',' in segment:
key = []
Expand All @@ -53,7 +53,7 @@ def extract_from_object(object_, segment):
key.append(subkey)
return object_[tuple(key)]
# else:
raise NotImplementedError(f'Cannot extract segment {segment} from object {object_}')
raise ValueError(f'Cannot extract segment {segment} from object {object_}') from error

# Finally, it is a regular object
return getattr(object_, segment)
Expand All @@ -63,7 +63,15 @@ def get_in_object_from_path(object_, path):
segments = path.lstrip('#/').split('/')
element = object_
for segment in segments:
element = extract_from_object(element, segment)
if isinstance(element, dict) and '$ref' in element:
# Going down in the object and it is a reference
# Evaluating subreference
element = get_in_object_from_path(object_, element['$ref'])
try:
element = extract_from_object(element, segment)
except ValueError as err:
print(err)
raise ValueError(f'Cannot get segment {segment} from path {path} in element {element}') from err

return element

Expand Down
49 changes: 39 additions & 10 deletions dessia_common/core.py
Expand Up @@ -3,6 +3,8 @@
"""
"""

import time
import sys
import warnings
import math
Expand All @@ -12,7 +14,7 @@
from copy import deepcopy
import inspect
import json
from operator import attrgetter
# from operator import attrgetter

from typing import List, Dict, Any, Tuple, get_type_hints
import traceback as tb
Expand Down Expand Up @@ -420,10 +422,10 @@ def dict_to_arguments(self, dict_, method):
value = dict_[str(i)]
try:
deserialized_value = deserialize_argument(arg_specs, value)
except TypeError:
except TypeError as err:
msg = 'Error in deserialisation of value: '
msg += f'{value} of expected type {arg_specs}'
raise TypeError(msg)
raise TypeError(msg) from err
arguments[arg] = deserialized_value
return arguments

Expand Down Expand Up @@ -527,7 +529,7 @@ def mpl_plot(self, **kwargs):
try:
plot_datas = self.plot_data(**kwargs)
except TypeError as error:
raise TypeError(f'{self.__class__.__name__}.{error}')
raise TypeError(f'{self.__class__.__name__}.{error}') from error
for data in plot_datas:
if hasattr(data, 'mpl_plot'):
ax = data.mpl_plot()
Expand Down Expand Up @@ -580,9 +582,37 @@ def _displays(self, **kwargs) -> List[JsonSerializable]:
displays.append(display_.to_dict())
return displays

def to_markdown(self):
def to_markdown(self) -> str:
"""
Render a markdown of the object output type: string
"""
return templates.dessia_object_markdown_template.substitute(name=self.name)

def _performance_analysis(self):
"""
Prints time of rendering some commons operations (serialization, hash, displays)
"""
data_hash_time = time.time()
self._data_hash()
data_hash_time = time.time() - data_hash_time
print(f'Data hash time: {round(data_hash_time, 3)} seconds')

todict_time = time.time()
dict_ = self.to_dict()
todict_time = time.time() - todict_time
print(f'to_dict time: {round(todict_time, 3)} seconds')

dto_time = time.time()
self.dict_to_object(dict_)
dto_time = time.time() - dto_time
print(f'dict_to_object time: {round(dto_time, 3)} seconds')

for display_setting in self.display_settings():
display_time = time.time()
self._display_from_selector(display_setting.selector)
display_time = time.time() - display_time
print(f'Generation of display {display_setting.selector} in: {round(display_time, 6)} seconds')

def _check_platform(self):
"""
Reproduce lifecycle on platform (serialization, display)
Expand All @@ -601,6 +631,7 @@ def _check_platform(self):
' after serialization/deserialization')
copied_object = self.copy()
if not copied_object._data_eq(self):
print('data diff: ', self._data_diff(copied_object))
raise dessia_common.errors.CopyError('Object is not equal to itself'
' after copy')

Expand All @@ -624,8 +655,7 @@ def to_xlsx_stream(self, stream):
writer = XLSXWriter(self)
writer.save_to_stream(stream)

@staticmethod
def _export_formats():
def _export_formats(self):
formats = [{"extension": "json", "method_name": "save_to_stream", "text": True, "args": {}},
{"extension": "xlsx", "method_name": "to_xlsx_stream", "text": False, "args": {}}]
return formats
Expand Down Expand Up @@ -707,9 +737,8 @@ def save_babylonjs_to_file(self, filename: str = None, use_cdn: bool = True,
use_cdn=use_cdn,
debug=debug)

@staticmethod
def _export_formats():
formats = DessiaObject._export_formats()
def _export_formats(self):
formats = DessiaObject._export_formats(self)
formats3d = [{"extension": "step", "method_name": "to_step_stream", "text": True, "args": {}},
{"extension": "stl", "method_name": "to_stl_stream", "text": False, "args": {}}]
formats.extend(formats3d)
Expand Down

0 comments on commit 1e8d5e2

Please sign in to comment.