ClassLogging.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 27 14:20:58 2019
  5. @author: tanya
  6. """
  7. import os
  8. import sys
  9. import pandas as pd
  10. sys.path.append(os.getcwd())
  11. class ClassLogging:
  12. '''
  13. '''
  14. def __init__(self, log_name: str = None):
  15. '''
  16. '''
  17. from libraries.log import Log
  18. self._log = Log(log_name)
  19. def log_and_raise(self, message):
  20. '''
  21. '''
  22. self._log.error(message)
  23. raise Exception(message)
  24. def log_and_warn(self, message):
  25. '''
  26. '''
  27. self._log.warning(message)
  28. def check_is_file(self, path):
  29. '''
  30. '''
  31. if not os.path.isfile(path):
  32. err = "File {} not found".format(path)
  33. self._log.error(err)
  34. raise FileNotFoundError(err)
  35. def _check_column_abscence(self, columns: (str, list), data: pd.DataFrame,
  36. error_or_warning: str):
  37. '''
  38. '''
  39. if isinstance(columns, str):
  40. columns = [columns]
  41. for column in columns:
  42. if column not in data.columns:
  43. err = ("{} is not an internal column name".format(column))
  44. getattr(self._log, error_or_warning)(err)
  45. if error_or_warning == "error":
  46. raise Exception(err)
  47. def error_column_abscence(self, columns: (str, list), data: pd.DataFrame):
  48. '''
  49. '''
  50. return self._check_column_abscence(columns=columns,
  51. data=data,
  52. error_or_warning="error")
  53. def warn_column_abscence(self, columns: (str, list), data: pd.DataFrame):
  54. '''
  55. '''
  56. return self._check_column_abscence(columns=columns,
  57. data=data,
  58. error_or_warning="warning")