티스토리 뷰
# First of all, need to read python doc. about ctypes.. from ctypes import * class Obj(Structure) : _fields_ = [("name", c_char_p), ("id", c_int)] class Person(Structure) : _fields_ = [("stInfo", Obj)] def __init__(self, person=None): self.stInfo = Obj() if person is not None : self.stInfo = person.stInfo def __str__(self) : return "I'm %s, and my id is %d" % (self.stInfo.name, self.stInfo.id) qsort = cdll.msvcrt.qsort def cmpFunc(a, b) : # a, b are POINTER(Person) type return a.contents.stInfo.id - b.contents.stInfo.id # (*Person).stInfo.id - (*Person).stInfo.id in C CmpFuncType = CFUNCTYPE(c_int, POINTER(Person), POINTER(Person)) if "__main__" == __name__ : # Virtual process converting data from DB to Python p1 = Person() p2 = Person() p3 = Person() p1.stInfo.name = "David" p1.stInfo.id = 2342 p2.stInfo.name = "Tom" p2.stInfo.id = 4233 p3.stInfo.name = "Sarah" p3.stInfo.id = 1243 numPeople = 3 # retrieved number from DB # Python only from now on people = (Person*numPeople)() people[0] = p1 #Person(p1) people[1] = p2 #Person(p2) people[2] = p3 #Person(p3) for i, p in enumerate(people) : print("i :" + str(p)) qsort(byref(people), numPeople, sizeof(Person), CmpFuncType(cmpFunc)) print("\n\nAfter qsort with id...\n\n") for i, p in enumerate(people) : print("i :" + str(p))
'Python > 요리 방법' 카테고리의 다른 글
Eclipse + Python (0) | 2011.09.22 |
---|---|
CentOS 5.6 에서 Python2.6 + PyQt4 + matplotlib 설치하기 (0) | 2011.08.31 |
Calling Windows API using ctypes and win32con (0) | 2009.10.28 |
Python에서 ctypes를 이용하여 구조체 얻어오기 (2) | 2009.10.27 |
Python에서 ctypes을 이용하여 구조체(Structure) 정의 할 때 정렬값(byte order) 지정하기 (0) | 2009.10.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 설치
- QPrinter.Letter
- CanDrA
- volume dial
- how to solve it
- Visual C++
- Python
- pandas
- Item 9
- 볼륨 조절
- MSVC++
- Accelerated C++
- armadillo c++
- TensorBoard
- C++
- matrix multiplication
- 이상한 문자
- structure
- tensorflow
- destructor
- ctypes
- GSX 1000 pro
- 볼륨 낮춤
- cython
- PyQt
- QPrinter.A4
- GSX 1200 pro
- dll
- TCGA
- QT
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함