From e341f9e836b6a8bca64704acdeb93e1ef40e810f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20K=C3=A1cha?= <ph@cesnet.cz> Date: Wed, 30 Aug 2017 15:30:57 +0200 Subject: [PATCH] Refactored json_default, implemented to_json method to Idea classes --- lib_python/lib/idea-format/bench_idea.py | 1 - lib_python/lib/idea-format/idea/__init__.py | 4 ++-- lib_python/lib/idea-format/idea/base.py | 18 +++++++++------ lib_python/lib/idea-format/idea/lite.py | 25 +++++++++++---------- lib_python/lib/idea-format/idea/valid.py | 1 - lib_python/lib/idea-format/setup.py | 5 ++++- lib_python/lib/idea-format/test_idea.py | 5 ++--- 7 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib_python/lib/idea-format/bench_idea.py b/lib_python/lib/idea-format/bench_idea.py index 47e1608..39efcdb 100755 --- a/lib_python/lib/idea-format/bench_idea.py +++ b/lib_python/lib/idea-format/bench_idea.py @@ -8,7 +8,6 @@ from idea import lite from idea import valid import json from jsonschema import Draft4Validator -import typedcol import timeit raw_idea = { diff --git a/lib_python/lib/idea-format/idea/__init__.py b/lib_python/lib/idea-format/idea/__init__.py index 9e32ead..1e3d9a4 100644 --- a/lib_python/lib/idea-format/idea/__init__.py +++ b/lib_python/lib/idea-format/idea/__init__.py @@ -1,8 +1,8 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # -# Copyright (c) 2016, CESNET, z. s. p. o. +# Copyright (c) since 2016, CESNET, z. s. p. o. # Use of this source is governed by an ISC license, see LICENSE file. -__version__ = '0.1.7' +__version__ = '0.1.8' __author__ = 'Pavel Kácha <pavel.kacha@cesnet.cz>' diff --git a/lib_python/lib/idea-format/idea/base.py b/lib_python/lib/idea-format/idea/base.py index da5228f..72dad74 100644 --- a/lib_python/lib/idea-format/idea/base.py +++ b/lib_python/lib/idea-format/idea/base.py @@ -336,10 +336,14 @@ class IdeaBase(typedcols.TypedDict): allow_unknown = True typedef = {} -def json_default(o): - if isinstance(o, typedcols.TypedDict): - return o.data - elif isinstance(o, typedcols.TypedList): - return o.data - else: - raise ValueError(o) + @staticmethod + def json_default(o): + if isinstance(o, typedcols.TypedDict): + return o.data + elif isinstance(o, typedcols.TypedList): + return o.data + else: + raise ValueError(o) + + def to_json(self, *args, **kwargs): + return json.dumps(self, default=self.json_default, *args, **kwargs) diff --git a/lib_python/lib/idea-format/idea/lite.py b/lib_python/lib/idea-format/idea/lite.py index f5c7e4a..6a38f61 100644 --- a/lib_python/lib/idea-format/idea/lite.py +++ b/lib_python/lib/idea-format/idea/lite.py @@ -232,15 +232,16 @@ class Idea(base.IdeaBase): typedcols.typed_list("AttachList", AttachDict), typedcols.typed_list("NodeList", NodeDict)) -def json_default(o): - if isinstance(o, (ipranges.IPBase, uuid.UUID)): - return str(o) - elif isinstance(o, datetime.datetime): - return o.isoformat() + "Z" - elif isinstance(o, datetime.timedelta): - hours = o.seconds // 3600 - mins = (o.seconds % 3600) // 60 - secs = o.seconds % 60 - return "%s%02i:%02i:%02i%s" % (("%iD" % o.days) if o.days else "", hours, mins, secs, (".%i" % o.microseconds) if o.microseconds else "") - else: - return base.json_default(o) + @staticmethod + def json_default(o): + if isinstance(o, (ipranges.IPBase, uuid.UUID)): + return str(o) + elif isinstance(o, datetime.datetime): + return o.isoformat() + "Z" + elif isinstance(o, datetime.timedelta): + hours = o.seconds // 3600 + mins = (o.seconds % 3600) // 60 + secs = o.seconds % 60 + return "%s%02i:%02i:%02i%s" % (("%iD" % o.days) if o.days else "", hours, mins, secs, (".%i" % o.microseconds) if o.microseconds else "") + else: + return base.IdeaBase.json_default(o) diff --git a/lib_python/lib/idea-format/idea/valid.py b/lib_python/lib/idea-format/idea/valid.py index dc4c27b..59a87c6 100644 --- a/lib_python/lib/idea-format/idea/valid.py +++ b/lib_python/lib/idea-format/idea/valid.py @@ -11,7 +11,6 @@ import struct import socket from .base import unicode -from .base import json_default def Version(s): diff --git a/lib_python/lib/idea-format/setup.py b/lib_python/lib/idea-format/setup.py index 9f22c72..8f9d8b8 100644 --- a/lib_python/lib/idea-format/setup.py +++ b/lib_python/lib/idea-format/setup.py @@ -17,6 +17,8 @@ from setuptools import setup, find_packages from codecs import open from os import path +import idea + here = path.abspath(path.dirname(__file__)) # Get the long description from the README file @@ -25,7 +27,8 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f: setup( name = 'idea-format', - version = '0.1.7', + #version = '0.1.8', + version = idea.__version__, description = 'Python library for working with IDEA messages.', long_description = long_description, classifiers = [ diff --git a/lib_python/lib/idea-format/test_idea.py b/lib_python/lib/idea-format/test_idea.py index 14056ce..e91ba5e 100755 --- a/lib_python/lib/idea-format/test_idea.py +++ b/lib_python/lib/idea-format/test_idea.py @@ -4,7 +4,6 @@ # Copyright (c) 2016, CESNET, z. s. p. o. # Use of this source is governed by an ISC license, see LICENSE file. -import typedcol import unittest import json import difflib @@ -80,13 +79,13 @@ class TestIdea(unittest.TestCase): def testLiteIdea(self): idea = lite.Idea(raw_idea) orig = json.dumps(raw_idea, indent=4, sort_keys=True) - new = json.dumps(idea, indent=4, sort_keys=True, default=lite.json_default) + new = idea.to_json(indent=4, sort_keys=True) self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))])) def testValidIdea(self): idea = valid.Idea(raw_idea) orig = json.dumps(raw_idea, indent=4, sort_keys=True) - new = json.dumps(idea, indent=4, sort_keys=True, default=valid.json_default) + new = idea.to_json(indent=4, sort_keys=True) self.assertEqual(orig, new, "\n".join([l for l in difflib.context_diff(orig.split("\n"), new.split("\n"))])) -- GitLab