Skip to content

Commit c241f63

Browse files
committed
feat: add shelve
1 parent bab2746 commit c241f63

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

README-zh-cn.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
**文本处理**: [``string``](#string), [``re``](#re), [``difflib``](#difflib),
4040
[``textwrap``](#textwrap), [``unicodedata``](#unicodedata), [``readline``](#readline)
4141

42-
**二进制数据**: [``codecs``](#codecs), [``struct``](#struct)
42+
**二进制数据服务**: [``codecs``](#codecs), [``struct``](#struct)
4343

4444
**数据类型**: [``datetime``](#datetime), [``zoneinfo``](#zoneinfo), [``calendar``](#calendar),
4545
[``collections``](#collections),[``copy``](#copy), [``pprint``](#pprint),
@@ -55,7 +55,7 @@
5555
[``tempfile``](#tempfile), [``filecmp``](#filecmp), [``fileinput``](#fileinput),
5656
[``shutil``](#shutil), [``linecache``](#linecache)
5757

58-
**数据持久化**: [``pickle``](#pickle), [``copyreg``](#copyreg)
58+
**数据持久化**: [``pickle``](#pickle), [``copyreg``](#copyreg), [``shelve``](#shelve)
5959

6060
**数据压缩**: [``zlib``](#zlib), [``lzma``](#lzma), [``zipfile``](#zipfile)
6161

@@ -948,6 +948,45 @@ pickle A
948948
pickle A
949949
```
950950

951+
## shelve
952+
953+
####
954+
955+
```python
956+
>>> import shelve
957+
>>>
958+
>>> # Open a shelf file (creates if not exists)
959+
>>> with shelve.open('geenrated/mydata.db') as db:
960+
... # Basic operations: set, get, delete, check existence
961+
... db['name'] = 'Alice'
962+
... db['numbers'] = [1, 2, 3]
963+
...
964+
... db['name']
965+
... 'numbers' in db # Check key
966+
... list(db.keys()) # List keys
967+
...
968+
... # Modify mutable object without writeback (requires re-assignment)
969+
... temp = db['numbers'] # Get copy
970+
... temp.append(4) # Modify
971+
... db['numbers'] = temp # Re-assign to persist
972+
...
973+
... del db['name']
974+
...
975+
... # Sync to ensure data is written to disk
976+
... db.sync()
977+
...
978+
Alice
979+
True
980+
['name', 'numbers']
981+
>>>
982+
>>> # Re-open with writeback=True for direct modification
983+
>>> db = shelve.open('mydata.db', writeback = True)
984+
>>> db['numbers'].append(5) # Direct modify (auto-persisted on close)
985+
>>> db['numbers']
986+
[1, 2, 3, 4, 5]
987+
>>> db.close() # Close and sync
988+
```
989+
951990
## zlib
952991

953992
#### compress, decompress

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
4040
**Text Processing**: [``string``](#string), [``re``](#re), [``difflib``](#difflib),
4141
[``textwrap``](#textwrap), [``unicodedata``](#unicodedata), [``readline``](#readline)
4242

43-
**Binary Data**: [``codecs``](#codecs), [``struct``](#struct)
43+
**Binary Data Services**: [``codecs``](#codecs), [``struct``](#struct)
4444

4545
**Data Type**: [``datetime``](#datetime), [``zoneinfo``](#zoneinfo), [``calendar``](#calendar),
4646
[``collections``](#collections),[``copy``](#copy), [``pprint``](#pprint),
@@ -56,7 +56,7 @@ Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
5656
[``tempfile``](#tempfile), [``filecmp``](#filecmp), [``fileinput``](#fileinput),
5757
[``shutil``](#shutil), [``linecache``](#linecache)
5858

59-
**Data Persistence**: [``pickle``](#pickle), [``copyreg``](#copyreg)
59+
**Data Persistence**: [``pickle``](#pickle), [``copyreg``](#copyreg), [``shelve``](#shelve)
6060

6161
**Data Compression**: [``zlib``](#zlib), [``lzma``](#lzma), [``zipfile``](#zipfile)
6262

@@ -949,6 +949,45 @@ pickle A
949949
pickle A
950950
```
951951

952+
## shelve
953+
954+
####
955+
956+
```python
957+
>>> import shelve
958+
>>>
959+
>>> # Open a shelf file (creates if not exists)
960+
>>> with shelve.open('geenrated/mydata.db') as db:
961+
... # Basic operations: set, get, delete, check existence
962+
... db['name'] = 'Alice'
963+
... db['numbers'] = [1, 2, 3]
964+
...
965+
... db['name']
966+
... 'numbers' in db # Check key
967+
... list(db.keys()) # List keys
968+
...
969+
... # Modify mutable object without writeback (requires re-assignment)
970+
... temp = db['numbers'] # Get copy
971+
... temp.append(4) # Modify
972+
... db['numbers'] = temp # Re-assign to persist
973+
...
974+
... del db['name']
975+
...
976+
... # Sync to ensure data is written to disk
977+
... db.sync()
978+
...
979+
Alice
980+
True
981+
['name', 'numbers']
982+
>>>
983+
>>> # Re-open with writeback=True for direct modification
984+
>>> db = shelve.open('mydata.db', writeback = True)
985+
>>> db['numbers'].append(5) # Direct modify (auto-persisted on close)
986+
>>> db['numbers']
987+
[1, 2, 3, 4, 5]
988+
>>> db.close() # Close and sync
989+
```
990+
952991
## zlib
953992

954993
#### compress, decompress

0 commit comments

Comments
 (0)