Skip to content

Commit

Permalink
Merge 186c37f into 17600b4
Browse files Browse the repository at this point in the history
  • Loading branch information
miki725 committed May 9, 2017
2 parents 17600b4 + 186c37f commit a59fe18
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion importanize/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def merged_statements(self):
else:
leafless_counter[statement.stem].append(statement)

merged_statements = [i[0] for i in leafless_counter.values()]
merged_statements = list(itertools.chain(*leafless_counter.values()))

def merge(statements):
_special = []
Expand Down
18 changes: 12 additions & 6 deletions importanize/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def __hash__(self):
return hash(self.as_string())

def __eq__(self, other):
return self.name == other.name
return all([self.name == other.name,
self.as_name == other.as_name])

def __gt__(self, other):
def _type(obj):
Expand Down Expand Up @@ -121,6 +122,13 @@ def __init__(self, line_numbers, stem, leafs=None,
self.comments = comments or []
self.file_artifacts = kwargs.get('file_artifacts', {})

@property
def full_stem(self):
stem = self.stem
if self.as_name:
stem += ' as {}'.format(self.as_name)
return stem

@property
def unique_leafs(self):
return sorted(list(set(self.leafs)))
Expand All @@ -137,10 +145,7 @@ def root_module(self):

def as_string(self):
if not self.leafs:
data = 'import {}'.format(self.stem)
if self.as_name:
data += ' as {}'.format(self.as_name)
return data
return 'import {}'.format(self.full_stem)
else:
return (
'from {} import {}'
Expand Down Expand Up @@ -179,6 +184,7 @@ def __add__(self, other):

def __eq__(self, other):
return all((self.stem == other.stem,
self.as_name == other.as_name,
self.unique_leafs == other.unique_leafs))

def __gt__(self, other):
Expand Down Expand Up @@ -224,4 +230,4 @@ def __gt__(self, other):
return self_len > other_len

# alphabetical sort
return self.stem > other.stem
return self.full_stem > other.full_stem
3 changes: 3 additions & 0 deletions tests/test_data/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os.path as ospath
import datetime
from package.subpackage.module.submodule import CONSTANT, Klass, foo, bar, rainbows
import datetime.parser
import datetime as mydatetime
from .module import foo, bar
from ..othermodule import rainbows
from a import b
Expand All @@ -18,6 +20,7 @@
import coverage # in site-packages
from z import foo

import datetime.parser
import something # with comment
from other.package.subpackage.module.submodule import CONSTANT, Klass, foo, bar, rainbows # noqa
from other import(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data/output_grouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import print_function, unicode_literals
import datetime
import datetime as mydatetime
import datetime.parser
from os import path as ospath

import coverage # in site-packages
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data/output_inline_grouped.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import print_function, unicode_literals
import datetime
import datetime as mydatetime
import datetime.parser
from os import path as ospath

import coverage # in site-packages
Expand Down
8 changes: 8 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ def test_import_statements(self):
'import a as b',
'import a as b'
)
self._test_import_string_matches(
'import a.b',
'import a.b'
)
self._test_import_string_matches(
'import a.b as b',
'from a import b'
Expand All @@ -287,6 +291,10 @@ def test_import_statements(self):
'import a.b as c',
'from a import b as c'
)
self._test_import_string_matches(
'import a.b.c as d',
'from a.b import c as d'
)
self._test_import_string_matches(
'import a.b.c',
'import a.b.c',
Expand Down

0 comments on commit a59fe18

Please sign in to comment.