Python string 模块,capitalize() 实例源码
我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用string.capitalize()。
def _processSentence(self, elem, sessionID):
"""Process a <sentence> aimL element.
<sentence> elements process their contents recursively,and
then capitalize the first letter of the results.
"""
response = ""
for e in elem[2:]:
response += self._processElement(e, sessionID)
try:
response = response.strip()
words = string.split(response, " ", 1)
words[0] = string.capitalize(words[0])
response = string.join(words)
return response
except IndexError: # response was empty
return ""
# <set>
def _processSentence(self, 1)
words[0] = string.capitalize(words[0])
response = string.join(words)
return response
except IndexError: # response was empty
return ""
# <set>
def handle_chat(self, data):
"""
Handle the chat from players.
"""
if string.capitalize(data['div_class']) == 'Chatnotice':
return
func = 'onChat_type%s' % (string.capitalize(data['div_class']))
if hasattr(self, func):
self.debug('routing ----> %s' % func)
func = getattr(self, func)
event = func(data)
if event:
if event != 'unable to auth client':
self.queueEvent(event)
else:
return
else:
self.warning('Todo: handle: %s(%s)' % (func, data))
else:
self.warning('Todo: handle packet : %s' % data)
self.queueEvent(self.getEvent('EVT_UNKNowN', data))
def _processFormal(self, sessionID):
"""Process a <formal> aimL element.
<formal> elements process their contents recursively,and then
capitalize the first letter of each word of the result.
"""
response = ""
for e in elem[2:]:
response += self._processElement(e, sessionID)
return string.capwords(response)
# <gender>
def letters(t):
letter_map = {}
for char in string.lowercase:
letter_map[char] = (t(char), string.capitalize(char))
return letter_map
def expandcasedict(self):
for i in lowercase_ligatures:
casedict[i] = i.upper()
for i in lowercase:
if i not in casedict.keys():
if string.capitalize(i) in uppercase:
casedict[i] = string.capitalize(i)
def _processFormal(self, sessionID)
return string.capwords(response)
# <gender>
def _parse_line(line):
match = prog.match(line)
if not match:
return line
name, value = match.group(1, 2)
if name[0] == 'N':
name = string.join(string.split(name),'')
name = string.lower(name)
name = string.capitalize(name)
try:
pf = _parse_func[name]
except KeyError:
pf = _parse_num
value = pf(value)
return (name, value)
def _parse_line(line):
match = prog.match(line)
if not match:
return line
name, value)
def _parse_line(line):
match = prog.match(line)
if not match:
return line
name, value)
def routeFrostbitePacket(self, packet):
"""
Route a Frostbite packet.
"""
if packet is None:
self.warning('Cannot route empty packet : %s' % traceback.extract_tb(sys.exc_info()[2]))
eventType = packet[0]
eventData = packet[1:]
data = ''
func = ''
match = re.search(r"^(?P<actor>[^.]+)\.on(?P<event>.+)$", eventType)
if match:
func = 'On%s%s' % (string.capitalize(match.group('actor')), string.capitalize(match.group('event')))
#self.debug("-==== FUNC!!: " + func)
if match and hasattr(self, func):
func = getattr(self, func)
event = func(eventType, eventData)
if event:
self.queueEvent(event)
elif eventType in self._eventMap:
self.queueEvent(b3.events.Event(self._eventMap[eventType], eventData))
else:
if func:
data = func + ' '
data += str(eventType) + ': ' + str(eventData)
self.debug('Todo: %r' % packet)
self.queueEvent(self.getEvent('EVT_UNKNowN', data))
def routeFrostbitePacket(self, packet):
"""
Route a frostbite packet.
"""
if packet is None:
self.warning('Cannot route empty packet: %s' % traceback.extract_tb(sys.exc_info()[2]))
eventType = packet[0]
eventData = packet[1:]
match = re.search(r"^(?P<actor>[^.]+)\.(on)?(?P<event>.+)$", eventType)
func = None
if match:
func = 'On%s%s' % (string.capitalize(match.group('actor')),
string.capitalize(match.group('event')))
self.verbose2("Looking for event handling method called : " + func)
if match and hasattr(self, func):
#self.verbose2('routing ----> %s(%r)' % (func,eventData))
func = getattr(self, eventData)
#self.debug('event : %s' % event)
if event:
self.queueEvent(event)
elif eventType in self._eventMap:
self.queueEvent(b3.events.Event(
self._eventMap[eventType],
eventData))
else:
data = ''
if func:
data = func + ' '
data += str(eventType) + ': ' + str(eventData)
self.warning('Todo : handle \'%r\' frostbite2 events' % packet)
self.queueEvent(self.getEvent('EVT_UNKNowN', data))
def get_inidata (self):
# Return data describing the installation.
lines = []
Metadata = self.distribution.Metadata
# Write the [Metadata] section.
lines.append("[Metadata]")
# 'info' will be displayed in the installer's dialog Box,
# describing the items to be installed.
info = (Metadata.long_description or '') + '\n'
# Escape newline characters
def escape(s):
return string.replace(s, "\n", "\\n")
for name in ["author", "author_email", "description", "maintainer",
"maintainer_email", "name", "url", "version"]:
data = getattr(Metadata, name, "")
if data:
info = info + ("\n %s: %s" % \
(string.capitalize(name), escape(data)))
lines.append("%s=%s" % (name, escape(data)))
# The [setup] section contains entries controlling
# the installer runtime.
lines.append("\n[Setup]")
if self.install_script:
lines.append("install_script=%s" % self.install_script)
lines.append("info=%s" % escape(info))
lines.append("target_compile=%d" % (not self.no_target_compile))
lines.append("target_optimize=%d" % (not self.no_target_optimize))
if self.target_version:
lines.append("target_version=%s" % self.target_version)
if self.user_access_control:
lines.append("user_access_control=%s" % self.user_access_control)
title = self.title or self.distribution.get_fullname()
lines.append("title=%s" % escape(title))
import time
import distutils
build_info = "Built %s with distutils-%s" % \
(time.ctime(time.time()), distutils.__version__)
lines.append("build_info=%s" % build_info)
return string.join(lines, "\n")
# get_inidata()
def routePacket(self, packet):
if packet is None:
self.warning('Cannot route empty packet')
if packet.message == MessageType.SERVER_TRANSMISSION \
and packet.data == "PONG":
self.verbose2("%s" % packet)
else:
self.console("%s" % packet)
if packet.message == MessageType.SERVER_TRANSMISSION:
if packet.channel == ChannelType.SERVER:
if packet.data == "PONG":
return
match = re.search(r"^(?P<event>[A-Z ]+): (?P<data>.*)$", packet.data, re.MULTILINE | re.DOTALL)
if match:
func = 'onServer%s' % (string.capitalize(match.group('event').replace(' ','_')))
data = match.group('data')
#self.debug('DATA: %s' % data)
#self.debug("-==== FUNC!!: " + func)
if hasattr(self, func):
#self.debug('routing ----> %s' % func)
func = getattr(self, func)
event = func(data)
if event:
self.queueEvent(event)
else:
self.warning('Todo: handle: %s(%s)' % (func, data))
else:
self.warning('Todo: handle packet : %s' % packet)
self.queueEvent(self.getEvent('EVT_UNKNowN', packet))
elif packet.channel == ChannelType.CHATTER:
if packet.data.startswith('broADCAST:'):
data = packet.data[10:]
func = 'onChatterbroadcast'
if hasattr(self, func):
func = getattr(self, data))
else:
data = packet.data
func = 'onChatter'
if hasattr(self, func)
event = func(data)
if event:
self.queueEvent(event)
else:
self.warning('Todo handle: %s(%s)' % (func, data))
else:
self.warning("unhandled channel type : %s" % packet.getChannelTypeAsstr())
else:
self.warning("unhandled message type : %s" % packet.getMessageTypeAsstr())