kopia lustrzana https://github.com/micropython/micropython-lib
string: Add implementation of translate() method.
As MicroPython doesn't have str.translate() method, here it's implemented as string module function.pull/26/head
rodzic
9f925a6e60
commit
6e64994ec2
|
@ -1,3 +1,3 @@
|
||||||
srctype=micropython-lib
|
srctype=micropython-lib
|
||||||
type=module
|
type=module
|
||||||
version=0.0.1
|
version=0.1
|
||||||
|
|
|
@ -6,7 +6,7 @@ from setuptools import setup
|
||||||
|
|
||||||
|
|
||||||
setup(name='micropython-string',
|
setup(name='micropython-string',
|
||||||
version='0.0.1',
|
version='0.1',
|
||||||
description='string module for MicroPython',
|
description='string module for MicroPython',
|
||||||
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
|
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
|
||||||
url='https://github.com/micropython/micropython/issues/405',
|
url='https://github.com/micropython/micropython/issues/405',
|
||||||
|
|
|
@ -8,3 +8,19 @@ hexdigits = digits + 'abcdef' + 'ABCDEF'
|
||||||
octdigits = '01234567'
|
octdigits = '01234567'
|
||||||
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
|
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
|
||||||
printable = digits + ascii_letters + punctuation + whitespace
|
printable = digits + ascii_letters + punctuation + whitespace
|
||||||
|
|
||||||
|
|
||||||
|
def translate(s, map):
|
||||||
|
import io
|
||||||
|
sb = io.StringIO()
|
||||||
|
for c in s:
|
||||||
|
v = ord(c)
|
||||||
|
if v in map:
|
||||||
|
v = map[v]
|
||||||
|
if isinstance(v, int):
|
||||||
|
sb.write(chr(v))
|
||||||
|
elif v is not None:
|
||||||
|
sb.write(v)
|
||||||
|
else:
|
||||||
|
sb.write(c)
|
||||||
|
return sb.getvalue()
|
||||||
|
|
Ładowanie…
Reference in New Issue