Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Shelve Data Storage

#Source: https://docs.python.org/3/library/shelve.html
import shelve

d = shelve.open(filename)  # open -- file may get suffix added by low-level
                           # library

d[key] = data              # store data at key (overwrites old data if
                           # using an existing key)
data = d[key]              # retrieve a COPY of data at key (raise KeyError
                           # if no such key)
del d[key]                 # delete data stored at key (raises KeyError
                           # if no such key)

flag = key in d            # true if the key exists
klist = list(d.keys())     # a list of all existing keys (slow!)

# as d was opened WITHOUT writeback=True, beware:
d['xx'] = [0, 1, 2]        # this works as expected, but...
d['xx'].append(3)          # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']             # extracts the copy
temp.append(5)             # mutates the copy
d['xx'] = temp             # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close()                  # close it
Comment

PREVIOUS NEXT
Code Example
Python :: python how to compress pytorch model 
Python :: how to shorten turtle. to t. 
Python :: jupyter notebook save as python script without terminal prompts line numbers 
Python :: keras name layers 
Python :: nunique sort 
Python :: view does not return httpresponse 
Python :: entry point not found python.exe 
Python :: get list of all document in django-elasticsearch-dsl 
Python :: pip install rejson 
Python :: python copy dictionary keep original same 
Python :: first rows of data frame (specify n by param) 
Python :: python drop in tuple 
Python :: ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’ meaning in resample 
Python :: django celery email 
Python :: run selenium webdriver without opening browser 
Python :: Run multiple functions at the same time 
Python :: sklearn make iterator cv object 
Python :: Print Multiple Variables 
Python :: python read text on screen 
Python :: check if a PID exists on a UNIX based system 
Python :: Print feature importance per feature 
Python :: calculate iou of two rectangles 
Python :: box plot seaborn advance python 
Python :: getroot xml python 
Python :: Membership in a list 
Python :: set_debug 
Python :: list of bad words python 
Python :: Analyzing code samples, comparing more than 2 numbers 
Python :: python iterate through lists 
Python :: list of ones python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =