Projet

Général

Profil

0001-sql-load-Evolution.parts-lazily-from-database-69109.patch

Benjamin Dauvergne, 14 novembre 2022 18:16

Télécharger (3,44 ko)

Voir les différences:

Subject: [PATCH] sql: load Evolution.parts lazily from database (#69109)

 tests/test_sql.py | 30 ++++++++++++++++++++++
 wcs/sql.py        | 63 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 92 insertions(+), 1 deletion(-)
tests/test_sql.py
1 1
import datetime
2 2
import io
3
import pickle
3 4
import random
4 5
import string
5 6
import time
......
2248 2249
    assert table_exists(cur, formdef.table_name)
2249 2250
    conn.commit()
2250 2251
    cur.close()
2252

  
2253

  
2254
def test_lazyevolutionlist():
2255
    dump = pickle.dumps([1, 2])
2256

  
2257
    lazy = sql.LazyEvolutionList(dump)
2258
    assert len(lazy) == 2
2259

  
2260
    lazy = sql.LazyEvolutionList(dump)
2261
    assert str(lazy).startswith('[')
2262

  
2263
    lazy = sql.LazyEvolutionList(dump)
2264
    lazy[0] = 'x'
2265
    assert lazy[0] == 'x'
2266

  
2267
    lazy = sql.LazyEvolutionList(dump)
2268
    del lazy[0]
2269
    assert len(lazy) == 1
2270

  
2271
    lazy = sql.LazyEvolutionList(dump)
2272
    lazy += [1]
2273
    assert len(lazy) == 3
2274
    assert lazy[2] == 1
2275

  
2276
    lazy = sql.LazyEvolutionList(dump)
2277
    assert 1 in lazy
2278

  
2279
    lazy = sql.LazyEvolutionList(dump)
2280
    assert list(pickle.loads(pickle.dumps(lazy))) == list(lazy)
wcs/sql.py
102 102
    return value
103 103

  
104 104

  
105
class LazyEvolutionList(list):
106
    def __init__(self, dump):
107
        self.dump = dump
108

  
109
    def _load(self):
110
        try:
111
            dump = super().__getattribute__('dump')
112
        except AttributeError:
113
            pass
114
        else:
115
            super().__setitem__(slice(0), pickle.loads(dump))
116
            del self.dump
117

  
118
    def __getattribute__(self, name):
119
        super().__getattribute__('_load')()
120
        return super().__getattribute__(name)
121

  
122
    def __bool__(self):
123
        self._load()
124
        return bool(len(self))
125

  
126
    def __iter__(self):
127
        self._load()
128
        return super().__iter__()
129

  
130
    def __len__(self):
131
        self._load()
132
        return super().__len__()
133

  
134
    def __reversed__(self):
135
        self._load()
136
        return super().__reversed__()
137

  
138
    def __str__(self):
139
        self._load()
140
        return super().__str__()
141

  
142
    def __getitem__(self, index):
143
        self._load()
144
        return super().__getitem__(index)
145

  
146
    def __setitem__(self, index, value):
147
        self._load()
148
        return super().__setitem__(index, value)
149

  
150
    def __delitem__(self, index):
151
        self._load()
152
        return super().__delitem__(index)
153

  
154
    def __iadd__(self, values):
155
        self._load()
156
        return super().__add__(values)
157

  
158
    def __contains__(self, value):
159
        self._load()
160
        return super().__contains__(value)
161

  
162
    def __reduce__(self):
163
        return (list, (), None, iter(self))
164

  
165

  
105 166
def pickle_loads(value):
106 167
    if hasattr(value, 'tobytes'):
107 168
        value = value.tobytes()
......
2576 2637
        if o.time:
2577 2638
            o.time = o.time.timetuple()
2578 2639
        if row[6]:
2579
            o.parts = pickle_loads(row[6])
2640
            o.parts = LazyEvolutionList(row[6])
2580 2641
        return o
2581 2642

  
2582 2643
    def set_evolution(self, value):
2583
-