pytilities.mangle'

Package pytilities :: Module mangle' |
[hide private]
[frames] | no frames]

# Source Code for Module pytilities.mangle'

     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 -def mangle(obj, attribute):
    22      """
    23      Get the mangled name of an attribute
    24
    25      Overloaded parameters:
    26
    27      :a:
    28
    29          `instance`
    30              the instance to which the attribute belongs to. It's class name
    31              will be used for mangling.
    32
    33          `attribute` :: string
    34              the attribute name to mangle
    35
    36      :b:
    37          `cls_name` :: string
    38              the name of the class to which the attribute belongs
    39
    40      Returns mangled attribute, if it starts with '__' and doesn't end with
    41      '__'. Otherwise, `attribute` is returned :: string
    42      """
    43
    44      assert(isinstance(attribute, basestring))
    45
    46      if attribute.startswith("__") and not attribute.endswith("__"):
    47          if isinstance(obj, basestring):
    48              class_name = obj
    49          else:
    50              class_name = obj.__class__.__name__
    51
    52          return "_" + class_name + attribute
    53      else:
    54          return attribute
    55