Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```python
- from dataclasses import dataclass, field
- @dataclass
- class iSort:
- imports: str = field(default=None)
- def _imports(self):
- imports = self.imports
- imports = self._parse()
- imports = self._classify()
- imports = self._sort()
- self.imports = imports
- def _sort(self):
- import_imports: List[str] = []
- from_imports: List[str] = []
- for section in self._sections():
- for i in section:
- try:
- import_type = i.split()[0]
- if i.split()[0] == "import":
- import_imports.append(i)
- else:
- from_imports.append(i)
- except IndexError:
- pass
- return map(sorted, import_imports, from_imports)
- def _sections(self):
- builtins = []
- third_party = []
- first_party = []
- try:
- builtins, third_party, first_party = self._parse()
- except IndexError:
- builtins, third_party = self._parse()
- except:
- builtins = self._parse()
- return builtins, third_party, first_party
- def _parse(self):
- return self.imports.strip().split("\n")
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement