Skip to content

Commit 314bb6a

Browse files
committed
Added json to enhanced preimports
1 parent 7bbec46 commit 314bb6a

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

docs/pages/enhancements.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ A context manager is also available:
8989

9090
-----
9191

92+
## `json`
93+
94+
`json`, while imported with Tinyscript, is enhanced with an additional convenience function, that is:
95+
96+
- `loadc`: this loads a JSON that has comments (starting with `#`) so that it does not fail when loading with `json` while annotated.
97+
98+
-----
99+
92100
## `logging`
93101

94102
`logging` is slightly enhanced with a few things:

src/tinyscript/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.30.10
1+
1.30.11

src/tinyscript/preimports/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'hashlib': "hash",
1919
'inspect': "inspectp",
2020
'itertools': "itools",
21+
'json': "jsonp",
2122
'logging': "log",
2223
'random': "rand",
2324
're': "regex",
@@ -35,7 +36,6 @@
3536
"configparser",
3637
"ctypes",
3738
"fileinput",
38-
"json",
3939
"os",
4040
"platform",
4141
"shlex",

src/tinyscript/preimports/jsonp.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Module for enhancing json preimport.
3+
4+
"""
5+
import json
6+
7+
8+
def loadc(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None,
9+
**kw):
10+
""" Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document with comments) to a
11+
Python object. """
12+
_b = lambda s: s.encode() if 'b' in fp.mode else s
13+
s = _b("\n").join(l.strip().split(_b("#"), 1)[0].strip() for l in fp.readlines())
14+
return json.loads(s, cls=cls, object_hook=object_hook, parse_float=parse_float, parse_int=parse_int,
15+
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
16+
json.loadc = loadc
17+

tests/test_preimports_json.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Preimports string assets' tests.
3+
4+
"""
5+
from tinyscript.preimports import json
6+
7+
from utils import *
8+
9+
10+
FNAME = ".test.json"
11+
TEST_JSON = """
12+
# test comment 1
13+
{
14+
"test": ["a", "b", "c"],
15+
"other": 1 # test comment 2
16+
}
17+
"""
18+
19+
20+
class TestPreimportsJson(TestCase):
21+
@classmethod
22+
def tearDownClass(cls):
23+
remove(FNAME)
24+
25+
def test_commented_json_loading(self):
26+
with open(FNAME, 'wt') as f:
27+
f.write(TEST_JSON)
28+
with open(FNAME) as f:
29+
self.assertIsNotNone(json.loadc(f))
30+
with open(FNAME, 'wb') as f:
31+
f.write(TEST_JSON.encode())
32+
with open(FNAME, 'rb') as f:
33+
self.assertIsNotNone(json.loadc(f))
34+

0 commit comments

Comments
 (0)