从 Ruby 调用 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3679501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:12:35  来源:igfitidea点击:

Calling Python from Ruby

pythonruby

提问by Bryan Ash

I have a compiled Python library and API docs that I would like to use from Ruby.

我有一个编译好的 Python 库和 API 文档,我想从 Ruby 中使用它们。

Is it possible to load a Python library, instantiate a class defined in it and call methods on that object from Ruby?

是否可以加载 Python 库、实例化其中定义的类并从 Ruby 调用该对象上的方法?

采纳答案by kindall

This articlegives some techniques for running Ruby code from Python which should also be applicable in the reverse direction (such as XML-RPC or pipes) as well as specific techniques for running Python code from Ruby. In particular rubypythonor Ruby/Pythonlook like they may do what you want.

本文提供了一些从 Python 运行 Ruby 代码的技术,这些技术也应该适用于反向(例如 XML-RPC 或管道),以及从 Ruby 运行 Python 代码的特定技术。特别是rubypythonRuby/Python看起来他们可能会做你想做的。

回答by AShelly

Are you on Windows? Could you create a DLL or COM object from your python liband call it with Ruby's Win32Apior Win32Ole?

你在 Windows 上吗?你能从你的 python 库中创建一个DLL 或 COM 对象并用 Ruby 的Win32ApiWin32Ole调用它吗?

回答by Joshua

Even you can make this work, you might want to consider if this is the best architectural choice. You could run into all sorts of versioning hell trying to maintain such a beast.

即使您可以完成这项工作,您也可能需要考虑这是否是最佳架构选择。试图维护这样一个野兽,你可能会遇到各种版本控制地狱。

If you really can't find an equivalent Ruby library (or it's a big investment in Python you want to leverage,) consider using a queue (like RabbitMQ) to implement a message passing design. Then you can keep your Python bits Python and your Ruby bits Ruby and not try to maintain a Frankenstein build environment.

如果您真的找不到等效的 Ruby 库(或者您想利用 Python 的大量投资),请考虑使用队列(如 RabbitMQ)来实现消息传递设计。然后你可以保持你的 Python 位 Python 和你的 Ruby 位 Ruby,而不是试图维护一个 Frankenstein 构建环境。

回答by postfuturist

This little library makes it super easy to do this: https://github.com/steeve/rupy

这个小库让这件事变得非常容易:https: //github.com/steeve/rupy

回答by Martin

It sounds like you would want to use something like Apache Thrift which allows either your python or your ruby code to be a server/client and call each other. http://thrift.apache.org/

听起来你想使用像 Apache Thrift 这样的东西,它允许你的 python 或你的 ruby​​ 代码成为服务器/客户端并相互调用。 http://thrift.apache.org/

You can instantiate your objects in ruby and or in python based on your thrift definition. This is an example from the thrift website.

您可以根据您的 thrift 定义在 ruby​​ 和/或 python 中实例化您的对象。这是节俭网站上的一个例子。

struct UserProfile {
    1: i32 uid,
    2: string name,
    3: string blurb
  }
  service UserStorage {
    void store(1: UserProfile user),
    UserProfile retrieve(1: i32 uid)
  }

Basically your ruby or python will be able to call store()and retrieve()and create UserProfileobjects etc.

基本上,你的Ruby或Python将能够调用store()retrieve()创造UserProfile对象等。

回答by HappyFace

https://github.com/mrkn/pycall.rb

https://github.com/mrkn/pycall.rb

Here is a simple example to call Python's math.sin function and compare it to the Math.sin in Ruby:

这是调用 Python 的 math.sin 函数并将其与 Ruby 中的 Math.sin 进行比较的简单示例:

require 'pycall/import'
include PyCall::Import
pyimport :math
math.sin(math.pi / 4) - Math.sin(Math::PI / 4)   # => 0.0

Type conversions from Ruby to Python are automatically performed for numeric, boolean, string, arrays, and hashes.

从 Ruby 到 Python 的类型转换会自动为数字、布尔值、字符串、数组和散列执行。