Package paramz :: Package core :: Module nameable
[hide private]
[frames] | no frames]

Source Code for Module paramz.core.nameable

 1  #=============================================================================== 
 2  # Copyright (c) 2015, Max Zwiessele 
 3  # All rights reserved. 
 4  #  
 5  # Redistribution and use in source and binary forms, with or without 
 6  # modification, are permitted provided that the following conditions are met: 
 7  #  
 8  # * Redistributions of source code must retain the above copyright notice, this 
 9  #   list of conditions and the following disclaimer. 
10  #  
11  # * Redistributions in binary form must reproduce the above copyright notice, 
12  #   this list of conditions and the following disclaimer in the documentation 
13  #   and/or other materials provided with the distribution. 
14  #  
15  # * Neither the name of paramz.core.nameable nor the names of its 
16  #   contributors may be used to endorse or promote products derived from 
17  #   this software without specific prior written permission. 
18  #  
19  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
22  # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
23  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
24  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
25  # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
26  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
27  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
28  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
29  #=============================================================================== 
30  from .gradcheckable import Gradcheckable 
31  import re 
32 33 -def adjust_name_for_printing(name):
34 """ 35 Make sure a name can be printed, alongside used as a variable name. 36 """ 37 if name is not None: 38 name2 = name 39 name = name.replace(" ", "_").replace(".", "_").replace("-", "_m_") 40 name = name.replace("+", "_p_").replace("!", "_I_") 41 name = name.replace("**", "_xx_").replace("*", "_x_") 42 name = name.replace("/", "_l_").replace("@", '_at_') 43 name = name.replace("(", "_of_").replace(")", "") 44 if re.match(r'^[a-zA-Z_][a-zA-Z0-9-_]*$', name) is None: 45 raise NameError("name {} converted to {} cannot be further converted to valid python variable name!".format(name2, name)) 46 return name 47 return ''
48
49 50 -class Nameable(Gradcheckable):
51 """ 52 Make an object nameable inside the hierarchy. 53 """
54 - def __init__(self, name, *a, **kw):
55 self._name = name or self.__class__.__name__ 56 super(Nameable, self).__init__(*a, **kw)
57 58 @property
59 - def name(self):
60 """ 61 The name of this object 62 """ 63 return self._name
64 @name.setter
65 - def name(self, name):
66 """ 67 Set the name of this object. 68 Tell the parent if the name has changed. 69 """ 70 from_name = self.name 71 assert isinstance(name, str) 72 self._name = name 73 if self.has_parent(): 74 self._parent_._name_changed(self, from_name)
75
76 - def hierarchy_name(self, adjust_for_printing=True):
77 """ 78 return the name for this object with the parents names attached by dots. 79 80 :param bool adjust_for_printing: whether to call :func:`~adjust_for_printing()` 81 on the names, recursively 82 83 """ 84 if adjust_for_printing: adjust = lambda x: adjust_name_for_printing(x) 85 else: adjust = lambda x: x 86 if self.has_parent(): 87 return self._parent_.hierarchy_name() + "." + adjust(self.name) 88 return adjust(self.name)
89