Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. ```python
  2. from dataclasses import dataclass, field
  3.  
  4.  
  5. @dataclass
  6. class iSort:
  7. imports: str = field(default=None)
  8.  
  9. def _imports(self):
  10. imports = self.imports
  11. imports = self._parse()
  12. imports = self._classify()
  13. imports = self._sort()
  14. self.imports = imports
  15.  
  16. def _sort(self):
  17. import_imports: List[str] = []
  18. from_imports: List[str] = []
  19. for section in self._sections():
  20. for i in section:
  21. try:
  22. import_type = i.split()[0]
  23. if i.split()[0] == "import":
  24. import_imports.append(i)
  25. else:
  26. from_imports.append(i)
  27. except IndexError:
  28. pass
  29. return map(sorted, import_imports, from_imports)
  30.  
  31. def _sections(self):
  32. builtins = []
  33. third_party = []
  34. first_party = []
  35. try:
  36. builtins, third_party, first_party = self._parse()
  37. except IndexError:
  38. builtins, third_party = self._parse()
  39. except:
  40. builtins = self._parse()
  41. return builtins, third_party, first_party
  42.  
  43. def _parse(self):
  44. return self.imports.strip().split("\n")
  45. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement