On Thu, 27 Oct 2011 16:00:57 -0700, DevPlayer wrote: > def isvalid_named_reference( astring ): > # "varible name" is really a named_reference > # import string # would be cleaner
I don't understand the comment about "variable name". > valid_first_char = > '_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' > valid_rest = > '_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' This would be better: import string valid_first_char = '_' + string.ascii_letters valid_rest = string.digits + valid_first_char > # I think it's ok here for the rare type-check > # as unicode named-references are not allowed > if type(astring) is not str: return False In Python 3 they are: http://www.python.org/dev/peps/pep-3131/ > if len(astring) == 0: return False > if astring[0] not in valid_first_char: return False > for c in astring[1:]: > if c not in valid_rest: return False > > # Python keywords not allowed as named references (variable names) > for astr in ['and', 'assert', 'break', 'class', 'continue', > 'def', 'del', 'elif', 'else', 'except', 'exec', > 'finally', 'for', 'from', 'global', 'if', 'import', > 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', > 'raise', 'return', 'try', 'while', 'yield',]: > if astring == astr: return False You missed 'as' and 'with'. And 'nonlocal' in Python 3. Possibly others. Try this instead: from keywords import iskeyword if iskeyword(astring): return False -- Steven -- http://mail.python.org/mailman/listinfo/python-list