Custom Query (26791 matches)

Filters
 
Or
 
  
 
Columns

Show under each result:


Results (4 - 6 of 26791)

1 2 3 4 5 6 7 8 9 10 11 12
Ticket Resolution Summary Owner Reporter
#28909 fixed Use unpacking generalizations added in Python 3.5 Nick Pope Nick Pope
Description

Now that master is Python 3.5+ we can look to using unpacking generalizations for dict, list, set and tuple.

https://docs.python.org/3.5/whatsnew/3.5.html#whatsnew-pep-448

One benefit to this is that slow function/method calls can avoided and specific operations for unpacking are used instead:

$ python -c "import dis; dis.dis(\"d0 = {'a': 1}; d1 = {'b': 2}; x = {**d0, **d1}\")"
  1           0 LOAD_CONST               0 ('a')
              2 LOAD_CONST               1 (1)
              4 BUILD_MAP                1
              6 STORE_NAME               0 (d0)
              8 LOAD_CONST               2 ('b')
             10 LOAD_CONST               3 (2)
             12 BUILD_MAP                1
             14 STORE_NAME               1 (d1)
             16 LOAD_NAME                0 (d0)
             18 LOAD_NAME                1 (d1)
             20 BUILD_MAP_UNPACK         2
             22 STORE_NAME               2 (x)
             24 LOAD_CONST               4 (None)
             26 RETURN_VALUE
    
$ python -c "import dis; dis.dis(\"d0 = {'a': 1}; d1 = {'b': 2}; x = dict(d0, **d1)\")"
  1           0 LOAD_CONST               0 ('a')
              2 LOAD_CONST               1 (1)
              4 BUILD_MAP                1
              6 STORE_NAME               0 (d0)
              8 LOAD_CONST               2 ('b')
             10 LOAD_CONST               3 (2)
             12 BUILD_MAP                1
             14 STORE_NAME               1 (d1)
             16 LOAD_NAME                2 (dict)
             18 LOAD_NAME                0 (d0)
             20 BUILD_TUPLE              1
             22 LOAD_NAME                1 (d1)
             24 CALL_FUNCTION_EX         1
             26 STORE_NAME               3 (x)
             28 LOAD_CONST               4 (None)
             30 RETURN_VALUE
    
$ python -c "import dis; dis.dis(\"d0 = {'a': 1}; d1 = {'b': 2}; x = d0.copy(); x.update(d1)\")"
  1           0 LOAD_CONST               0 ('a')
              2 LOAD_CONST               1 (1)
              4 BUILD_MAP                1
              6 STORE_NAME               0 (d0)
              8 LOAD_CONST               2 ('b')
             10 LOAD_CONST               3 (2)
             12 BUILD_MAP                1
             14 STORE_NAME               1 (d1)
             16 LOAD_NAME                0 (d0)
             18 LOAD_ATTR                2 (copy)
             20 CALL_FUNCTION            0
             22 STORE_NAME               3 (x)
             24 LOAD_NAME                3 (x)
             26 LOAD_ATTR                4 (update)
             28 LOAD_NAME                1 (d1)
             30 CALL_FUNCTION            1
             32 POP_TOP
             34 LOAD_CONST               4 (None)
             36 RETURN_VALUE
    
$ python -m timeit "d0 = {'a': 1}; d1 = {'b': 2}; x = {**d0, **d1}"
1000000 loops, best of 3: 0.24 usec per loop
    
$ python -m timeit "d0 = {'a': 1}; d1 = {'b': 2}; x = dict(d0, **d1)"
1000000 loops, best of 3: 0.39 usec per loop
    
$ python -m timeit "d0 = {'a': 1}; d1 = {'b': 2}; x = d0.copy(); x.update(d1)"
1000000 loops, best of 3: 0.45 usec per loop

Obviously these are fairly contrived examples, and in many cases there can be multiple calls to methods such as dict.update() or list.extend().

Here are examples of a number of changes that could be made:

# Unpack directly into initial definition:
-d = {...}
-d.update(extra)
+d = {..., **extra}

# Unpack instead of using dict() to combine (#1):
-d = dict(original, **extra)
+d = {**original, **extra}

# Unpack instead of using dict() to combine (#2):
-d = dict(original, ...)
+d = {**original, ...}

# Unpacking peforms a shallow copy like dict():
-d = dict(original)
-d.update(extra)
+d = {**original, **extra}

# Unpacking peforms a shallow copy like dict.copy():
-d = original.copy()
-d.update(extra)
+d = {**original, **extra}

# Unpacking peforms a shallow copy like copy.copy():
-import copy
-d = copy.copy(original)
-d.update(extra)
+d = {**original, **extra}

# More complex examples that become simple:
-d = dict(original, ...)
-d.update(extra or {})
+d = {**original, ... **(extra or {})}

# Unpacking for sets also makes things simple:
-s = {...}
-s.update(extra)
-s.update(override)
+s = {..., *extra, *override}

# Unpacking for lists also makes things simple:
-l = [...]
-l.extend(extra)
-l.extend(override)
+l = [..., *extra, *override]
#28907 fixed Exploit that iterating over empty sequences is noop nobody Дилян Палаузов
Description

.. and don't check if a sequence is empty, before looping over it

#28906 fixed Reduce calls to bool() nobody Дилян Палаузов
Description

Compared to the attached patch, these changes might be more controversial:

diff --git a/django/contrib/gis/db/backends/spatialite/features.py b/django/contrib/gis/db/backends/spatialite/features
--- a/django/contrib/gis/db/backends/spatialite/features.py
+++ b/django/contrib/gis/db/backends/spatialite/features.py
@@ -10,4 +10,4 @@ class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures):
 
     @cached_property
     def supports_area_geodetic(self):
-        return bool(self.connection.ops.lwgeom_version())
+        return self.connection.ops.lwgeom_version()
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -51,7 +51,7 @@ class SQLCompiler:
         order_by = self.get_order_by()
         self.where, self.having = self.query.where.split_having()
         extra_select = self.get_extra_select(order_by, self.select)
-        self.has_extra_select = bool(extra_select)
+        self.has_extra_select = extra_select
         group_by = self.get_group_by(self.select + extra_select, order_by)
         return extra_select, order_by, group_by
 @@ -1216,11 +1216,10 @@ class SQLInsertCompiler(SQLCompiler):
         insert_statement = self.connection.ops.insert_statement(on_conflict=self.query.on_conflict)
         result = ['%s %s' % (insert_statement, qn(opts.db_table))]
 
-        has_fields = bool(self.query.fields)
-        fields = self.query.fields if has_fields else [opts.pk]
+        fields = self.query.fields or [opts.pk]
         result.append('(%s)' % ', '.join(qn(f.column) for f in fields))
 
-        if has_fields:
+        if self.query.fields:
             value_rows = [
                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
                 for obj in self.query.objs
1 2 3 4 5 6 7 8 9 10 11 12
Note: See TracQuery for help on using queries.
Back to Top