I was recently working on a project where I had the following setup:
from stargaze.sg import SGObject
from stargaze.constants import NegInf, Infinite, PATH_CLAMPED, PATH_REVERSIBLE, PATH_ACCELERATABLE
from stargaze.utils import clamp
__all__ = [
'PathIdentifier', '_basePath', '_clampedPath', '_reversiblePath',
'_acceleratablePath', 'Path'
]
class PathIdentifier(SGObject):
"""
INTERNATL: The base identifier class for a class object.
"""
OBJ_REPR_PREFIX = "StarGaze Path:: "
class _basePath(PathIdentifier):
def __init__(self, **config):
self._time = 0
self.speed = config['speed']
def __move__(self):
self._time += self.speed
class _clampedPath(PathIdentifier):
def __init__(self, **config):
start = config['start']
end = config['end']
if start > end:
errmsg = "Start point cannot be greater " \
"than the end point."
raise ValueError(errmsg)
self._time = start
self.end = end
self.finished = False
def __move__(self):
if self.finished:
return
if self._time >= self.end:
self.finished = True
class _reversiblePath(PathIdentifier):
def __init__(self, **config):
self.direction = 1
def __move__(self):
if self.direction == 1 and self._time >= 1:
self.direction = -1
self.speed *= -1
elif self.direction == -1 and self._time <= 0:
self.direction = 1
self.speed *= -1
class _acceleratablePath(PathIdentifier):
def __init__(self, speed, acceleration, min_speed=NegInf, max_speed=Infinite):
def __init__(self, **config):
self.accel = config['acceleration']
self.min_speed = config.get('min_speed', NegInf)
self.max_speed = config.get('max_speed', Infinite)
def __move__(self):
if self.accel != 1:
self.speed = clamp(self.accel * self.speed,
self.max_speed,
self.min_speed)
class Path:
def __init__(self, flags, **config):
_basePath.__init__(self, **config)
if flags & PATH_CLAMPED:
_clampedPath.__init__(self, **config)
if flags & PATH_REVERSED:
_reversiblePath.__init__(self, **config)
if flags & PATH_ACCELERATABLE:
_acceleratablePath.__init__(self, **config)
self.flags = flags
def move(self):
flags = self.flags
if flags & PATH_CLAMPED:
_clampedPath.__move__(self)
if flags & PATH_REVERSED:
_reversiblePath.__move__(self)
if flags & PATH_ACCELERATABLE:
_acceleratablePath.__move__(self)
None of the code is relevant, you won't have to be able to run any of it to answer my question. What I do in this code essentially is I create classes that define their own __init__
and __move_
methods, but I don't call them from instances of their class. Instead, I call them as class methods acting on the instance of another class. Is this bad style in Python? How else might I achieve what I was trying to do here?