

def namedtuple(typename, field_names, verbose=False, rename=False):
    'A collections.namedtuple implementation written in Python\n        to support Python versions < 2.6.\n\n        Taken from: http://code.activestate.com/recipes/500261/\n        '
    if isinstance(field_names, basestring):
        field_names = field_names.replace(',', ' ').split()
    field_names = tuple(map(str, field_names))
    if rename:
        names = list(field_names)
        seen = set()
        for (i, name) in enumerate(names):
            if ((not min(((c.isalnum() or (c == '_')) for c in name))) or _iskeyword(name) or (not name) or name[0].isdigit() or name.startswith('_') or (name in seen)):
                names[i] = ('_%d' % i)
            seen.add(name)
        field_names = tuple(names)
    for name in ((typename,) + field_names):
        if (not min(((c.isalnum() or (c == '_')) for c in name))):
            raise ValueError(('Type names and field names can only contain alphanumeric characters and underscores: %r' % name))
        if _iskeyword(name):
            raise ValueError(('Type names and field names cannot be a keyword: %r' % name))
        if name[0].isdigit():
            raise ValueError(('Type names and field names cannot start with a number: %r' % name))
    seen_names = set()
    for name in field_names:
        if (name.startswith('_') and (not rename)):
            raise ValueError(('Field names cannot start with an underscore: %r' % name))
        if (name in seen_names):
            raise ValueError(('Encountered duplicate field name: %r' % name))
        seen_names.add(name)
    numfields = len(field_names)
    argtxt = repr(field_names).replace("'", '')[1:(- 1)]
    reprtxt = ', '.join((('%s=%%r' % name) for name in field_names))
    template = ("class %(typename)s(tuple):\n        '%(typename)s(%(argtxt)s)' \n\n        __slots__ = () \n\n        _fields = %(field_names)r \n\n        def __new__(_cls, %(argtxt)s):\n            return _tuple.__new__(_cls, (%(argtxt)s)) \n\n        @classmethod\n        def _make(cls, iterable, new=tuple.__new__, len=len):\n            'Make a new %(typename)s object from a sequence or iterable'\n            result = new(cls, iterable)\n            if len(result) != %(numfields)d:\n                raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))\n            return result \n\n        def __repr__(self):\n            return '%(typename)s(%(reprtxt)s)' %% self \n\n        def _asdict(self):\n            'Return a new dict which maps field names to their values'\n            return dict(zip(self._fields, self)) \n\n        def _replace(_self, **kwds):\n            'Return a new %(typename)s object replacing specified fields with new values'\n            result = _self._make(map(kwds.pop, %(field_names)r, _self))\n            if kwds:\n                raise ValueError('Got unexpected field names: %%r' %% kwds.keys())\n            return result \n\n        def __getnewargs__(self):\n            return tuple(self) \n\n" % locals())
    for (i, name) in enumerate(field_names):
        template += ('        %s = _property(_itemgetter(%d))\n' % (name, i))
    if verbose:
        sys.stdout.write((template + '\n'))
        sys.stdout.flush()
    namespace = dict(_itemgetter=_itemgetter, __name__=('namedtuple_%s' % typename), _property=property, _tuple=tuple)
    try:
        exec_(template, namespace)
    except SyntaxError:
        e = sys.exc_info()[1]
        raise SyntaxError(((e.message + ':\n') + template))
    result = namespace[typename]
    try:
        result.__module__ = _sys._getframe(1).f_globals.get('__name__', '__main__')
    except (AttributeError, ValueError):
        pass
    return result

dataset/ETHPy150Open packages/psutil/psutil/_compat.py namedtuple/original