Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c68c61

Browse files
authored
Merge pull requestwilfredinni#132 from wilfredinni/next
2022-10-02: zipfile and shelve modules
2 parents6178fb6 +471d8f6 commit2c68c61

File tree

7 files changed

+173
-100
lines changed

7 files changed

+173
-100
lines changed

‎.vscode/settings.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"IGNORECASE",
6161
"imag",
6262
"importlib",
63+
"infolist",
6364
"isabs",
6465
"isalpha",
6566
"isdecimal",

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ for a regex, [slice a list](https://www.pythoncheatsheet.org/cheatsheet/lists-an
3131
|[Functions](/docs/cheatsheet/functions.md)|[Random Module](/docs/modules/random-module.md)|
3232
|[Lists and Tuples](/docs/cheatsheet/lists-and-tuples.md)|[Os Module](/docs/modules/os-module.md)|
3333
|[Dictionaries](/docs/cheatsheet/dictionaries.md)|[Pathlib Module](/docs/modules/pathlib-module.md)|
34-
|[Sets](/docs/cheatsheet/sets.md)||
35-
|[Comprehensions](/docs/cheatsheet/comprehensions.md)||
34+
|[Sets](/docs/cheatsheet/sets.md)|[Shelve Module](/docs/modules/shelve-module.md)|
35+
|[Comprehensions](/docs/cheatsheet/comprehensions.md)|[Zipfile Module](/docs/modules/zipfile-module.md)|
3636
|[Manipulating Strings](/docs/cheatsheet/manipulating-strings.md)||
3737
|[String Formatting](/docs/cheatsheet/string-formatting.md)||
3838
|[Regular Expressions](/docs/cheatsheet/regular-expressions.md)||

‎docs/cheatsheet/reading-and-writing-files.md‎

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -67,100 +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-
```
113-
114-
##Reading ZIP files
115-
116-
```python
117-
>>>import zipfile, os
118-
119-
>>> os.chdir('C:\\')# move to the folder with example.zip
120-
>>>with zipfile.ZipFile('example.zip')as example_zip:
121-
...print(example_zip.namelist())
122-
... spam_info= example_zip.getinfo('spam.txt')
123-
...print(spam_info.file_size)
124-
...print(spam_info.compress_size)
125-
...print('Compressed file is%sx smaller!'% (round(spam_info.file_size/ spam_info.compress_size,2)))
126-
...
127-
# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
128-
# 13908
129-
# 3828
130-
# 'Compressed file is 3.63x smaller!'
131-
```
132-
133-
##Extracting from ZIP Files
134-
135-
The`extractall()` method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
136-
137-
```python
138-
>>>import zipfile, os
139-
140-
>>> os.chdir('C:\\')# move to the folder with example.zip
141-
142-
>>>with zipfile.ZipFile('example.zip')as example_zip:
143-
... example_zip.extractall()
144-
```
145-
146-
The`extract()` method for ZipFile objects will extract a single file from the ZIP file:
147-
148-
```python
149-
>>>with zipfile.ZipFile('example.zip')as example_zip:
150-
...print(example_zip.extract('spam.txt'))
151-
...print(example_zip.extract('spam.txt','C:\\some\\new\\folders'))
152-
...
153-
# 'C:\\spam.txt'
154-
# 'C:\\some\\new\\folders\\spam.txt'
155-
```
156-
157-
##Creating and Adding to ZIP Files
158-
159-
```python
160-
>>>import zipfile
161-
162-
>>>with zipfile.ZipFile('new.zip','w')as new_zip:
163-
... new_zip.write('spam.txt',compress_type=zipfile.ZIP_DEFLATED)
164-
```
165-
166-
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.

‎docs/modules/shelve-module.md‎

Lines changed: 47 additions & 0 deletions
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+
```

‎docs/modules/zipfile-module.md‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title:Python Zipfile Module - Python Cheatsheet
3+
description:This module provides tools to create, read, write, append, and list a ZIP file.
4+
---
5+
6+
#Python Zipfile Module
7+
8+
<base-disclaimer>
9+
<base-disclaimer-title>
10+
From the <a target="_blank" href="https://docs.python.org/3/library/zipfile.html">Python 3 documentation</a>
11+
</base-disclaimer-title>
12+
<base-disclaimer-content>
13+
This module provides tools to create, read, write, append, and list a ZIP file.
14+
</base-disclaimer-content>
15+
</base-disclaimer>
16+
17+
##Reading ZIP files
18+
19+
```python
20+
>>>with zipfile.ZipFile('example.zip')as example_zip:
21+
...print(example_zip.namelist())
22+
... spam_info= example_zip.getinfo('spam.txt')
23+
...print(spam_info.file_size)
24+
...print(spam_info.compress_size)
25+
...print('Compressed file is%sx smaller!'% (round(spam_info.file_size/ spam_info.compress_size,2)))
26+
...
27+
# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
28+
# 13908
29+
# 3828
30+
# 'Compressed file is 3.63x smaller!'
31+
```
32+
33+
##Extracting from ZIP Files
34+
35+
The`extractall()` method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
36+
37+
```python
38+
>>>with zipfile.ZipFile('example.zip')as example_zip:
39+
... example_zip.extractall()
40+
```
41+
42+
The`extract()` method for ZipFile objects will extract a single file from the ZIP file:
43+
44+
```python
45+
>>>with zipfile.ZipFile('example.zip')as example_zip:
46+
...print(example_zip.extract('spam.txt'))
47+
...print(example_zip.extract('spam.txt','C:\\some\\new\\folders'))
48+
...
49+
# 'C:\\spam.txt'
50+
# 'C:\\some\\new\\folders\\spam.txt'
51+
```
52+
53+
##Creating and Adding to ZIP Files
54+
55+
```python
56+
>>>import zipfile
57+
>>>
58+
>>>with zipfile.ZipFile('new.zip','w')as new_zip:
59+
... new_zip.write('spam.txt',compress_type=zipfile.ZIP_DEFLATED)
60+
```
61+
62+
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
63+
64+
##Reading metadata of ZIP files
65+
66+
###Getting the filenames with namelist()
67+
68+
```python
69+
>>>import zipfile
70+
>>>
71+
>>>with zipfile.ZipFile('example.zip','r')as zf:
72+
...print(zf.namelist())
73+
...
74+
# ['README.txt']
75+
```
76+
77+
###Getting all metadata with infolist()
78+
79+
```python
80+
>>>import datetime
81+
>>>import zipfile
82+
>>>
83+
>>>with zipfile.ZipFile(archive_name)as zf:
84+
...for infoin zf.infolist():
85+
... system='Windows'if info.create_system==0else'Unix'
86+
... modified= datetime.datetime(*info.date_time)
87+
...print(info.filename)
88+
...print(f'Comment :{info.comment}')
89+
...print(f'Modified :{modified}')
90+
...print(f'System :{system}')
91+
...print(f'ZIP version :{info.create_version}')
92+
...print(f'Compressed :{info.compress_size} bytes')
93+
...print(f'Uncompressed:{info.file_size} bytes')
94+
...
95+
# README.txt
96+
# Comment : b''
97+
# Modified : 2022-11-15 06:48:02
98+
# System : Unix
99+
# ZIP version : 30
100+
# Compressed : 65 bytes
101+
# Uncompressed: 76 bytes
102+
```

‎src/pages/changelog.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ updated: July 19, 2022
99

1010
<!-- ## [Unreleased](https://github.com/wilfredinni/python-cheatsheet/tree/next)-->
1111

12+
##2022-10-02
13+
14+
###Website
15+
16+
- Updated dependencies to the latest version
17+
- Removed some workarounds for workbox that were not needed anymore
18+
19+
###Cheatsheet
20+
21+
- Added Python <router-linkto='/modules/zipfile-module'>Zipfile Module</router-link> ([e7538ee](https://github.com/wilfredinni/python-cheatsheet/commit/e7538ee7e8d7ab9b5888ad39560ad940a25dde6d)).
22+
- Added Python <router-linkto='/modules/shelve-module'>Shelve Module</router-link> ([0afd2b3](https://github.com/wilfredinni/python-cheatsheet/commit/0afd2b3002764c13b7d016c1e39b6c124445340c)).
23+
1224
##2022-09-12
1325

14-
- Added Python <router-linkto='/cheatsheet/control-flow#python-switch-case-statements'>Switch-Case Statement</router-link> or**Structural Pattern Matching** ([6397b0f](https://github.com/wilfredinni/python-cheatsheet/commit/6397b0f1f9f5295dd53168eb2587a492ac4d4dfa)).
26+
- Added Python <router-linkto='/cheatsheet/control-flow#switch-case-statement'>Switch-Case Statement</router-link> or**Structural Pattern Matching** ([6397b0f](https://github.com/wilfredinni/python-cheatsheet/commit/6397b0f1f9f5295dd53168eb2587a492ac4d4dfa)).
1527

1628
##2022-09-03
1729

‎src/store/navigation.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ export const useNavigationStore = defineStore('navigation', {
136136
name:'Pathlib',
137137
path:'/modules/pathlib-module',
138138
},
139+
{
140+
name:'Shelve',
141+
path:'/modules/shelve-module',
142+
},
143+
{
144+
name:'Zipfile',
145+
path:'/modules/zipfile-module',
146+
},
139147
],
140148
}),
141149
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp