utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. elif len(matches) == 0:
  65. ret_val = ''
  66. return ret_val
  67. # matches = re.findall(r'\d+([A-Za-z-]+\s[A-Za-z :.-]*)', string)
  68. # if len(matches) == 0:
  69. # ret_val = ''
  70. # return ret_val
  71. # else:
  72. # ret_val = matches[0]
  73. # return ret_val
  74. elif len(matches) > 1:
  75. ret_val = matches[1]
  76. return ret_val
  77. else:
  78. ret_val = matches[0]
  79. return ret_val
  80. def re_check_otp(string, pattern=r'(?<=Ben: )[A-Za-z-]*\w [A-Za-z :.-]*(?= - )'):
  81. matches = re.findall(pattern, string, flags=re.RegexFlag.IGNORECASE)
  82. if len(matches) == 0:
  83. ret_val = ''
  84. return ret_val
  85. elif len(matches) > 1:
  86. ret_val = matches[1]
  87. return ret_val
  88. else:
  89. ret_val = matches[0]
  90. return ret_val
  91. def re_check_rncb(string, pattern=r'(?<=Beneficiar: )[A-Za-z-]*\w [A-Za-z :.-]*(?=;)|(?<=Platitor: )[A-Za-z-]*\w [A-Za-z :.-]*(?=;)'):
  92. matches = re.findall(pattern, string, flags=re.RegexFlag.IGNORECASE)
  93. # print(matches)
  94. if len(matches) == 0:
  95. ret_val = ''
  96. return ret_val
  97. # elif len(matches) > 1:
  98. # ret_val = matches[1]
  99. # return ret_val
  100. else:
  101. ret_val = matches[0]
  102. return ret_val
  103. if __name__ == '__main__':
  104. # 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'
  105. # r = re_check_rncb(s)
  106. # print(r)
  107. line = find_line_number('../csv/RO23BTRL01301202A93957XX-01.08.2023-31.08.2023.csv', 'Rezultat cautare')
  108. print(line)
  109. # print(get_last_month())
  110. # for i in get_partners():
  111. # print(i)
  112. # print(diff_checker(s, check_list))