def inner(start=0): ok, best_idx, best_qlty = start, -1, 0 i, count = start, len(names) assert start < count, 'start is "%s", max is "%s".' % (start, count) while i < count: name = names[i] if '%(next)s' in name: next = inner(i + 1) names[i] = name % {'next': next} count = i + 1 else: if is_valid_slug(name): qlty = 25 if is_valid_identifier(name): qlty = 75 if is_valid_public_identifier(name) else 50 elif is_valid_full_identifier(name): qlty = 100 else: qlty = -25 if best_qlty <= qlty: best_idx = i best_qlty = qlty ok = i i += 1 idx = best_idx if best_idx >= 0 else ok return names[idx]
def __parse_arguments(self, *args, **kwargs): '''Assign parsed arguments to the just created instance.''' from xoutil.validators import (is_valid_identifier, predicate) self.attr_name = Unset self.init = Unset self.default = Unset self.do_assigning = True self.validator = True for i, arg in enumerate(args): if self.attr_name is Unset and is_valid_identifier(arg): self.attr_name = arg elif self.init is Unset and callable(arg): self.init = arg else: msg = ('Invalid positional arguments: %s at %s\n' 'Valid arguments are the attribute name and a ' 'callable constructor for initial value.') raise ValueError(msg % (args[i:], i)) bads = {} for key in kwargs: value = kwargs[key] if (self.default is Unset and self.init is Unset and key in ('default', 'value', 'initial_value')): self.default = value elif (self.validator is True and key in ('validator', 'checker', 'check')): self.validator = value elif (self.do_assigning is True and key == 'do_assigning' and value is False): self.do_assigning = False else: bads[key] = value self.validator = predicate(self.validator) if bads: msg = ('Invalid keyword arguments: %s\n' 'See constructor documentation for more info.') raise ValueError(msg % bads) if self.attr_name is Unset: from xoutil.names import nameof if self.init is not Unset: if isinstance(self.init, type): self.attr_name = str('_%s' % self.init.__name__) else: self.attr_name = nameof(self.init, safe=True) else: self.attr_name = self._unique_name() self.inner_name = str('__%s__' % self.attr_name.strip('_'))
def setter(key, val): if is_valid_identifier(key): setattr(target, key, val)