本文整理汇总了Python中properties.Properties.has_key方法的典型用法代码示例。如果您正苦于以下问题:Python Properties.has_key方法的具体用法?Python Properties.has_key怎么用?Python Properties.has_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类properties.Properties
的用法示例。
在下文中一共展示了Properties.has_key方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: runTest
# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import has_key [as 别名]
def runTest(self):
"""Test blank"""
prefString= ""
prefs = Properties(prefString)
prefString = "default.name=Bilbo Baggins\nhomepage.feeds=42,24"
prefs = Properties(prefString)
assert cmp('Bilbo Baggins', prefs.get('default.name')) == 0, 'Error retrieving value from key'
assert cmp('', prefs.get('jeff')) == 0, "Missing keys should default to blank string"
prefs.set('Hulk', 'Hogan')
assert cmp('Hogan', prefs.get('Hulk')) == 0, 'Error retrieving set key/value'
stringValue = prefs.convertToPropertiesFile()
#this last test needs to be fixed, can fail if keys are not brought back in same order
assert cmp(prefString + "\nHulk=Hogan", stringValue) == 0, 'Error converting to properties'
prefString = "jeff=1"
prefs = Properties(prefString)
assert cmp('1', prefs.get('jeff')) == 0
assert cmp('1', prefs.pop('jeff')) == 0
try:
prefs.pop('badkey')
unittest.fail('A KeyError exception should have prevented this')
except KeyError:
pass
assert False == prefs.has_key('jeff')
示例2: ResourceBundle
# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import has_key [as 别名]
class ResourceBundle(PyWebMvcObject):
"""Extends a L{Properties<properties.Properties>} file to allow for
parameterized substitution and property references (terms). Parameters are in
the form C{{N}} where N is an zero-based index of the paramters to the
getMessage function. Terms take the form C{${resourcekey}} and allow a
resource entry to refer to another resource entry. It is not recommended that
you place parameters in your terms, but it is allowed since parameter
substitution is performed after term expansion. Example::
term.foo=Foo
term.idea=idea
term.modifiedIdea={1} ${term.idea}
message.bar=This is my {0} ${term.foo}.
message.badIdea=This is a {0} ${term.modifiedIdea}.
message.fineIdea=This is a {0} {1}.
message.goodIdea=This is a {0} {1} ${term.idea}.
>>> bundle["term.foo"]
'Foo'
>>> bundle["term.modifiedIdea"]
'{1} idea'
>>> bundle.getMessage("message.bar","Silly")
'This is my Silly Foo.'
>>> bundle.getMessage("message.badIdea","really", "bad")
'This is a really bad idea.'
>>> bundle.getMessage("message.fineIdea","perfectly",
... bundle.getMessage("term.modifiedIdea","fine"))
'This is a perfectly fine idea.'
>>> bundle.getMessage("message.goodIdea","much", "better")
'This is a much better idea.'
"""
def __init__(self, propertiesFile = None):
self.props = Properties()
if propertiesFile:
self.addPropertiesFile(propertiesFile)
def addPropertiesFile(self, propertiesFile):
if isinstance(propertiesFile, types.StringTypes):
self.props.read(propertiesFile)
else:
self.props.load(propertiesFile)
self.resolve_terms()
def addPropertiesFiles(self, propertiesFiles):
for f in propertiesFiles:
self.addPropertiesFile(f)
def resolve_terms(self):
for key in self.props.keys():
self.props[key] = self.replaceTerms(self.props[key])
def has_term(self,msg):
idx = msg.find("${")
if idx < 0:
return False
else:
return msg.find("}",idx) >= 0
def get_term(self,msg,all=False):
tokens = re.split("(\\$\\{|\\})",msg)
startCount = 0
term = ""
terms = []
for token in tokens:
if token == "${":
startCount += 1
if startCount > 1:
term += token
elif token == "}" and startCount > 0:
startCount -= 1
if startCount == 0:
if all:
terms.append(term)
term = ""
else:
return term
else:
term += token
elif startCount > 0:
term += token
if startCount > 0:
raise ValueError("Malformed term found in message resource: "+msg)
if all:
return terms
else:
return None
def replaceTerms(self, msg, paramMap = {}):
terms = self.get_term(msg,all=True)
for term in terms:
origTerm = term
try:
if self.has_term(term):
term = self.replaceTerms(term, paramMap)
if paramMap.has_key(term):
termVal = paramMap[term]
else:
termVal = self.getMessage(term,paramMap)
msg = msg.replace("${"+origTerm+"}",termVal)
except KeyError:
pass
return msg
def keys(self):
return self.props.keys()
def has_key(self, key):
return self.props.has_key(key)
#.........这里部分代码省略.........