Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(9)  |  Call(5)  |  Derive(0)  |  Import(4)

        def safe_encode(u, encoding=None):
    '''Similar to unicode `encode` method returning bytes.

    Encodes `u` using the given `encoding`, or determining one from the system.

    Returning type is always `bytes`; but in python 2.x is also `str`.

    .. versionadded:: 1.1.3

    '''
    if isinstance(u, bytes):
        return u
    else:
        encoding = force_encoding(encoding)
        try:
            if isinstance(u, _str_base):
                # In Python 2.x bytes does not allows an encoding argument.
                return bytes(u)
            else:
                return _unicode(u).encode(encoding, 'replace')
        except:
            return _unicode(u).encode(encoding, 'replace')
        


src/x/o/xoutil-1.5.4/xoutil/fs/__init__.py   xoutil(Download)
        """
        import stat as st
        from xoutil.string import safe_encode
        def _get_masked_mode(mode):
            mask = os.umask(0)
            cdir = os.curdir
            if isinstance(tail, str):
                cdir = safe_encode(os.curdir, 'ASCII')
            if tail == cdir:     # xxx/newdir/. exists if xxx/newdir exists
                return

src/x/o/xoutil-1.5.4/xoutil/html/__init__.py   xoutil(Download)
def _further_escape(s):
    import re
    from xoutil.string import safe_encode
    ASCII = getattr(re, 'ASCII', 0)  # Py3k
    what = re.compile(br'[\x00-\x1F\x80-\xFF]', ASCII)
        assert start + 1 == end
        res += s[pos:start]
        res += b'&#' + safe_encode(str(ord(char))) + b';'
        pos = end
    res += s[pos:]
        """
        from xoutil.six import text_type
        from xoutil.string import safe_decode, safe_encode
        if not isinstance(s, text_type):
            arg = safe_decode(s)
            res = arg.translate(_escape_map)
        if not isinstance(res, type(s)):
            return safe_encode(res)
        return res
 

src/x/o/xoutil-1.5.4/tests/test_string.py   xoutil(Download)
def test_normal_safe_formatter():
    from xoutil.string import SafeFormatter, safe_encode
 
    f = SafeFormatter(x=1, y=2)
    result = f.format(safe_encode('CWD: "{cwd}"; "x+d["x"]": {x+d["x"]}.'),
                      cwd=safe_encode('~/tmp/foĆ³bar'), d=dict(x=1))