Example #1
0
def uncompact_int(str_val):
    """ Makes an int from a compact string


    Args:
        str_val (str): The string representing a compact integer value
    Returns:
        int. The integer value
    """
    # int(x, base) not used because it's limited to base 36
    unit = 1
    result = 0
    for char in reversed(str_val):
        result += chars.index(char) * unit
        unit *= base
    log.info('uncompact(%s) = %s', str_val, result)
    return result
Example #2
0
def compact_int(ival):
    """ Makes an int compact


    Args:
        ival (int): the integer value you want to shorten
    Returns:
        str. A string equivalent to the integer but with a more compact representation
    """
    result = []
    rest = ival
    b = base
    while True:
        int_part, rest = divmod(rest, b)
        result.append(chars[rest])
        if not int_part:
            break
        rest = int_part
    result = "".join(reversed(result))
    log.info('compact(%s) = %s', ival, result)
    return result
Example #3
0
try:
    from json import dumps as jdump, loads as jload
    json_engine = "python's built-in"
except ImportError:
    try:
        from cjson import encode as jdump, decode as jload
        json_engine = 'cjson'
    except ImportError:
        try:
            from simplejson import dumps as jdump, loads as jload
            json_engine = 'simplejson'
        except ImportError:
            from demjson import encode as jdump, decode as jload
            json_engine = 'demjson'

log.info("using %s engine."%json_engine)
################################################################################

def dump_data_as_text(d, format):
    """ Dumps simple types (dict, iterable, float, int, unicode)
    as: json or plain text (compromise between human readable and parsable form)
    Returns an iterator returning text
    """
    if format == "json":
        if isinstance(d, GeneratorType):
            d = list(d)
        yield jdump(d)
    elif format == "html":
        yield "<html><body>"
        if isinstance(d, dict):
            for k, v in d.iteritems():