Skip to content

Commit

Permalink
used init_subclass for CompositeShape
Browse files Browse the repository at this point in the history
This makes it a bit easier to maintain, and proves the concept

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Oct 10, 2023
1 parent cd31c0d commit 330276b
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/sisl/shape/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ def __init__(self, A, B):
self.A = A.copy()
self.B = B.copy()

def __init_subclass__(cls, /,
composite_name: str,
**kwargs):
super().__init_subclass__(**kwargs)
cls.__slots__ = ()
cls.__str__ = _composite_name(composite_name)

@property
def center(self):
""" Average center of composite shapes """
Expand Down Expand Up @@ -264,10 +271,8 @@ def _str(self):


@set_module("sisl.shape")
class OrShape(CompositeShape):
class OrShape(CompositeShape, composite_name="|"):
""" Boolean ``A | B`` shape """
__slots__ = ()
__str__ = _composite_name("|")

def within_index(self, *args, **kwargs):
A = self.A.within_index(*args, **kwargs)
Expand All @@ -276,10 +281,8 @@ def within_index(self, *args, **kwargs):


@set_module("sisl.shape")
class XOrShape(CompositeShape):
class XOrShape(CompositeShape, composite_name="^"):
""" Boolean ``A ^ B`` shape """
__slots__ = ()
__str__ = _composite_name("^")

def within_index(self, *args, **kwargs):
A = self.A.within_index(*args, **kwargs)
Expand All @@ -288,10 +291,8 @@ def within_index(self, *args, **kwargs):


@set_module("sisl.shape")
class SubShape(CompositeShape):
class SubShape(CompositeShape, composite_name="-"):
""" Boolean ``A - B`` shape """
__slots__ = ()
__str__ = _composite_name("-")

def within_index(self, *args, **kwargs):
A = self.A.within_index(*args, **kwargs)
Expand All @@ -300,10 +301,8 @@ def within_index(self, *args, **kwargs):


@set_module("sisl.shape")
class AndShape(CompositeShape):
class AndShape(CompositeShape, composite_name="&"):
""" Boolean ``A & B`` shape """
__slots__ = ()
__str__ = _composite_name("&")

def toSphere(self):
""" Create a sphere which is surely encompassing the *full* shape """
Expand Down

0 comments on commit 330276b

Please sign in to comment.