print_function as _py3_print, unicode_literals as _py3_unicode) from re import compile as _regex_compile from xoutil.eight import string_types from xoutil.names import strlist as strs __all__ = strs('is_valid_identifier', 'is_valid_full_identifier', 'is_valid_public_identifier', 'is_valid_slug') del strs # TODO: In Py3k "ña" is a valid identifier and this regex won't allow it _IDENTIFIER_REGEX = _regex_compile('(?i)^[_a-z][\w]*$') def is_valid_identifier(name): '''Returns True if `name` a valid Python identifier. .. note:: Only Python 2's version of valid identifier. This means that some Python 3 valid identifiers are not considered valid. This helps to keep things working the same in Python 2 and 3. ''' return isinstance(name, string_types) and _IDENTIFIER_REGEX.match(name) def check_identifier(name): '''Checks if `name` a valid Python identifier.
from ast import parse as _ast_parse from six import string_types as _str_base from xoutil.functools import partial _ast_parse = partial(_ast_parse, filename="<annotations>", mode="eval") from xoutil.decorator.meta import decorator from xoutil.names import strlist as strs __all__ = strs('annotate') del strs _SIGNATURE = _regex_compile(r'''(?ixm) \( # Required opening for the argumens (?P<args>(.)*) \)\s* # Required close (?:->\s*(?P<return>.+))?$ ''') _ARG_SEP = _regex_compile(r'(?im)^\*{0,2}(?P<argname>[_\w\d]+)\s*:') def _split_signature(signature): signature = (signature.strip() if isinstance(signature, _str_base) else '') if signature: matches = _SIGNATURE.match(signature) return matches.group('args'), matches.group('return') else: return '', None
@deprecated(assure) def new_datetime(d): '''Generate a safe "datetime" from a "datetime.date" or "datetime.datetime" object. ''' args = [d.year, d.month, d.day] if isinstance(d, datetime.__base__): # legacy datetime args.extend([d.hour, d.minute, d.second, d.microsecond, d.tzinfo]) return datetime(*args) # This library does not support strftime's "%s" or "%y" format strings. # Allowed if there's an even number of "%"s because they are escaped. _illegal_formatting = _regex_compile(br"((^|[^%])(%%)*%[sy])") def _year_find_all(fmt, year, no_year_tuple): text = _time_strftime(fmt, (year,) + no_year_tuple) regex = _regex_compile(str(year)) return {match.start() for match in regex.finditer(text)} _TD_LABELS = 'dhms' # days, hours, minutes, seconds def strfdelta(delta): ''' Format a timedelta using a smart pretty algorithm.
def _year_find_all(fmt, year, no_year_tuple): text = _time_strftime(fmt, (year,) + no_year_tuple) regex = _regex_compile(str(year)) return {match.start() for match in regex.finditer(text)}
_calculate_meta) #: The type of methods that are builtin in Python. #: #: This is roughly the type of the ``object.__getattribute__`` method. WrapperDescriptorType = SlotWrapperType = type(object.__getattribute__) from xoutil.eight.exceptions import StandardError, BaseException # noqa # TODO: deprecate next ExceptionBase = BaseException from re import compile as _regex_compile RegexPattern = type(_regex_compile('')) del _regex_compile def type_coerce(obj): '''Ensure return a valid type from `obj`.''' from xoutil.eight import class_types as ctypes return obj if isinstance(obj, ctypes) else obj.__class__ class mro_dict(Mapping): '''Utility behaving like a read-only dict of `target` MRO attributes. For example::