Eigentümliche Erstellung von Methoden...
Verfasst: Mittwoch 20. Januar 2016, 23:42
Hab' mir gerade das Trac Mercurial Plugin angeschaut und bin über das hier gestolpert:
Welchen Grund könnte es geben, ``self.to_u`` und ``self.to_s`` auf diese Weise zu definieren?
Code: Alles auswählen
class MercurialRepository(Repository):
"""Repository implementation based on the mercurial API.
This wraps an hg.repository object. The revision navigation
follows the branches, and defaults to the first parent/child in
case there are many. The eventual other parents/children are
listed as additional changeset properties.
"""
def __init__(self, path, params, log, connector):
self.ui = connector.ui
self._show_rev = connector.show_rev
self._node_fmt = connector.node_format
# TODO 0.13: per repository ui and options
# -- encoding
encoding = connector.encoding
if not encoding:
encoding = ['utf-8']
# verify given encodings
for enc in encoding:
try:
u''.encode(enc)
except LookupError, e:
log.warning("'[hg] encoding' (%r) not valid", e)
if 'latin1' not in encoding:
encoding.append('latin1')
self.encoding = encoding
def to_u(s):
if isinstance(s, unicode):
return s
for enc in encoding:
try:
return unicode(s, enc)
except UnicodeDecodeError:
pass
def to_s(u):
if isinstance(u, str):
return u
for enc in encoding:
try:
return u.encode(enc)
except UnicodeEncodeError:
pass
self.to_u = to_u
self.to_s = to_s
...