def exact_arg_count(num, given): """ Shorthand for raising a TypeError telling the user that more than the maximum number of arguments was given. Uses the standard python form of the expression. """ return TypeError(caller() + "() takes exactly " + str(num) + " argument" + ("" if num == 1 else "s") + "(" + str(given) + " given)")
def min_arg_count(min, given): """ Shorthand for raising a TypeError telling the user that fewer than the minimum number of arguments was given. Uses the standard python form of the expression. """ return TypeError(caller() + "() takes at least " + str(min) + " argument" + ("" if min == 1 else "s") + "(" + str(given) + " given)")
def __init__(self, keyword1, keyword2, func=None): if caller is None: callfunc = caller() else: callfunc = func message = ("Function {0}() called with conflicting keyword arguments:" " It can be called with the keyword {1} or {2}, but not both.").format(callfunc, keyword1, keyword2) super(ConflictingKwargs, self).__init__(message)
def __new__(cls, argname, arg, *allowed_types, **kwargs): allowed = listify_args(allowed_types) my_caller = get_kwarg(kwargs, 'caller') or (caller() + '()') if len(allowed) == 1: return TypeError('Argument {0} to {1} must be {2} (got {3})'.format( argname, my_caller, classname(allowed[0]), type(arg).__name__ )) else: return TypeError("Argument {0} to {1} must be one of ({2}) (got {3})".format( argname, my_caller, ', '.join(classname(a) for a in allowed), type(arg).__name__ ))
def fail_if_frozen(self): if self.frozen: raise FrozenObjectError( "Can't call method {0}() on instance {1} of {2} once " "it has been frozen.".format(caller(), self, classname(self)))
def fail_if_frozen(self): if self.frozen: raise FrozenObjectError("Can't call method {0}() on instance {1} of {2} once " "it has been frozen.".format(caller(), self, classname(self)))