From 6e64994ec27293bce2da7a1e2da88993c08dc2e8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 28 Mar 2015 19:48:36 +0200 Subject: [PATCH] string: Add implementation of translate() method. As MicroPython doesn't have str.translate() method, here it's implemented as string module function. --- string/metadata.txt | 2 +- string/setup.py | 2 +- string/string.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/string/metadata.txt b/string/metadata.txt index 7fd02826..6b5dc732 100644 --- a/string/metadata.txt +++ b/string/metadata.txt @@ -1,3 +1,3 @@ srctype=micropython-lib type=module -version=0.0.1 +version=0.1 diff --git a/string/setup.py b/string/setup.py index a0895c3c..a8cd9327 100644 --- a/string/setup.py +++ b/string/setup.py @@ -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', diff --git a/string/string.py b/string/string.py index dff21124..d8ed89e3 100644 --- a/string/string.py +++ b/string/string.py @@ -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()