diff --git a/python-array/README.md b/python-array/README.md new file mode 100644 index 0000000000..21d90e0e8a --- /dev/null +++ b/python-array/README.md @@ -0,0 +1,39 @@ +# Python's Array: Working With Numeric Data Efficiently + +Sample code for the [Python's Array: Working With Numeric Data Efficiently](https://realpython.com/python-array/) tutorial on Real Python. + +## audio + +Read 24-bit PCM-encoded audio samples from a WAV file: + +```shell +$ cd audio/ +$ python read_audio.py +len(raw_bytes) = 793800 +len(samples) = 264600 +wave_file.getnframes() = 132300 +samples.itemsize = 4 +samples.itemsize * len(samples) = 1058400 +``` + +## cruncher + +Pass a Python array to a compiled C library: + +```shell +$ cd cruncher/ +$ gcc -shared -fPIC -O3 -o cruncher.so cruncher.c +$ python cruncher.py +array('i', [-20, 14, -7, 6, -2, 3, 0, 2, 1, 2]) +``` + +## save_load + +Persist an array in a file and load it back into Python: + +```shell +$ cd save_load/ +$ python save_load.py +Saved array as: 'binary.data' +array('H', [12, 42, 7, 15, 42, 38, 21]) +``` diff --git a/python-array/audio/PCM_24_bit_signed.wav b/python-array/audio/PCM_24_bit_signed.wav new file mode 100644 index 0000000000..65292cdb5c Binary files /dev/null and b/python-array/audio/PCM_24_bit_signed.wav differ diff --git a/python-array/audio/read_audio.py b/python-array/audio/read_audio.py new file mode 100644 index 0000000000..e6f430d129 --- /dev/null +++ b/python-array/audio/read_audio.py @@ -0,0 +1,23 @@ +import wave +from array import array + +with wave.open("PCM_24_bit_signed.wav", mode="rb") as wave_file: + if wave_file.getsampwidth() == 3: + raw_bytes = wave_file.readframes(wave_file.getnframes()) + samples = array( + "i", + ( + int.from_bytes( + raw_bytes[i : i + 3], + byteorder="little", + signed=True, + ) + for i in range(0, len(raw_bytes), 3) + ), + ) + +print(f"{len(raw_bytes) = }") +print(f"{len(samples) = }") +print(f"{wave_file.getnframes() = }") +print(f"{samples.itemsize = }") +print(f"{samples.itemsize * len(samples) = }") diff --git a/python-array/cruncher/cruncher.c b/python-array/cruncher/cruncher.c new file mode 100644 index 0000000000..0758bc9858 --- /dev/null +++ b/python-array/cruncher/cruncher.c @@ -0,0 +1,5 @@ +void increment(int* numbers, unsigned int length) { + for (int i = 0; i < length; i++) { + numbers[i]++; + } +} diff --git a/python-array/cruncher/cruncher.py b/python-array/cruncher/cruncher.py new file mode 100644 index 0000000000..c22ff21a89 --- /dev/null +++ b/python-array/cruncher/cruncher.py @@ -0,0 +1,12 @@ +from array import array +from ctypes import POINTER, c_int, c_uint, cdll + +cruncher = cdll.LoadLibrary("./cruncher.so") +cruncher.increment.argtypes = (POINTER(c_int), c_uint) +cruncher.increment.restype = None + +python_array = array("i", [-21, 13, -8, 5, -3, 2, -1, 1, 0, 1]) +c_array = (c_int * len(python_array)).from_buffer(python_array) +cruncher.increment(c_array, len(c_array)) + +print(python_array) diff --git a/python-array/save_load/save_load.py b/python-array/save_load/save_load.py new file mode 100644 index 0000000000..1317aa1a8c --- /dev/null +++ b/python-array/save_load/save_load.py @@ -0,0 +1,23 @@ +from array import array +from struct import pack, unpack + + +def save(filename, numbers): + with open(filename, mode="wb") as file: + file.write(numbers.typecode.encode("ascii")) + file.write(pack("