【问题标题】:Redis lua scripting - What is the python equivalent of the lua script written in Ruby? [closed]Redis lua scripting - 用 Ruby 编写的 lua 脚本的 python 等价物是什么? [关闭]
【发布时间】:2012-06-08 16:54:25
【问题描述】:

我不知道 ruby​​,但我知道 python。运行以下代码的 python 等效项是什么?这个例子取自 redis 网站。那么,这是什么?

<<EOF?

它会在 python 中吗:

  RandomPushScript = """
    Lua code here
   """


RandomPushScript = <<EOF
    local i = tonumber(ARGV[1])
    local res
    math.randomseed(tonumber(ARGV[2]))
    while (i > 0) do
        res = redis.call('lpush',KEYS[1],math.random())
        i = i-1
    end
    return res
EOF

r.del(:mylist)
puts r.eval(RandomPushScript,1,:mylist,10,rand(2**32))

【问题讨论】:

  • 抱歉,“用 Ruby 编写的 Lua 脚本”?? Lua 和 Ruby 是不同的语言,Python 是第三种。
  • @DanielRoseman 如上所见,它是一个 Ruby 脚本,似乎将 Lua 脚本创建为一个大字符串,然后对其进行处理。
  • 很少有关于使用 redis 编写 lua 脚本的文档。确实存在的文档;它使用 ruby​​ 将 lua 函数传递给 redis。对于python程序员来说,如果有python例子就好了。

标签: python ruby redis


【解决方案1】:

&lt;&lt; 表示 HereDoc。在它之后有一个标记(这里是EOF)。下一行是一个字符串,直到标记再次出现。 所以是的,""" 将是 Python 的等价物。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2011-08-23
  • 1970-01-01
  • 2011-05-24
  • 2018-02-04
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多