Example #1
0
 def test_sorted(self):
     from logilab.common.compat import sorted
     l = [3, 1, 2, 5, 4]
     s = sorted(l)
     self.assertEquals(s, [1, 2, 3, 4, 5])
     self.assertEquals(l, [3, 1, 2, 5, 4])
     self.assertEquals(sorted('FeCBaD'), list('BCDFae'))
     self.assertEquals(sorted('FeCBaD', key=str.lower), list('aBCDeF'))
     self.assertEquals(sorted('FeCBaD', key=str.lower, reverse=True), list('FeDCBa'))
     def strcmp(s1, s2):
         return cmp(s1.lower(), s2.lower())
     self.assertEquals(sorted('FeCBaD', cmp=strcmp), list('aBCDeF'))
Example #2
0
 def test_sorted(self):
     from logilab.common.compat import sorted
     l = [3, 1, 2, 5, 4]
     s = sorted(l)
     self.assertEquals(s, [1, 2, 3, 4, 5])
     self.assertEquals(l, [3, 1, 2, 5, 4])
     self.assertEquals(sorted('FeCBaD'), list('BCDFae'))
     self.assertEquals(sorted('FeCBaD', key=str.lower), list('aBCDeF'))
     self.assertEquals(sorted('FeCBaD', key=str.lower, reverse=True), list('FeDCBa'))
     def strcmp(s1, s2):
         return cmp(s1.lower(), s2.lower())
     self.assertEquals(sorted('FeCBaD', cmp=strcmp), list('aBCDeF'))
 def test_sorted(self):
     if sys.version_info >= (3, 0):
         self.skip("don't test 2.4 compat 'sorted' on >= 3.0")
     from logilab.common.compat import sorted
     l = [3, 1, 2, 5, 4]
     s = sorted(l)
     self.assertEqual(s, [1, 2, 3, 4, 5])
     self.assertEqual(l, [3, 1, 2, 5, 4])
     self.assertEqual(sorted('FeCBaD'), list('BCDFae'))
     self.assertEqual(sorted('FeCBaD', key=str.lower), list('aBCDeF'))
     self.assertEqual(sorted('FeCBaD', key=str.lower, reverse=True), list('FeDCBa'))
     def strcmp(s1, s2):
         return cmp(s1.lower(), s2.lower())
     self.assertEqual(sorted('FeCBaD', cmp=strcmp), list('aBCDeF'))
Example #4
0
 def test_enable_checkers2(self):
     self.linter.enable_checkers(['design'], True)
     self.assertEquals(
         sorted([
             c.name for c in self.linter._checkers.values()
             if c.is_enabled()
         ]), ['design', 'master'])
 def test_enable_checkers1(self):
     self.linter.enable_checkers(['design'], False)
     self.assertEquals(sorted([c.name for c in self.linter._checkers.values()
                               if c.is_enabled()]),
                       ['basic', 'classes', 'exceptions', 'format', 'imports',
                        'master', 'metrics', 'miscellaneous', 'newstyle',
                        'similarities', 'typecheck', 'variables'])
Example #6
0
def ordered_nodes(graph):
    """takes a dependency graph dict as arguments and return an ordered tuple of
    nodes starting with nodes without dependencies and up to the outermost node.

    If there is some cycle in the graph, :exc:`UnorderableGraph` will be raised.

    Also the given graph dict will be emptied.
    """
    cycles = get_cycles(graph)
    if cycles:
        cycles = '\n'.join([' -> '.join(cycle) for cycle in cycles])
        raise UnorderableGraph(cycles)
    ordered = []
    while graph:
        # sorted to get predictable results
        for node, deps in sorted(graph.items()):
            if not deps:
                ordered.append(node)
                del graph[node]
                for deps in graph.itervalues():
                    try:
                        deps.remove(node)
                    except KeyError:
                        continue
    return tuple(reversed(ordered))
Example #7
0
def ordered_nodes(graph):
    """takes a dependency graph dict as arguments and return an ordered tuple of
    nodes starting with nodes without dependencies and up to the outermost node.

    If there is some cycle in the graph, :exc:`UnorderableGraph` will be raised.

    Also the given graph dict will be emptied.
    """
    cycles = get_cycles(graph)
    if cycles:
        cycles = '\n'.join([' -> '.join(cycle) for cycle in cycles])
        raise UnorderableGraph(cycles)
    ordered = []
    while graph:
        # sorted to get predictable results
        for node, deps in sorted(graph.items()):
            if not deps:
                ordered.append(node)
                del graph[node]
                for deps in graph.itervalues():
                    try:
                        deps.remove(node)
                    except KeyError:
                        continue
    return tuple(reversed(ordered))
Example #8
0
 def test_enable_checkers1(self):
     self.linter.enable_checkers(['design'], False)
     self.assertEquals(
         sorted([
             c.name for c in self.linter._checkers.values()
             if c.is_enabled()
         ]), [
             'basic', 'classes', 'exceptions', 'format', 'imports',
             'logging', 'master', 'metrics', 'miscellaneous', 'newstyle',
             'similarities', 'string_format', 'typecheck', 'variables'
         ])
    def test_classmethod_attributes(self):
        data = '''
class WebAppObject(object):
    def registered(cls, application):
        cls.appli = application
        cls.schema = application.schema
        cls.config = application.config
        return cls
    registered = classmethod(registered)
        '''
        astng = abuilder.string_build(data, __name__, __file__)
        cls = astng['WebAppObject']
        self.assertEqual(sorted(cls.locals.keys()),
                         ['appli', 'config', 'registered', 'schema'])
    def test_classmethod_attributes(self):
        data = '''
class WebAppObject(object):
    def registered(cls, application):
        cls.appli = application
        cls.schema = application.schema
        cls.config = application.config
        return cls
    registered = classmethod(registered)
        '''
        astng = abuilder.string_build(data, __name__, __file__)
        cls = astng['WebAppObject']
        self.assertEquals(sorted(cls.locals.keys()),
                          ['appli', 'config', 'registered', 'schema'])
