Skip to content

Commit 0afd2b3

Browse files
committed
feat: add shelve module
1 parent 24b2fc6 commit 0afd2b3

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

Diff for: docs/cheatsheet/reading-and-writing-files.md

-43
Original file line numberDiff line numberDiff line change
@@ -67,46 +67,3 @@ You can also iterate through the file line by line:
6767
# Hello world!
6868
# Bacon is not a vegetable.
6969
```
70-
71-
## The shelve module
72-
73-
<base-disclaimer>
74-
<base-disclaimer-title>
75-
From the <a target="_blank" href="https://docs.python.org/3/library/shelve.html">Python 3 documentation</a>
76-
</base-disclaimer-title>
77-
<base-disclaimer-content>
78-
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.
79-
</base-disclaimer-content>
80-
</base-disclaimer>
81-
82-
To save variables:
83-
84-
```python
85-
>>> import shelve
86-
87-
>>> cats = ['Zophie', 'Pooka', 'Simon']
88-
>>> with shelve.open('mydata') as shelf_file:
89-
... shelf_file['cats'] = cats
90-
```
91-
92-
To open and read variables:
93-
94-
```python
95-
>>> with shelve.open('mydata') as shelf_file:
96-
... print(type(shelf_file))
97-
... print(shelf_file['cats'])
98-
...
99-
# <class 'shelve.DbfilenameShelf'>
100-
# ['Zophie', 'Pooka', 'Simon']
101-
```
102-
103-
Just like dictionaries, `shelf` values have `keys()` and `values()` methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the `list()` function to get them in list form.
104-
105-
```python
106-
>>> with shelve.open('mydata') as shelf_file:
107-
... print(list(shelf_file.keys()))
108-
... print(list(shelf_file.values()))
109-
...
110-
# ['cats']
111-
# [['Zophie', 'Pooka', 'Simon']]
112-
```

Diff for: docs/modules/shelve-module.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Python Shelve Module - Python Cheatsheet
3+
description: A “shelf” is a persistent, dictionary-like object. in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle.
4+
---
5+
6+
# Python Shelve Module
7+
8+
<base-disclaimer>
9+
<base-disclaimer-title>
10+
From the <a target="_blank" href="https://docs.python.org/3/library/shelve.html">Python 3 documentation</a>
11+
</base-disclaimer-title>
12+
<base-disclaimer-content>
13+
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.
14+
</base-disclaimer-content>
15+
</base-disclaimer>
16+
17+
## Save variables
18+
19+
```python
20+
>>> import shelve
21+
22+
>>> wife = ['Pretty', 'Lovely', 'Nice']
23+
>>> with shelve.open('mydata') as shelf_file:
24+
... shelf_file['wife'] = wife
25+
```
26+
27+
## Open and read variables:
28+
29+
```python
30+
>>> with shelve.open('mydata') as shelf_file:
31+
... print(type(shelf_file))
32+
... print(shelf_file['wife'])
33+
...
34+
# <class 'shelve.DbfilenameShelf'>
35+
# ['Pretty', 'Lovely', 'Nice']
36+
```
37+
38+
Just like dictionaries, `shelf` values have `keys()` and `values()` methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the `list()` function to get them in list form.
39+
40+
```python
41+
>>> with shelve.open('mydata') as shelf_file:
42+
... print(list(shelf_file.keys()))
43+
... print(list(shelf_file.values()))
44+
...
45+
# ['wife']
46+
# [['Pretty', 'Lovely', 'Nice']]
47+
```

Diff for: src/store/navigation.ts

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ export const useNavigationStore = defineStore('navigation', {
136136
name: 'Pathlib',
137137
path: '/modules/pathlib-module',
138138
},
139+
{
140+
name: 'Shelve',
141+
path: '/modules/shelve-module',
142+
},
139143
{
140144
name: 'Zipfile',
141145
path: '/modules/zipfile-module',

0 commit comments

Comments
 (0)