TSVZ 3.11__tar.gz → 3.13__tar.gz

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.11
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: TSVZ
3
- Version: 3.11
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
@@ -1,4 +1,9 @@
1
1
  #!/usr/bin/env python3
2
+ # /// script
3
+ # requires-python = ">=3.6"
4
+ # dependencies = [
5
+ # ]
6
+ # ///
2
7
  import os , sys
3
8
  from collections import OrderedDict , deque
4
9
  import time
@@ -17,7 +22,8 @@ if os.name == 'nt':
17
22
  elif os.name == 'posix':
18
23
  import fcntl
19
24
 
20
- version = '3.11'
25
+ version = '3.13'
26
+ __version__ = version
21
27
  author = 'pan@zopyr.us'
22
28
 
23
29
  DEFAULT_DELIMITER = '\t'
@@ -591,22 +597,45 @@ def appendTabularFile(fileName,lineToAppend,teeLogger = None,header = '',createI
591
597
  - Exception: If the file does not exist and createIfNotExist is False.
592
598
  - Exception: If the existing header does not match the provided header.
593
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
+ """
594
620
  delimiter = get_delimiter(delimiter,file_name=fileName)
595
621
  header = _formatHeader(header,verbose = verbose,teeLogger = teeLogger,delimiter=delimiter)
596
622
  if not _verifyFileExistence(fileName,createIfNotExist = createIfNotExist,teeLogger = teeLogger,header = header,encoding = encoding,strict = strict,delimiter=delimiter):
597
623
  return
598
- if type(lineToAppend) == str:
599
- lineToAppend = lineToAppend.split(delimiter)
600
- else:
601
- for i in range(len(lineToAppend)):
602
- if type(lineToAppend[i]) != str:
603
- try:
604
- lineToAppend[i] = str(lineToAppend[i])
605
- except Exception as e:
606
- 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)
607
636
 
608
637
  with open(fileName, mode ='r+b')as file:
609
- correctColumnNum = len(lineToAppend)
638
+ correctColumnNum = max([len(line) for line in formatedLines])
610
639
  if header.rstrip():
611
640
  if verifyHeader:
612
641
  line = file.readline().decode(encoding=encoding)
@@ -614,18 +643,19 @@ def appendTabularFile(fileName,lineToAppend,teeLogger = None,header = '',createI
614
643
  correctColumnNum = len(header.split(delimiter))
615
644
  if verbose:
616
645
  __teePrintOrNot(f"correctColumnNum: {correctColumnNum}",teeLogger=teeLogger)
617
- # truncate / fill the lineToAppend to the correct number of columns
618
- if len(lineToAppend) < correctColumnNum:
619
- lineToAppend += ['']*(correctColumnNum-len(lineToAppend))
620
- elif len(lineToAppend) > correctColumnNum:
621
- 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]
622
652
  # check if the file ends in a newline
623
653
  file.seek(-1, os.SEEK_END)
624
654
  if file.read(1) != b'\n':
625
655
  file.write(b'\n')
626
- 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')
627
657
  if verbose:
628
- __teePrintOrNot(f"Appended {lineToAppend} to {fileName}",teeLogger=teeLogger)
658
+ __teePrintOrNot(f"Appended {len(formatedLines)} lines to {fileName}",teeLogger=teeLogger)
629
659
 
630
660
  def clearTSV(fileName,teeLogger = None,header = '',verifyHeader = False,verbose = False,encoding = 'utf8',strict = False,delimiter = '\t'):
631
661
  """
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes