pytilities.mangling

Package pytilities :: Module mangling |
[hide private]
[frames] | no frames]

# Source Code for Module pytilities.mangling

     1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net>
     2  #
     3  # This file is part of pytilities.
     4  #
     5  # pytilities is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # pytilities is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>.
    17  #
    18
    19  __docformat__ = 'reStructuredText'
    20
    21  from pytilities.overloading import overloaded, Overload, Param
    22
    23 -def __mangle_cls(cls_name, attribute):
    24      assert(isinstance(attribute, basestring))
    25
    26      if attribute.startswith("__") and not attribute.endswith("__"):
    27          return "_" + cls_name + attribute
    28      else:
    29          return attribute
    30
    31 -def __mangle_instance(instance, attribute):
    32      return __mangle_cls(instance.__class__.__name__, attribute)
    33
    34  @overloaded((
    35      Overload(__mangle_cls,
    36          Param('cls_name', basestring),
    37          Param('attribute')),
    38      Overload(__mangle_instance,
    39          Param('instance'),
    40          Param('attribute'))),
    41      False)
    42 -def mangle():
    43      """
    44      Get the mangled name of an attribute
    45
    46      Overloaded parameters:
    47
    48      :a:
    49
    50          `cls_name` :: string
    51              the name of the class to which the attribute belongs
    52
    53          `attribute` :: string
    54              the attribute name to mangle
    55
    56      :b:
    57
    58          `instance`
    59              the instance to which the attribute belongs to. It's class name
    60              will be used for mangling.
    61
    62          `attribute` :: string
    63              the attribute name to mangle
    64
    65      Returns mangled attribute, if it starts with '__' and doesn't end with
    66      '__'. Otherwise, `attribute` is returned :: string
    67      """
    68