Mercurial > pub > dyncall > bindings
comparison python/pydc/examples/callback.py @ 56:16151547265e
made callback example python 2 compatible, and added some randomness to numbers to sort
author | Tassilo Philipp |
---|---|
date | Tue, 02 Feb 2021 21:07:41 +0100 |
parents | 918dab7a6606 |
children |
comparison
equal
deleted
inserted
replaced
55:2e8a56976bf8 | 56:16151547265e |
---|---|
7 # data to compare, further adding to the overhead | 7 # data to compare, further adding to the overhead |
8 | 8 |
9 from pydc import * | 9 from pydc import * |
10 import sys | 10 import sys |
11 import platform | 11 import platform |
12 import random | |
12 import struct | 13 import struct |
13 | 14 |
14 if sys.platform == "win32": | 15 if sys.platform == "win32": |
15 libc = load("msvcrt") | 16 libc = load("msvcrt") |
16 elif sys.platform == "darwin": | 17 elif sys.platform == "darwin": |
28 fp_qsort = find(libc,"qsort") # void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); | 29 fp_qsort = find(libc,"qsort") # void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); |
29 fp_memcpy = find(libc,"memcpy") # void * memcpy(void *dst, const void *src, size_t len); | 30 fp_memcpy = find(libc,"memcpy") # void * memcpy(void *dst, const void *src, size_t len); |
30 | 31 |
31 | 32 |
32 | 33 |
33 nums = bytearray(struct.pack("i"*8, 12, 3, 5, 99, 3, -9, -9, 0)) | 34 n = 8 |
34 es = int(len(nums)/8) # element size | 35 nums = bytearray(struct.pack("i"*n, *[random.randrange (-10, 50) for i in range (n)])) |
36 es = int(len(nums)/n) # element size | |
35 | 37 |
36 | 38 |
37 def compar(a, b): | 39 def compar(a, b): |
38 ba = bytearray(es) | 40 ba = bytearray(es) |
39 call(fp_memcpy,"ppi)p", ba, a, es) | 41 call(fp_memcpy,"ppi)p", ba, a, es) |
44 | 46 |
45 cb = new_callback("pp)i", compar) | 47 cb = new_callback("pp)i", compar) |
46 | 48 |
47 # -------- | 49 # -------- |
48 | 50 |
49 print(*struct.unpack("i"*8, nums)) | 51 print('%d '*n % struct.unpack("i"*n, nums)) |
50 | 52 |
51 print('... qsort ...') | 53 print('... qsort ...') |
52 call(fp_qsort,"piip)v", nums, 8, es, cb) | 54 call(fp_qsort,"piip)v", nums, n, es, cb) |
53 | 55 |
54 print(*struct.unpack("i"*8, nums)) | 56 print('%d '*n % struct.unpack("i"*n, nums)) |
55 | 57 |
56 | 58 |
57 free_callback(cb) | 59 free_callback(cb) |
58 | 60 |