Skip to content

Commit a7f21c9

Browse files
committed
Refactoring. Using black code style
1 parent d59f2bf commit a7f21c9

10 files changed

+154
-113
lines changed
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# pip install simpleeval
8-
from simpleeval import simple_eval
8+
from simpleeval import simple_eval, SimpleEval
99

1010

1111
my_functions = {
12-
'my_pos': lambda: {
13-
'x': 12,
14-
'y': 10
15-
},
16-
'map': map,
17-
'str': str,
12+
"my_pos": lambda: {"x": 12, "y": 10},
13+
"map": map,
14+
"str": str,
1815
}
1916

20-
print(simple_eval('my_pos()["x"] * my_pos()["y"]', functions=my_functions)) # 120
21-
print(simple_eval('"x".join(map(str, my_pos().values()))', functions=my_functions)) # 12x10
17+
print(simple_eval('my_pos()["x"] * my_pos()["y"]', functions=my_functions)) # 120
18+
print(
19+
simple_eval('"x".join(map(str, my_pos().values()))', functions=my_functions)
20+
)
21+
# 12x10
2222
print()
2323

2424
# OR:
25-
from simpleeval import SimpleEval
2625
my_eval = SimpleEval(functions=my_functions)
2726

28-
print(my_eval.eval('my_pos()["x"] * my_pos()["y"]')) # 120
27+
print(my_eval.eval('my_pos()["x"] * my_pos()["y"]')) # 120
2928
print(my_eval.eval('"x".join(map(str, my_pos().values()))')) # 12x10

simpleeval__examples__calc/get_value_deep_attribute.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# pip install simpleeval
@@ -18,6 +18,6 @@ class C:
1818

1919

2020
my_eval = simpleeval.SimpleEval()
21-
my_eval.names['foo'] = Foo
21+
my_eval.names["foo"] = Foo
2222

23-
print(my_eval.eval('foo.A.B.C.value * 10 + foo.A.value')) # 55
23+
print(my_eval.eval("foo.A.B.C.value * 10 + foo.A.value")) # 55
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7+
import requests
8+
79
# pip install simpleeval
810
from simpleeval import SimpleEval
911

1012

1113
def get_from_url(value):
12-
import requests
13-
rs = requests.get('https://httpbin.org/get', params={'value': value})
14-
return rs.json()['args']['value']
14+
rs = requests.get("https://httpbin.org/get", params={"value": value})
15+
return rs.json()["args"]["value"]
1516

1617

1718
my_eval = SimpleEval()
18-
my_eval.functions['get'] = get_from_url
19+
my_eval.functions["get"] = get_from_url
1920

20-
print(my_eval.eval("get('123') + get('45')")) # '12345'
21+
print(my_eval.eval("get('123') + get('45')")) # '12345'
2122
print(my_eval.eval("int(get('123')) + int(get('45'))")) # 168
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7+
import hashlib
8+
import math
9+
710
# pip install simpleeval
8-
from simpleeval import simple_eval
11+
from simpleeval import simple_eval, SimpleEval, EvalWithCompoundTypes
912

1013

11-
print(simple_eval("21 + 21")) # 42
12-
print(simple_eval("'21' + '21'")) # '2121'
14+
print(simple_eval("21 + 21")) # 42
15+
print(simple_eval("'21' + '21'")) # '2121'
1316
print(simple_eval("int('21' + '21')")) # 2121
1417
print()
1518

1619
print(simple_eval("2 + 2 * 2")) # 6
17-
print(simple_eval('10 ** 123')) # 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
20+
print(simple_eval("10 ** 123"))
21+
# 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
22+
1823
print(simple_eval("21 + 19 / 7 + (8 % 3) ** 9")) # 535.7142857142857
1924
print()
2025

2126
# Call methods
2227
print(simple_eval("'1,2,3,4'.split(',')")) # ['1', '2', '3', '4']
23-
print(simple_eval("'+'.join('1234')")) # 1+2+3+4
28+
print(simple_eval("'+'.join('1234')")) # 1+2+3+4
2429
print()
2530

26-
from simpleeval import EvalWithCompoundTypes
2731
print(EvalWithCompoundTypes().eval('list("Hello").count("l")')) # 2
28-
print(simple_eval('list("Hello").count("l")', functions={'list': list})) # 2
32+
print(simple_eval('list("Hello").count("l")', functions={"list": list})) # 2
2933
print()
3034

3135
# User functions
3236
print(simple_eval("square(11)", functions={"square": lambda x: x * x})) # 121
3337

34-
import math
35-
print(simple_eval("abs(sin(3) * cos(3))", functions={'sin': math.sin, 'cos': math.cos, 'abs': abs})) # 0.13970774909946293
38+
print(
39+
simple_eval(
40+
"abs(sin(3) * cos(3))", functions={"sin": math.sin, "cos": math.cos, "abs": abs}
41+
)
42+
)
43+
# 0.13970774909946293
3644

3745

3846
def my_md5(value):
39-
import hashlib
40-
return hashlib.md5(bytes(value, 'utf-8')).hexdigest()
47+
return hashlib.md5(bytes(value, "utf-8")).hexdigest()
48+
4149

50+
print(
51+
simple_eval("md5('Hello World!')", functions={"md5": my_md5})
52+
)
53+
# ed076287532e86365e841e92bfc50d8c
4254

43-
print(simple_eval("md5('Hello World!')", functions={'md5': my_md5})) # ed076287532e86365e841e92bfc50d8c
44-
print(simple_eval("list('1234')", functions={'list': list})) # ['1', '2', '3', '4']
55+
print(simple_eval("list('1234')", functions={"list": list})) # ['1', '2', '3', '4']
4556
print()
4657

4758
# Using SimpleEval class
48-
from simpleeval import SimpleEval
49-
5059
my_eval = SimpleEval()
51-
my_eval.names['a'] = 2
52-
my_eval.functions['square'] = lambda x: x * x
60+
my_eval.names["a"] = 2
61+
my_eval.functions["square"] = lambda x: x * x
5362

54-
print(my_eval.eval('1 + 1 * a')) # 3
55-
print(my_eval.eval('square(1 + 1 * a)')) # 9
63+
print(my_eval.eval("1 + 1 * a")) # 3
64+
print(my_eval.eval("square(1 + 1 * a)")) # 9
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# pip install simpleeval
88
from simpleeval import SimpleEval
99

1010

1111
my_functions = {
12-
'dict': dict,
13-
'list': list,
14-
'set': set,
15-
'tuple': tuple,
16-
'str': str,
17-
'sorted': sorted,
12+
"dict": dict,
13+
"list": list,
14+
"set": set,
15+
"tuple": tuple,
16+
"str": str,
17+
"sorted": sorted,
1818
}
1919

2020
my_eval = SimpleEval(functions=my_functions)
2121

22-
print(my_eval.eval('dict(a="1", b=2)["a"]')) # "1"
23-
print(my_eval.eval('list("123")[0]')) # "1"
22+
print(my_eval.eval('dict(a="1", b=2)["a"]')) # "1"
23+
print(my_eval.eval('list("123")[0]')) # "1"
2424
print(my_eval.eval('sorted(set("12213"))[0]')) # "1"
25-
print(my_eval.eval('tuple("123")[0]')) # "1"
26-
print(my_eval.eval('str(123)[0]')) # "1"
27-
print(my_eval.eval('sorted("43198")[0]')) # "1"
25+
print(my_eval.eval('tuple("123")[0]')) # "1"
26+
print(my_eval.eval("str(123)[0]")) # "1"
27+
print(my_eval.eval('sorted("43198")[0]')) # "1"
2828
print()
2929

30-
print(my_eval.eval('"1234567890"[:3]')) # 123
31-
print(my_eval.eval('list("1234567890")[:3]')) # ['1', '2', '3']
30+
print(my_eval.eval('"1234567890"[:3]')) # 123
31+
print(my_eval.eval('list("1234567890")[:3]')) # ['1', '2', '3']
3232
print(my_eval.eval('"+".join(list("1234567890")[:3])')) # 1+2+3

simpleeval__examples__calc/operators__user_custom__mad_evil.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://github.com/danthedeckie/simpleeval#operators
88

99

10-
# pip install simpleeval
11-
from simpleeval import SimpleEval
12-
1310
import ast
1411
import operator as op
1512