def repr_tree_defs(data, indent_str=None):
    """return a string which represents imports as a tree"""
    lines = []
    nodes = data.items()
    for i, (mod, (sub, files)) in enumerate(sorted(nodes, key=lambda x: x[0])):
        if not files:
            files = ''
        else:
            files = '(%s)' % ','.join(files)
        if indent_str is None:
            lines.append('%s %s' % (mod, files))
            sub_indent_str = '  '
        else:
            lines.append('%s\-%s %s' % (indent_str, mod, files))
            if i == len(nodes) - 1:
                sub_indent_str = '%s  ' % indent_str
            else:
                sub_indent_str = '%s| ' % indent_str
        if sub:
            lines.append(repr_tree_defs(sub, sub_indent_str))
    return '\n'.join(lines)
Example #12
0
def repr_tree_defs(data, indent_str=None):
    """return a string which represents imports as a tree"""
    lines = []
    nodes = data.items()
    for i, (mod, (sub, files)) in enumerate(sorted(nodes, key=lambda x: x[0])):
        if not files:
            files = ''
        else:
            files = '(%s)' % ','.join(files)
        if indent_str is None:
            lines.append('%s %s' % (mod, files))
            sub_indent_str = '  '
        else:
            lines.append('%s\-%s %s' % (indent_str, mod, files))
            if i == len(nodes)-1:
                sub_indent_str = '%s  ' % indent_str
            else:
                sub_indent_str = '%s| ' % indent_str
        if sub:
            lines.append(repr_tree_defs(sub, sub_indent_str))
    return '\n'.join(lines)
Example #13
0
 def test_enable_checkers1(self):
     self.linter.enable_checkers(["design"], False)
     self.assertEquals(
         sorted([c.name for c in self.linter._checkers.values() if c.is_enabled()]),
         [
             "basic",
             "classes",
             "exceptions",
             "format",
             "imports",
             "logging",
             "master",
             "metrics",
             "miscellaneous",
             "newstyle",
             "similarities",
             "string_format",
             "typecheck",
             "variables",
         ],
     )
Example #14
0
 def test_create_files(self):
     chroot = tempfile.mkdtemp()
     path_to = lambda path: join(chroot, path)
     dircontent = lambda path: sorted(os.listdir(join(chroot, path)))
     try:
         self.failIf(isdir(path_to('a/')))
         create_files(['a/b/foo.py', 'a/b/c/', 'a/b/c/d/e.py'], chroot)
         # make sure directories exist
         self.failUnless(isdir(path_to('a')))
         self.failUnless(isdir(path_to('a/b')))
         self.failUnless(isdir(path_to('a/b/c')))
         self.failUnless(isdir(path_to('a/b/c/d')))
         # make sure files exist
         self.failUnless(isfile(path_to('a/b/foo.py')))
         self.failUnless(isfile(path_to('a/b/c/d/e.py')))
         # make sure only asked files were created
         self.assertEquals(dircontent('a'), ['b'])
         self.assertEquals(dircontent('a/b'), ['c', 'foo.py'])
         self.assertEquals(dircontent('a/b/c'), ['d'])
         self.assertEquals(dircontent('a/b/c/d'), ['e.py'])
     finally:
         shutil.rmtree(chroot)
 def test_create_files(self):
     chroot = tempfile.mkdtemp()
     path_to = lambda path: join(chroot, path)
     dircontent = lambda path: sorted(os.listdir(join(chroot, path)))
     try:
         self.assertFalse(isdir(path_to('a/')))
         create_files(['a/b/foo.py', 'a/b/c/', 'a/b/c/d/e.py'], chroot)
         # make sure directories exist
         self.assertTrue(isdir(path_to('a')))
         self.assertTrue(isdir(path_to('a/b')))
         self.assertTrue(isdir(path_to('a/b/c')))
         self.assertTrue(isdir(path_to('a/b/c/d')))
         # make sure files exist
         self.assertTrue(isfile(path_to('a/b/foo.py')))
         self.assertTrue(isfile(path_to('a/b/c/d/e.py')))
         # make sure only asked files were created
         self.assertEqual(dircontent('a'), ['b'])
         self.assertEqual(dircontent('a/b'), ['c', 'foo.py'])
         self.assertEqual(dircontent('a/b/c'), ['d'])
         self.assertEqual(dircontent('a/b/c/d'), ['e.py'])
     finally:
         shutil.rmtree(chroot)
 def __hash__(self):
     # FIXME: to be able to add constraints in Sets (and compare them)
     # FIXME: improve implementation
     variables = tuple(sorted(self._variables))
     return hash((self.__class__.__name__, variables))
Example #17
0
 def __hash__(self):
     # FIXME: to be able to add constraints in Sets (and compare them)
     # FIXME: improve implementation
     variables = tuple(sorted(self._variables))
     return hash((self.__class__.__name__, variables))
Example #18
0
 def test_enable_checkers2(self):
     self.linter.enable_checkers(["design"], True)
     self.assertEquals(
         sorted([c.name for c in self.linter._checkers.values() if c.is_enabled()]), ["design", "master"]
     )