kisa-utils 0.42.6__py3-none-any.whl → 0.42.8__py3-none-any.whl

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.
kisa_utils/dates.py CHANGED
@@ -396,14 +396,17 @@ def endOfCurrentMonth() -> str:
396
396
  year, month, day = dateToday.split('-')
397
397
  return endOfMonth(year, month)
398
398
 
399
- def dateIsValid(date:str) -> bool:
399
+ def dateIsValid(date:str, dateFormat:str='YYYY-MM-DD') -> bool:
400
400
  '''
401
- check if a date is valid in the format 'YYYY-MM-DD'
401
+ check if a date is valid in the provided format
402
402
  Args:
403
403
  date(str): the date to check
404
+ dateFormat(str): the date format for which to check the date. common formats are;
405
+ - 'YYYY-MM-DD'
406
+ - 'YYYY-MM-DD HH:MM:SS'
404
407
  '''
405
408
  try:
406
- standardizedDate(date, 'YYYY-MM-DD')
409
+ standardizedDate(date, dateFormat)
407
410
  except:
408
411
  return False
409
412
 
kisa_utils/db.py CHANGED
@@ -222,6 +222,8 @@ class Api:
222
222
  self._path = path
223
223
 
224
224
  if path not in RAM_DB_PATHS:
225
+ path = storage.Path.getAbsolutePath(path)
226
+
225
227
  while path.endswith('/'): path = path[:-1]
226
228
  if not path.endswith(f'.{__EXT__}') and not os.path.isfile(path):
227
229
  path += f'.{__EXT__}'
@@ -642,12 +644,13 @@ class Api:
642
644
 
643
645
  #---------------------------------------------------------------------------
644
646
  @__checkContext
645
- def execute(self, cmd:str, cmdData:list=[]) -> dict[str,bool|str|int|sqlite3.Cursor] | Response:
647
+ def execute(self, cmd:str, cmdData:list=[], executingMultiple:bool=False) -> dict[str,bool|str|int|sqlite3.Cursor] | Response:
646
648
  '''
647
649
  attempt to execute arbitrary SQLite3 commands
648
650
  Args:
649
651
  cmd(str): SQLite command to run
650
652
  cmdData(list): list to hold the values for the `?` placeholders in `cmd`
653
+ executingMultiple(bool): indicate if we're executing/inserting multiple statements/rows at ago. this allows us to use cursor.executemany instead of cursor.execute
651
654
  Returns:
652
655
  ```
653
656
  # if a dict is returned
@@ -685,7 +688,7 @@ class Api:
685
688
  return reply if not self.__returnKISAResponse else Error(reply['log'])
686
689
 
687
690
  try:
688
- cursor = self.cursor.execute(cmd,cmdData)
691
+ cursor = (self.cursor.execute if not executingMultiple else self.cursor.executemany)(cmd,cmdData)
689
692
  affectedRows = cursor.rowcount
690
693
 
691
694
  if affectedRows < 0:
@@ -704,6 +707,20 @@ class Api:
704
707
  reply['status'] = True
705
708
  return reply if not self.__returnKISAResponse else Ok({'cursor':reply['cursor'], 'affectedRows':reply['affectedRows']})
706
709
 
710
+ @__checkContext
711
+ def executeScript(self, sql:str) -> Response:
712
+ '''
713
+ directly execute complex sql commands. this is useful when for you need to execute complex transactions example
714
+ Args:
715
+ sql(str): the sql to execute
716
+ '''
717
+
718
+ try:
719
+ self.db.executescript(sql)
720
+ except Exception as e:
721
+ return Error(f'{e}')
722
+ return Ok()
723
+
707
724
  @__checkContext
708
725
  def __createFetchResultsGenerator(self, results:sqlite3.Cursor, columnTitles:list, parseJson:bool, mode:str) -> Generator[tuple|dict|SimpleNamespace,None,None]:
709
726
 
@@ -867,7 +884,7 @@ class Api:
867
884
  '''
868
885
  insert a new row(s) into the database
869
886
  Args:
870
- table(str): the table itno which to insert data
887
+ table(str): the table into which to insert data
871
888
  data(list): 1d/2d list of the data to insert
872
889
  + 1d = insert single row
873
890
  + 2d = insert multple row
@@ -1370,5 +1387,9 @@ class Table:
1370
1387
  if __name__=='__main__':
1371
1388
  # Column('ab', int)
1372
1389
 
1390
+ with Api('/tmp/bukman/database.db2') as handle:
1391
+ print(handle.fetch('keys',['*'],'',[]))
1392
+ # Column('ab', int)
1393
+
1373
1394
  with Api('/tmp/bukman/database.db2') as handle:
1374
1395
  print(handle.fetch('keys',['*'],'',[]))
@@ -48,9 +48,10 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
48
48
 
49
49
  __schema = {
50
50
  'data': '''
51
- tstamp varchar(30) not null,
52
- dataType varcahr(30) not null,
53
- data json not null
51
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
52
+ tstamp VARCHAR(30) NOT NULL,
53
+ dataType VARCHAR(30) NOT NULL,
54
+ data JSON NOT NULL
54
55
  ''',
55
56
  }
56
57
 
@@ -107,6 +108,9 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
107
108
  '''
108
109
  return self.__length
109
110
 
111
+ def __len__(self) -> int:
112
+ return self.length
113
+
110
114
  def __load__(self) -> Response:
111
115
  '''
112
116
  load underlying queue database
@@ -148,6 +152,7 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
148
152
 
149
153
  with Handle(self.dbPath, readonly=False, returnKISAResponse=True) as handle:
150
154
  if not (resp := handle.insert('data', [
155
+ None,
151
156
  dates.currentTimestamp(),
152
157
  dataType,
153
158
  data
@@ -165,7 +170,7 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
165
170
  index = resp.data
166
171
 
167
172
  with Handle(self.dbPath, returnKISAResponse=True) as handle:
168
- if not (resp := handle.fetch('data', ['dataType', 'data'],'',[],limit=1, offset=index, parseJson=True)):
173
+ if not (resp := handle.fetch('data', ['dataType', 'data'],'1 order by id',[],limit=1, offset=index, parseJson=True)):
169
174
  return resp
170
175
 
171
176
  dataType, data = resp.data[0]
@@ -185,7 +190,7 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
185
190
  index = resp.data
186
191
 
187
192
  with Handle(self.dbPath, readonly=False, returnKISAResponse=True) as handle:
188
- if not (resp := handle.fetch('data', ['rowid','dataType', 'data'],'',[],limit=1, offset=index, parseJson=True)):
193
+ if not (resp := handle.fetch('data', ['id','dataType', 'data'],'1 order by id',[],limit=1, offset=index, parseJson=True)):
189
194
  return resp
190
195
 
191
196
  rowId, dataType, data = resp.data[0]
@@ -195,7 +200,7 @@ class PersistentQueue(metaclass=__PersistentQueueSingleton):
195
200
  except:
196
201
  return Error(f'failed to convert value to data-type `{dataType}`. the database was most likely corrupted')
197
202
 
198
- if not (resp := handle.delete('data','rowid=?',[rowId])):
203
+ if not (resp := handle.delete('data','id=?',[rowId])):
199
204
  return resp
200
205
 
201
206
  self.__length -= 1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kisa-utils
3
- Version: 0.42.6
3
+ Version: 0.42.8
4
4
  Summary: Utility functions and modules for KISA Developers
5
5
  Author: Tom Bukenya
6
6
  Author-email: glayn2bukman@gmail.com
@@ -3,8 +3,8 @@ kisa_utils/cache.py,sha256=WOL3e0wvoyESXTtlbVnUg9TYUoLzg64sLP9J4b4ti9k,7377
3
3
  kisa_utils/codes.py,sha256=PV_S53Skggf4XetOdYoIKtEmM8cpN5wZwUlxje70WZY,904
4
4
  kisa_utils/config.py,sha256=NfluzGKTh66qfNtC-Ae0zNb1XzMTgU2Me9Vi82R9c1E,2285
5
5
  kisa_utils/dataStructures.py,sha256=ZgLpttJ66jfpU1NWzLDD1Czqxzj6sWereffgTQWhlV8,2679
6
- kisa_utils/dates.py,sha256=zxe4n0PdKReZjK5ZkvnCZtJ55lk5oqu9oS8VX_nLozw,13966
7
- kisa_utils/db.py,sha256=-0Q7iw9VEtf4oyOIj_IhJqmzeh0CZVp3vEcwIQn4Zls,54939
6
+ kisa_utils/dates.py,sha256=JN0vm-7_fOdpzLtondVmpjpWoePPJn4aGK0NkBuyF8I,14142
7
+ kisa_utils/db.py,sha256=fQfvpSJz1A0KFpgXOjo_fyHcLwp9-d9GcDfyH6zhvMY,55788
8
8
  kisa_utils/encryption.py,sha256=nFzNpzWV_D9uSEq4FsgCnlS7FQtqWP9fvM_81rsfcLo,4218
9
9
  kisa_utils/figures.py,sha256=pYIpQzu1OXRSsY1d98GhgPifnIRmgl-r7S32ai-Ms0c,3731
10
10
  kisa_utils/functionUtils.py,sha256=PlXjnmU1uJWNdISlJJ3SCgavTsgNBoebaa9dtWSFhRA,6553
@@ -18,7 +18,7 @@ kisa_utils/token.py,sha256=Y2qglWYWpmHxoXBh-TH0r1as0uPV5LLqMNcunLvM4vM,7850
18
18
  kisa_utils/permissions/__config__.py,sha256=i3ELkOydDnjKx2ozQTxLZdZ8DXSeUncnl2kRxANjFmM,613
19
19
  kisa_utils/permissions/__init__.py,sha256=iAsGEf5Ktw3gPJ5ZKL8BnuqX8e_S4QgsCVgfaRYi4Qg,48068
20
20
  kisa_utils/queues/__init__.py,sha256=VvhceyN5qeiMel1JFQwLRuVk48oBXaWvDtriCubDOms,48
21
- kisa_utils/queues/persistent.py,sha256=Gie6eUX44BLzC2udO4mbP-njA6BRbsovlMcDNQKFXJM,6706
21
+ kisa_utils/queues/persistent.py,sha256=KueLVqkVcTqvVe5yt9DEM_PvtUEannukixRS8icONTI,6872
22
22
  kisa_utils/queues/callables/__init__.py,sha256=OJL3AQnaAS1Eek4H6WBH3WefA2wf-x03cwFmRSK8hoU,141
23
23
  kisa_utils/queues/callables/enqueueFunctionCalls.py,sha256=VIliaMvw4MUdOqts0dXdZCYNxs-QrOVjIRAR3scGrRM,11786
24
24
  kisa_utils/queues/callables/executorQueues.py,sha256=x6bAqxBSZRZ_kL8CK1lSN6JYAYFLxzM84LC1RmwaOLw,6626
@@ -27,7 +27,7 @@ kisa_utils/servers/flask.py,sha256=XZYY1pWnP1mSvaS5Uv8G3EFJV5BJBQtU2gDbO8suvLc,4
27
27
  kisa_utils/structures/__init__.py,sha256=JBU1j3A42jQ62ALKnsS1Hav9YXcYwjDw1wQJtohXPbU,83
28
28
  kisa_utils/structures/utils.py,sha256=665rXIapGwFqejizeJwy3DryeskCQOdgP25BCdLkGvk,2898
29
29
  kisa_utils/structures/validator.py,sha256=JhD9jcfbjTwBr_7OfuNaJd_cYr7wR2emFhsCEo5MCHQ,4323
30
- kisa_utils-0.42.6.dist-info/METADATA,sha256=Dyk0-x1A4skPorhBkLVW6Dwf2Qs7WgilsaMlLR3mXTE,477
31
- kisa_utils-0.42.6.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
32
- kisa_utils-0.42.6.dist-info/top_level.txt,sha256=GFOLXZYqpBG9xtscGa2uGJAEiZ5NwsqHBH9NylnB29M,11
33
- kisa_utils-0.42.6.dist-info/RECORD,,
30
+ kisa_utils-0.42.8.dist-info/METADATA,sha256=_ZX5rSDVBZgDWSObQzQ-nwdCu07AosdSNXpsywBxWco,477
31
+ kisa_utils-0.42.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
32
+ kisa_utils-0.42.8.dist-info/top_level.txt,sha256=GFOLXZYqpBG9xtscGa2uGJAEiZ5NwsqHBH9NylnB29M,11
33
+ kisa_utils-0.42.8.dist-info/RECORD,,