# Path: cuburn/code/util.py
# def crep(s):
#     """Multiline literal escape for inline PTX assembly."""
#     if isinstance(s, unicode):
#         s = s.encode('utf-8')
#     return '"%s"' % s.encode("string_escape")


import base64
import numpy as np
import spectypes
from hashlib import sha1
from cuburn.code.util import crep
def _js_enc_obj(obj, indent=0):
    isnum = lambda v: isinstance(v, (float, int, np.number))

    def wrap(pairs, delims):
        do, dc = delims
        i = ' ' * indent
        out = ''.join([do, ', '.join(pairs), dc])
        if '\n' not in out and len(out) + indent < 70:
            return out
        return ''.join(['\n', i, do, ' ', ('\n'+i+', ').join(pairs),
                        '\n', i, dc])

    if isinstance(obj, dict):
        if not obj:
            return '{}'
        digsort = lambda kv: (int(kv[0]), kv[1]) if kv[0].isdigit() else kv
        ks, vs = zip(*sorted(obj.items(), key=digsort))
        if ks == ('b', 'g', 'r'):
            ks, vs = reversed(ks), reversed(vs)
        ks = [crep('%.6g' % k if isnum(k) else str(k)) for k in ks]
        vs = [_js_enc_obj(v, indent+2) for v in vs]
        return wrap(['%s: %s' % p for p in zip(ks, vs)], '{}')
    elif isinstance(obj, list):
        vs = [_js_enc_obj(v, indent+2) for v in obj]
        if vs and len(vs) % 2 == 0 and isnum(obj[1]):
            vs = map(', '.join, zip(vs[::2], vs[1::2]))
        return wrap(vs, '[]')
    #elif isinstance(obj, SplEval):
        #return _js_enc_obj(obj.knotlist, indent)
    elif isinstance(obj, basestring):

# Path: cuburn/code/util.py
# def crep(s):
#     """Multiline literal escape for inline PTX assembly."""
#     if isinstance(s, unicode):
#         s = s.encode('utf-8')
#     return '"%s"' % s.encode("string_escape")


# Path: cuburn/genome/util.py
import base64
import numpy as np
import spectypes
from hashlib import sha1
from cuburn.code.util import crep

def _js_enc_obj(obj, indent=0):
    isnum = lambda v: isinstance(v, (float, int, np.number))

    def wrap(pairs, delims):
        do, dc = delims
        i = ' ' * indent
        out = ''.join([do, ', '.join(pairs), dc])
        if '\n' not in out and len(out) + indent < 70:
            return out
        return ''.join(['\n', i, do, ' ', ('\n'+i+', ').join(pairs),
                        '\n', i, dc])

    if isinstance(obj, dict):
        if not obj:
            return '{}'
        digsort = lambda kv: (int(kv[0]), kv[1]) if kv[0].isdigit() else kv
        ks, vs = zip(*sorted(obj.items(), key=digsort))
        if ks == ('b', 'g', 'r'):
            ks, vs = reversed(ks), reversed(vs)
        ks = [crep('%.6g' % k if isnum(k) else str(k)) for k in ks]
        vs = [_js_enc_obj(v, indent+2) for v in vs]
        return wrap(['%s: %s' % p for p in zip(ks, vs)], '{}')
    elif isinstance(obj, list):
        vs = [_js_enc_obj(v, indent+2) for v in obj]
        if vs and len(vs) % 2 == 0 and isnum(obj[1]):
            vs = map(', '.join, zip(vs[::2], vs[1::2]))
        return wrap(vs, '[]')
    #elif isinstance(obj, SplEval):
        #return _js_enc_obj(obj.knotlist, indent)
    elif isinstance(obj, basestring):

        return crep(obj)