-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from karandesai-96/enums-for-cmontecarlo
Mirroring C enums in Python for CMontecarlo tests.
- Loading branch information
Showing
3 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from ctypes import c_int | ||
|
||
|
||
class EnumerationType(type(c_int)): | ||
""" | ||
From http://code.activestate.com/recipes/576415-ctype-enumeration-class | ||
Metaclass for CEnumeration class. | ||
""" | ||
def __new__(metacls, name, bases, dictionary): | ||
if "_members_" not in dictionary: | ||
_members_ = {} | ||
for key, value in dictionary.items(): | ||
if not key.startswith("_"): | ||
_members_[key] = value | ||
|
||
dictionary["_members_"] = _members_ | ||
else: | ||
_members_ = dictionary["_members_"] | ||
|
||
dictionary["_reverse_map_"] = {value: key for key, value in _members_.items()} | ||
cls = type(c_int).__new__(metacls, name, bases, dictionary) | ||
|
||
for key, value in cls._members_.items(): | ||
globals()[key] = value | ||
return cls | ||
|
||
def __repr__(self): | ||
return "<Enumeration %s>" % self.__name__ | ||
|
||
|
||
class CEnumeration(c_int): | ||
""" | ||
From http://code.activestate.com/recipes/576415-ctype-enumeration-class | ||
Python implementation about `enum` datatype of C. | ||
""" | ||
__metaclass__ = EnumerationType | ||
_members_ = {} | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, int): | ||
return self.value == other | ||
return type(self) == type(other) and self.value == other.value | ||
|
||
def __repr__(self): | ||
return "<%s.%s: %d>" % (self.__class__.__name__, | ||
self._reverse_map_.get(self.value, '(unknown)'), | ||
self.value) | ||
|
||
|
||
class TardisError(CEnumeration): | ||
OK = 0 | ||
BOUNDS_ERROR = 1 | ||
COMOV_NU_LESS_THAN_NU_LINE = 2 | ||
|
||
|
||
class RPacketStatus(CEnumeration): | ||
IN_PROCESS = 0 | ||
EMITTED = 1 | ||
REABSORBED = 2 | ||
|
||
|
||
class ContinuumProcessesStatus(CEnumeration): | ||
OFF = 0 | ||
ON = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters