jsonata-python 0.3.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jsonata/cli/__main__.py CHANGED
@@ -34,7 +34,7 @@ def get_options(argv: Optional[list[str]] = None) -> argparse.ArgumentParser:
34
34
  """
35
35
  parser = argparse.ArgumentParser(prog="jsonata.cli", description="Pure Python JSONata CLI")
36
36
  parser.add_argument(
37
- "-v", "--version", action='version', version='%(prog)s 0.3.0')
37
+ "-v", "--version", action='version', version='%(prog)s 0.5.0')
38
38
 
39
39
  parser.add_argument(
40
40
  "-e", "--expression", metavar="<file>",
jsonata/functions.py CHANGED
@@ -755,22 +755,82 @@ class Functions:
755
755
  if isinstance(pattern, str):
756
756
  if not pattern:
757
757
  raise jexception.JException("Second argument of replace function cannot be an empty string", 0)
758
- if limit is None:
759
- if isinstance(pattern, str):
760
- return re.sub(pattern, str(replacement), string)
761
- else:
762
- return Functions.safe_replace_all(string, pattern, replacement)
763
- else:
758
+
759
+ if limit is not None and limit < 0:
760
+ raise jexception.JException("Fourth argument of replace function must evaluate to a positive number", 0)
761
+
762
+ def string_replacer(match):
763
+ result = ''
764
+ position = 0
765
+ repl = str(replacement)
766
+ while position < len(repl):
767
+ index = repl.find('$', position)
768
+ if index == -1:
769
+ result += repl[position:]
770
+ break
771
+ result += repl[position:index]
772
+ position = index + 1
773
+ if position < len(repl):
774
+ dollar_val = repl[position]
775
+ if dollar_val == '$':
776
+ result += '$'
777
+ position += 1
778
+ elif dollar_val == '0':
779
+ result += match.group(0)
780
+ position += 1
781
+ else:
782
+ max_digits = len(str(len(match.groups())))
783
+ group_num = repl[position:position+max_digits]
784
+ if group_num.isdigit():
785
+ group_index = int(group_num)
786
+ if 0 < group_index <= len(match.groups()):
787
+ result += match.group(group_index) or ''
788
+ position += len(group_num)
789
+ else:
790
+ result += '$'
791
+ else:
792
+ result += '$'
793
+ else:
794
+ result += '$'
795
+ return result
764
796
 
765
- if limit < 0:
766
- raise jexception.JException("Fourth argument of replace function must evaluate to a positive number", 0)
797
+ if callable(replacement):
798
+ replacer = lambda m: replacement(m.groupdict())
799
+ elif isinstance(replacement, str):
800
+ replacer = string_replacer
801
+ else:
802
+ replacer = lambda m: str(replacement)
767
803
 
768
- for i in range(0, limit):
769
- if isinstance(pattern, str):
770
- string = re.sub(pattern, str(replacement), string, 1)
771
- else:
772
- string = Functions.safe_replace_first(string, pattern, str(replacement))
773
- return string
804
+ if isinstance(pattern, str):
805
+ # Use string methods for literal string patterns
806
+ result = ''
807
+ position = 0
808
+ count = 0
809
+ while True:
810
+ if limit is not None and count >= limit:
811
+ result += string[position:]
812
+ break
813
+ index = string.find(pattern, position)
814
+ if index == -1:
815
+ result += string[position:]
816
+ break
817
+ result += string[position:index]
818
+ match = re.match(re.escape(pattern), string[index:])
819
+ result += replacer(match)
820
+ position = index + len(pattern)
821
+ count += 1
822
+ return result
823
+ else:
824
+ # Use regex for pattern objects
825
+ if limit is None:
826
+ return Functions.safe_replace_all(string, pattern, replacement)
827
+ else:
828
+ count = 0
829
+ result = string
830
+ while count < limit:
831
+ result = Functions.safe_replace_first(result, pattern, str(replacement))
832
+ count += 1
833
+ return result
774
834
 
775
835
  #
776
836
  # Base64 encode a string
@@ -1510,8 +1570,11 @@ class Functions:
1510
1570
  from jsonata import jsonata
1511
1571
  if isinstance(func, jsonata.Jsonata.JFunction):
1512
1572
  return func.signature.get_min_number_of_args()
1573
+ elif isinstance(func, jsonata.Jsonata.JLambda):
1574
+ from inspect import signature
1575
+
1576
+ return len(signature(func.function).parameters)
1513
1577
  else:
1514
- # Lambda
1515
1578
  return len(func.arguments)
1516
1579
 
1517
1580
  #
jsonata/parser.py CHANGED
@@ -1180,6 +1180,7 @@ class Parser:
1180
1180
  result.position = expr.position
1181
1181
  result.lhs = self.process_ast(expr.lhs)
1182
1182
  result.rhs = self.process_ast(expr.rhs)
1183
+ result.keep_array = result.lhs.keep_array or result.rhs.keep_array
1183
1184
  else:
1184
1185
  result = Parser.Infix(self, None)
1185
1186
  result.type = expr.type
jsonata/signature.py CHANGED
@@ -307,6 +307,7 @@ class Signature:
307
307
  validated_args.append(arg)
308
308
  arg_index += 1
309
309
  else:
310
+ arg = args[arg_index] if arg_index < len(args) else None
310
311
  validated_args.append(arg)
311
312
  arg_index += 1
312
313
  return validated_args
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: jsonata-python
3
- Version: 0.3.0
3
+ Version: 0.5.0
4
4
  Summary: Pure Python implementation of JSONata
5
5
  Project-URL: Homepage, https://github.com/rayokota/jsonata-python
6
6
  Project-URL: Bug Reports, https://github.com/rayokota/jsonata-python/issues
@@ -1,17 +1,17 @@
1
1
  jsonata/__init__.py,sha256=4r8USHj4SoBy_TD8dLxt9HJIgpLfXApFrZBipi4rgr0,388
2
2
  jsonata/constants.py,sha256=WtdH_l_s5KD-SiOJ4GR2az7WpVgKB2HguXUnyfy4tvs,3017
3
3
  jsonata/datetimeutils.py,sha256=IzU6y-vwyhwobHyw9rsImBye-RfWDV7K8Vk0Xs-yiG0,48567
4
- jsonata/functions.py,sha256=VAHFQtXQI7tY-EVwIrku95jKniUimTTKY6-exBTQo1k,73260
4
+ jsonata/functions.py,sha256=FryvSvotfvoUvLy0Kem1RvHudCrTJL-YK0d23xDq6l8,75743
5
5
  jsonata/jexception.py,sha256=6Jz7WMsIiNlQ7-1Hq8RKiE2HxcHq2PDekw0qsSe3lqo,12885
6
6
  jsonata/jsonata.py,sha256=jZIxVrNcZI3aBvE0PURZ4-cxpaK9JiJVH5Q_cllCXS0,84247
7
- jsonata/parser.py,sha256=OSxO9eNQy2WrDOPX_Obow487PWwngRN7bCMtDoK55AI,53794
8
- jsonata/signature.py,sha256=zTO9zmT7XzxA4GHYf2Rgquu419D7UZ-NYzIwiqqNvCM,20184
7
+ jsonata/parser.py,sha256=U8nNxvsDIy0wVTfNCs2DNN7RjNVm0SqfociD9cRcsWM,53877
8
+ jsonata/signature.py,sha256=pP6Y8XHUkFYqSsKwLSl0mX3Dz5TXJpTdXM-3TE2Lf48,20269
9
9
  jsonata/timebox.py,sha256=bnevNR_ONvKUiIZCJZEWsRiR0gCWTGOwn5RCY7dKqYc,2861
10
10
  jsonata/tokenizer.py,sha256=6WMK6M-XxmOwPtr7ZqWMTtaZPdGFhVMcGHxQlP97jdk,11856
11
11
  jsonata/utils.py,sha256=U13I49Ie3hEn3PKGR4361TPengDAkbyHmwipDFZJlXo,5192
12
12
  jsonata/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- jsonata/cli/__main__.py,sha256=pzzkZomVSkoDpiWg1f_0-TrC5uLHg1wu2aNYNPnT3zM,6971
14
- jsonata_python-0.3.0.dist-info/METADATA,sha256=29v4aOPgmo8ZKgPUy49Mq0kHf7ewahj1j9bYP6p8H1w,17297
15
- jsonata_python-0.3.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
16
- jsonata_python-0.3.0.dist-info/licenses/LICENSE,sha256=y16Ofl9KOYjhBjwULGDcLfdWBfTEZRXnduOspt-XbhQ,11325
17
- jsonata_python-0.3.0.dist-info/RECORD,,
13
+ jsonata/cli/__main__.py,sha256=fXmXJ5YcxEP6ih8rguW64HymAXorC1tjrbrZ9V9TKQU,6971
14
+ jsonata_python-0.5.0.dist-info/METADATA,sha256=eqwggupt-9XE2Oooomxad_mKAyWy-mRoO4_bUi0EPFo,17297
15
+ jsonata_python-0.5.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
16
+ jsonata_python-0.5.0.dist-info/licenses/LICENSE,sha256=y16Ofl9KOYjhBjwULGDcLfdWBfTEZRXnduOspt-XbhQ,11325
17
+ jsonata_python-0.5.0.dist-info/RECORD,,