#NEWLINE# def format_args_in_redis_protocol( * args ) : #NEWLINE# #INDENT# 'Formats arguments into redis protocol...\n\n    This function makes and returns a string/buffer corresponding to\n    given arguments formated with the redis protocol.\n\n    integer, text, string or binary types are automatically converted\n    (using utf8 if necessary).\n\n    More informations about the protocol: http://redis.io/topics/protocol\n\n    Args:\n        *args: full redis command as variable length argument list\n\n    Returns:\n        binary string (arguments in redis protocol)\n\n    Examples:\n        >>> format_args_in_redis_protocol("HSET", "key", "field", "value")\n        \'*4\r\n$4\r\nHSET\r\n$3\r\nkey\r\n$5\r\nfield\r\n$5\r\nvalue\r\n\'\n    ' #NEWLINE# buf = WriteBuffer ( ) #NEWLINE# l = ( '*%d\r\n' % len ( args ) ) #NEWLINE# if six . PY2 : #NEWLINE# #INDENT# buf . append ( l ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# buf . append ( l . encode ( 'utf-8' ) ) #NEWLINE# #UNINDENT# for arg in args : #NEWLINE# #INDENT# if isinstance ( arg , six . text_type ) : #NEWLINE# #INDENT# arg = arg . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( arg , six . string_types ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( arg , six . binary_type ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( arg , six . integer_types ) : #NEWLINE# #INDENT# tmp = ( '%d' % arg ) #NEWLINE# if six . PY2 : #NEWLINE# #INDENT# arg = l #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# arg = tmp . encode ( 'utf-8' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( arg , WriteBuffer ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( ( "don't know what to do with %s" % type ( arg ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# l = ( '$%d\r\n' % len ( arg ) ) #NEWLINE# if six . PY2 : #NEWLINE# #INDENT# buf . append ( l ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# buf . append ( l . encode ( 'utf-8' ) ) #NEWLINE# #UNINDENT# buf . append ( arg ) #NEWLINE# buf . append ( b'\r\n' ) #NEWLINE# #UNINDENT# return buf