Python Forum
Need help with a script (indentation)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a script (indentation)
#1
Hi.

Im setting up my raspberry pi for mailbox alert(push notification to my phone), and have problems with a python / pyrowl script.
Can someone help me with indentations on this? Sorry for my english

#!/bin/usr/bin/python

#Imports for Pyrowl
from pyrowl import pyrowl
from pprint import pprint
import os

#Imports for GPIO
import time
import RPi.GPIO as GPIO

#Setup pyrowl with API key
p = none
p = pyrowl("MY API KEY")

#Setup GPIO pin as input
#Check that this is the same pin you have your switch input plugged into!
GPIO.setup (15, GPIO.IN)

#Set initial state
previousstate = GPIO.input(15)

#Main loop	while True:
#Check that this is the same pin you have your switch input plugged into!
mybutton = GPIO.input(15)
if mybutton != previousstate:
	if mybutton == True:
		print "Du har post :)"
res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
	else:
print "Du har post :)"
res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)

previousstate = mybutton
#Print result if you like
#pprint(res)
time.sleep(.2)
Reply
#2
I'm assuming the indentation problems are with the if clause starting on line 27. But lines 28 & 29 (the if portion?) and lines 31 & 32 (the else portion?) are the same. So why do you have that if clause?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
there are two ways to fix indentation problems automatically:

1. Install visual studio code. it's a very light and breezy code editor. if the python extension isn't installed go to extensions and (Ctrl + Shift + x) and search for it. open the script right click on the text and press format document( it'll require additional module; all you have to do is hit install if it asks )

2. install JetBrains Pycharm (Community edition is free) and open the file in the ide. it'll point out every indentation and whitespace mistake after a few seconds. Cool
Reply
#4
if mybutton != previousstate:
    if mybutton == True:
        print "Du har post :)"
res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
    else:
print "Du har post :)"
res on line 4 brings you out of the first if statement so else from second if(line 2) shouldn't do anything. is that intentional?
Reply
#5
(Oct-04-2018, 09:46 AM)pseudo Wrote:
if mybutton != previousstate:
    if mybutton == True:
        print "Du har post :)"
res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
    else:
print "Du har post :)"
res on line 4 brings you out of the first if statement so else from second if(line 2) shouldn't do anything. is that intentional?

Its not intentional. Im just wandering a bit in the dark here.

(Oct-04-2018, 08:41 AM)pseudo Wrote: there are two ways to fix indentation problems automatically:

1. Install visual studio code. it's a very light and breezy code editor. if the python extension isn't installed go to extensions and (Ctrl + Shift + x) and search for it. open the script right click on the text and press format document( it'll require additional module; all you have to do is hit install if it asks )

2. install JetBrains Pycharm (Community edition is free) and open the file in the ide. it'll point out every indentation and whitespace mistake after a few seconds. Cool

Thnx. I will test it out :)
Reply
#6
(Oct-04-2018, 04:53 PM)Fresh_Coder Wrote: Thnx. I will test it out :)
Can look at VS Code from start.
There is a post about Format code.
Reply
#7
(Oct-04-2018, 01:51 AM)ichabod801 Wrote: I'm assuming the indentation problems are with the if clause starting on line 27. But lines 28 & 29 (the if portion?) and lines 31 & 32 (the else portion?) are the same. So why do you have that if clause?

You saying i should delete one of them? im a bit blank on this. I found this code online, and edited it a bit.
Reply
#8
The code I am talking about looks like the indentation should be this:

if mybutton != previousstate:
    if mybutton == True:
        print "Du har post :)"
        res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
    else:
        print "Du har post :)"
        res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
However, that code doesn't make sense. That code is totally equivalent to this code:

if mybutton != previousstate:
    print "Du har post :)"
    res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
So I am totally not clear on what this code is supposed to do. Without that clarity, I can't really solve the problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Oct-04-2018, 06:16 PM)ichabod801 Wrote: The code I am talking about looks like the indentation should be this:

if mybutton != previousstate:
    if mybutton == True:
        print "Du har post :)"
        res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
    else:
        print "Du har post :)"
        res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
However, that code doesn't make sense. That code is totally equivalent to this code:

if mybutton != previousstate:
    print "Du har post :)"
    res = p.push("Min Raspberry Pi", 'Advarsel', 'Luke Lukket', batch_mode=False)
So I am totally not clear on what this code is supposed to do. Without that clarity, I can't really solve the problem.


if you don't have enough experience with python it goes like this:
statements won't count as part of your if statements, if they are not indented, so the first res = p.push brings you out of both of nested if and the first if statements and the two lines after else probably won't work as intended because they are not indented after the else either.
how ichabod801 wrote it is the correct format. but we won't be able to help further unless we know what this code is about.
Reply
#10
Seems to be happy with indents now. Thnx all for help :)
Its for sending a push notification through prowl api server when pin 15 and ground are connected on my RPi.

#!/bin/usr/bin/python

# Imports for Pyrowl
from pyrowl import Pyrowl
from pprint import pprint
import os

# Imports for GPIO
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

# Setup pyrowl with API key
p = None
p = Pyrowl("4f9b21b6b3cdf3cf41xxxxxxxxxxxx")

# Setup GPIO pin as input
# Check that this is the same pin you have your switch input plugged into!
GPIO.setup(15, GPIO.IN)


# Set initial state
previousstate = GPIO.input(15)

# Main loop
while True:
    # Check that this is the same pin you have your switch input plugged into!
       mybutton = GPIO.input(15)
if mybutton != previousstate:
    if mybutton == True:
        print "Door opened"
        res = p.push("My Raspberry Pi", 'House Warning','Door opened', batch_mode=False)
    else:
        print "Door closed"
        res = p.push("My Raspberry Pi", 'House Warning','Door closed', batch_mode=False)

previousstate = mybutton
# Print result if you like
# pprint(res)
time.sleep(.2)
Nothing happens when i run python ./1.py
Its like its loading. But i guess i should ask on a raspberry forum for that matter :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020