Skip to content

Commit

Permalink
feat(error): fabric_error with json encode (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
portellaa authored Oct 14, 2022
1 parent 8bd9783 commit 546830c
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements-dev.txt
- name: Lint
run: make lint PYTHON=$PYTHON

- name: Test
run: make test PYTHON=$PYTHON
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ name: Releases
on:
release:
types:
- prereleased
- released


Expand Down
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ endif
venv3: ### Creates a virtual environment for this project
test -d $(VENV) || python3.8 -m venv $(VENV)
$(PIP) install --upgrade pip wheel setuptools twine
$(PIP) install -r requirements-dev.txt

clean: clean-build clean-pyc ### Cleans artifacts

Expand Down Expand Up @@ -53,11 +54,15 @@ upload: ### Upload build package into pypi
$(call UPLOAD,core)

lint: ### Run prospector
$(PYTHON) -m prospector src/dask
$(PYTHON) -m prospector src/core

define LINK_LOCAL
$(PIP) install -e src/$1
endef

link-local:
link-core:
echo "0.0.0" > src/core/VERSION
$(call LINK_LOCAL,core)

test: link-core ### Runs the tests
$(PYTHON) -m pytest src/core
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
prospector==1.7.*
pytest==7.1.*
6 changes: 6 additions & 0 deletions setup.cfg
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
1 change: 1 addition & 0 deletions src/core/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.0
21 changes: 21 additions & 0 deletions src/core/setup.py
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}})
43 changes: 43 additions & 0 deletions src/core/tests/test_fabric_error.py
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
1 change: 1 addition & 0 deletions src/core/ydata/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
6 changes: 6 additions & 0 deletions src/core/ydata/core/error/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .fabric_error import FabricError


__all__ = [
"FabricError"
]
41 changes: 41 additions & 0 deletions src/core/ydata/core/error/fabric_error.py
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)

0 comments on commit 546830c

Please sign in to comment.