Skip to content

Commit 57dec9a

Browse files
authored
Merge pull request #476 from realpython/python-array
Python Array: Materials
2 parents fe67c99 + 3db8403 commit 57dec9a

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

python-array/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Python's Array: Working With Numeric Data Efficiently
2+
3+
Sample code for the [Python's Array: Working With Numeric Data Efficiently](https://realpython.com/python-array/) tutorial on Real Python.
4+
5+
## audio
6+
7+
Read 24-bit PCM-encoded audio samples from a WAV file:
8+
9+
```shell
10+
$ cd audio/
11+
$ python read_audio.py
12+
len(raw_bytes) = 793800
13+
len(samples) = 264600
14+
wave_file.getnframes() = 132300
15+
samples.itemsize = 4
16+
samples.itemsize * len(samples) = 1058400
17+
```
18+
19+
## cruncher
20+
21+
Pass a Python array to a compiled C library:
22+
23+
```shell
24+
$ cd cruncher/
25+
$ gcc -shared -fPIC -O3 -o cruncher.so cruncher.c
26+
$ python cruncher.py
27+
array('i', [-20, 14, -7, 6, -2, 3, 0, 2, 1, 2])
28+
```
29+
30+
## save_load
31+
32+
Persist an array in a file and load it back into Python:
33+
34+
```shell
35+
$ cd save_load/
36+
$ python save_load.py
37+
Saved array as: 'binary.data'
38+
array('H', [12, 42, 7, 15, 42, 38, 21])
39+
```
775 KB
Binary file not shown.

python-array/audio/read_audio.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import wave
2+
from array import array
3+
4+
with wave.open("PCM_24_bit_signed.wav", mode="rb") as wave_file:
5+
if wave_file.getsampwidth() == 3:
6+
raw_bytes = wave_file.readframes(wave_file.getnframes())
7+
samples = array(
8+
"i",
9+
(
10+
int.from_bytes(
11+
raw_bytes[i : i + 3],
12+
byteorder="little",
13+
signed=True,
14+
)
15+
for i in range(0, len(raw_bytes), 3)
16+
),
17+
)
18+
19+
print(f"{len(raw_bytes) = }")
20+
print(f"{len(samples) = }")
21+
print(f"{wave_file.getnframes() = }")
22+
print(f"{samples.itemsize = }")
23+
print(f"{samples.itemsize * len(samples) = }")

python-array/cruncher/cruncher.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void increment(int* numbers, unsigned int length) {
2+
for (int i = 0; i < length; i++) {
3+
numbers[i]++;
4+
}
5+
}

python-array/cruncher/cruncher.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from array import array
2+
from ctypes import POINTER, c_int, c_uint, cdll
3+
4+
cruncher = cdll.LoadLibrary("./cruncher.so")
5+
cruncher.increment.argtypes = (POINTER(c_int), c_uint)
6+
cruncher.increment.restype = None
7+
8+
python_array = array("i", [-21, 13, -8, 5, -3, 2, -1, 1, 0, 1])
9+
c_array = (c_int * len(python_array)).from_buffer(python_array)
10+
cruncher.increment(c_array, len(c_array))
11+
12+
print(python_array)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from array import array
2+
from struct import pack, unpack
3+
4+
5+
def save(filename, numbers):
6+
with open(filename, mode="wb") as file:
7+
file.write(numbers.typecode.encode("ascii"))
8+
file.write(pack("<I", len(numbers)))
9+
numbers.tofile(file)
10+
11+
12+
def load(filename):
13+
with open(filename, mode="rb") as file:
14+
typecode = file.read(1).decode("ascii")
15+
(length,) = unpack("<I", file.read(4))
16+
numbers = array(typecode)
17+
numbers.fromfile(file, length)
18+
return numbers
19+
20+
21+
save("binary.data", array("H", [12, 42, 7, 15, 42, 38, 21]))
22+
print("Saved array as: 'binary.data'")
23+
print(load("binary.data"))

0 commit comments

Comments
 (0)