Skip to content

Commit

Permalink
Fix for new release (#10)
Browse files Browse the repository at this point in the history
* small code changes

- add deregister
- add tests for register / deregister
- replace relative imports with absoluts

* update black version

* change license to `Apache License, Version 2.0`

* register all converters that are available in the __init__

* bugfix + change tests

* remove no kwargs req

* use mark.parametrize

* small change
  • Loading branch information
PythonFZ committed Mar 8, 2022
1 parent e5b44ab commit dcba60a
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 309 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fail_fast: true

repos:
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black

Expand Down
528 changes: 251 additions & 277 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
line-length = 90
experimental_string_processing = true
preview = true

[tool.isort]
profile = 'black'
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name="znjson",
version="0.1.1",
version="0.1.0",
author="zincwarecode",
author_email="zincwarecode@gmail.com",
description="A Python Package to Encode/Decode some common file formats to json",
Expand All @@ -19,7 +19,7 @@
classifiers=[
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: Eclipse Public License 2.0 (EPL-2.0)",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
],
python_requires=">=3.7",
Expand Down
17 changes: 13 additions & 4 deletions tests/converter/test_numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@

import znjson

znjson.register([znjson.converter.NumpyConverter, znjson.converter.SmallNumpyConverter])


@pytest.fixture
def numpy_array():
return np.arange(100)


def test_encode(numpy_array):
arr = json.dumps(numpy_array, cls=znjson.ZnEncoder)
encoded_str = json.dumps(numpy_array, cls=znjson.ZnEncoder)
# check that the correct encoder is used
assert arr.startswith('{"_type": "np.ndarray64"')
assert encoded_str.startswith('{"_type": "np.ndarray64"')


def test_decode(numpy_array):
encoded_str = json.dumps(numpy_array, cls=znjson.ZnEncoder)
np.testing.assert_array_equal(
numpy_array, json.loads(encoded_str, cls=znjson.ZnDecoder)
)


def test_decode_latin1(numpy_array):
znjson.deregister(znjson.converter.NumpyConverter)
encoded_str = json.dumps(numpy_array, cls=znjson.ZnEncoder)
# check that the correct encoder is used
assert encoded_str.startswith('{"_type": "np.ndarray"')
znjson.register(znjson.converter.NumpyConverter)
np.testing.assert_array_equal(
numpy_array, json.loads(encoded_str, cls=znjson.ZnDecoder)
)
2 changes: 0 additions & 2 deletions tests/converter/test_pandas_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import znjson

znjson.register(znjson.converter.PandasConverter)


@pytest.fixture
def pandas_dataframe():
Expand Down
2 changes: 0 additions & 2 deletions tests/converter/test_small_numpy_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import znjson

znjson.register(znjson.converter.SmallNumpyConverter)


@pytest.fixture
def numpy_array():
Expand Down
48 changes: 48 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest

import znjson.converter


@pytest.fixture(autouse=True)
def run_before_and_after_tests():
"""Fixture to execute before and after a test is run"""
znjson.deregister(znjson.config.ACTIVE_CONVERTER)
yield # this is where the testing happens
znjson.register()


@pytest.mark.parametrize(
"converters",
(
[],
[znjson.converter.PathlibConverter],
[znjson.converter.PathlibConverter, znjson.converter.PathlibConverter],
[znjson.converter.PathlibConverter, znjson.converter.ClassConverter],
),
)
def test_register(converters):
znjson.register(converters)
assert set(znjson.config.ACTIVE_CONVERTER) == set(converters)


def test_register_PathlibConverter():
znjson.register(znjson.converter.PathlibConverter)
assert znjson.config.ACTIVE_CONVERTER == [znjson.converter.PathlibConverter]


def test_deregister_single():
znjson.register([znjson.converter.PathlibConverter, znjson.converter.ClassConverter])

znjson.deregister(znjson.converter.PathlibConverter)

assert znjson.config.ACTIVE_CONVERTER == [znjson.converter.ClassConverter]


def test_deregister_multiple():
znjson.register([znjson.converter.PathlibConverter, znjson.converter.ClassConverter])

znjson.deregister(
[znjson.converter.PathlibConverter, znjson.converter.ClassConverter]
)

assert znjson.config.ACTIVE_CONVERTER == []
2 changes: 0 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import znjson

znjson.register(znjson.PathlibConverter)


@pytest.fixture
def simple_dict():
Expand Down
14 changes: 8 additions & 6 deletions znjson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .base import ConverterBase
from .config import config, register
from .converter import *
from .main import ZnDecoder, ZnEncoder
from znjson.base import ConverterBase
from znjson.config import config, deregister, register
from znjson.converter import *
from znjson.main import ZnDecoder, ZnEncoder

__all__ = ["ConverterBase", "ZnDecoder", "ZnEncoder", "register", "config"]
__all__ = ["ConverterBase", "ZnDecoder", "ZnEncoder", "register", "deregister", "config"]

__version__ = "0.1.1"
__version__ = "0.1.0"

register()
41 changes: 34 additions & 7 deletions znjson/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass, field
from typing import List, Tuple, Type, Union

from .base import ConverterBase
from znjson.base import ConverterBase


@dataclass()
Expand All @@ -10,25 +10,52 @@ class Config:

def sort(self):
"""Sort the ACTIVE_CONVERTER by their order"""
sort = sorted([x() for x in self.ACTIVE_CONVERTER])
active_converters = set(self.ACTIVE_CONVERTER)
sort = sorted([x() for x in active_converters])
self.ACTIVE_CONVERTER = [type(x) for x in sort]


config = Config()


def register(
obj: Union[List[Type[ConverterBase]], Tuple[Type[ConverterBase]], Type[ConverterBase]]
obj: Union[
List[Type[ConverterBase]], Tuple[Type[ConverterBase]], Type[ConverterBase]
] = None,
):
"""register converters to be used with zn.En/DeCoder
Attributes
----------
obj:
If None, all available converters will be registered
Updated the znconf.config which is used in the main converters
"""
if isinstance(obj, list):
config.ACTIVE_CONVERTER += obj
elif isinstance(obj, tuple):
if obj is None:
from znjson import converter

[register(getattr(converter, name)) for name in converter.__all__]
config.sort()
return

if isinstance(obj, (list, tuple)):
config.ACTIVE_CONVERTER += obj
else:
config.ACTIVE_CONVERTER.append(obj)
config.ACTIVE_CONVERTER += [obj]

config.sort()


def deregister(
obj: Union[
List[Type[ConverterBase]], Tuple[Type[ConverterBase]], Type[ConverterBase]
],
):
"""remove the given zn.En/DeCoder from the config"""

if isinstance(obj, (list, tuple)):
[deregister(x) for x in obj]
else:
config.ACTIVE_CONVERTER = [x for x in config.ACTIVE_CONVERTER if x is not obj]
config.sort()
8 changes: 4 additions & 4 deletions znjson/converter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .class_converter import ClassConverter
from .pathlib_converter import PathlibConverter
from znjson.converter.class_converter import ClassConverter
from znjson.converter.pathlib_converter import PathlibConverter

__all__ = ["PathlibConverter", "ClassConverter"]

Expand All @@ -13,8 +13,8 @@
pass

try:
from .pandas_converter import PandasConverter
from znjson.converter.pandas_converter import PandasConverter

__all__.append("PandasConverter")
__all__ += ["PandasConverter"]
except ModuleNotFoundError:
pass
5 changes: 4 additions & 1 deletion znjson/converter/class_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def _decode(self, value):
return pickle.load(f)

def __eq__(self, other):
return self._import(other.__module__, other.__class__.__name__) is not False
try:
return self._import(other.__module__, other.__class__.__name__) is not False
except AttributeError:
return False

@staticmethod
def _import(module, name):
Expand Down

0 comments on commit dcba60a

Please sign in to comment.