13+
# pip install simpleeval
14+
from simpleeval import SimpleEval
15+
1616

1717
expr = "((2 + 2 * 2) / 3) ** 10 - 24"
1818

simpleeval__examples__calc/operators__user_custom__only__add_and_sub.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://github.com/danthedeckie/simpleeval#operators
88

99

10-
# pip install simpleeval
11-
from simpleeval import simple_eval, SimpleEval
12-
1310
import ast
1411
import operator as op
1512

13+
# pip install simpleeval
14+
from simpleeval import simple_eval, SimpleEval
15+
1616

1717
SUPPORTED_OPERATORS = {
1818
ast.Add: op.add,
1919
ast.Sub: op.sub,
2020
}
2121

2222

23-
print(simple_eval("2 + 2 - 1", operators=SUPPORTED_OPERATORS)) # 3
23+
print(simple_eval("2 + 2 - 1", operators=SUPPORTED_OPERATORS))
24+
# 3
2425

2526
try:
26-
print(simple_eval("2 + 2 * 2", operators=SUPPORTED_OPERATORS)) # KeyError: <class '_ast.Mult'>
27+
print(
28+
simple_eval("2 + 2 * 2", operators=SUPPORTED_OPERATORS)
29+
)
30+
# KeyError: <class '_ast.Mult'>
2731
except Exception as e:
2832
print(repr(e))
2933

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7+
import hashlib
8+
79
# pip install simpleeval
810
from simpleeval import SimpleEval
911

10-
import hashlib
11-
1212

1313
def hashlib_func(value, algo_name):
14-
data = bytes(str(value), 'utf-8')
14+
data = bytes(str(value), "utf-8")
1515
return hashlib.new(algo_name, data).hexdigest()
1616

1717

@@ -20,21 +20,39 @@ def __init__(self):
2020
functions = dict()
2121

2222
for algo_name in hashlib.algorithms_guaranteed:
23-
functions[algo_name] = lambda value, algo_name=algo_name: hashlib_func(value, algo_name)
23+
functions[algo_name] = lambda value, algo_name=algo_name: hashlib_func(
24+
value, algo_name
25+
)
2426

2527
super().__init__(functions=functions)
2628

2729

28-
if __name__ == '__main__':
30+
if __name__ == "__main__":
2931
hashlib_eval = SimpleHashlibEval()
3032

31-
print(hashlib_eval.eval('md5("Hello World!")')) # ed076287532e86365e841e92bfc50d8c
32-
print(hashlib_eval.eval('sha1("Hello World!")')) # 2ef7bde608ce5404e97d5f042f95f89f1c232871
33-
print(hashlib_eval.eval('sha256("Hello World!")')) # 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
33+
print(hashlib_eval.eval('md5("Hello World!")'))
34+
# ed076287532e86365e841e92bfc50d8c
35+
print(
36+
hashlib_eval.eval('sha1("Hello World!")')
37+
)
38+
# 2ef7bde608ce5404e97d5f042f95f89f1c232871
39+
print(
40+
hashlib_eval.eval('sha256("Hello World!")')
41+
)
42+
# 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
3443
print()
3544

36-
print(hashlib_eval.eval('md5(md5("Hello World!"))')) # 153163e20c7dd03b131fe2bf21927e1e
37-
print(hashlib_eval.eval('md5(md5("Hello") + md5("World!"))')) # 72a6d987a2848481595dba5ea4426b6e
45+
print(
46+
hashlib_eval.eval('md5(md5("Hello World!"))')
47+
)
48+
# 153163e20c7dd03b131fe2bf21927e1e
49+
print(
50+
hashlib_eval.eval('md5(md5("Hello") + md5("World!"))')
51+
)
52+
# 72a6d987a2848481595dba5ea4426b6e
3853
print()
3954

40-
print(hashlib_eval.eval('md5("Hello World!" + "my_secret_salt")')) # 9ee8e345639a8f7d51853eb459abaa81
55+
print(
56+
hashlib_eval.eval('md5("Hello World!" + "my_secret_salt")')
57+
)
58+
# 9ee8e345639a8f7d51853eb459abaa81

0 commit comments

Comments
 (0)