# -*- coding: utf-8 -*- """ Created on Fri Apr 26 21:38:35 2024 @author: Arnd Helmut Hafner """ '''pooling common tools''' import sys # for sys.exit after error import re from re import compile, search #concerning output def listoutput(outputlist,filename,delimiter = '\n'): ''' outputs a simple list Parameters ---------- liste : list DESCRIPTION. filename : str delimiter : str, optional DESCRIPTION. The default is '\n'. Returns ------- None. ''' outputfile = open(filename, 'w', encoding='utf-8-sig') for i in range(len(outputlist)): outputfile.write('{}{}'.format(outputlist[i],delimiter)) def tuplelistoutput(outputlist,fieldnumber,filename,delimiter = '\t'): ''' outputs a list to a file Parameters ---------- outputlist : list list to output fieldnumber : str number of fields to output for every record filename : str delimiter : str, optional DESCRIPTION. The default is '\t'. Returns ------- None. ''' outputfile = open(filename, 'w', encoding='utf-8-sig') for i in range(len(outputlist)): outputfile.write('{}'.format(i))#ID number for j in range(fieldnumber): outputfile.write('{}{}'.format(delimiter,outputlist[i][j])) outputfile.write('\n') outputfile.close() print('outputed {} lines to {}'.format(len(outputlist),filename)) def dictoutput(outputdic,filename,delimiter): '''Outputs a dictionary into a file Parameters ---------- outputdic : dic filename : str delimiter : str Returns ------- None. ''' dicfile = open(filename, 'w', encoding='utf-8-sig') for key in outputdic.keys(): dicfile.write('{}{}{}\n'.format(key,delimiter,outputdic[key])) dicfile.close() print('outputed {} items to {}'.format(len(outputdic),filename)) #concerning list processing def lookuplistitem(item, liste): '''looks up an item in a list and returns the position of item as a list Parameters ---------- item : TYPE not defined. Whatever items are in a list liste : list Returns ------- position : list ''' position = [] for n in range(len(liste)): if item == liste[n]:position.append(n) return(position) def listitemcount(liste): ''' Counts items of a list and returns a dict items with number of occurence for each item as value Parameters ---------- liste : TYPE DESCRIPTION. Returns ------- itemdic : dic {item:number} ''' itemdic = {} for n in range(len(liste)): if liste[n] not in itemdic.keys(): itemdic[liste[n]] = 1 else: itemdic[liste[n]] +=1 return(itemdic) def listduplicationcheck(liste): ''' Checks whether there are same items in a list and returns a list of tuples of list items and their appearance frequencies Parameters ---------- liste : list Must be a list of strings! Returns ------- newlist : list [(item,frequency)] ''' tempdic = {} for n in range(len(liste)): if liste[n] not in tempdic.keys():tempdic[liste[n]] = 1 else:tempdic[liste[n]] += 1 newlist = [] for item in tempdic.keys(): newlist.append((item,tempdic[item])) return(newlist) def listduplicationdel(liste): ''' Deletes duplications in lists Parameters ---------- liste : list Returns ------- liste ''' duplications = [] for n in range(len(liste)): if n in duplications:continue for m in range(n+1,len(liste)): if m in duplications:continue elif liste[n] == liste[m]:duplications.append(m) duplications = sorted(duplications , key=lambda x: x*-1) for n in range(len(duplications)): del liste[duplications[n]] return(liste) def listaddrecords(newrecords,liste): ''' Adds new records to list at specified position Parameters ---------- newrecords : list [(record,position)] liste : list Returns ------- liste : list ''' newrecords = sorted(newrecords , key=lambda x: x[1]*-1) for n in range(len(newrecords)): liste.insert(newrecords[n][1]+1,newrecords[n][0]) return(liste) #concerning search def searchpreparation(string,sufflate,prefix = '',suffix = ''): ''' Inserts regex into strings in order to suppress interference of irregular symbols during search Parameters ---------- string : str searched string sufflate : str Regex inserted between the constituent characters of the string prefix : str, optional Regex inserted before the string. The default is ''. suffix : str, optional Regex inserted behind the string. The default is ''. Returns ------- string: str ''' #記号交じりの原文から文字列を検索するために、文字列を正規表現に置き換える #string = 検索したい文字列 #prefex = 原文では検索したい文字列の前にあり得る記号のリストの正規表現 #sufflate = 検索したい文字列の間に出現しうる記号のリストの正規表現 #suffix = 検索したい文字列の後ろに出現しうる記号のリストの正規表現 string = re.findall('.',string) tempstring = prefix + string[0] for c in range(1,len(string)): tempstring += sufflate + string[c] string = tempstring + suffix return(string) if __name__ == "__main__": zero = 0