Run my Python Functions on the terminal

Hi,

I have no experience with Linux and python, but thanks to http://aquaticus.info/beaglebone-web-led I’ve just written my first python scrip for the BBB .
As you can see at the end of the post, the code has one function to turn on, and other to turn off an onboard led.

The problem is that at the end of the script I’ve added a line to turn on the led. I wonder if I could use directly the functions "OnLed " and "OffLed " in the terminal without having to run a script with lines like “OnLed (2)”.

def writeDevice(device, value):
with open(device,“w”) as f:
f.write("%d\n" % int(value))
def OnLed (Led):
if Led in [0,1,2,3]:
writeDevice("/sys/class/leds/beaglebone:green:usr" + str(Led) + “/brightness”,1)
else:
print str(Led) + " is not correct value for a LED"
def OffLed (Led):
if Led in [0,1,2,3]:
writeDevice("/sys/class/leds/beaglebone:green:usr" + str(Led) + “/brightness”,0)
else:
print str(Led) + " is not correct value for a LED"
OnLed (2)

Thanks you in advance!

Hello,

Let me know if you would like further explaining but it is fairly simple:

Consider your script. filename.py, with the function myfunc().
You would you the following at the shell prompt:

python -c 'import filename; filename.myfunc()'

Of course since this is arduous, you could make an alias in your shell.

alias myfunkyPy="python -c 'import filename; print filename.myfunc()'"

And for it to persist on your next session, you would want to add that to your ~/.bashrc file.

See:
http://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line
http://tldp.org/LDP/abs/html/aliases.html

:slight_smile:
PS I have not inspected your code, run at your own risk