Skip to content

Commit 98857b3

Browse files
committed
support functions with no parameters or parameters on several lines
1 parent a63e8e8 commit 98857b3

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ Changelog
22
=========
33

44

5+
2021-04-25a : v0.9.3 'Hello, World!'
6+
------------------------------------
7+
8+
* support functions with no parameters or parameters on several lines
9+
510
2021-04-20a : v0.9.2 'Give PyPy a chance!'
611
------------------------------------------
712

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Features
1818

1919
* Support of sql-embedded Python functions
2020

21-
* Easy to distribute : 1 Python source file, 2.7/3.4+ compatible
21+
* Easy to distribute : 1 Python source file, Python and PyPy3 compatible
2222

2323
* Easy to start : just launch sqlite_bro
2424

sqlite_bro/sqlite_bro.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class App:
3232
"""the GUI graphic application"""
3333
def __init__(self):
3434
"""create a tkk graphic interface with a main window tk_win"""
35-
self.__version__ = '0.9.2'
36-
self._title = "of 2021-04-20a : 'Give PyPy a chance!'"
35+
self.__version__ = '0.9.3'
36+
self._title = "of 2021-04-25a : 'Hello, World!'"
3737
self.conn = None # Baresql database object
3838
self.database_file = ""
3939
self.tk_win = Tk()
@@ -1388,14 +1388,15 @@ def execute(self, sql, env=None):
13881388

13891389
def createpydef(self, sql):
13901390
"""generates and register a pydef instruction"""
1391+
import re
13911392
instruction = sql.strip('; \t\n\r')
13921393
# create Python function in Python
13931394
exec(instruction[2:], globals(), locals())
13941395
# add Python function in SQLite
1395-
firstline = (instruction[5:].splitlines()[0]).lstrip()
1396-
firstline = firstline.replace(" ", "") + "("
1397-
instr_name = firstline.split("(", 1)[0].strip()
1398-
instr_parms = firstline.count(',')+1
1396+
instr_header=re.findall('\w+',
1397+
instruction[:(instruction+')').find(')')])
1398+
instr_name = instr_header[1]
1399+
instr_parms = len(instr_header)-2
13991400
instr_add = (("self.conn.create_function('%s', %s, %s)" % (
14001401
instr_name, instr_parms, instr_name)))
14011402
exec(instr_add, globals(), locals())
@@ -1580,6 +1581,9 @@ def _main():
15801581
INSERT INTO item select "A","Merced",1250 union all select "W","Wheel",9 ;
15811582
INSERT INTO part select ItemNo,"W","needed",Kg/250 from item where Kg>250;
15821583
\n-- to CREATE a Python embedded function (enclose them by "py" and ";") :
1584+
pydef py_hello():
1585+
"hello world"
1586+
return ("Hello, World !");
15831587
pydef py_sin(s):
15841588
"sinus function : example loading module, handling input/output as strings"
15851589
import math as py_math
@@ -1588,12 +1592,13 @@ def _main():
15881592
"fibonacci : example with function call (may only be internal) "
15891593
fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
15901594
return("%s" % fib(n*1));
1591-
pydef py_power(x,y):
1595+
pydef py_power(x,
1596+
y):
15921597
"power function : example loading module, handling input/output as strings"
15931598
import math as py_math
15941599
return ("%s" % ((x*1) ** (y*1)) );
15951600
\n-- to USE a python embedded function and nesting of embedded functions:
1596-
select py_sin(1) as sinus, py_power(2, 1*py_fib(6)) as power, sqlite_version();
1601+
select py_hello(), py_sin(1) as sinus, py_power(2, 1*py_fib(6)) as power, sqlite_version();
15971602
\n-- to EXPORT :
15981603
-- a TABLE, select TABLE, then click on icon 'SQL->CSV'
15991604
-- a QUERY RESULT, select the SCRIPT text, then click on icon '???->CSV',

0 commit comments

Comments
 (0)