generated from ydataai/opensource-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(error): fabric_error with json encode (#2)
- Loading branch information
Showing
11 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ name: Releases | |
on: | ||
release: | ||
types: | ||
- prereleased | ||
- released | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
prospector==1.7.* | ||
pytest==7.1.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[pycodestyle] | ||
count = False | ||
ignore = E111,E114,E266 | ||
indent-size = 2 | ||
max-line-length = 120 | ||
statistics = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pathlib import Path | ||
from setuptools import setup, find_packages | ||
|
||
here = Path(__file__).parent.resolve() | ||
|
||
version = (here / 'VERSION').read_text().rstrip("\n") | ||
|
||
setup(name='ydata-core', | ||
version=version, | ||
description='Core functionality for all python packages at YData', | ||
author='YData', | ||
author_email='[email protected]', | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'Programming Language :: Python :: 3 :: Only', | ||
'Topic :: Software Development :: Libraries :: Python Modules' | ||
], | ||
url='https://github.com/ydataai/python-core', | ||
packages=find_packages(exclude=['ydata', 'tests']), | ||
include_package_data=True, | ||
options={"bdist_wheel": {"universal": True}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from operator import contains | ||
from ydata.core.error.fabric_error import FabricError | ||
|
||
|
||
def test_basic_error(): | ||
basic_error = FabricError() | ||
basic_error.name = "BasicError" | ||
basic_error.description = "This is a basic test error" | ||
basic_error.return_value = -1 | ||
|
||
basic_error_dict = dict(basic_error) | ||
|
||
assert 'name' in basic_error_dict | ||
assert 'description' in basic_error_dict | ||
assert 'return_value' in basic_error_dict | ||
|
||
def test_full_error(): | ||
basic_error = FabricError(context={'a': 'a'}, http_code=500, name="FullError") | ||
basic_error.description = "This is a full test error" | ||
basic_error.return_value = -1 | ||
|
||
basic_error_dict = dict(basic_error) | ||
|
||
assert 'name' in basic_error_dict | ||
assert 'description' in basic_error_dict | ||
assert 'return_value' in basic_error_dict | ||
assert 'context' in basic_error_dict | ||
assert 'http_code' in basic_error_dict | ||
|
||
def test_extended_error(): | ||
class CustomError(FabricError): | ||
description = "This is extended error" | ||
return_value = -2 | ||
|
||
custom_error = CustomError(context={'a': 'a'}, http_code=500) | ||
|
||
custom_error_dict = dict(custom_error) | ||
|
||
assert 'name' in custom_error_dict | ||
assert 'description' in custom_error_dict | ||
assert 'return_value' in custom_error_dict | ||
assert 'context' in custom_error_dict | ||
assert 'http_code' in custom_error_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__path__ = __import__('pkgutil').extend_path(__path__, __name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .fabric_error import FabricError | ||
|
||
|
||
__all__ = [ | ||
"FabricError" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import json | ||
from typing import Optional | ||
|
||
|
||
def _camelcased(value: str) -> str: | ||
capitalized = ''.join(x.capitalize() for x in value.split('_')) | ||
return capitalized[0].lower() + capitalized[1:] | ||
|
||
|
||
class FabricError(Exception): | ||
context: Optional[dict] | ||
description: str | ||
http_code: Optional[int] | ||
name: Optional[str] | ||
return_value: int | ||
|
||
def __init__(self, context: Optional[dict] = None, http_code: Optional[int] = None, name: Optional[str] = None): | ||
self.context = context | ||
self.http_code = http_code | ||
self.name = name | ||
|
||
def __iter__(self): | ||
yield from { | ||
"context": self.context, | ||
"name": self.name if self.name else self.__class__.__name__, | ||
"description": self.description, | ||
"http_code": self.http_code, | ||
"return_value": self.return_value | ||
}.items() | ||
|
||
def __str__(self) -> str: | ||
return f''' | ||
{self.__class__.__name__}(name={self.name}, context={self.context}, description={self.description}, \ | ||
return_value={self.return_value}) | ||
''' | ||
|
||
def __repr__(self) -> str: | ||
return self.__str__() | ||
|
||
def json(self): | ||
return json.dumps(self, default=lambda o: {_camelcased(k): v for k, v in dict(o).items()}, ensure_ascii=False) |