Last active
December 27, 2015 05:39
-
-
Save getadeo/7275936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
def _iter_encode(iterable, func): | |
for item in iterable: | |
encoded_item = func(item) | |
if encoded_item: | |
yield encoded_item | |
encoded_item = func('', True) | |
if encoded_item: | |
yield encoded_item | |
def iter_encode(iterable, codec, errors='strict'): | |
cls = codecs.getincrementalencoder(codec) | |
return _iter_encode(iterable, cls(errors).encode) | |
def iter_decode(iterable, codec, errors='strict'): | |
cls = codecs.getincrementaldecoder(codec) | |
return _iter_encode(iterable, cls(errors).decode) | |
Samples: | |
>>>u'Foo \N{SNOWMAN}'.encode('utf-8') | |
'Foo \xe2\x98\x83' | |
>>>list(iter_decode(_, 'utf-‐8')) | |
[u'F', u'o', u'o', u'', u'\u2603'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment