evalidate 2.0.3__tar.gz → 2.0.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: evalidate
3
- Version: 2.0.3
3
+ Version: 2.0.4
4
4
  Summary: Validation and secure evaluation of untrusted python expressions
5
5
  Project-URL: Homepage, https://github.com/yaroslaff/evalidate
6
6
  Project-URL: Issues, https://github.com/yaroslaff/evalidate/issues
@@ -25,7 +25,7 @@ Originally it's developed for filtering complex data structures e.g.
25
25
 
26
26
  Find cheap smartphones available for sale:
27
27
  ```python
28
- category="smartphones" and price<300 and stock>0
28
+ category=="smartphones" and price<300 and stock>0
29
29
  ```
30
30
 
31
31
  But also, it can be used for other expressions, e.g. arithmetical, like
@@ -33,7 +33,7 @@ But also, it can be used for other expressions, e.g. arithmetical, like
33
33
  a+b-100
34
34
  ```
35
35
 
36
- Evalidate is fastest among all (known to me) secure eval pythong modules.
36
+ Evalidate is fastest among all (known to me) secure eval python modules.
37
37
 
38
38
  ## Install
39
39
 
@@ -121,6 +121,7 @@ def one():
121
121
 
122
122
  m = base_eval_model.clone()
123
123
  m.nodes.append('Call')
124
+ m.imported_functions["one"] = one
124
125
  Expr('one()', model=m).eval()
125
126
  ~~~
126
127
 
@@ -11,7 +11,7 @@ Originally it's developed for filtering complex data structures e.g.
11
11
 
12
12
  Find cheap smartphones available for sale:
13
13
  ```python
14
- category="smartphones" and price<300 and stock>0
14
+ category=="smartphones" and price<300 and stock>0
15
15
  ```
16
16
 
17
17
  But also, it can be used for other expressions, e.g. arithmetical, like
@@ -19,7 +19,7 @@ But also, it can be used for other expressions, e.g. arithmetical, like
19
19
  a+b-100
20
20
  ```
21
21
 
22
- Evalidate is fastest among all (known to me) secure eval pythong modules.
22
+ Evalidate is fastest among all (known to me) secure eval python modules.
23
23
 
24
24
  ## Install
25
25
 
@@ -107,6 +107,7 @@ def one():
107
107
 
108
108
  m = base_eval_model.clone()
109
109
  m.nodes.append('Call')
110
+ m.imported_functions["one"] = one
110
111
  Expr('one()', model=m).eval()
111
112
  ~~~
112
113
 
evalidate-2.0.4/ev.py ADDED
@@ -0,0 +1,60 @@
1
+ from evalidate import Expr, base_eval_model, ExecutionException
2
+ import sys
3
+
4
+ my_model = base_eval_model.clone()
5
+ my_model.nodes.extend(
6
+ [
7
+ "Call",
8
+ "Attributes",
9
+ "ListComp",
10
+ "DictComp",
11
+ "comprehension",
12
+ "Store",
13
+ "ForOfStatement",
14
+ "Subscript",
15
+ "GeneratorExp",
16
+ "For",
17
+ ]
18
+ )
19
+ my_model.allowed_functions.append("sum")
20
+ my_model.allowed_functions.append("len")
21
+
22
+ my_shelve = {
23
+ "height": 200,
24
+ "boxes": {
25
+ "box1": {"volume": 110},
26
+ "box2": {"volume": 90},
27
+ },
28
+ "column_width": [20, 480, 40],
29
+ }
30
+
31
+ box_volumes = [my_shelve["boxes"][box]["volume"] for box in my_shelve["boxes"]]
32
+ total_volume = sum(box_volumes)
33
+ print(total_volume)
34
+
35
+ # works fine
36
+ exp_string = "sum( my_shelve['column_width'])"
37
+ exp = Expr(exp_string, my_model)
38
+ print("code:", exp.code)
39
+ res = exp.eval({"my_shelve": my_shelve})
40
+ print(res)
41
+
42
+
43
+ # throws an Exception:
44
+ # evalidate.ExecutionException: name 'my_shelve' is not defined
45
+
46
+ # exp_string = "sum([my_shelve['boxes'][box]['volume'] for box in my_shelve['boxes'] ])"
47
+ # exp_string = "sum([my_shelve['boxes'][box]['volume'] for box in my_shelve['boxes'] ])"
48
+ exp_string = "len(my_shelve)"
49
+ exp = Expr(exp_string, my_model)
50
+
51
+ #print("basic eval")
52
+ #res = eval(exp.code, dict(), {"my_shelve": my_shelve})
53
+ #print("basic res:",res)
54
+ # sys.exit(0)
55
+ try:
56
+ res = exp.eval({"my_shelve": my_shelve})
57
+ except ExecutionException as e:
58
+ print(e)
59
+
60
+ # print(res)
@@ -6,7 +6,7 @@ import ast
6
6
  import dataclasses
7
7
  from typing import Callable
8
8
 
9
- __version__ = '2.0.3'
9
+ __version__ = '2.0.4'
10
10
 
11
11
 
12
12
  class EvalException(Exception):
@@ -124,9 +124,17 @@ class Expr():
124
124
  self.code = compile(self.node, filename or '<usercode>', 'eval')
125
125
 
126
126
  def eval(self, ctx=None):
127
-
127
+
128
+ if ctx:
129
+ global_ctx = {
130
+ **self.model.imported_functions,
131
+ **ctx
132
+ }
133
+ else:
134
+ global_ctx = self.model.imported_functions
135
+
128
136
  try:
129
- result = eval(self.code, self.model.imported_functions, ctx)
137
+ result = eval(self.code, global_ctx)
130
138
  except Exception as e:
131
139
  raise ExecutionException(e)
132
140
 
@@ -1,6 +1,6 @@
1
1
  [build-system]
2
2
  # requires = ["setuptools >= 40.6.0", "wheel"]
3
- requires = ["hatchling", "wheel"]
3
+ requires = ["hatchling>=1.27.0", "wheel"]
4
4
 
5
5
  # build-backend = "setuptools.build_meta"
6
6
  build-backend = "hatchling.build"
@@ -1,4 +1,3 @@
1
- import evalidate
2
1
  from dataclasses import dataclass
3
2
  from evalidate import Expr, EvalException, base_eval_model
4
3
 
@@ -20,9 +19,9 @@ mymodel = base_eval_model.clone()
20
19
  mymodel.nodes.extend(['Attribute', 'Call'])
21
20
  mymodel.attributes.append('get_weight')
22
21
 
23
- validated_expr = evalidate.Expr(sum_expr, model=mymodel)
22
+ validated_expr = Expr(sum_expr, model=mymodel)
24
23
 
25
- total_weight = eval(validated_expr.code, {"john": john, "jack": jack}, None)
24
+ total_weight = eval(validated_expr.code, passengers, None)
26
25
 
27
26
  print(total_weight)
28
27
 
@@ -16,7 +16,7 @@ passengers = {"john": john, "jack": jack}
16
16
 
17
17
  sum_expr = "john.get_weight() + jack.get_weight()"
18
18
 
19
- mymodel = evalidate.EvalModel()
19
+ mymodel =
20
20
 
21
21
  mymodel.nodes.extend(['Attribute', 'Call'])
22
22
  mymodel.attributes.append('get_weight')
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes