last day (17 days later) » 

10:26 PM
0
A: Exit from an if [Python]

deweyredmanTry this out for size: if you're talking about truly exiting your program...this should work. #!/usr/bin/python2.7 import os import sys import re import time global state #print status_on.group() def main(): get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'""" ...

 
Nope, what I am trying to do is to execute only one time the "notify-send ecc..." command when the status_on == state
 
You realize that you're resetting state, and checking it again in the next if statement, right?
also, as someone mentioned above, you have main wrapped in a while True...
Check out my edits: i'd recommend using if and elif in stead of two ifs. That might be the root of your problem...
 
Yes I know that I've used while true 'cause I have to run this script in background. What I am trying to do is to send a notification when the jack is plugged in or unplugged.
 
just try the if/ elif edits...that should do the trick then, I believe.
Actually, I think I see what you need to do...give me a second and I'll edit my post.
Check out my updated answer: 1) state is initialized to the current state. 2) whenever status_on is not state, reset it to state, and indicate the change that happened.
 
I've executed the script but when I added (for debugging) a "print 'true' after the "plugged in" case it prints infinite
 
10:26 PM
hello, are you there?
 
yep
 
alright, so you're saying that you're seeing an infinite loop?
 
so what i want to do is to get a notify-send when the headphone jack is plugged in or unplugged
yep i'm in an infinite loop
 
right, i've got that...lol
 
.-.
 
10:28 PM
so, can you try throwing a "print state" at the first line of main and see what you get?
 
yep
let's edit
#!/usr/bin/python2.7
import os
import re
import time
global state


#print status_on.group()
def main():
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if status_on:
state = status_on
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i /home/ivan/Immagini/audacity.png")
if status_on == state:
print "True"
state = 1
if status_on != state:
print state
this is the main code
it prints an infinite loop of "bla"
#!/usr/bin/python2.7
import os
import re
import time
global state


#print status_on.group()
def main():
print "bla"
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if status_on:
state = status_on
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i /home/ivan/Immagini/audacity.png")
if status_on == state:
print "True"
state = 1
if status_on != state:
 
right, "bla" is correct, as you have main looped infinitely.
 
yep
 
did you try the code i linked?
 
did you understand what i'm trying to do so... right?
 
10:31 PM
I dont' see it there
 
yep
but it fails
now i'll retry
 
right, i was saying what happens if you insert "print state" at the first line of main in my code
 
UnboundLocalError: local variable 'state' referenced before assignment
 
in main?
 
yep
 
10:32 PM
hmmm that's intereseting because it was defined as a global
let me see if i get that
 
check the tabulation of while True 'cause main has got double \t
 
so:
#!/usr/bin/python2.7
import os
import sys
import re
import time
global state

#print status_on.group()
def main():
print state
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if status_on != state:
state = status_on
if status_on:
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
else:
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
you tried that out?
 
yep
 
if you take out the print state, what happens?
do you still get the unboundlocalerror?
 
yep
 
10:35 PM
i thought you said it printed something infinitely? i'm a little confused
 
maybe i should use acpi_listen and do a diff?
yep
it printed something infinitely
 
right, so it's not giving you the unboundlocalerror then?
 
yep
 
can you screenshot me what you ran?
 
if i define state as "a" it prints infinitely
ok
 
10:36 PM
state is going to either be a positive number or -1
 
where do i paste the screenshot?
1
1
1
1
1
1
1
1
1
1
1
1
1
1
 
imgur.com i guess
 
this is the output
 
actually,I think status_on is giving you different values
and taht's our problem
 
10:37 PM
one sec
got it
let me amend
 
yeah, the match object is different every time
i'm going to amend my ansewr, one sec
 
so, try replacing the first if statement in main with..
if (not status_on and state) or (not state and status_on):
 
so it should work with that edit?
 
10:41 PM
try that out
 
on line 15?
 
#!/usr/bin/python2.7
import os
import sys
import re
import time
global state

#print status_on.group()
def main():
get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'"""
output = os.popen(get_current_jack_status).read()
status_on = re.search('values=on', output)
if (not status_on and state) or (not state and status_on):
state = status_on
if status_on:
os.system("notify-send 'Audio Manager' 'An audio jack was plugged in' -i audacity.png")
else:
os.system("notify-send 'Audio Manager' 'An audio jack was unplugged' -i audacity.png")
 
