utils.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. '''
  2. Created on Feb 18, 2021 @author: vnc-console
  3. '''
  4. import os
  5. import traceback
  6. import operator
  7. import sys
  8. import platform
  9. import subprocess as sp
  10. from time import sleep
  11. def filter_files(code, _ext='.xml', path=None):
  12. '''lists all pdf's in current directory'''
  13. if path:
  14. my_files = filter(lambda file: file.endswith(_ext) and code in file or
  15. file.endswith(_ext.upper()) and code in file, os.listdir(path))
  16. else:
  17. my_files = filter(lambda file: file.endswith(_ext) and code in file or
  18. file.endswith(_ext.upper()) and code in file, os.listdir())
  19. return my_files
  20. def count_down(t, d_type, up_down, max_count=None):
  21. if up_down == 'down':
  22. one_ = -1
  23. OPERAND_ = operator.ge
  24. max_count = 0
  25. finish = 'count%s finished' % up_down
  26. elif up_down == 'up':
  27. one_ = 1
  28. OPERAND_ = operator.le
  29. finish = 'count%s finished' % up_down
  30. max_count = max_count
  31. while OPERAND_(t, max_count):
  32. if d_type == 'm':
  33. mins, secs = divmod(t, 60)
  34. tf = '{0:2d}:{1:2d}'.format(mins, secs)
  35. print(tf, end='\r')
  36. sleep(1)
  37. t += one_
  38. elif d_type == 'h':
  39. hour, rem = divmod(t, 60)
  40. _, secs = divmod(rem, 60)
  41. hours, mins = divmod(hour, 60)
  42. tf = '{0:02d}:{1:02d}:{2:02d}'.format(hours, mins, secs)
  43. print(tf, end='\r')
  44. sleep(1)
  45. t += one_
  46. else:
  47. print(finish)
  48. def pretty_exc(exc):
  49. exc_type, _exc_obj, exc_tb = sys.exc_info()
  50. # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  51. for tb in list(traceback.format_exception(exc_type, _exc_obj, exc_tb)):
  52. print(tb)
  53. # print(exc_type, fname, 'ln', exc_tb.tb_lineno, '-' + str(exc))
  54. def file_open(file_name):
  55. abs_path = os.path.abspath(file_name)
  56. if platform.system() == 'Windows':
  57. sp.run(f'start {abs_path}', shell=True)
  58. elif platform.system() == 'Linux':
  59. sp.run(['xdg-open', f'{abs_path}'])
  60. else:
  61. raise NotImplementedError