files.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. This file is a wrapper around pandas built-in functionalities to handle character delimited files
  3. """
  4. import pandas as pd
  5. import numpy as np
  6. import os
  7. class File :
  8. def __init__(self,**params):
  9. """
  10. @param path absolute path of the file to be read
  11. """
  12. self.path = params['path'] if 'path' in params else None
  13. self.delimiter = params['delimiter'] if 'delimiter' in params else ','
  14. def isready(self):
  15. return os.path.exists(self.path)
  16. def meta(self,**_args):
  17. return []
  18. class Reader (File):
  19. """
  20. This class is designed to read data from disk (location on hard drive)
  21. @pre : isready() == True
  22. """
  23. def __init__(self,**_args):
  24. super().__init__(**_args)
  25. def read(self,**args):
  26. _path = self.path if 'path' not in args else args['path']
  27. _delimiter = self.delimiter if 'delimiter' not in args else args['delimiter']
  28. return pd.read_csv(_path,delimiter=self.delimiter)
  29. def stream(self,**args):
  30. raise Exception ("streaming needs to be implemented")
  31. class Writer (File):
  32. """
  33. This function writes output to disk in a designated location. The function will write a text to a text file
  34. - If a delimiter is provided it will use that to generate a xchar-delimited file
  35. - If not then the object will be dumped as is
  36. """
  37. # THREAD_LOCK = RLock()
  38. def __init__(self,**_args):
  39. super().__init__(**_args)
  40. self._mode = 'w' if 'mode' not in _args else _args['mode']
  41. def write(self,info,**_args):
  42. """
  43. This function writes a record to a designated file
  44. @param label <passed|broken|fixed|stats>
  45. @param row row to be written
  46. """
  47. try:
  48. _delim = self.delimiter if 'delimiter' not in _args else _args['delimiter']
  49. _path = self.path if 'path' not in _args else _args['path']
  50. _mode = self._mode if 'mode' not in _args else _args['mode']
  51. info.to_csv(_path,index=False,sep=_delim)
  52. pass
  53. except Exception as e:
  54. #
  55. # Not sure what should be done here ...
  56. print (e)
  57. pass
  58. finally:
  59. # DiskWriter.THREAD_LOCK.release()
  60. pass