Qfp!dZddlmZmZmZddlmZddlZgdZ GddejjZ Gdd ejjZ Gd d e ZGd d e ZddZy)a Manage shelves of pickled objects. A "shelf" is a persistent, dictionary-like object. The difference with dbm databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects -- anything that the "pickle" module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings. To summarize the interface (key is a string, data is an arbitrary object): import shelve d = shelve.open(filename) # open, with (g)dbm filename -- no suffix d[key] = data # store data at key (overwrites old data if # using an existing key) data = d[key] # retrieve a COPY of the data at key (raise # KeyError if no such key) -- NOTE that this # access returns a *copy* of the entry! del d[key] # delete data stored at key (raises KeyError # if no such key) flag = key in d # true if the key exists list = d.keys() # a list of all existing keys (slow!) d.close() # close it Dependent on the implementation, closing a persistent dictionary may or may not be necessary to flush changes to disk. Normally, d[key] returns a COPY of the entry. This needs care when mutable entries are mutated: for example, if d[key] is a list, d[key].append(anitem) does NOT modify the entry d[key] itself, as stored in the persistent mapping -- it only modifies the copy, which is then immediately discarded, so that the append has NO effect whatsoever. To append an item to d[key] in a way that will affect the persistent mapping, use: data = d[key] data.append(anitem) d[key] = data To avoid the problem with mutable entries, you may pass the keyword argument writeback=True in the call to shelve.open. When you use: d = shelve.open(filename, writeback=True) then d keeps a cache of all entries you access, and writes them all back to the persistent mapping when you call d.close(). This ensures that such usage as d[key].append(anitem) works as intended. However, using keyword argument writeback=True may consume vast amount of memory for the cache, and it may make d.close() very slow, if you access many of d's entries after opening it in this way: d has no way to check which of the entries you access are mutable and/or which ones you actually mutate, so it must cache, and write back at close, all of the entries that you access. You can call d.sync() to write back all the entries in the cache, and empty the cache (d.sync() also synchronizes the persistent dictionary on disk, if feasible). )DEFAULT_PROTOCOLPickler Unpickler)BytesION)Shelf BsdDbShelfDbfilenameShelfopenc4eZdZdZdZexZxZxZxZxZ Z dZ y) _ClosedDictz>Marker for a closed dict. Access attempts raise a ValueError.ctd)Nz!invalid operation on closed shelf) ValueError)selfargss -/opt/alt/python312/lib64/python3.12/shelve.pyclosedz_ClosedDict.closedEs<==cy)Nzrs r__repr__z_ClosedDict.__repr__Is$rN) __name__ __module__ __qualname____doc__r__iter____len__ __getitem__ __setitem__ __delitem__keysrrrrr r Bs-D>JPPHPwPP{P[4%rr cfeZdZdZ ddZdZdZdZddZdZ d Z d Z d Z d Z d ZdZdZy)rzBase class for shelf implementations. This is initialized with a dictionary-like object. See the module's __doc__ string for an overview of the interface. NcZ||_|t}||_||_i|_||_yN)dictr _protocol writebackcache keyencodingrr%protocolr'r)s r__init__zShelf.__init__Ts1  'H!" &rc#K|jjD]}|j|j!ywr$)r%r!decoder))rks rrzShelf.__iter__^s0!A((4++, ,"s>Ac,t|jSr$)lenr%rs rrz Shelf.__len__bs499~rcP|j|j|jvSr$encoder)r%rkeys r __contains__zShelf.__contains__es zz$**+tyy88rc^|j|j|jvr||S|Sr$r3)rr6defaults rgetz Shelf.geths, ::d&& '499 49 rc |j|}|S#t$rit|j|j |j }t |j}|jr||j|<Y|SwxYwr$) r(KeyErrorrr%r4r)rloadr'rr6valuefs rrzShelf.__getitem__msz (JJsOE   ( #**T-=-=">?@AaL%%'E~~"' 3  (sA.BBc|jr||j|<t}t||j}|j ||j |j|j|j<yr$) r'r(rrr&dumpgetvaluer%r4r))rr6r?r@ps rrzShelf.__setitem__ws[ >>#DJJsO I At~~ & u 23**, #**T--./rc|j|j|j= |j|=y#t$rYywxYwr$)r%r4r)r(r<r5s rr zShelf.__delitem__s? IIcjj!1!12 3  3   s 6 AAc|Sr$rrs r __enter__zShelf.__enter__s rc$|jyr$)close)rtyper? tracebacks r__exit__zShelf.__exit__s  rc|jy |j |jj t |_y#t$rYwxYw#d|_YyxYw# t |_w#d|_YwxYwxYwr$)r%syncrIAttributeErrorr rs rrIz Shelf.closes{ 99   ! IIK  !  !'M "   !   !'M  !  sKA(A A AA(AA( A%(B*A:9B: BBc>t|dsy|jy)Nr')hasattrrIrs r__del__z Shelf.__del__st[)  rc|jrH|jr>djj"DN"jj..0 U!S 1!DNDJ 499f % IINN  &rNFzutf-8r$)rrrrr,rrr7r:rrr rGrLrIrRrNrrrrrMsP 7<$'-9 ?!"rrc:eZdZdZ d dZdZdZdZdZdZ y) raShelf implementation using the "BSD" db interface. This adds methods first(), next(), previous(), last() and set_location() that have no counterpart in [g]dbm databases. The actual database must be opened using one of the "bsddb" modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or bsddb.rnopen) and passed to the constructor. See the module's __doc__ string for an overview of the interface. Nc6tj|||||yr$)rr,r*s rr,zBsdDbShelf.__init__s tT8Y Drc|jj|\}}t|}|j|jt |j fSr$)r% set_locationrr.r)rr=r>s rrZzBsdDbShelf.set_locationsJyy--c2 e EN 4++,il.?.?.ABBrct|j\}}t|}|j|jt |j fSr$)nextr%rr.r)rr=r>s rr\zBsdDbShelf.nextsCDII e EN 4++,il.?.?.ABBrc|jj\}}t|}|j|jt |j fSr$)r%previousrr.r)rr=r>s rr^zBsdDbShelf.previoussHyy))+ e EN 4++,il.?.?.ABBrc|jj\}}t|}|j|jt |j fSr$)r%firstrr.r)rr=r>s rr`zBsdDbShelf.firstsFyy( e EN 4++,il.?.?.ABBrc|jj\}}t|}|j|jt |j fSr$)r%lastrr.r)rr=r>s rrbzBsdDbShelf.lastsFyy~~' e EN 4++,il.?.?.ABBrrV) rrrrr,rZr\r^r`rbrrrrrs3 7<$EC C C C CrrceZdZdZddZy)r zShelf implementation using the "dbm" generic dbm interface. This is initialized with the filename for the dbm database. See the module's __doc__ string for an overview of the interface. Nc\ddl}tj||j||||y)Nr)dbmrr,r )rfilenameflagr+r'res rr,zDbfilenameShelf.__init__s" tSXXh5xKrcNF)rrrrr,rrrr r s  Lrr ct||||S)aOpen a persistent dictionary for reading and writing. The filename parameter is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created. The optional flag parameter has the same interpretation as the flag parameter of dbm.open(). The optional protocol parameter specifies the version of the pickle protocol. See the module's __doc__ string for an overview of the interface. )r )rfrgr+r's rr r s 8T8Y ??rrh)rpicklerrriorcollections.abc collections__all__abcMutableMappingr rrr r rrrrrsp8t87 <%+//00%_KOO * *_D(C(CV Le L @r