TSVZ 3.12__py3-none-any.whl → 3.14__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: TSVZ
3
- Version: 3.12
3
+ Version: 3.14
4
4
  Summary: An simple in memory wrapper around a TSV file to function as a database
5
5
  Home-page: https://github.com/yufei-pan/TSVZ
6
6
  Author: Yufei Pan
@@ -0,0 +1,6 @@
1
+ TSVZ.py,sha256=Z2GXsKWfXFrjIHSg3gRldub4d6YvGt1fMTzuh-EZMjk,66968
2
+ TSVZ-3.14.dist-info/METADATA,sha256=9L4OSMoMJKZrPr4MyPZn74S-1nHnf3LmSI_Kdd1u1ks,1826
3
+ TSVZ-3.14.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
4
+ TSVZ-3.14.dist-info/entry_points.txt,sha256=WeXidyV5yKCRLaVsnAY35xGa08QgytOfvr1CK9aescI,60
5
+ TSVZ-3.14.dist-info/top_level.txt,sha256=OPx4LvOpaYykaos7oL_jGaObSWXxLzhHiWLuz-K147g,5
6
+ TSVZ-3.14.dist-info/RECORD,,
TSVZ.py CHANGED
@@ -22,7 +22,7 @@ if os.name == 'nt':
22
22
  elif os.name == 'posix':
23
23
  import fcntl
24
24
 
25
- version = '3.12'
25
+ version = '3.14'
26
26
  __version__ = version
27
27
  author = 'pan@zopyr.us'
28
28
 
@@ -289,7 +289,7 @@ def _processLine(line,taskDic,correctColumnNum,verbose = False,teeLogger = None,
289
289
  elif lineCache[0] == DEFAULTS_INDICATOR_KEY:
290
290
  if verbose:
291
291
  __teePrintOrNot(f"Empty defaults line found: {line}",teeLogger=teeLogger)
292
- defaults = []
292
+ defaults.clear()
293
293
  else:
294
294
  if verbose:
295
295
  __teePrintOrNot(f"Key {lineCache[0]} found with empty value, deleting such key's representaion",teeLogger=teeLogger)
@@ -319,7 +319,7 @@ def _processLine(line,taskDic,correctColumnNum,verbose = False,teeLogger = None,
319
319
  if lineCache[0] == DEFAULTS_INDICATOR_KEY:
320
320
  if verbose:
321
321
  __teePrintOrNot(f"Defaults line found: {line}",teeLogger=teeLogger)
322
- defaults = lineCache
322
+ defaults[:] = lineCache
323
323
  return correctColumnNum , []
324
324
  taskDic[lineCache[0]] = lineCache
325
325
  if verbose:
@@ -597,22 +597,45 @@ def appendTabularFile(fileName,lineToAppend,teeLogger = None,header = '',createI
597
597
  - Exception: If the file does not exist and createIfNotExist is False.
598
598
  - Exception: If the existing header does not match the provided header.
599
599
  """
600
+ return appendLinesTabularFile(fileName,[lineToAppend],teeLogger = teeLogger,header = header,createIfNotExist = createIfNotExist,verifyHeader = verifyHeader,verbose = verbose,encoding = encoding, strict = strict, delimiter = delimiter)
601
+
602
+ def appendLinesTabularFile(fileName,linesToAppend,teeLogger = None,header = '',createIfNotExist = False,verifyHeader = True,verbose = False,encoding = 'utf8', strict = True, delimiter = ...):
603
+ """
604
+ Append lines of data to a Tabular file.
605
+ Parameters:
606
+ - fileName (str): The path of the Tabular file.
607
+ - linesToAppend (list): The lines of data to append. If it is a list of string, then each string will be split by delimiter to form a list.
608
+ - teeLogger (optional): A logger object for logging messages.
609
+ - header (str, optional): The header line to verify against. If provided, the function will check if the existing header matches the provided header.
610
+ - createIfNotExist (bool, optional): If True, the file will be created if it does not exist. If False and the file does not exist, an exception will be raised.
611
+ - verifyHeader (bool, optional): If True, the function will verify if the existing header matches the provided header. If False, the header will not be verified.
612
+ - verbose (bool, optional): If True, additional information will be printed during the execution.
613
+ - encoding (str, optional): The encoding of the file.
614
+ - strict (bool, optional): If True, the function will raise an exception if there is a data format error. If False, the function will ignore the error and continue.
615
+ - delimiter (str, optional): The delimiter used in the Tabular file. Defaults to '\t' for TSV, ',' for CSV, '\0' for NSV.
616
+ Raises:
617
+ - Exception: If the file does not exist and createIfNotExist is False.
618
+ - Exception: If the existing header does not match the provided header.
619
+ """
600
620
  delimiter = get_delimiter(delimiter,file_name=fileName)
601
621
  header = _formatHeader(header,verbose = verbose,teeLogger = teeLogger,delimiter=delimiter)
602
622
  if not _verifyFileExistence(fileName,createIfNotExist = createIfNotExist,teeLogger = teeLogger,header = header,encoding = encoding,strict = strict,delimiter=delimiter):
603
623
  return
604
- if type(lineToAppend) == str:
605
- lineToAppend = lineToAppend.split(delimiter)
606
- else:
607
- for i in range(len(lineToAppend)):
608
- if type(lineToAppend[i]) != str:
609
- try:
610
- lineToAppend[i] = str(lineToAppend[i])
611
- except Exception as e:
612
- lineToAppend[i] = str(e)
624
+ formatedLines = []
625
+ for line in linesToAppend:
626
+ if type(line) == str:
627
+ line = line.split(delimiter)
628
+ else:
629
+ for i in range(len(line)):
630
+ if type(line[i]) != str:
631
+ try:
632
+ line[i] = str(line[i])
633
+ except Exception as e:
634
+ line[i] = str(e)
635
+ formatedLines.append(line)
613
636
 
614
637
  with open(fileName, mode ='r+b')as file:
615
- correctColumnNum = len(lineToAppend)
638
+ correctColumnNum = max([len(line) for line in formatedLines])
616
639
  if header.rstrip():
617
640
  if verifyHeader:
618
641
  line = file.readline().decode(encoding=encoding)
@@ -620,18 +643,19 @@ def appendTabularFile(fileName,lineToAppend,teeLogger = None,header = '',createI
620
643
  correctColumnNum = len(header.split(delimiter))
621
644
  if verbose:
622
645
  __teePrintOrNot(f"correctColumnNum: {correctColumnNum}",teeLogger=teeLogger)
623
- # truncate / fill the lineToAppend to the correct number of columns
624
- if len(lineToAppend) < correctColumnNum:
625
- lineToAppend += ['']*(correctColumnNum-len(lineToAppend))
626
- elif len(lineToAppend) > correctColumnNum:
627
- lineToAppend = lineToAppend[:correctColumnNum]
646
+ # truncate / fill the lines to the correct number of columns
647
+ for i in range(len(formatedLines)):
648
+ if len(formatedLines[i]) < correctColumnNum:
649
+ formatedLines[i] += ['']*(correctColumnNum-len(formatedLines[i]))
650
+ elif len(formatedLines[i]) > correctColumnNum:
651
+ formatedLines[i] = formatedLines[i][:correctColumnNum]
628
652
  # check if the file ends in a newline
629
653
  file.seek(-1, os.SEEK_END)
630
654
  if file.read(1) != b'\n':
631
655
  file.write(b'\n')
632
- file.write(get_delimiter(delimiter).join(lineToAppend).encode(encoding=encoding) + b'\n')
656
+ file.write(b'\n'.join([delimiter.join(line).encode(encoding=encoding) for line in formatedLines]) + b'\n')
633
657
  if verbose:
634
- __teePrintOrNot(f"Appended {lineToAppend} to {fileName}",teeLogger=teeLogger)
658
+ __teePrintOrNot(f"Appended {len(formatedLines)} lines to {fileName}",teeLogger=teeLogger)
635
659
 
636
660
  def clearTSV(fileName,teeLogger = None,header = '',verifyHeader = False,verbose = False,encoding = 'utf8',strict = False,delimiter = '\t'):
637
661
  """
@@ -1,6 +0,0 @@
1
- TSVZ.py,sha256=6SDOsWnaOjsgdElBAh56cjF_bdBxjJNLCgqyhLMcsrI,64898
2
- TSVZ-3.12.dist-info/METADATA,sha256=xzt34oOvoMSxbtzcuqgW94HpnWUHHvdHEN_OpGb7Kh0,1826
3
- TSVZ-3.12.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
4
- TSVZ-3.12.dist-info/entry_points.txt,sha256=WeXidyV5yKCRLaVsnAY35xGa08QgytOfvr1CK9aescI,60
5
- TSVZ-3.12.dist-info/top_level.txt,sha256=OPx4LvOpaYykaos7oL_jGaObSWXxLzhHiWLuz-K147g,5
6
- TSVZ-3.12.dist-info/RECORD,,
File without changes