Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:11
created

coalib/output/JSONEncoder.py (15 issues)

1
import collections
2
import json
3
from datetime import datetime
4
5
from coalib.misc.Decorators import get_public_members
6
from coalib.settings.FunctionMetadata import FunctionMetadata
7
8
9
def create_json_encoder(**kwargs):
10
    class JSONEncoder(json.JSONEncoder):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable json does not seem to be defined.
Loading history...
11
12
        @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
13
        def _filter_params(cls, op, nop):
14
            params = set(op) | set(nop)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable op does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable nop does not seem to be defined.
Loading history...
15
            return {key: kwargs[key] for key in set(kwargs) & (params)}
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable params does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable kwargs does not seem to be defined.
Loading history...
16
17
        def default(self, obj):
18
            if hasattr(obj, "__json__"):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable obj does not seem to be defined.
Loading history...
19
                fdata = FunctionMetadata.from_function(obj.__json__)
20
                params = self._filter_params(
21
                    fdata.optional_params, fdata.non_optional_params)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable fdata does not seem to be defined.
Loading history...
22
                return obj.__json__(**params)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable params does not seem to be defined.
Loading history...
23
            elif isinstance(obj, collections.Iterable):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable collections does not seem to be defined.
Loading history...
24
                return list(obj)
25
            elif isinstance(obj, datetime):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable datetime does not seem to be defined.
Loading history...
26
                return obj.isoformat()
27
            elif hasattr(obj, "__getitem__") and hasattr(obj, "keys"):
28
                return dict(obj)
29
            elif hasattr(obj, "__dict__"):
30
                return {member: getattr(obj, member)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable member does not seem to be defined.
Loading history...
31
                        for member in get_public_members(obj)}
32
33
            return json.JSONEncoder.default(self, obj)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
34
    return JSONEncoder
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable JSONEncoder does not seem to be defined.
Loading history...
35