예제 #1
0
    def get_statements(self, separate_from_imports=True):
        """
        Canonicalized L{ImportStatement}s.
        These have been merged by module and sorted.

          >>> importset = ImportSet('''
          ...     import a, b as B, c, d.dd as DD
          ...     from __future__ import division
          ...     from _hello import there
          ...     from _hello import *
          ...     from _hello import world
          ... ''')

          >>> for s in importset.get_statements(): print(s)
          from __future__ import division
          import a
          import b as B
          import c
          from _hello import *
          from _hello import there, world
          from d import dd as DD

        @rtype:
          C{tuple} of L{ImportStatement}s
        """
        groups = self._by_module_name
        if not separate_from_imports:

            def union_dicts(*dicts):
                result = {}
                for label, dict in enumerate(dicts):
                    for k, v in six.iteritems(dict):
                        result[(k, label)] = v
                return result

            groups = [groups[0], union_dicts(*groups[1:])]
        result = []
        for importgroup in groups:
            for _, imports in sorted(importgroup.items()):
                star_imports, nonstar_imports = (partition(
                    imports, lambda imp: imp.import_as == "*"))
                assert len(star_imports) <= 1
                if star_imports:
                    result.append(ImportStatement(star_imports))
                if nonstar_imports:
                    result.append(ImportStatement(sorted(nonstar_imports)))
        return tuple(result)
예제 #2
0
def test_partition_1():
    result = partition('12321233221', lambda c: int(c) % 2 == 0)
    expected = (['2', '2', '2', '2', '2'], ['1', '3', '1', '3', '3', '1'])
    assert result == expected