Python copy.extra() Examples

The following are 30 code examples for showing how to use copy.extra(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module copy , or try the search function .

Example 1
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 6 votes vote down vote up
def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        sql_parts = []
        params = []
        for arg in self.source_expressions:
            arg_sql, arg_params = compiler.compile(arg)
            sql_parts.append(arg_sql)
            params.extend(arg_params)
        data = self.extra.copy()
        data.update(**extra_context)
        # Use the first supplied value in this order: the parameter to this
        # method, a value supplied in __init__()'s **extra (the value in
        # `data`), or the value defined on the class.
        if function is not None:
            data['function'] = function
        else:
            data.setdefault('function', self.function)
        template = template or data.get('template', self.template)
        arg_joiner = arg_joiner or data.get('arg_joiner', self.arg_joiner)
        data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
        return template % data, params 
Example 2
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 6 votes vote down vote up
def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        sql_parts = []
        params = []
        for arg in self.source_expressions:
            arg_sql, arg_params = compiler.compile(arg)
            sql_parts.append(arg_sql)
            params.extend(arg_params)
        data = {**self.extra, **extra_context}
        # Use the first supplied value in this order: the parameter to this
        # method, a value supplied in __init__()'s **extra (the value in
        # `data`), or the value defined on the class.
        if function is not None:
            data['function'] = function
        else:
            data.setdefault('function', self.function)
        template = template or data.get('template', self.template)
        arg_joiner = arg_joiner or data.get('arg_joiner', self.arg_joiner)
        data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
        return template % data, params 
Example 3
Project: python   Author: Yeah-Kun   File: expressions.py    License: Apache License 2.0 6 votes vote down vote up
def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        sql_parts = []
        params = []
        for arg in self.source_expressions:
            arg_sql, arg_params = compiler.compile(arg)
            sql_parts.append(arg_sql)
            params.extend(arg_params)
        data = self.extra.copy()
        data.update(**extra_context)
        # Use the first supplied value in this order: the parameter to this
        # method, a value supplied in __init__()'s **extra (the value in
        # `data`), or the value defined on the class.
        if function is not None:
            data['function'] = function
        else:
            data.setdefault('function', self.function)
        template = template or data.get('template', self.template)
        arg_joiner = arg_joiner or data.get('arg_joiner', self.arg_joiner)
        data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
        return template % data, params 
Example 4
Project: python2017   Author: bpgc-cte   File: expressions.py    License: MIT License 6 votes vote down vote up
def as_sql(self, compiler, connection, function=None, template=None, arg_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        sql_parts = []
        params = []
        for arg in self.source_expressions:
            arg_sql, arg_params = compiler.compile(arg)
            sql_parts.append(arg_sql)
            params.extend(arg_params)
        data = self.extra.copy()
        data.update(**extra_context)
        # Use the first supplied value in this order: the parameter to this
        # method, a value supplied in __init__()'s **extra (the value in
        # `data`), or the value defined on the class.
        if function is not None:
            data['function'] = function
        else:
            data.setdefault('function', self.function)
        template = template or data.get('template', self.template)
        arg_joiner = arg_joiner or data.get('arg_joiner', self.arg_joiner)
        data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
        return template % data, params 
Example 5
Project: GTDWeb   Author: lanbing510   File: expressions.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *expressions, **extra):
        output_field = extra.pop('output_field', None)
        super(Func, self).__init__(output_field=output_field)
        self.source_expressions = self._parse_expressions(*expressions)
        self.extra = extra 
Example 6
Project: GTDWeb   Author: lanbing510   File: expressions.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __repr__(self):
        args = self.arg_joiner.join(str(arg) for arg in self.source_expressions)
        extra = ', '.join(str(key) + '=' + str(val) for key, val in self.extra.items())
        if extra:
            return "{}({}, {})".format(self.__class__.__name__, args, extra)
        return "{}({})".format(self.__class__.__name__, args) 
Example 7
Project: GTDWeb   Author: lanbing510   File: expressions.py    License: GNU General Public License v2.0 5 votes vote down vote up
def as_sql(self, compiler, connection, function=None, template=None):
        connection.ops.check_expression_support(self)
        sql_parts = []
        params = []
        for arg in self.source_expressions:
            arg_sql, arg_params = compiler.compile(arg)
            sql_parts.append(arg_sql)
            params.extend(arg_params)
        if function is None:
            self.extra['function'] = self.extra.get('function', self.function)
        else:
            self.extra['function'] = function
        self.extra['expressions'] = self.extra['field'] = self.arg_joiner.join(sql_parts)
        template = template or self.extra.get('template', self.template)
        return template % self.extra, params 
Example 8
Project: GTDWeb   Author: lanbing510   File: expressions.py    License: GNU General Public License v2.0 5 votes vote down vote up
def copy(self):
        copy = super(Func, self).copy()
        copy.source_expressions = self.source_expressions[:]
        copy.extra = self.extra.copy()
        return copy 
Example 9
Project: GTDWeb   Author: lanbing510   File: expressions.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *cases, **extra):
        if not all(isinstance(case, When) for case in cases):
            raise TypeError("Positional arguments must all be When objects.")
        default = extra.pop('default', None)
        output_field = extra.pop('output_field', None)
        super(Case, self).__init__(output_field)
        self.cases = list(cases)
        self.default = self._parse_expressions(default)[0] 
Example 10
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, *expressions, output_field=None, **extra):
        if self.arity is not None and len(expressions) != self.arity:
            raise TypeError(
                "'%s' takes exactly %s %s (%s given)" % (
                    self.__class__.__name__,
                    self.arity,
                    "argument" if self.arity == 1 else "arguments",
                    len(expressions),
                )
            )
        super().__init__(output_field=output_field)
        self.source_expressions = self._parse_expressions(*expressions)
        self.extra = extra 
Example 11
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def __repr__(self):
        args = self.arg_joiner.join(str(arg) for arg in self.source_expressions)
        extra = dict(self.extra, **self._get_repr_options())
        if extra:
            extra = ', '.join(str(key) + '=' + str(val) for key, val in sorted(extra.items()))
            return "{}({}, {})".format(self.__class__.__name__, args, extra)
        return "{}({})".format(self.__class__.__name__, args) 
Example 12
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def _get_repr_options(self):
        """Return a dict of extra __init__() options to include in the repr."""
        return {} 
Example 13
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def copy(self):
        copy = super().copy()
        copy.source_expressions = self.source_expressions[:]
        copy.extra = self.extra.copy()
        return copy 
Example 14
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, *cases, default=None, output_field=None, **extra):
        if not all(isinstance(case, When) for case in cases):
            raise TypeError("Positional arguments must all be When objects.")
        super().__init__(output_field)
        self.cases = list(cases)
        self.default = self._parse_expressions(default)[0]
        self.extra = extra 
Example 15
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def as_sql(self, compiler, connection, template=None, case_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        if not self.cases:
            return compiler.compile(self.default)
        template_params = self.extra.copy()
        template_params.update(extra_context)
        case_parts = []
        sql_params = []
        for case in self.cases:
            try:
                case_sql, case_params = compiler.compile(case)
            except EmptyResultSet:
                continue
            case_parts.append(case_sql)
            sql_params.extend(case_params)
        default_sql, default_params = compiler.compile(self.default)
        if not case_parts:
            return default_sql, default_params
        case_joiner = case_joiner or self.case_joiner
        template_params['cases'] = case_joiner.join(case_parts)
        template_params['default'] = default_sql
        sql_params.extend(default_params)
        template = template or template_params.get('template', self.template)
        sql = template % template_params
        if self._output_field_or_none is not None:
            sql = connection.ops.unification_cast_sql(self.output_field) % sql
        return sql, sql_params 
Example 16
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, queryset, output_field=None, **extra):
        self.queryset = queryset
        self.extra = extra
        if output_field is None and len(self.queryset.query.select) == 1:
            output_field = self.queryset.query.select[0].field
        super().__init__(output_field) 
Example 17
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def as_sql(self, compiler, connection, template=None, **extra_context):
        connection.ops.check_expression_support(self)
        template_params = self.extra.copy()
        template_params.update(extra_context)
        template_params['subquery'], sql_params = self.queryset.query.get_compiler(connection=connection).as_sql()

        template = template or template_params.get('template', self.template)
        sql = template % template_params
        return sql, sql_params 
Example 18
Project: bioforum   Author: reBiocoder   File: expressions.py    License: MIT License 5 votes vote down vote up
def __invert__(self):
        return type(self)(self.queryset, negated=(not self.negated), **self.extra) 
Example 19
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, *expressions, output_field=None, **extra):
        if self.arity is not None and len(expressions) != self.arity:
            raise TypeError(
                "'%s' takes exactly %s %s (%s given)" % (
                    self.__class__.__name__,
                    self.arity,
                    "argument" if self.arity == 1 else "arguments",
                    len(expressions),
                )
            )
        super().__init__(output_field=output_field)
        self.source_expressions = self._parse_expressions(*expressions)
        self.extra = extra 
Example 20
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def __repr__(self):
        args = self.arg_joiner.join(str(arg) for arg in self.source_expressions)
        extra = {**self.extra, **self._get_repr_options()}
        if extra:
            extra = ', '.join(str(key) + '=' + str(val) for key, val in sorted(extra.items()))
            return "{}({}, {})".format(self.__class__.__name__, args, extra)
        return "{}({})".format(self.__class__.__name__, args) 
Example 21
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def _get_repr_options(self):
        """Return a dict of extra __init__() options to include in the repr."""
        return {} 
Example 22
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def copy(self):
        copy = super().copy()
        copy.source_expressions = self.source_expressions[:]
        copy.extra = self.extra.copy()
        return copy 
Example 23
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, *cases, default=None, output_field=None, **extra):
        if not all(isinstance(case, When) for case in cases):
            raise TypeError("Positional arguments must all be When objects.")
        super().__init__(output_field)
        self.cases = list(cases)
        self.default = self._parse_expressions(default)[0]
        self.extra = extra 
Example 24
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def as_sql(self, compiler, connection, template=None, case_joiner=None, **extra_context):
        connection.ops.check_expression_support(self)
        if not self.cases:
            return compiler.compile(self.default)
        template_params = {**self.extra, **extra_context}
        case_parts = []
        sql_params = []
        for case in self.cases:
            try:
                case_sql, case_params = compiler.compile(case)
            except EmptyResultSet:
                continue
            case_parts.append(case_sql)
            sql_params.extend(case_params)
        default_sql, default_params = compiler.compile(self.default)
        if not case_parts:
            return default_sql, default_params
        case_joiner = case_joiner or self.case_joiner
        template_params['cases'] = case_joiner.join(case_parts)
        template_params['default'] = default_sql
        sql_params.extend(default_params)
        template = template or template_params.get('template', self.template)
        sql = template % template_params
        if self._output_field_or_none is not None:
            sql = connection.ops.unification_cast_sql(self.output_field) % sql
        return sql, sql_params 
Example 25
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def __init__(self, queryset, output_field=None, **extra):
        self.queryset = queryset
        self.extra = extra
        super().__init__(output_field) 
Example 26
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def as_sql(self, compiler, connection, template=None, **extra_context):
        connection.ops.check_expression_support(self)
        template_params = {**self.extra, **extra_context}
        template_params['subquery'], sql_params = self.queryset.query.get_compiler(connection=connection).as_sql()

        template = template or template_params.get('template', self.template)
        sql = template % template_params
        return sql, sql_params 
Example 27
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: expressions.py    License: MIT License 5 votes vote down vote up
def __invert__(self):
        return type(self)(self.queryset, negated=(not self.negated), **self.extra) 
Example 28
Project: python   Author: Yeah-Kun   File: expressions.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, *expressions, **extra):
        if self.arity is not None and len(expressions) != self.arity:
            raise TypeError(
                "'%s' takes exactly %s %s (%s given)" % (
                    self.__class__.__name__,
                    self.arity,
                    "argument" if self.arity == 1 else "arguments",
                    len(expressions),
                )
            )
        output_field = extra.pop('output_field', None)
        super(Func, self).__init__(output_field=output_field)
        self.source_expressions = self._parse_expressions(*expressions)
        self.extra = extra 
Example 29
Project: python   Author: Yeah-Kun   File: expressions.py    License: Apache License 2.0 5 votes vote down vote up
def __repr__(self):
        args = self.arg_joiner.join(str(arg) for arg in self.source_expressions)
        extra = ', '.join(str(key) + '=' + str(val) for key, val in self.extra.items())
        if extra:
            return "{}({}, {})".format(self.__class__.__name__, args, extra)
        return "{}({})".format(self.__class__.__name__, args) 
Example 30
Project: python   Author: Yeah-Kun   File: expressions.py    License: Apache License 2.0 5 votes vote down vote up
def copy(self):
        copy = super(Func, self).copy()
        copy.source_expressions = self.source_expressions[:]
        copy.extra = self.extra.copy()
        return copy