Skip to content

Commit

Permalink
Factorization for Edges and Nodes (#113)
Browse files Browse the repository at this point in the history
* Factorization for Edges and Nodes
* Add NetworkObject
* Add AbstractDocSubstitutionMeta to the Edges/Nodes class
* Add get to the Edges to match the node interface
* Deprecate the returning of ids using get/properties for edges
* Put _resolve_node_ids in the EdgePopulation class
  • Loading branch information
tomdele committed Dec 14, 2020
1 parent 7780d5e commit 87a65ad
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 296 deletions.
33 changes: 28 additions & 5 deletions bluepysnap/_doctools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import inspect
import types
import functools
import abc


def _word_swapper(doc, source_word, target_word):
Expand All @@ -36,22 +37,44 @@ def _copy_func(f):
return g


# better than decorator to do that due to the returned type being correct with this
# with wrapper <class 'bluepysnap._doctools.DocSubstitutionDecorator.__call__.<locals>.Wrapped'>
# works well with Sphinx also
class DocSubstitutionMeta(type):
"""Tool to update an inherited class documentation."""
"""Tool to update an inherited class documentation.
Notes:
Using a metaclass is better than decorator to do that due to the returned type being
incorrect when using a wrapper. Ex with CircuitNodeIds:
type(CircuitNodeIds)
<class 'bluepysnap._doctools.DocSubstitutionDecorator.__call__.<locals>.Wrapped'>
when with metaclass:
type(CircuitNodeIds)
<class 'bluepysnap.CircuitNodeIds'>
It works well with Sphinx also.
"""
def __new__(mcs, name, parents, attrs, source_word=None, target_word=None):
"""Define the new class to return."""
for parent in parents:
# skip classmethod with isfunction if I use also ismethod as a predicate I can have the
# classmethod docstring changed but then the cls argument is not automatically skipped.
for fun_name, fun_value in inspect.getmembers(parent, predicate=inspect.isfunction):
# skip abstract methods. This is fine because we must override them anyway
try:
if fun_name in parent.__abstractmethods__:
continue
except AttributeError:
pass
# skip special methods
if fun_name.startswith("__"):
continue

changed_fun = _copy_func(fun_value)
changed_fun.__doc__ = _word_swapper(changed_fun.__doc__, source_word, target_word)
attrs[fun_name] = changed_fun
# create the class
obj = super(DocSubstitutionMeta, mcs).__new__(mcs, name, parents, attrs)
return obj


class AbstractDocSubstitutionMeta(abc.ABCMeta, DocSubstitutionMeta):
"""Mixin class to use with abstract classes.
It solves the metaclass conflict.
"""
9 changes: 1 addition & 8 deletions bluepysnap/circuit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@

import bluepysnap.utils
from bluepysnap.exceptions import BluepySnapError
from bluepysnap._doctools import DocSubstitutionMeta
from bluepysnap._doctools import AbstractDocSubstitutionMeta


CircuitNodeId = namedtuple("CircuitNodeId", ("population", "id"))
CircuitEdgeId = namedtuple("CircuitEdgeId", ("population", "id"))


class AbstractDocSubstitutionMeta(abc.ABCMeta, DocSubstitutionMeta):
"""Mixin class to use with abstract classes.
It solves the metaclass conflict.
"""


class CircuitIds(abc.ABC):
"""High performances CircuitID container.
Expand Down

0 comments on commit 87a65ad

Please sign in to comment.