Skip to content

Commit 517af5a

Browse files
committed
Added new function: normalize_key and change the default debug error message to False
1 parent 9a55858 commit 517af5a

File tree

2 files changed

+65
-34
lines changed

2 files changed

+65
-34
lines changed

README.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ pip install json_duplicate_keys
1919
```
2020

2121
## Basic Usage
22-
### loads(`Jstr`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=True)
22+
### normalize_key(`name`, `dupSign_start`="{{{", `dupSign_end`="}}}")
23+
_Normalize Key name_
24+
- `name`: key name
25+
- `dupSign_start`:
26+
- `dupSign_end`:
27+
```python
28+
import json_duplicate_keys as jdks
29+
30+
print(jdks.normalize_key("version{{{_2_}}}"))
31+
# OUTPUT: version
32+
```
33+
---
34+
35+
### loads(`Jstr`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
2336
_Deserialize a JSON format string to a class `JSON_DUPLICATE_KEYS`_
2437
- `Jstr`: a JSON format string
2538
- `dupSign_start`:
@@ -38,7 +51,7 @@ print(JDKSObject)
3851
```
3952
---
4053

41-
### load(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=True)
54+
### load(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
4255
_Deserialize a JSON format string from a file to a class `JSON_DUPLICATE_KEYS`_
4356
- `Jfilepath`: The path to the file containing the JSON format string
4457
- `dupSign_start`:
@@ -73,7 +86,7 @@ print(JDKSObject.getObject())
7386
```
7487
---
7588

76-
### JSON_DUPLICATE_KEYS.get(`name`, `separator`="||", `parse_index`="$", `_isDebug_`=True)
89+
### JSON_DUPLICATE_KEYS.get(`name`, `separator`="||", `parse_index`="$", `_isDebug_`=False)
7790
_Get value in the JSON object by `name`_
7891
- `name`: the key name of the JSON object. Supported flatten key name format
7992
- `separator`:
@@ -97,7 +110,7 @@ print(JDKSObject.get("snapshot||author"))
97110
```
98111
---
99112

100-
### JSON_DUPLICATE_KEYS.set(`name`, `value`, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=True)
113+
### JSON_DUPLICATE_KEYS.set(`name`, `value`, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
101114
_Set a new `name` and `value` for the JSON object_
102115
- `name`: new key name for the JSON object. Supported flat key name format
103116
- `value`: value for key `name`
@@ -161,7 +174,7 @@ print(JDKSObject.getObject())
161174
```
162175
---
163176

164-
### JSON_DUPLICATE_KEYS.update(`name`, `value`, `separator`="||", `parse_index`="$", `_isDebug_`=True)
177+
### JSON_DUPLICATE_KEYS.update(`name`, `value`, `separator`="||", `parse_index`="$", `_isDebug_`=False)
165178
_Update new `value` for existing `name` in the JSON object_
166179
- `name`: the key name of the JSON object. Supported flatten key name format
167180
- `value`: new value for key `name`
@@ -186,7 +199,7 @@ print(JDKSObject.getObject())
186199
```
187200
---
188201

189-
### JSON_DUPLICATE_KEYS.delete(`name`, `separator`="||", `parse_index`="$", `_isDebug_`=True)
202+
### JSON_DUPLICATE_KEYS.delete(`name`, `separator`="||", `parse_index`="$", `_isDebug_`=False)
190203
_Delete a key-value pair in a JSON object by key `name`_
191204
- `name`: the key name of the JSON object. Supported flatten key name format
192205
- `separator`:
@@ -211,7 +224,7 @@ print(JDKSObject.getObject())
211224
```
212225
---
213226

214-
### JSON_DUPLICATE_KEYS.dumps(`dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=True, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
227+
### JSON_DUPLICATE_KEYS.dumps(`dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=False, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
215228
_Serialize a JSON object to a JSON format string_
216229
- `dupSign_start`:
217230
- `dupSign_end`:
@@ -236,7 +249,7 @@ print(JDKSObject.dumps())
236249
```
237250
---
238251

239-
### JSON_DUPLICATE_KEYS.dump(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=True, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
252+
### JSON_DUPLICATE_KEYS.dump(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=False, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
240253
_Serialize a JSON object to a JSON format string and write to a file_
241254
- `Jfilepath`: the path to the file to save the JSON format string
242255
- `dupSign_start`:
@@ -266,7 +279,7 @@ print(JDKSObject_load.getObject())
266279
```
267280
---
268281

269-
### JSON_DUPLICATE_KEYS.flatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=True)
282+
### JSON_DUPLICATE_KEYS.flatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=False)
270283
_Flatten a JSON object to a single key-value pairs_
271284
- `separator`:
272285
- `parse_index`:
@@ -289,7 +302,7 @@ print(JDKSObject.getObject())
289302
```
290303
---
291304

292-
### JSON_DUPLICATE_KEYS.unflatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=True)
305+
### JSON_DUPLICATE_KEYS.unflatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=False)
293306
_Unflatten a flattened JSON object back to a JSON object_
294307
- `separator`:
295308
- `parse_index`:
@@ -310,4 +323,4 @@ JDKSObject.unflatten()
310323
print(JDKSObject.getObject())
311324
# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
312325
```
313-
---
326+
---

json_duplicate_keys/__init__.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
2+
# # # # # # # # # # # Normalize Key name # # # # # # # # # # #
3+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
4+
def normalize_key(name, dupSign_start="{{{", dupSign_end="}}}"):
5+
import re
6+
7+
# User input data type validation
8+
if type(dupSign_start) != str: dupSign_start = "{{{"
9+
if type(dupSign_end) != str: dupSign_end = "}}}"
10+
11+
if type(name) == str:
12+
return re.sub('{dupSign_start}_\d+_{dupSign_end}$'.format(dupSign_start=re.escape(dupSign_start), dupSign_end=re.escape(dupSign_end)), "", name)
13+
return name
14+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
15+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
16+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
17+
18+
119
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
220
# # # # # # # # # # # # # # # loads # # # # # # # # # # # # # #
321
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
4-
def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=True):
22+
def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
523
import json, re
624
from collections import OrderedDict
725

826
# User input data type validation
927
if type(dupSign_start) != str: dupSign_start = "{{{"
1028
if type(dupSign_end) != str: dupSign_end = "}}}"
1129
if type(ordered_dict) != bool: ordered_dict = False
12-
if type(_isDebug_) != bool: _isDebug_ = True
30+
if type(_isDebug_) != bool: _isDebug_ = False
1331

1432
if type(Jstr) in [str]:
1533
def __convert_Jloads_to_Jobj(Jloads, Jobj):
@@ -115,7 +133,7 @@ def __convert_Jloads_to_Jobj(Jloads, Jobj):
115133
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
116134
# # # # # # # # # # # # # # # load # # # # # # # # # # # # # #
117135
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
118-
def load(Jfilepath, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=True):
136+
def load(Jfilepath, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
119137
try:
120138
Jfile = open(Jfilepath)
121139
Jstr = Jfile.read()
@@ -153,15 +171,15 @@ def getObject(self):
153171
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
154172
# # # # # # # # # # # # # # # get # # # # # # # # # # # # # # #
155173
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
156-
def get(self, name, separator="||", parse_index="$", _isDebug_=True):
174+
def get(self, name, separator="||", parse_index="$", _isDebug_=False):
157175
import re
158176
from collections import OrderedDict
159177

160178
# User input data type validation
161179
if type(name) != str: name = str(name)
162180
if type(separator) != str: separator = "||"
163181
if type(parse_index) != str: parse_index = "$"
164-
if type(_isDebug_) != bool: _isDebug_ = True
182+
if type(_isDebug_) != bool: _isDebug_ = False
165183

166184
if type(self.getObject()) in [list, dict, OrderedDict]:
167185
try:
@@ -194,7 +212,7 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
194212
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
195213
# # # # # # # # # # # # # # # set # # # # # # # # # # # # # # #
196214
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
197-
def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=True):
215+
def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
198216
import re
199217
from collections import OrderedDict
200218

@@ -205,7 +223,7 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
205223
if type(dupSign_start) != str: dupSign_start = "{{{"
206224
if type(dupSign_end) != str: dupSign_end = "}}}"
207225
if type(ordered_dict) != bool: ordered_dict = False
208-
if type(_isDebug_) != bool: _isDebug_ = True
226+
if type(_isDebug_) != bool: _isDebug_ = False
209227

