Skip to content

Commit 2d75485

Browse files
committed
Add possibility for few SQL variables in Python file
Python file reading tests became slower due to this. Now there is some extra checking of variables, that may be the reson. However, it seems that SQL file reading tests became slower as well even tough their part of code was not changed.
1 parent 3beebc3 commit 2d75485

6 files changed

Lines changed: 61 additions & 14 deletions

File tree

readsql/__main__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ def read_file(file_name, inplace=True):
3333
out.write(lines)
3434

3535

36-
def read_python_file(file_name, variable='query', inplace=True):
36+
def read_python_file(file_name, variables=None, inplace=True):
37+
variables = variables if variables else ['query']
38+
variables_regex = f"(?:{'|'.join(variables)})" if len(variables) > 1 else variables[0]
39+
3740
with open(file_name, 'r') as inp:
3841
lines = inp.read()
3942
subs = []
4043

4144
regex = [
4245
m
4346
for m in re.finditer(
44-
r'(?:\s*' + variable + r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
47+
r'(?:\s*' + variables_regex + r'\s*=\s*f?)(?:"{1,3}|\'{1,3})([^"]*)(:?"|\')',
4548
lines,
4649
)
4750
]

readsql/parse_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def parse_args():
1313
parser.add_argument(
1414
'-py',
1515
'--python_var',
16-
action='store',
17-
default='query',
16+
nargs='+',
17+
default=['query'],
1818
help='Look for certain variables in a Python file (default variable is query)',
1919
)
2020

tests/speed.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
func:test_read_python_file__var:(((), {'variable': 'sql'}))__took: 0.000690 sec
1+
func:test_read_python_file__var:(((), {'variable': ['sql']}))__took: 0.001723 sec
22

3-
func:test_read_file__var:(((), {}))__took: 0.002316 sec
3+
func:test_read_file__var:(((), {}))__took: 0.005423 sec
44

5-
func:test_read_python_file__var:(((), {}))__took: 0.000885 sec
5+
func:test_read_python_file__var:(((), {}))__took: 0.003344 sec
66

7-
func:test_double_select__var:(((), {}))__took: 0.000245 sec
7+
func:test_double_select__var:(((), {}))__took: 0.000642 sec
88

9-
func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000202 sec
9+
func:test_select_from_groub_by_where__var:(((), {}))__took: 0.000595 sec
1010

11-
func:test_is_not_null__var:(((), {}))__took: 0.000236 sec
11+
func:test_is_not_null__var:(((), {}))__took: 0.000583 sec
1212

13-
func:test_distinct__var:(((), {}))__took: 0.000196 sec
13+
func:test_distinct__var:(((), {}))__took: 0.001059 sec
1414

15-
func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000252 sec
15+
func:test_create_table_if_not_exists__var:(((), {}))__took: 0.000418 sec
16+
17+
func:test_read_python_file__var:(((), {'variable': ['sql', 'query_template', 'query']}))__took: 0.001387 sec
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def get_sql():
2+
limit = 6
3+
sql = f"SELEct speed from world where animal='dolphin' limit {limit}"
4+
return sql
5+
6+
7+
def get_query_template():
8+
limit = 6
9+
query_template = f"SELEct speed from world where animal='dolphin' group by family limit {limit}"
10+
return query_template
11+
12+
13+
def get_query():
14+
limit = 99
15+
query = f"SELEct speed from world where animal='dolphin' and name is not null group by family limit {limit}"
16+
return query
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def get_sql():
2+
limit = 6
3+
sql = f"SELECT speed FROM world WHERE animal='dolphin' LIMIT {limit}"
4+
return sql
5+
6+
7+
def get_query_template():
8+
limit = 6
9+
query_template = f"SELECT speed FROM world WHERE animal='dolphin' GROUP BY family LIMIT {limit}"
10+
return query_template
11+
12+
13+
def get_query():
14+
limit = 99
15+
query = f"SELECT speed FROM world WHERE animal='dolphin' and name IS NOT NULL GROUP BY family LIMIT {limit}"
16+
return query

tests/test_files.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ def test_read_python_file():
2929
def test_read_python_file_variable_wrap():
3030
@timing
3131
def test_read_python_file(variable):
32-
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variable_example.py', variable=variable, inplace=False)
32+
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variable_example.py', variables=variable, inplace=False)
3333

34-
example = test_read_python_file(variable='sql')
34+
example = test_read_python_file(variable=['sql'])
3535
with open(DIR + '/sql_in_python_variable_example_correct.py', 'r') as inp:
3636
assert inp.read() == example
37+
38+
39+
def test_read_python_file_variables_wrap():
40+
@timing
41+
def test_read_python_file(variable):
42+
return rsql.read_python_file(file_name=DIR + '/sql_in_python_variables_example.py', variables=variable, inplace=False)
43+
44+
example = test_read_python_file(variable=['sql', 'query_template', 'query'])
45+
with open(DIR + '/sql_in_python_variables_example_correct.py', 'r') as inp:
46+
assert inp.read() == example

0 commit comments

Comments
 (0)