done
but still printing infinitely 1
 
that's basically saying
 
so... how can i do this?
 
10:43 PM
you see what I'm trying to accomplish, right?
essentially, if your regex finds values=on
it returns a match object
if it doesn't, it returns None
so...
you want to detect a cahnge
 
yep
exactly
 
if (status_on is None and state) or (state is None and status_on):
howa bout that instead ?
 
wait a moment
but if i do a killall notify-osd and then i execute the main function?
nope
 
that didn't wor, either?
work*
 
nope it didn't work
 
10:47 PM
well...lol
one question
have you tried plugging and unplugging and seeing what happens?
 
yep
 
what happens?
 
when i plug the jack it prints true infinitely
 
wait a second
in the code i sent you there should be no print statements/
?
 
right
 
10:48 PM
try the code exactly as it is in my answer
 
i've lost it :'(
resend plz
 
0
A: Exit from an if [Python]

deweyredmanTry this out for size: if you're talking about truly exiting your program...this should work. #!/usr/bin/python2.7 import os import sys import re import time global state #print status_on.group() def main(): get_current_jack_status = """amixer -c1 contents | grep -A 2 'Headphone Jack'""" ...

just copy paste the code portion
if I paste here you'll lose formatting
 
Traceback (most recent call last):
File "jack.py", line 27, in <module>
main()
File "jack.py", line 13, in main
if (status_on is None and state) or (state is None and status_on):
UnboundLocalError: local variable 'state' referenced before assignment
 
one second
 
10:55 PM
alright...hmm
 
# When plugged in:
cat /proc/asound/card1/codec#0 > pluggedin.txt

# When not plugged in:
cat /proc/asound/card1/codec#0 > notplugged.txt

# Then compare the differences
DIFF=$(diff pluggedin.txt notplugged.txt)
if i use bash instead?
 
i recreated somewhat your script in my environment
one sec
 
ok
paste in pastebin or something similar
 
sure
give me a second i'm troubleshooting in vim :P
 
11:01 PM
alright
i found an issue
so move the "global state" from outside main
to inside main
 
as the first line
 
def main():
global state

state = None
so it should be that. right?
 
none
where are you getting state = NOne?
lol
no*
 
ah sorry ahhaha
:* works
 
11:02 PM
it does?
 
yes
 
ayyyy
lol
 
thank you <3
.-.
 
can you please accept my answer/
i'll,. modify it
 
nice to meet you deweyredman
yes update it
 
11:04 PM
ayy thanks lol
 
.-.
do u have telegram?
@dr34mwh1te
 
what is telegram?
 
seriously?
u don't know what is telegram?
 
nope lol
lemme google
 
11:09 PM
ahhh got it. nope i dont have that lol
 
it's like whatsapp but the users if don't know the phone number of the other contact it will be mysterious
if u know good python u can try learning telegram api. you can make a bot .-.
 
nice
lol
i'll have to check it out
it looks pretty awesome
 
yeah
i use it for my house .-.
 
tight
 
i type on my telegram bot: /turnonlight room2 and it turns the light on ahahha
domotics .-
 
11:14 PM
that's pretty cool...lol
i haven't done a lot of smarthome stuff for my place yet
 
.-.
are u american?
 
yeah
you/
well, american/brazilian
 
italian .-.
 
buona notte
lol
 
?
serio?
sei italiano?
 
11:22 PM
haha no
soó portugues
so*
ho capito un po 'di italiano
 
bello
.-.
 
non molto
haha
 
yo soy pablo emilio gaviria escobar ahhahahaha
 
hahaha
entonces hablas un poquito de espanol?
hehe
parli portoghese?
 
nope ma lo capisco un po
sono simili
 
11:34 PM
si
 
comunque instalar telegram
 
got it
 
then go on the lens and type @dr34mwh1te
 

  last day (17 days later) »