210228
if type(self.getObject()) in [list, dict, OrderedDict]:
211229
try:
@@ -237,10 +255,10 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
237255
=> Add new key ( self.__Jobj[name_split_lastKey] = value )
238256
"""
239257
# Add duplicate key
240-
if self.get(separator.join(name_split), separator=separator, parse_index=parse_index, _isDebug_=False) != "JSON_DUPLICATE_KEYS_ERROR":
258+
if self.get(separator.join(name_split), separator=separator, parse_index=parse_index) != "JSON_DUPLICATE_KEYS_ERROR":
241259
index = 2
242260
while True:
243-
if self.get(separator.join(name_split)+dupSign_start+"_"+str(index)+"_"+dupSign_end, separator=separator, parse_index=parse_index, _isDebug_=False) == "JSON_DUPLICATE_KEYS_ERROR":
261+
if self.get(separator.join(name_split)+dupSign_start+"_"+str(index)+"_"+dupSign_end, separator=separator, parse_index=parse_index) == "JSON_DUPLICATE_KEYS_ERROR":
244262
break
245263
index += 1
246264

@@ -255,8 +273,8 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
255273

256274
exec(exec_expression+"="+repr(value))
257275
# Add new key
258-
elif self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index, _isDebug_=False) != "JSON_DUPLICATE_KEYS_ERROR":
259-
if type(self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index, _isDebug_=False)) == list:
276+
elif self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index) != "JSON_DUPLICATE_KEYS_ERROR":
277+
if type(self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index)) == list:
260278
if name_split_lastKey == "":
261279
exec_expression = "self.getObject()"
262280

@@ -277,7 +295,7 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
277295
exec_expression += "["+repr(k)+"]"
278296

279297
exec(exec_expression+".append({"+repr(name_split_lastKey)+":"+repr(value)+"})")
280-
elif type(self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index, _isDebug_=False)) == dict:
298+
elif type(self.get(separator.join(name_split_first), separator=separator, parse_index=parse_index)) == dict:
281299
exec_expression = "self.getObject()"
282300

283301
for k in name_split_first:
@@ -314,14 +332,14 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
314332
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
315333
# # # # # # # # # # # # # # update # # # # # # # # # # # # # #
316334
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
317-
def update(self, name, value, separator="||", parse_index="$", _isDebug_=True):
335+
def update(self, name, value, separator="||", parse_index="$", _isDebug_=False):
318336
import re
319337

320338
# User input data type validation
321339
if type(name) != str: name = str(name)
322340
if type(separator) != str: separator = "||"
323341
if type(parse_index) != str: parse_index = "$"
324-
if type(_isDebug_) != bool: _isDebug_ = True
342+
if type(_isDebug_) != bool: _isDebug_ = False
325343

326344
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_ERROR":
327345
try:
@@ -344,14 +362,14 @@ def update(self, name, value, separator="||", parse_index="$", _isDebug_=True):
344362
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
345363
# # # # # # # # # # # # # # delete # # # # # # # # # # # # #
346364
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
347-
def delete(self, name, separator="||", parse_index="$", _isDebug_=True):
365+
def delete(self, name, separator="||", parse_index="$", _isDebug_=False):
348366
import re
349367

350368
# User input data type validation
351369
if type(name) != str: name = str(name)
352370
if type(separator) != str: separator = "||"
353371
if type(parse_index) != str: parse_index = "$"
354-
if type(_isDebug_) != bool: _isDebug_ = True
372+
if type(_isDebug_) != bool: _isDebug_ = False
355373

356374
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_ERROR":
357375
try:
@@ -374,14 +392,14 @@ def delete(self, name, separator="||", parse_index="$", _isDebug_=True):
374392
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
375393
# # # # # # # # # # # # # # dumps # # # # # # # # # # # # # #
376394
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
377-
def dumps(self, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
395+
def dumps(self, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
378396
import json, re
379397
from collections import OrderedDict
380398

381399
# User input data type validation
382400
if type(dupSign_start) != str: dupSign_start = "{{{"
383401
if type(dupSign_end) != str: dupSign_end = "}}}"
384-
if type(_isDebug_) != bool: _isDebug_ = True
402+
if type(_isDebug_) != bool: _isDebug_ = False
385403

386404
if type(self.getObject()) in [list, dict, OrderedDict]:
387405
dupSign_start_escape_regex = re.escape(json.dumps({dupSign_start:""})[2:-6])
@@ -400,7 +418,7 @@ def dumps(self, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys
400418
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
401419
# # # # # # # # # # # # # # dump # # # # # # # # # # # # # #
402420
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
403-
def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
421+
def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
404422
Jstr = self.dumps(dupSign_start=dupSign_start, dupSign_end=dupSign_end, _isDebug_=_isDebug_, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, cls=cls, indent=indent, separators=separators, default=default, sort_keys=sort_keys)
405423

406424
try:
@@ -417,14 +435,14 @@ def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True
417435
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
418436
# # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
419437
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
420-
def flatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=True):
438+
def flatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=False):
421439
from collections import OrderedDict
422440

423441
# User input data type validation
424442
if type(separator) != str: separator = "||"
425443
if type(parse_index) != str: parse_index = "$"
426444
if type(ordered_dict) != bool: ordered_dict = False
427-
if type(_isDebug_) != bool: _isDebug_ = True
445+
if type(_isDebug_) != bool: _isDebug_ = False
428446

429447
if type(self.getObject()) in [list, dict, OrderedDict]:
430448
if len(self.getObject()) > 0:
@@ -472,15 +490,15 @@ def __convert_Jobj_to_Jflat(Jobj, key=None):
472490
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
473491
# # # # # # # # # # # # # unflatten # # # # # # # # # # # # #
474492
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
475-
def unflatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=True):
493+
def unflatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=False):
476494
import re
477495
from collections import OrderedDict
478496

479497
# User input data type validation
480498
if type(separator) != str: separator = "||"
481499
if type(parse_index) != str: parse_index = "$"
482500
if type(ordered_dict) != bool: ordered_dict = False
483-
if type(_isDebug_) != bool: _isDebug_ = True
501+
if type(_isDebug_) != bool: _isDebug_ = False
484502

485503
if type(self.getObject()) in [dict, OrderedDict]:
486504
if len(self.getObject()) > 0:

0 commit comments

Comments
 (0)