Esempio n. 1
0
def incrbyfloat_handler(client, argv):
    '''
    Increment the string representing a floating point number stored at key by the specified increment.
    If the key does not exist, it is set to 0 before performing the operation. An error is returned if
    one of the following conditions occur:

    * The key contains a value of the wrong type (not a string).
    * The current key content or the specified increment are not parsable as a double precision floating
      point number.

    If the command is successful the new incremented value is stored as the new value of the key (replacing
    the old one), and returned to the caller as a string.

    Both the value already contained in the string key and the increment argument can be optionally provided
    in exponential notation, however the value computed after the increment is stored consistently in the
    same format, that is, an integer number followed (if needed) by a dot, and a variable number of digits
    representing the decimal part of the number. Trailing zeroes are always removed.

    The precision of the output is fixed at 17 digits after the decimal point regardless of the actual internal
    precision of the computation.

    .. code::
        INCRBYFLOAT key increment

    :return: the value of key after the increment.
    :rtype: bytes

    '''

    key, increment = argv[1], argv[2]

    try:
        increment = Decimal(increment.decode())
    except InvalidOperation:
        abort(message='value is not a valid float')

    try:
        obj = get_object(client.db, key, type=RedisStringObject)
        value = obj.get_integer()
    except KeyError:
        value = 0
        obj = RedisStringObject()
    except TypeError:
        abort(errtype='WRONGTYPE', message='Operation against a key holding the wrong kind of value')

    try:
        value = obj.get_decimal()
    except InvalidOperation:
        abort(message='value is not a valid float')

    value += increment

    obj.value = value
    client.db.key_space[key] = obj
    return obj
def get_sqltype_decimal(buf_view, column_desc, nonull_value_offset):
    first_byte, _ = Convert.get_bytes(buf_view[nonull_value_offset:],
                                      length=1,
                                      little=True)
    int_first_byte = int.from_bytes(first_byte, byteorder='little')
    if int_first_byte & 0x80:
        ret_obj, _ = Convert.get_bytes(buf_view[nonull_value_offset:],
                                       length=column_desc.max_len,
                                       little=True)
        ret_obj = '-' + (bytes([ret_obj[0] & 0x7F]) + ret_obj[1:]).decode()
        ret_obj = Decimal(ret_obj) / (10**column_desc.scale)
    else:
        ret_obj, _ = Convert.get_bytes(buf_view[nonull_value_offset:],
                                       length=column_desc.max_len,
                                       little=True)
        ret_obj = Decimal(ret_obj.decode()) / (10**column_desc.scale)
    return ret_obj