Skip to content

Instantly share code, notes, and snippets.

@arc279
Created February 23, 2017 05:46
Show Gist options
  • Save arc279/7ec6b170e5b29198717c5c570ef960cd to your computer and use it in GitHub Desktop.
Save arc279/7ec6b170e5b29198717c5c570ef960cd to your computer and use it in GitHub Desktop.
def cmd2bulk(*cmd):
z = lambda x: ((lambda y=bytes(str(x), "utf-8"): len(y))(), x)
return "*%d\r\n%s" % (len(cmd), "".join(["$%d\r\n%s\r\n" % z(x) for x in cmd]))
if __name__ == '__main__':
import sys
import unittest
class TestRedisBulk(unittest.TestCase):
def test_redis_proto1(self):
expect = "*3\r\n$3\r\nSET\r\n$6\r\nKEY985\r\n$8\r\nVALUE985\r\n"
result = cmd2bulk("SET", "KEY985", "VALUE985")
self.assertEqual(expect, result)
def test_redis_proto2(self):
expect = "*5\r\n$4\r\nSADD\r\n$4\r\nhoge\r\n$9\r\nあああ\r\n$6\r\nいい\r\n$3\r\nう\r\n"
result = cmd2bulk("SADD", "hoge", "あああ", "いい", "う")
self.assertEqual(expect, result)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment