Skip to content

Commit

Permalink
meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Freakwill committed Dec 5, 2023
1 parent 958541b commit 0a6dffa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
20 changes: 15 additions & 5 deletions pyrimidine/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ class UnknownSizeError(Exception):

def __init__(self, cls):
self.cls = cls

def __str__(self):
return f'The size of `{self.cls}` is unkown, the object could not be generated.'
return f'The size of class `{self.cls.__name__}` is unkown, the object could not be generated.'


class UnavalibleAttributeError(AttributeError):

def __init__(self, cls, attr_name):
self.cls = cls
self.attr_name = attr_name


def __str__(self):
return f'Did not define attribute `{self.attr_name}` for the class `{self.cls.__name__}`.'


class RegesterError(AttributeError):

def __init__(self, cls, attr_name):
self.cls = cls
self.attr_name = attr_name

def __str__(self):
return f'Did not define attribute `{self.attr_name}` for `{self.cls}`.'
return AttributeError(f'`{attr_name}` is an attribute of the class `{self.cls.__name__}`, and would not be redefined.')
11 changes: 10 additions & 1 deletion pyrimidine/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,22 @@ def _regester_map(self, name, key=None, force=True):
def m(obj, *args, **kwargs):
return obj.map(key, obj.__elements, *args, **kwargs)
if not force and hasattr(self, name):
raise AttributeError(f'`{name}` is an attribute of {self.__class__.__name__}, and would not be regestered.')
raise RegesterError(self.__class__, name)
setattr(self, name, MethodType(m, self))

attrs.update({
'regester_map': _regester_map
})

def _regester(self, name, key, force=True):
if not force and hasattr(self, name):
raise RegesterError(self.__class__, name)
setattr(self, name, MethodType(key, self))

attrs.update({
'regester': _regester
})

return super().__new__(cls, name, bases, attrs)

def __call__(self, *args, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def after_setter(self):

C = self.mc
c = C([UserString('I'), UserString('love'), UserString('you')], lasting='for ever')
C.set_methods(n_elems=lambda c: 1)
C.set(n_elems=lambda c: 1)
self.C = C
self.c = c

Expand All @@ -41,7 +41,6 @@ def n_vowels(s):
c.regester_map('length', n_vowels)
assert list(c.length()) == [1, 2, 2]


def test_subclass(self):
C = self.C
class D(C):
Expand Down

0 comments on commit 0a6dffa

Please sign in to comment.