MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / expand

Function expand

17-it-generator/isis2json/subfield.py:29–53  ·  view source on GitHub ↗

Parse a field into an association list of keys and subfields >>> expand('zero^1one^2two^3three') [('_', 'zero'), ('1', 'one'), ('2', 'two'), ('3', 'three')]

(content, subkeys=None)

Source from the content-addressed store, hash-verified

27DEFAULT_ENCODING = u'utf-8'
28
29def expand(content, subkeys=None):
30 ''' Parse a field into an association list of keys and subfields
31
32 >>> expand('zero^1one^2two^3three')
33 [('_', 'zero'), ('1', 'one'), ('2', 'two'), ('3', 'three')]
34
35 '''
36 if subkeys is None:
37 regex = SUBFIELD_MARKER_RE
38 elif subkeys == '':
39 return [(MAIN_SUBFIELD_KEY, content)]
40 else:
41 regex = re.compile(r'\^(['+subkeys+'])', re.IGNORECASE)
42 content = content.replace('^^', '^^ ')
43 parts = []
44 start = 0
45 key = MAIN_SUBFIELD_KEY
46 while True:
47 found = regex.search(content, start)
48 if found is None: break
49 parts.append((key, content[start:found.start()].rstrip()))
50 key = found.group(1).lower()
51 start = found.end()
52 parts.append((key, content[start:].rstrip()))
53 return parts
54
55
56class CompositeString(object):

Callers 2

iter_iso_recordsFunction · 0.90
__init__Method · 0.70

Calls 3

replaceMethod · 0.45
searchMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected