call function inside argument of a function in python - python

I am starting with python, and I am trying to understand the sample code that Phidget website give to me (I bought a Phidget Bridge 4 input where I have plug on 4 gauge cell). For my project I need to use python but not much use to it.
To read only one channel, sol only one gauge cell with my Phidget bridge, the website give me this code.
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time
def onVoltageRatioChange(self, voltageRatio):
print("VoltageRatio: " + str(voltageRatio))
def main():
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
voltageRatioInput0.openWaitForAttachment(5000)
time.sleep(5)
voltageRatioInput0.close()
main()
There is the function def onVoltageRatioChange(self, voltageRatio): which takes 2 arguments, self and voltageRatio, and this function is used inside setOnVoltageRatioChangeHandler(onVoltageRatioChange) with no argument. What I mean, it's that I do not get why we give as an argument onVoltageChange (which is normally a function) to the function setOnVoltageRatioChangeHandler...
It someone could explain it to me, it could help me to build my own code... at least try..
thank you

Phidget22 knows how to detect a voltage ratio change, but it doesn't know what you want to do with it: maybe you want to sound an alarm. maybe you want to send an email with the new ratio. maybe you just want to print it to the screen.
So it uses setOnVoltageRatioChangeHandler to ask you what you would like to do once it detects this voltage change, saying "hey, if you'll give me a function that takes a voltage ratio, I'll call it when I detect a change" then you can do:
setOnVoltageRatioChangeHandler(soundTheAlarm)
or
setOnVoltageRatioChangeHandler(sendAnEmail)
or:
setOnVoltageRatioChangeHandler(printToScreen)
The supplied onVoltageRatioChange will just print the new value to the screen.
Generally, passing a function as an argument is a great way to give flexibility in the way events are handled. It's basically saying "I know how to detect a certain situation or get a certain value. You let me know what you want me to do once I do"

Related

i have challenges implementing OOP in python

I am facing challenges implementing OOP in python to enable me to call the functions whenever i want , so far i have no syntax errors which makes quite challenging for me . The first part of the code runs ehich is to accept data but the function part does not run.
I have tried different ways of calling the function by creating an instance of it.
print (list)
def tempcheck(self,newList):
temp=newList[0]
if temp==27:
print ("Bedroom has ideal temperature ")
elif temp>=28 or temp<=26:
print ("Bedroom Temperature is not ideal ,either too low or too cold. ")
print ("Please to adjust the temperature to the optimum temperature which is 27 degree Celsuis")
# now to initialize args
def __init__(self,temp,puri1,bedwashroom,newList):
self.temp=temp
self.puri1=puri1
self.bedwashroom=bedwashroom
tempcheck(newList)
# now calling the functions
newvalue=tempcheck(list)
# where list contains the values from the input function.
I expected the function to to check the specific value at the location in the list provided which is called list and also for the function to return a string based on the if statements.
i got it right ,i figured out an alternative to my bug thanks for the critique however any further addition is welcome,
the main goal was to create a function that takes input and passes it to list to be used later i guess this code is less cumbersome
the link to the full code is pasted below

Repeating a function with another function

for an assignment we needed to make a function that flipped a coin and another to flip it 100 times. I was able to make a function that flipped a coin, but got stuck when trying to call it a 100 times with another function. This is what I have right now:
import random
def TC():
face = random.randint(0,1)
if face == 1:
return "head"
else:
return "tail"
print TC()
def ply(flips):
for i in range(flips):
return TC()
print ply(100)
When I run it it just says 'none.' Please tell me where I am going wrong. Thank You!
Just to start, your method naming is very bad. I doubt this is how your professor is teaching you to name methods and variables. It's ugly, against Python standards and hard to read I suggest you take some time and read PEP 8 it's how python was intended to be written.
So instead of TC you should use something like flip_coin and instead of ply use something like play_coin_flip or even simply play.
Next I don't know if I'm stepping outside of what you have learned but instead of using randon.randint you can use randon.choice.
And finally, as others have said, when you return you quit any other execution in a function and return whatever variable you retrun in that statement thus nullifying any other iterations of the loop you're performing. I suggest something like the below as a better program with corrections applied to make it function as intended.
from random import choice
faces = ['head', 'tail']
def flip_coin():
face = choice(faces)
return face
def play_coin_flip(flips = 1):
for i in range(flips):
print(flip_coin)
if __name__ == "__main__":
play_coin_flip(100)

Check if my point is in a part of my screen

I know the title is confusing but I don't know how to make it any better. I didn't find anything on google so here I am. So I am using OpenCV and I have a small screen where if my point(Template matches) I would like to give my location(x, y) a name. Check the picture for better understanding, sorry!
So you get the idea. My script will post a value like x, y every 3 sec and I want to use a function to determine where the point is located. Something to return saying "top side" or "bot side" etc. I don't know where to begin thanks.
It doesn't have to be python if you can provide vb.net example that would work too.
By using opencv python use:
import cv2
img=cv2.imread("yourPath")
h,w,_= img.shape()
def func(x,y):
num_h=h/numOfColumnYouWant
num_w=w/numOfColumnYouWant
height_num=(y/num_h)%numOfColumnYouWant
width_num=(x/num_w)%numOfColumnYouWant
return height_num,width_num
the return value from func(x,y) is the row and column, if you want name you can change numOfColumnYouWant to 2 and if for example it return 0,0 you know it top left

python autopy problems/confusion

so im trying to make a bot script that when a certain hex color is on a certain pixel it will execute some code to move the mouse,click etc. and i have it to where it takes a screenshot every 1 second to the same png file and updates the png file's pic. i have the hex color for the pixel cords print to the console so i can see if its updating or not. it never updates it just stays the same. ive tried writing this script many ways and sadly i only have one version to show you but hopefully you will understand what i was trying to accomplish. im on python 2.7 btw. thank you all for your time!!!!
import autopy
from time import sleep
color_grabber = hex(autopy.bitmap.Bitmap.open("screen1.png").get_color(292,115))
def color_checker():
global color_grabber
color_grabber = color_grabber
return
def mouse_move_click():
autopy.mouse.smooth_move(433,320)
autopy.mouse.click()
def screen_grab():
autopy.bitmap.capture_screen().save("screen1.png")
def the_ifs(mouse_move_click):
if color_checker == "0xffcb05":
mouse_move_click()
while 1==1:
sleep(1)
screen_grab()
color_checker()
the_ifs(mouse_move_click)
print color_grabber
from autopy.mouse import LEFT_BUTTON
autopy.mouse.click(LEFT_BUTTON)
autopy.mouse.toggle(True, LEFT_BUTTON)
autopy.mouse.toggle(False, LEFT_BUTTON)
I see the need to do this in other people's code, but I don't understand why want to use the up and down after the click.In fact when I test on Windows 7, click is effective, but is not very correct, feel more like the down to my operation
I believe your problem is how you're using color_grabber. Saying color_grabber = color_grabber does nothing. What's happening in your code is that when you run it, after the imports, you define the value of color_grabber to be the color in your image. Then your while loop executes and in that loop you call color_checker. This function brings in the variable color_grabber from the global namespace and then you set that variable equal to itself. You're not re-executing the command you used to define color_grabber in the first place. You're just storing the color value back into itself so clearly its not going to change.
You also have a problem in how you're calling your mouse_move_click function. You don't want to pass in the function name, as that isn't really necessary. However, you also performed the check color_checker == "0xffcb05" which was comparing your function (the function itself, not the returned value) to the hex code. That doesn't do you any good. You want to compare the color. The solution is to pass the color into the_ifs and use that color to compare to the hex code. I should note though that you don't need to make the_ifs into its own function. Just put that if statement in your while loop. I left it how you had it though.
What you want is something like this.
import autopy
from time import sleep
def color_checker():
color_grabber = hex(autopy.bitmap.Bitmap.open("screen1.png").get_color(292,115))
return color_grabber
def mouse_move_click():
autopy.mouse.smooth_move(433,320)
autopy.mouse.click()
def screen_grab():
autopy.bitmap.capture_screen().save("screen1.png")
def the_ifs(color):
if color == "0xffcb05":
mouse_move_click()
while 1==1:
sleep(1)
screen_grab()
color = color_checker()
the_ifs(color)
print color
Note that I have not run this code myself so I can't guarantee it works, but I believe it should.

3dsmax python add float script to sub animations

I have the next setup: I have a sphere which has a morpher modifier. This morpher modifier has a certain amount of channels filled with morph targets aka sub animations. Now I want to add a controller to each of these subanimations, more specifically a controller with a float script. I have the code snippet that should work but when I go to the curve editor, the morph channels/ sub animations did not change controller, nor is the value of their controller changed.
import MaxPlus
target = MaxPlus.INode.GetINodeByName('Sphere001')
#Retrieve the morpher modifier
mod = target.GetModifier(0)
#ID of a float script controller
id = MaxPlus.Class_ID(1233584870,1911625032)
#Create float controller
float_co = MaxPlus.Factory.CreateFloatController(id)
#Retrieve the first morph channel / sub animation
sub = mod.GetSubAnim(1)
#Controller is assigned to the sub animation
sub.AssignController(float_co,1)
#Basic test which assigns 20 to the sub animation
float_co.ParameterBlock.Script.Value = '20'
When I add a wrong value to the script, for example:
float_co.ParameterBlock.Script.Value = '=20'
I receive an error and the usual window when you manually add a controller to an object or node. However the strange thing is that at the top of the window: the name of the object to which it is connected, does not show. See figure for clarification:
Can someone tell me what I'm doing wrong? Thank you!
I solved it by using the ugly way:
import MaxPlus
test = MaxPlus.FPValue()
success = MaxPlus.Core.EvalMAXScript(string_with_command,test)
This is used twice: first to create the float script controller and a second time to add the script to the controller. Be careful if anyone wants to try this, the script for the controller needs to be a string. Do not use
x as string
with the expressing you want to use as script for the float script controller since maxscript will evaluate x on the timeframe you are currently in 3ds max and will convert this value to a string. This value will be used as script for all time frames which clearly is not what you want. A small hack I used was:
script_value_example = '"amax #(0, ($sphere.position.x - cube.position.y))"'
This is still a string for python and maxscript will see the " " and will interpret it as a string. The other way around doesn't work, maxscript does not interpret ' ' as string.
Maybe this will help someone in the future. Also if someone knows the proper way to do it using the code in the question, please leave a reply, I'm interested to know.

Categories

Resources