我正在尝试运行我使用 RubyPython 从 Ruby on Rails 控制器编写的 Python 脚本
版本号是
Ruby 2.1.2p95
Rails 4.1.6
RubyPython 0.6.3
Python 3.2.3
Python 类没有存储在控制器目录中,但是我有两个指向 Python 脚本的符号链接。
lrwxrwxrwx 1 pi pi 30 Sep 16 22:17 current_lamp_state.py -> ../../../current_lamp_state.py
lrwxrwxrwx 1 pi pi 30 Sep 16 22:33 CurrentLampState.py -> ../../.. /current_lamp_state.py
当我尝试使用其中一个符号链接导入 Python 类时,出现以下错误之一
ImportError:没有名为 current_lamp_state 的模块
或
ImportError:没有名为 CurrentLampState 的模块
我也尝试将 Python 代码放在控制器目录中,但我得到了同样的错误
Python 脚本的代码是
import unittest
import os
from lamp_state import LampState
class CurrentLampState(unittest.TestCase):
CURRENT_STATE_FILENAME = 'current_lamp_state.txt'
def get(self):
if(os.path.isfile(self.CURRENT_STATE_FILENAME)):
file = open(self.CURRENT_STATE_FILENAME, "r")
value = file.readline()
file.close()
return int(value)
else:
lampState = LampState()
return lampState.OFF
def set(self, newState):
file = open(self.CURRENT_STATE_FILENAME, "w")
file.write(str(newState))
file.close()
def test_givenTheLampStateIsUnknown_whenGetIsCalled_thenTheCurrentLampStateShouldBeOff(self):
if(os.path.isfile(self.CURRENT_STATE_FILENAME)):
os.remove(self.CURRENT_STATE_FILENAME)
lampState = LampState()
expected = lampState.OFF
actual = self.get()
self.assertEqual(expected, actual)
def test_givenTheLampStateOfHigh_whenGetIsCalled_thenTheCurrentLampStateShouldBeHigh(self):
lampState = LampState()
expected = lampState.HIGH
self.set(expected)
actual = self.get()
self.assertEqual(expected, actual)
def test_givenALampState_whenSetIsCalled_thenTheLampStateShouldBeSaved(self):
lampState = LampState()
expected = lampState.LOW
self.set(expected)
actual = self.get()
self.assertEqual(expected, actual)
if __name__ == '__main__':
unittest.main()
控制器的 Ruby 代码是
require "rubypython"
class LampController < ApplicationController
def lamp
RubyPython.start
lamp_state = RubyPython.import 'current_lamp_state'
RubyPython.stop
end
end
我是 Ruby 和 Rails 的新手,我从来没有真正在愤怒中使用过 Python,所以我知道我做错了什么,我只是不知道是什么。任何建议将不胜感激。
PS我真的不喜欢与类在同一个文件中的单元测试,但从我所读到的,这是Python的做法。