Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. import re
  5.  
  6. class fileSort:
  7.  
  8. isstdinempty = sys.stdin.isatty()
  9.  
  10. def __init__(self):
  11. self.string_list = list()
  12.  
  13. if not self.isstdinempty:
  14. self.pipe_input = sys.stdin.readlines()
  15. else:
  16. self.file_input = sys.argv[1:]
  17. self.read_files_or_pipe()
  18.  
  19. def read_files_or_pipe(self):
  20. if not self.isstdinempty:
  21. self.string_list = [i.strip() for i in self.pipe_input]
  22. else:
  23. for arg in self.file_input:
  24. with open(arg, 'r') as stream_in:
  25. self.string_list += [i.strip() for i in stream_in.readlines()]
  26.  
  27. def __enter__(self):
  28. return self
  29.  
  30. def __exit__(self, _ex_type, _ex_val, _tb):
  31. pass
  32.  
  33. @staticmethod
  34. def sort(key):
  35. try:
  36. return re.search(r'(\d+)(\D*)$', key).group(1)
  37. except AttributeError:
  38. return re.search(r'^(\W*)(\w+)', key).group(2)
  39.  
  40. def sort_and_print(self):
  41. for i in sorted(self.string_list, key=self.sort):
  42. print(i)
  43. print(end='', flush=True)
  44.  
  45. if __name__ == "__main__":
  46. with fileSort() as fs:
  47. fs.sort_and_print()
Add Comment
Please, Sign In to add comment