Skip to content

Commit 4e6fc6e

Browse files
committed
__pycache__: Initial commit (materials)
1 parent 11b2f7c commit 4e6fc6e

File tree

18 files changed

+162
-0
lines changed

18 files changed

+162
-0
lines changed

python-pycache/Calculator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Calculator {
2+
public static void main(String[] args) {
3+
add(3, 4);
4+
}
5+
6+
private static int add(int a, int b) {
7+
return a + b;
8+
}
9+
}

python-pycache/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# What Is the __pycache__ Folder in Python?
2+
3+
Source code, shell scripts, and example projects for the [What Is the `__pycache__` Folder in Python?](https://realpython.com/python-pycache/) tutorial on Real Python.
4+
5+
## Setup
6+
7+
You don't need to create a virtual environments because you won't be installing anything.
8+
9+
## Cleanup Scripts
10+
11+
To recursively remove the `__pycache__` folders on macOS and Linux:
12+
13+
```shell
14+
$ ./pyclean.sh
15+
```
16+
17+
To do the same on Windows in PowerShell:
18+
19+
```shell
20+
PS> .\pyclean.ps1
21+
```
22+
23+
## X-Ray of `.pyc` Files
24+
25+
Compile sample bytecode into timestamp-based `.pyc` files:
26+
27+
```shell
28+
$ ./pyclean.sh
29+
$ python -m compileall --invalidation-mode timestamp example-2/
30+
$ python xray.py example-2/__pycache__/arithmetic*.pyc
31+
{'magic_number': b'\xcb\r\r\n',
32+
'magic_int': 3531,
33+
'python_version': '3.12',
34+
'bit_field': 0,
35+
'pyc_type': <PycInvalidationMode.TIMESTAMP: 1>,
36+
'timestamp': datetime.datetime(2024, 3, 28, 17, 8, 22, tzinfo=datetime.timezone.utc),
37+
'file_size': 32}
38+
```
39+
40+
Compile sample bytecode into unchecked hash-based `.pyc` files:
41+
42+
```shell
43+
$ ./pyclean.sh
44+
$ python -m compileall --invalidation-mode unchecked-hash example-2/
45+
$ python xray.py example-2/__pycache__/arithmetic*.pyc
46+
{'magic_number': b'\xcb\r\r\n',
47+
'magic_int': 3531,
48+
'python_version': '3.12',
49+
'bit_field': 1,
50+
'pyc_type': <PycInvalidationMode.UNCHECKED_HASH: 3>,
51+
'hash_value': b'\xf3\xdd\x87j\x8d>\x0e)'}
52+
```
53+
54+
Compile sample bytecode into checked hash-based `.pyc` files:
55+
56+
```shell
57+
$ ./pyclean.sh
58+
$ python -m compileall --invalidation-mode checked-hash example-2/
59+
$ python xray.py example-2/__pycache__/arithmetic*.pyc
60+
{'magic_number': b'\xcb\r\r\n',
61+
'magic_int': 3531,
62+
'python_version': '3.12',
63+
'bit_field': 3,
64+
'pyc_type': <PycInvalidationMode.CHECKED_HASH: 2>,
65+
'hash_value': b'\xf3\xdd\x87j\x8d>\x0e)'}
66+
```
67+
68+
## Java Bytecode Compiler
69+
70+
Compile the source code upfront and run the resulting class file:
71+
72+
```shell
73+
$ javac Calculator.java
74+
$ time java Calculator
75+
```
76+
77+
Let the `java` command handle the compilation:
78+
79+
```shell
80+
$ time java Calculator.java
81+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import mathematics.geometry # noqa

python-pycache/example-1/project/mathematics/__init__.py

Whitespace-only changes.

python-pycache/example-1/project/mathematics/arithmetic/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def add(a, b):
2+
return a + b
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def sub(a, b):
2+
return a - b

python-pycache/example-1/project/mathematics/geometry/__init__.py

Whitespace-only changes.

python-pycache/example-1/project/mathematics/geometry/shapes.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def add(a, b):
2+
return a + b

0 commit comments

Comments
 (0)