Skip to content

Commit 3842bef

Browse files
committed
Updated Cython wrap_arrays to be an excercise
1 parent b6be6d8 commit 3842bef

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

cython/wrap_arrays/Readme.md

+28
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,31 @@ Run pytest unit tests to verify everything is working as intended for the Cython
2424
* setup.py
2525
* pytest unit tests
2626
* test_cython_wrapper.py
27+
28+
29+
## Excercise
30+
You need to fill in the **TODO:** blocks in **cfastlz.pxd** and **cyfastlz.pyx**
31+
32+
Some useful things to keep in mind:
33+
34+
1. Read the documentation in fastlz.h to understand how the compress() and decompress() functions work.
35+
2. You can cast from a Python type to a C type using syntax like the following:
36+
37+
```python
38+
<const uint8_t*>in_buf
39+
```
40+
3. In Python, you can use *slicing* to return a subarray, substring, etc using syntax similar to:
41+
42+
```python
43+
my_array = range(10)
44+
my_array[2:7]
45+
[2, 3, 4, 5, 6]
46+
```
47+
48+
The slice includes the starting index, but excludes the ending index.
49+
50+
You know you are done when the unit test passes.
51+
52+
53+
### Solution
54+
The solution is on the **solutions** branch. Don't peak until you have given this an honest effort.

cython/wrap_arrays/cfastlz.pxd

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ from libc.stdint cimport uint8_t
66

77

88
cdef extern from "fastlz.h":
9-
int fastlz_compress(const uint8_t* inBuf, int length, uint8_t* output)
10-
int fastlz_decompress(const uint8_t* inBuf, int length, uint8_t* output, int maxout)
9+
# TODO: Copy function prototypes you want to wrap from the C header file here (without ending semicolon)
10+

cython/wrap_arrays/cyfastlz.pyx

+6-13
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ cpdef bytes compress(bytes in_buf):
2020
# Create the output buffer
2121
output = bytearray(M)
2222

23-
# wrap byte arrays to c arrays for the call
24-
fcret = cfastlz.fastlz_compress(<const uint8_t*>in_buf, N, <uint8_t*> output)
23+
# TODO: Call cfastlz.fastlz_compress(). Make sure to get the return value and to cast Python array type to C type.
2524

26-
if fcret <=0:
27-
return None
25+
# TODO: Check for invalid return value and return None if that occurs
2826

29-
# Return the compressed data as a bytes object
30-
return bytes(output[:fcret])
27+
# TODO: Return the compressed data as a bytes object
3128

3229

3330
cpdef bytes decompress(bytes in_buf):
@@ -43,12 +40,8 @@ cpdef bytes decompress(bytes in_buf):
4340
M = max(int(4*N), 66)
4441
output = bytearray(M)
4542

46-
# wrap byte arrays to c arrays for the call
47-
fcret = cfastlz.fastlz_decompress(<const uint8_t*> in_buf, N, <uint8_t*> output, M)
43+
# TODO: Call cfastlz.fastlz_decompress(). Make sure to get the return value and to cast Python array type to C type.
4844

49-
# If error occurs, e.g. the compressed data is corrupted or the output buffer is not large enough, then 0 (zero)
50-
if fcret <=0:
51-
return None
45+
# TODO: Check for invalid return value and return None if that occurs
5246

53-
# Return the uncompressed data as a bytes object
54-
return bytes(output[:fcret])
47+
# TODO: Return the uncompressed data as a bytes object

0 commit comments

Comments
 (0)