コード例 #1
0
ファイル: options.py プロジェクト: iMerica/dj-models
 def get_parent_list(self):
     """
     Return all the ancestors of this model as a list ordered by MRO.
     Useful for determining if something is an ancestor, regardless of lineage.
     """
     result = OrderedSet(self.parents)
     for parent in self.parents:
         for ancestor in parent._meta.get_parent_list():
             result.add(ancestor)
     return list(result)
コード例 #2
0
    def process_rhs(self, compiler, connection):
        db_rhs = getattr(self.rhs, '_db', None)
        if db_rhs is not None and db_rhs != connection.alias:
            raise ValueError(
                "Subqueries aren't allowed across different databases. Force "
                "the inner query to be evaluated using `list(inner_query)`.")

        if self.rhs_is_direct_value():
            try:
                rhs = OrderedSet(self.rhs)
            except TypeError:  # Unhashable items in self.rhs
                rhs = self.rhs

            if not rhs:
                raise EmptyResultSet

            # rhs should be an iterable; use batch_process_rhs() to
            # prepare/transform those values.
            sqls, sqls_params = self.batch_process_rhs(compiler, connection,
                                                       rhs)
            placeholder = '(' + ', '.join(sqls) + ')'
            return (placeholder, sqls_params)
        else:
            if not getattr(self.rhs, 'has_select_fields', True):
                self.rhs.clear_select_clause()
                self.rhs.add_fields(['pk'])
            return super().process_rhs(compiler, connection)
コード例 #3
0
ファイル: graph.py プロジェクト: iMerica/dj-models
 def iterative_dfs(self, start, forwards=True):
     """Iterative depth-first search for finding dependencies."""
     visited = []
     stack = [start]
     while stack:
         node = stack.pop()
         visited.append(node)
         stack += sorted(node.parents if forwards else node.children)
     return list(OrderedSet(reversed(visited)))
コード例 #4
0
ファイル: graph.py プロジェクト: iMerica/dj-models
 def descendants(self):
     # Use self.key instead of self to speed up the frequent hashing
     # when constructing an OrderedSet.
     if '_descendants' not in self.__dict__:
         descendants = []
         for child in sorted(self.children, reverse=True):
             descendants += child.descendants()
         descendants.append(self.key)
         self.__dict__['_descendants'] = list(OrderedSet(descendants))
     return self.__dict__['_descendants']
コード例 #5
0
ファイル: graph.py プロジェクト: iMerica/dj-models
 def ancestors(self):
     # Use self.key instead of self to speed up the frequent hashing
     # when constructing an OrderedSet.
     if '_ancestors' not in self.__dict__:
         ancestors = []
         for parent in sorted(self.parents, reverse=True):
             ancestors += parent.ancestors()
         ancestors.append(self.key)
         self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
     return self.__dict__['_ancestors']
コード例 #6
0
 def test_len(self):
     s = OrderedSet()
     self.assertEqual(len(s), 0)
     s.add(1)
     s.add(2)
     s.add(2)
     self.assertEqual(len(s), 2)
コード例 #7
0
    def python(self, options):
        import code
        # Set up a dictionary to serve as the environment for the shell, so
        # that tab completion works on objects that are imported at runtime.
        imported_objects = {}
        try:  # Try activating rlcompleter, because it's handy.
            import readline
        except ImportError:
            pass
        else:
            # We don't have to wrap the following import in a 'try', because
            # we already know 'readline' was imported successfully.
            import rlcompleter
            readline.set_completer(rlcompleter.Completer(imported_objects).complete)
            # Enable tab completion on systems using libedit (e.g. macOS).
            # These lines are copied from Python's Lib/site.py.
            readline_doc = getattr(readline, '__doc__', '')
            if readline_doc is not None and 'libedit' in readline_doc:
                readline.parse_and_bind("bind ^I rl_complete")
            else:
                readline.parse_and_bind("tab:complete")

        # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system
        # conventions and get $PYTHONSTARTUP first then .pythonrc.py.
        if not options['no_startup']:
            for pythonrc in OrderedSet([os.environ.get("PYTHONSTARTUP"), os.path.expanduser('~/.pythonrc.py')]):
                if not pythonrc:
                    continue
                if not os.path.isfile(pythonrc):
                    continue
                with open(pythonrc) as handle:
                    pythonrc_code = handle.read()
                # Match the behavior of the cpython shell where an error in
                # PYTHONSTARTUP prints an exception and continues.
                try:
                    exec(compile(pythonrc_code, pythonrc, 'exec'), imported_objects)
                except Exception:
                    traceback.print_exc()

        code.interact(local=imported_objects)
コード例 #8
0
 def get_constraints(self, cursor, table_name):
     """
     Retrieve any constraints or keys (unique, pk, fk, check, index) across
     one or more columns.
     """
     constraints = {}
     # Get the actual constraint names and columns
     name_query = """
         SELECT kc.`constraint_name`, kc.`column_name`,
             kc.`referenced_table_name`, kc.`referenced_column_name`
         FROM information_schema.key_column_usage AS kc
         WHERE
             kc.table_schema = DATABASE() AND
             kc.table_name = %s
         ORDER BY kc.`ordinal_position`
     """
     cursor.execute(name_query, [table_name])
     for constraint, column, ref_table, ref_column in cursor.fetchall():
         if constraint not in constraints:
             constraints[constraint] = {
                 'columns': OrderedSet(),
                 'primary_key': False,
                 'unique': False,
                 'index': False,
                 'check': False,
                 'foreign_key':
                 (ref_table, ref_column) if ref_column else None,
             }
         constraints[constraint]['columns'].add(column)
     # Now get the constraint types
     type_query = """
         SELECT c.constraint_name, c.constraint_type
         FROM information_schema.table_constraints AS c
         WHERE
             c.table_schema = DATABASE() AND
             c.table_name = %s
     """
     cursor.execute(type_query, [table_name])
     for constraint, kind in cursor.fetchall():
         if kind.lower() == "primary key":
             constraints[constraint]['primary_key'] = True
             constraints[constraint]['unique'] = True
         elif kind.lower() == "unique":
             constraints[constraint]['unique'] = True
     # Now add in the indexes
     cursor.execute("SHOW INDEX FROM %s" %
                    self.connection.ops.quote_name(table_name))
     for table, non_unique, index, colseq, column, type_ in [
             x[:5] + (x[10], ) for x in cursor.fetchall()
     ]:
         if index not in constraints:
             constraints[index] = {
                 'columns': OrderedSet(),
                 'primary_key': False,
                 'unique': False,
                 'check': False,
                 'foreign_key': None,
             }
         constraints[index]['index'] = True
         constraints[index][
             'type'] = Index.suffix if type_ == 'BTREE' else type_.lower()
         constraints[index]['columns'].add(column)
     # Convert the sorted sets to lists
     for constraint in constraints.values():
         constraint['columns'] = list(constraint['columns'])
     return constraints
コード例 #9
0
 def test_bool(self):
     # Refs #23664
     s = OrderedSet()
     self.assertFalse(s)
     s.add(1)
     self.assertTrue(s)