TSVZ 3.12__py3-none-any.whl → 3.13__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.13
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=ZCz71rVpAkeVxrN4vBFS7rdZJmkAtEQNatH83b7JMsM,66962
2
+ TSVZ-3.13.dist-info/METADATA,sha256=awkcZiJKtAnfJ9kuGt58o3bvzgweZx7ZTZjUPpYG6fU,1826
3
+ TSVZ-3.13.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
4
+ TSVZ-3.13.dist-info/entry_points.txt,sha256=WeXidyV5yKCRLaVsnAY35xGa08QgytOfvr1CK9aescI,60
5
+ TSVZ-3.13.dist-info/top_level.txt,sha256=OPx4LvOpaYykaos7oL_jGaObSWXxLzhHiWLuz-K147g,5
6
+ TSVZ-3.13.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.13'
26
26
  __version__ = version
27
27
  author = 'pan@zopyr.us'
28
28
 
@@ -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