Skip to content

Commit ac076b3

Browse files
committed
Dev commit: SQLite element
1 parent a6a9cb8 commit ac076b3

File tree

6 files changed

+110
-29
lines changed

6 files changed

+110
-29
lines changed

TODOS

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
General:
22

33
Documentation:
4-
When an element returns None, subsequent
5-
elements are still triggered
4+
5+
Terms:
6+
Configuration Data: Parameters passed over the GUI by the user
7+
Input Data: Data which is forwarded from other elements
8+
9+
General behaviour:
10+
When an element returns None, subsequent
11+
elements are still triggered
612

713
Exceptions:
8-
Elements should throw an exception when configuration
9-
data is missing or cannot be processed
1014

11-
Elements should try to handle all possible exceptions internally
12-
and only throw unhandled exceptions
15+
#1
16+
Elements should throw an exception when configuration
17+
data is missing or cannot be processed
18+
19+
#2
20+
Elements should try to handle all possible exceptions internally
21+
and only throw critical exceptions.
22+
23+
#3
24+
If appropriate, the result of a operation (if successfull or not) should
25+
be forwarded to subsequent elements in order to react to
26+
a possible unsuccesfull operation. When it was not possible to process the input data, return the PythonicError with related information.
27+
Combine this with logging.warning (preferred) or logging.error.
1328

14-
The result of a operation (if successfull or not) should
15-
be forwarded to subsequent elements in order to react to
16-
a possible unsuccesfull operation
29+
#4
30+
Avoid to use logging.error or logging.warning without the other mechanisms as those methods will only write to the log.
1731

18-
When input data is required to perform some task and it
19-
is missing, elements should pass this information to subsequent elements
20-
and not throw an exception.d
2132

33+
Check exception rules
2234

23-
CCXT Method: Remove large if-else areas for parsing config
35+
CCXT Method: Try to optimize large if-else areas for parsing config
2436

2537
Python: Add type hints
2638

2739
Check if the 'inline'-keyword can be applied to some loops
2840
(page 146 C++ Der Programmierer)
2941

42+
Telegram: Return error element when chat id is removed?
3043

3144
Elementeditor:
3245
Add the possibility to pass arbitrary number of keyword arguments or

examples/generic_pipe_0e7b8360.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import time, queue
2+
from random import randrange
3+
try:
4+
from element_types import Record, Function, ProcCMD, GuiCMD
5+
except ImportError:
6+
from Pythonic.element_types import Record, Function, ProcCMD, GuiCMD
7+
8+
class Element(Function):
9+
10+
def __init__(self, id, config, inputData, return_queue, cmd_queue):
11+
super().__init__(id, config, inputData, return_queue, cmd_queue)
12+
13+
14+
def execute(self):
15+
16+
17+
#####################################
18+
# #
19+
# REFERENCE IMPLEMENTATION #
20+
# #
21+
#####################################
22+
23+
24+
# list all tables
25+
# SELECT name FROM sqlite_master WHERE type='table'
26+
27+
# create table of not exist
28+
# CREATE TABLE IF NOT EXISTS my_table (timestamp INTEGER PRIMARY KEY NOT NULL, value UNSIGNED BIG INT);
29+
30+
# insert into table
31+
# INSERT INTO my_table VALUES (?, ?)
32+
#
33+
# epoch in seconds: int(timer.time())
34+
# random int: randrange(999)
35+
36+
# Read from table several rows
37+
# SELECT * FROM my_table WHERE timestamp BETWEEN {} AND {}'.format( int(time.time())-12000, int(time.time()) )
38+
39+
40+
output = 'SELECT * FROM my_table WHERE timestamp BETWEEN {} AND {}'.format(int(time.time())-12000, int(time.time()))
41+
42+
43+
44+
#########################################
45+
# #
46+
# The execution exits immediately #
47+
# after providing output data #
48+
# #
49+
#########################################
50+
51+
recordDone = Record(output, 'Sending value of cnt: {}'.format(output))
52+
self.return_queue.put(recordDone)

src/Pythonic/element_types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ def __getstate__(self):
5353
#logging.debug('__getstate__() called Record')
5454
return(self.data, self.message)
5555

56+
57+
class PythonicError:
58+
59+
def __init__(self, msg : str) -> None:
60+
self.msg = msg
61+
62+
def __str__(self) -> str:
63+
return 'Error: {}'.format(self.msg)
64+
5665
class GuiCMD:
5766

5867
def __init__(self, text):

src/Pythonic/executables/sqlite.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import time, queue, sqlite3
1+
import time, queue, sqlite3, logging
22
try:
3-
from element_types import Record, Function, ProcCMD, GuiCMD
3+
from element_types import Record, Function, ProcCMD, GuiCMD, PythonicError
44
except ImportError:
55
from Pythonic.element_types import Record, Function, ProcCMD, GuiCMD
66

@@ -12,7 +12,6 @@ def __init__(self, id, config, inputData, return_queue, cmd_queue):
1212

1313
def execute(self):
1414

15-
1615
#####################################
1716
# #
1817
# REFERENCE IMPLEMENTATION #
@@ -28,29 +27,36 @@ def execute(self):
2827
return
2928

3029
filename = None
30+
output = None
3131

3232
for attrs in specificConfig:
3333
if attrs['Name'] == 'Filename':
3434
filename = attrs['Data']
3535

36+
if self.inputData is None:
37+
recordDone = Record(None, message='No input provided')
38+
self.return_queue.put(recordDone)
39+
return
40+
3641
con = sqlite3.connect(filename)
37-
# TODO
42+
3843
if not con:
3944
raise Exception('Can not connect to database')
4045

41-
if self.inputData is None:
42-
output = 0
43-
else:
44-
output = self.inputData + 1
46+
cur = con.cursor()
4547

48+
try:
49+
cur.execute(self.inputData)
50+
except Exception as e:
51+
logging.warning(e)
52+
recordDone = Record(PythonicError(e), 'Query failed')
53+
self.return_queue.put(recordDone)
54+
con.close()
55+
return
4656

47-
48-
#########################################
49-
# #
50-
# The execution exits immediately #
51-
# after providing output data #
52-
# #
53-
#########################################
57+
output = cur.fetchall()
5458

55-
recordDone = Record(output, 'Sending value of cnt: {}'.format(output))
59+
con.commit()
60+
con.close()
61+
recordDone = Record(output, 'Query successful')
5662
self.return_queue.put(recordDone)

src/Pythonic/executables/telegram.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def message(update, context):
9696
try:
9797
dispatcher.bot.send_message(chat_id=chat_id, text=str(cmd.data))
9898
except Exception as e:
99+
# BAUSTELLE # Return Error Element ?
99100
logging.error(e)
100101
chat_ids.discard(chat_id)
101102
logging.warning('ChatId removed')
1.9 KB
Loading

0 commit comments

Comments
 (0)