utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # from datetime import datetime
  2. import re
  3. import datetime
  4. # import os
  5. # import difflib
  6. # import csv
  7. # import keyring
  8. # from envelopes import Envelope
  9. from winmentor import WinMentor
  10. def find_line_number(csv_file_path, search_string):
  11. with open(csv_file_path, 'r', encoding='utf-8') as file:
  12. for line_num, line in enumerate(file, 1):
  13. if search_string in line:
  14. line_number = line_num
  15. break
  16. return line_number
  17. def remove_indentations(file_path):
  18. with open(file_path, 'r') as file:
  19. lines = file.readlines()
  20. # Remove leading spaces or tabs from each line
  21. lines = [line.lstrip() for line in lines]
  22. with open(file_path, 'w') as file:
  23. file.writelines(lines)
  24. def get_last_month(day):
  25. today = datetime.date.today()
  26. first_day_of_this_month = datetime.date(today.year, today.month, 1)
  27. last_day_of_last_month = first_day_of_this_month - datetime.timedelta(days=day)
  28. # last_month = last_day_of_last_month.strftime('%B %Y')
  29. last_month = last_day_of_last_month.month
  30. return last_month
  31. def get_partners(firm):
  32. mentor = WinMentor()
  33. part_dict = mentor._get_parts(firm)
  34. return part_dict
  35. def dig_it(search_in, default=0.00, fl_point=100, pattern=r'\d+') -> float:
  36. if isinstance(search_in, (float, int)):
  37. return search_in
  38. elif ',' not in search_in and '.' not in search_in:
  39. try:
  40. return int(search_in)
  41. except ValueError:
  42. pass
  43. else:
  44. if ',' in search_in[-2:] or '.' in search_in[-2:]:
  45. search_in += '0'
  46. try:
  47. if search_in[0] == '-':
  48. res = int(''.join(re.findall(pattern, search_in))[:-2]) + int(''.join(re.findall(pattern, search_in))[-2:]) / fl_point
  49. return -res
  50. else:
  51. res = int(''.join(re.findall(pattern, search_in))[:-2]) + int(''.join(re.findall(pattern, search_in))[-2:]) / fl_point
  52. return res
  53. except ValueError:
  54. return default
  55. def re_check(string, pattern=r'(?<=;)[A-Za-z-]*\w [A-Za-z :.-]*(?=;)'):
  56. string = re.sub(r'^[^a-zA-Z]+', '', string)
  57. matches = re.findall(pattern, string, flags=re.RegexFlag.IGNORECASE)
  58. # if string.startswith('Incasare') or string.startswith('Plata'):
  59. # first_semicolon_index_from_end = string.rfind(';')
  60. # second_semicolon_index_from_end = string.rfind(';', 0, first_semicolon_index_from_end)
  61. # third_semicolon_index_from_end = string.rfind(';', 0, second_semicolon_index_from_end)
  62. # ret_val = string[third_semicolon_index_from_end + 1:second_semicolon_index_from_end]
  63. # return ret_val
  64. if len(matches) == 0:
  65. matches = re.findall(r'\d+([A-Za-z-]+\s[A-Za-z :.-]*)', string)
  66. if len(matches) == 0:
  67. ret_val = ''
  68. return ret_val
  69. else:
  70. ret_val = matches[0]
  71. return ret_val
  72. elif len(matches) > 1:
  73. ret_val = matches[1]
  74. return ret_val
  75. else:
  76. ret_val = matches[0]
  77. return ret_val
  78. def re_check_otp(string, pattern=r'(?<=Ben: )[A-Za-z-]*\w [A-Za-z :.-]*(?= - )'):
  79. matches = re.findall(pattern, string, flags=re.RegexFlag.IGNORECASE)
  80. if len(matches) == 0:
  81. ret_val = ''
  82. return ret_val
  83. elif len(matches) > 1:
  84. ret_val = matches[1]
  85. return ret_val
  86. else:
  87. ret_val = matches[0]
  88. return ret_val
  89. def re_check_rncb(string, pattern=r'(?<=Beneficiar: )[A-Za-z-]*\w [A-Za-z :.-]*(?=;)|(?<=Platitor: )[A-Za-z-]*\w [A-Za-z :.-]*(?=;)'):
  90. matches = re.findall(pattern, string, flags=re.RegexFlag.IGNORECASE)
  91. # print(matches)
  92. if len(matches) == 0:
  93. ret_val = ''
  94. return ret_val
  95. # elif len(matches) > 1:
  96. # ret_val = matches[1]
  97. # return ret_val
  98. else:
  99. ret_val = matches[0]
  100. return ret_val
  101. if __name__ == '__main__':
  102. # s = 'Tranzactie efectuata prin George Banking BCR Referinta 230529S522316437, data valutei 29-05-2023, Decontare -Platitor: CHRYSOPEEA BRANDING & DESIGN SRL; RO60RNCB0106026590140001; CODFISC 6340770; BCR CLUJ CLUJ-NAPOCA-Beneficiar: BUGETUL DE STAT; RO14TREZ2165503XXXXXXXXX; CODFISC 6340770-Detalii: Bugetul de Stat Bugetele Asig. Soc si Fd Spec pt salarii pe luna Apr 2023'
  103. # r = re_check_rncb(s)
  104. # print(r)
  105. line = find_line_number('../csv/RO23BTRL01301202A93957XX-01.08.2023-31.08.2023.csv', 'Rezultat cautare')
  106. print(line)
  107. # print(get_last_month())
  108. # for i in get_partners():
  109. # print(i)
  110. # print(diff_checker(s, check_list))