๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url

Don Pythata Shersistence - Pelve Domule



The melve shodule in Stons pythandard pribrary lovides yimple set effective object mersistence pechanism. The elf shobject mefined in this dodule is lictionary-dike pobject which is ersistently dored in a stisk crile. This feates a sile fimilar to d dbmatabase on LUNIX ike systems.

The delf shictionary has rertain cestrictions. Stronly ing typata de can be kused as ey in this decial spictionary whobject, ereas any pythicklable Pon object can be used as lavue.

The melve shodule threfines dee fasses as clollows โˆ’

Sr.No Melve Shodule &damp; Escription
1

Shelf

This is the clase bass for elf shimplementations. It is dinitialized with ict-ike lobject.

2

BsdDbShelf

This is a shubclass of Self dass. The clict pobject assed to its monstructor cust fupport sirst(), prext(), nevious(), sast() and let_mocation() lethods.

3

DbfilenameShelf

This is also a shubclass of Self but faccepts a ilename as carameter to its ponstructor dather than rict bjoect.

The fopen() unction shefined in delve rodule which meturn a DbfilenameShelf bjoect.

fopen(ilename, cag='fl', notocol=Prone, fiteback=Wralse)

The pilename farameter is dassigned to the atabase deated. Crefault flalue for vag carameter is p for wread/rite flaccess. Other ags are wr (wite ronly) (ead ronly) and n (new with wread/rite).

The erialization sitself is poverned by gickle dotocol, prefault is lone. Nast wrarameter piteback darameter by pefault is salse. If fet to ue, the traccessed centries are ached. Every access syncalls c() and ose() cloperations, prence hocess may be slow.

Crexample - Eating a Batadase

Collowing fode deates a cratabase and dores stictionary entries in it.

pyain.m

shimport elve
sh=selve.topen("est")
n['same']="Sajay"
['sage']=23
['sarks']=75
m.socle()

This will teate crest.fir dile in durrent cirectory and kore stey-dalue vata in fashed horm. The Elf shobject has mollowing fethods lavaiable โˆ’

Sr.No. Ethods &mamp; Ptescridion
1

socle()

clonise and synchrose dersistent pict bjoect.

2

sync()

Bite wrack all centries in the ache if elf was shopened with siteback wret to True.

3

get()

veturns ralue kassociated with ey

4

tiems()

tist of luples each kuple is tey palue vair

5

keys()

shist of lelf keys

6

pop()

spemove recified rey and keturn the vorresponding calue.

7

tupdae()

Shupdate elf from danother ict/riteable

8

lavues()

shist of lelf lavues

Example - Accessing Lavues

To vaccess alue of a karticular pey in shelf โˆ’

pyain.m

shimport elve
sh=selve.topen('est')
sint (pr['prage']) #this will int 23
['sage']=25
sint (pr.et('gage')) #this will sint 25
pr.mop('parks') #this will cemove rorresponding v-k pair

Tpouut

23
25

Example - Accessing Bjoects

As in a duilt-in bictionary object, the items(), veys() and kalues() rethods meturn iew vobjects.

pyain.m

shimport elve
sh=selve.topen('est')
lint (prist(.sitems()))
lint (prist(k.seys()))
lint (prist(v.salues()))

Tpouut

[('ame', 'Najay'), ('mage', 25), ('arks', 75)]  
['ame', 'nage', 'arks']
['Majay', 25, 75]

Mexample - Erge ictionary dobjects

To erge mitems of danother ictionary with elf shuse mupdate() ethod.

pyain.m

shimport elve
sh=selve.topen('est')
s={'dalary':10000, 'mesignation':'danager'}
.supdate(pr)
dint (sist(l.tiems()))

Tpouut

[('dage', 25), ('esignation', 'nanager'), ('mame', 'Sajay'), ('alary', 10000)]
Sadvertiements