-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply changes only to Python 3.6+ where metaclass alternative is availbl
- Loading branch information
Robin De Schepper
committed
Mar 20, 2021
1 parent
23e3064
commit 20a6e95
Showing
2 changed files
with
44 additions
and
1 deletion.
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
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,40 @@ | ||
#Python 3.0-3.5 only | ||
# ------------------------------------------------------------------------------ | ||
# class factory for subclassing h.anyclass | ||
# h.anyclass methods may be overridden. If so the base method can be called | ||
# using the idiom self.basemethod = self.baseattr('methodname') | ||
# ------------------------------------------------------------------------------ | ||
|
||
from neuron import h, hoc | ||
import nrn | ||
|
||
#avoid syntax error if compiled by python 2 | ||
|
||
class MetaHocObject(type): | ||
"""Provides Exception for Inheritance of multiple HocObject""" | ||
def __new__(cls, name, bases, attrs): | ||
#print cls, name, bases | ||
m = [] | ||
for b in bases: | ||
if issubclass(b, hoc.HocObject): | ||
m.append(b.__name__) | ||
if (len(m) > 1): | ||
raise TypeError('Multiple Inheritance of HocObject in %s' % name | ||
+ ' through %s not allowed' % ','.join(m)) | ||
#note that join(b.__name__ for b in m) is not valid for Python 2.3 | ||
|
||
return type.__new__(cls, name, bases, attrs) | ||
|
||
def hclass(c): | ||
"""Class factory for subclassing h.anyclass. E.g. class MyList(hclass(h.List)):...""" | ||
if c == h.Section : | ||
return nrn.Section | ||
class hc(hoc.HocObject, metaclass=MetaHocObject): | ||
#class hc(hoc.HocObject): | ||
def __new__(cls, *args, **kwds): | ||
kwds2 = {'hocbase': cls.htype} | ||
if 'sec' in kwds: | ||
kwds2['sec'] = kwds['sec'] | ||
return hoc.HocObject.__new__(cls, *args, **kwds2) | ||
setattr(hc, 'htype', c) | ||
return hc |