我正在尝试创建一个 python 类来使用我的 Raspberry Pi 控制步进电机。它主要有效,但是每当我将列表定义为类变量时,我都会收到“'instancemethod' object has no attribute '__getitem__'”错误。错误消息将这段代码列为罪魁祸首但是如果 seq[self.StepCounter][pin]!=0: 我看不出有什么问题。如果我将它定义为实例变量或全局变量,它就会起作用。
这是我的代码:
将 RPi.GPIO 导入为 GPIO
导入时间
调试=真
class stepper:
clockwise = []
clockwise = range(0,4)
clockwise[0] = [1,0,0,0]
clockwise[1] = [0,1,0,0]
clockwise[2] = [0,0,1,0]
clockwise[3] = [0,0,0,1]
def __init__(self,pin1,pin2,pin3,pin4):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
self.pin1 = pin1
self.pin2 = pin2
self.pin3 = pin3
self.pin4 = pin4
self.StepCounter = 0
self.pinarray = [pin1,pin2,pin3,pin4]
for pin in self.pinarray:
if debug == True:
print "Setup pin " + str(pin)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
self.stepNum = 512.0
self.coilNum = 4.0
def setup(self,stepNum,coilNum):
self.stepNum = float(stepNum)
self.coilNum = float(coilNum)
self.partNum = self.coilNum * self.stepNum
def clockwise(self,speed):
seq = stepper.clockwise
self.WaitTime = (1.0 / (self.stepNum * self.coilNum)) * speed
for pin in range(0, 4):
xpin = self.pinarray[pin]
if seq[self.StepCounter][pin]!=0:
GPIO.output(xpin, True)
else:
GPIO.output(xpin, False)
self.StepCounter += 1
if (self.StepCounter==len(seq)):
self.StepCounter = 0
if (self.StepCounter<0):
self.StepCounter = len(seq)
time.sleep(self.WaitTime)
print "Adding Motor Instance"
motor = stepper(24,25,8,7)
print "Spinning Motor"
while "True":
motor.clockwise(5)
请有人告诉我它有什么问题并解释原因。谢谢
最佳答案
你没有发布完整的回溯,但我可以猜测:
def clockwise(self,speed):
seq = stepper.clockwise
self.WaitTime = (1.0 / (self.stepNum * self.coilNum)) * speed
for pin in range(0, 4):
xpin = self.pinarray[pin]
if seq[self.StepCounter][pin]!=0:
您将 seq 设置为等于第一行的方法 stepper.clockwise。几行之后,您尝试对其进行索引:seq[self.StepCounter]。但是获取方法的第 self.StepCounter 元素意味着什么?
没什么,因为:
'instancemethod' object has no attribute '__getitem__'
你不应该将 clockwise 既用作列表的名称又用作方法的名称;只有最后执行的定义会保留,所以当您到达 seq = stepper.clockwise 时,它是方法,而不是列表。
关于python - 'instancemethod' 对象没有带有类变量的属性 '__getitem__',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21365521/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(