Skip to content

Instantly share code, notes, and snippets.

@santa4nt
Created June 2, 2011 18:06
Show Gist options
  • Save santa4nt/1004925 to your computer and use it in GitHub Desktop.
Save santa4nt/1004925 to your computer and use it in GitHub Desktop.
Demonstrate usage of ctypes Structure to synthesize a C struct
from ctypes import *
from ctypes.wintypes import *
class SYSTEMTIME(Structure):
"""See: WinBase.h"""
_fields_ = [
('wYear', WORD),
('wMonth', WORD),
('wDayOfWeek', WORD),
('wDay', WORD),
('wHour', WORD),
('wMinute', WORD),
('wSecond', WORD),
('wMilliseconds', WORD),
]
def __str__(self):
return '%4d/%02d/%02d %02d:%02d:%02d.%03d' % \
(self.wYear, self.wMonth, self.wDay,
self.wHour, self.wMinute, self.wSecond, self.wMilliseconds)
def __repr__(self):
return 'SYSTEMTIME(%4d,%02d,%02d,%02d,%02d,%02d,%d)' % \
(self.wYear, self.wMonth, self.wDay,
self.wHour, self.wMinute, self.wSecond, self.wMilliseconds)
def __lt__(self, other):
for attrstr, _ in self._fields_:
if getattr(self, attrstr) > getattr(other, attrstr):
return False
return True
def __gt__(self, other):
for attrstr, _ in self._fields_:
if getattr(self, attrstr) < getattr(other, attrstr):
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment