-
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.
Reworked the
neuron.hclass3
module (#1096)
* added pyenv's .python-version to gitignore * prevent import py2/3 module in py3/2. removed `exec` wrapper from hclass * Changed comment and removed non-existing `nonlocal_hclass` import * Removed traling whitespaces and double newline at eof * Removed the MetaHocObject metaclass and added `nonlocal_hclass` factory * New `HocBaseObject`; (nonlocal_)hclass uses this new base. * Added dev note to demystify quintessential link between HOC & Python * moved __all__ to PEP8 location * Fixed error with grandchildren of `HocBaseObject` * Apply changes only to Python 3.6+ where metaclass alternative is availbl * fixed import typo * hclass of hoc_type should precede use of hoc.HocObject API's * Fixed usage of module_name * added hclass35.py to Makefile, excessive trailing newlines. * `module` does not exist, see details on mixed approaches in PR#1096 * black formatting & updated hclass2 derived docstrings * Store dummy modules on `neuron` module to better emulate submodules * Add a TypeError if init but not new has been given (see #1129) * Added _pyobj_enabled flag to check whether Python object interface is up * Explicitly defined __new__ for 3.6+ pyobj compatibility * Class composition is allowed if HOC types are the same * added tests for HOC type Python classes, test only in Python 3.6+ * Removed 'super()' call for Python 2 * Revert "added hclass35.py to Makefile, excessive trailing newlines." This reverts commit 080f14a. * Added hclass35 to Makefile * Revert "Removed traling whitespaces and double newline at eof" This reverts commit 046fab4. * Reinstated deprecation messages * Merged hclass and nonlocal_hclass
- Loading branch information
1 parent
6856136
commit 9046aee
Showing
9 changed files
with
476 additions
and
90 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
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
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 |
---|---|---|
@@ -1,41 +1,53 @@ | ||
#Python2 only | ||
# Python2 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 3 | ||
exec(''' | ||
|
||
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) | ||
""" | ||
Provides error when trying to compose a class of multiple HOC types. | ||
""" | ||
|
||
def __new__(cls, name, bases, attrs): | ||
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) | ||
) | ||
|
||
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 : | ||
""" | ||
Class factory for subclassing any HOC type. | ||
Example: | ||
.. code-block:: python | ||
class MyList(hclass(h.List)): | ||
pass | ||
""" | ||
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'] | ||
kwds2 = {"hocbase": cls.htype} | ||
if "sec" in kwds: | ||
kwds2["sec"] = kwds["sec"] | ||
return hoc.HocObject.__new__(cls, *args, **kwds2) | ||
setattr(hc, 'htype', c) | ||
|
||
setattr(hc, "htype", c) | ||
return hc | ||
''') |
Oops, something went wrong.