Python中的动态属性访问?
我正在研究一个小方编程项目,我决定承担并遇到一个错误,我不知道如何解决...我已经找到了答案,但实际上我只是找到了似乎是不适用于这种特定情况(或者我可能不是在寻找合适的东西......)。这是我试图运行的代码(不包括完整的程序,它太长了):
def Bet(percent):
Screen = screenGrab()
print 'betting ' + percent
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
mousePos(Cord.percent)
leftClick()
time.sleep(.05)
leftClick()
BetNum = RNDORG.IntegerGenerator(1, 101)
if (BetNum < 51):
print 'Betting Left'
mousePos(Cord.Comp_Left)
leftClick()
time.sleep(.05)
leftClick()
if (BetNum >= 51):
print 'Betting Right'
mousePos(Cord.Comp_Right)
leftClick()
time.sleep(.05)
leftClick()
#(later maybe) calculate best option,click on a percent, click on a competitior
当我在python命令提示符下运行程序时,出现此错误:
Perecentage to bet? (format it as P_%, to go all in type ALLIN)P_10
bettingP_10
Traceback (most recent call last):
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 148, in <module>
main()
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 141, in main
Bet(UPercent)
File "C:\Users\\Documents\Documents\Documents\PythonSaltyBot\SaltyBasicBot.py", line 112, in Bet
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
AttributeError: class Cord has no attribute 'percent'
对我来说最麻烦的部分是python似乎将百分比参数解释为字面上的命令“百分比”,而不是实际传递给它的东西(在这种情况下,P_10如print语句所示)。我有办法处理通过bet()函数传递的内容,并将其限制为只有我有一个Cord参数的参数,我只是不确定如何让python不将“百分比”解释为命令中的“百分比”打电话:
if (Screen.getpixel(Cord.percent) == Colors.percent_button):
所以基本上我问我怎么能用这个参数来调用一个方法(如果那可能......)?我需要把它变成字符串还是什么?如果不可能,我如何解释参数以将其发送到Cord方法?
我对编程比较陌生,有些东西告诉我这不是我应该尝试的东西,或者应该有另一种方法。我想为每个案例创建一种Switch语句(类似于http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html),但我觉得我现在拥有的更有效率。
这是我用来预先过滤传递给参数的代码的代码:
UPercent = raw_input('Perecentage to bet? (format it as P_%, to go all in type ALLIN)')
if (UPercent == ('P_10' or 'P_20' or 'P_30' or 'P_40' or 'P_50' or 'P_60' or 'P_70' or 'P_80' or 'P_90' or 'ALLIN')):
Bet(UPercent)
谢谢,感谢任何回复!
好的,继承了解决的代码(我还修复了“if ... ==”声明,但那不是我所问的)......:
def Bet(percent):
Screen = screenGrab()
if (Screen.getpixel(getattr(Cord, percent)) == Colors.percent_button):
#Check bets-----------------------------------------------------------------
print 'betting ' + percent
mousePos(getattr(Cord, percent))
leftClick()
time.sleep(.05)
leftClick()
BetNum = RNDORG.IntegerGenerator(1, 101)
if (BetNum < 51):
print 'Betting Left'
mousePos(Cord.Comp_Left)
leftClick()
time.sleep(.05)
leftClick()
if (BetNum >= 51):
print 'Betting Right'
mousePos(Cord.Comp_Right)
leftClick()
time.sleep(.05)
leftClick()
#(later maybe) calculate best option,click on a percent, click on a competitior
继续澄清绳子和颜色是什么:
class Cord:
Comp_Left = (730,(ymax-140)-y_pad)
Comp_Right = (1190,(ymax-140)-y_pad)
#CompLeftValue = (,)
#CompRightValue = (,)
TextBox = (864, 767)
P_10 = (220,(ymax-50)-y_pad) #button centers seem to have a 160-170 pixel interval, .085 rati
P_20 = (380,(ymax-50)-y_pad) #60 is the distance from the bottom of the screen to the buttons
P_30 = (540,(ymax-50)-y_pad) #on the windows operating system, so y max -60 will always give
P_40 = (710,(ymax-50)-y_pad) #the approximate y coordinate
P_50 = (870,(ymax-50)-y_pad)
P_60 = (1030,(ymax-50)-y_pad)
P_70 = (1200,(ymax-50)-y_pad)
P_80 = (1370,(ymax-50)-y_pad)
P_90 = (1530,(ymax-50)-y_pad)
AllIN = (1710,(ymax-50)-y_pad)
TEST = (221,912)
class Colors:
percent_button = (100, 65, 165)
background = (26, 26, 26)
left_button = (176, 68, 68)
right_button = (52, 158, 255)
我希望这可以帮助那些遇到同样问题的人,对我来说这是一个令人沮丧的问题。
答案 0 :(得分:1)
语法Cord.percent
并不意味着*从percent
获取 Cord
中名为的属性;它只是意味着从percent
获取名为Cord
的属性。
要动态访问属性,请使用getattr()
function:
getattr(Cord, percent)
现在 Python将取消引用percent
,并从Cord
检索其中指定的属性。
请注意您的过滤器表达式:
UPercent == ('P_10' or 'P_20' or 'P_30' or 'P_40' or 'P_50' or 'P_60' or 'P_70' or 'P_80' or 'P_90' or 'ALLIN')
将无法正常工作,请使用:
UPercent in ('P_10', 'P_20', 'P_30', 'P_40', 'P_50', 'P_60', 'P_70', 'P_80', 'P_90', 'ALLIN')