Source code for xotl.tools.keywords

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Copyright (c) Merchise Autrement [~º/~] and Contributors
# All rights reserved.
#
# This is free software; you can do what the LICENCE file allows you to.
#

"""Tools for manage Python keywords as names.

Reserved Python keywords can't be used as attribute names, so this module
functions use the convention of rename the name using an underscore as
suffix when a reserved keyword is used as name.

"""


[docs]def suffix_kwd(name): """Add an underscore suffix if name if a Python keyword.""" from keyword import iskeyword return "{}_".format(name) if iskeyword(name) else name
[docs]def org_kwd(name): """Remove the underscore suffix if name starts with a Python keyword.""" from keyword import iskeyword if name.endswith("_"): res = name[:-1] return res if iskeyword(res) else name else: return name
[docs]def getkwd(obj, name, default=None): """Like `getattr` but taking into account Python keywords.""" return getattr(obj, suffix_kwd(name), default)
[docs]def setkwd(obj, name, value): """Like `setattr` but taking into account Python keywords.""" setattr(obj, suffix_kwd(name), value)
[docs]def delkwd(obj, name): """Like `delattr` but taking into account Python keywords.""" delattr(obj, suffix_kwd(name))
[docs]def kwd_getter(obj): """partial(getkwd, obj)""" from functools import partial return partial(getkwd, obj)
[docs]def kwd_setter(obj): """partial(setkwd, obj)""" from functools import partial return partial(setkwd, obj)
[docs]def kwd_deleter(obj): """partial(delkwd, obj)""" from functools import partial return partial(delkwd, obj)