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
Paul Sokolovsky 2015-03-28 19:48:36 +02:00
rodzic 9f925a6e60
commit 6e64994ec2
3 zmienionych plików z 18 dodań i 2 usunięć

Wyświetl plik

@ -1,3 +1,3 @@
srctype=micropython-lib
type=module
version=0.0.1
version=0.1

Wyświetl plik

@ -6,7 +6,7 @@ from setuptools import setup
setup(name='micropython-string',
version='0.0.1',
version='0.1',
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.",
url='https://github.com/micropython/micropython/issues/405',

Wyświetl plik

@ -8,3 +8,19 @@ hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
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()