MrChips
Posts: 7
Joined: Thu Nov 22, 2012 9:23 pm

Basic coding problem!

Mon Dec 10, 2012 5:16 pm

I thought that I would try a simple programme to turn on segments of a 7 segment display. I have managed to get something going by using repetitive, long-winded code, and now want to 'smarten' things up a bit. ... as a coding exercise.

Based on my previous coding experience ... mainly BASIC and VBA, I can't understand why the following will not work.

Advice please!!!

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)

aTrue = "GPIO.output(11, True)"     # tried "GPIO.output(" + 11 + ", True)" 
deLay = .3
SleeP= "time.sleep(" + deLay + ")"    #  '&'  doesn't work

while True:
    GPIO.output(11, True)        # I can't substitute varaible aTrue
    time.sleep(deLay)            # I can't substitute variable SleeP
    GPIO.output(11, False)
    time.sleep(deLay)

MattCook
Posts: 16
Joined: Tue Feb 21, 2012 1:46 pm
Location: S. Manchester

Re: Basic coding problem!

Mon Dec 10, 2012 10:51 pm

I'm no Python expert but ...

You appear to be asking why you can't replace

Code: Select all

while True:
    GPIO.output(11, True)        
    time.sleep(deLay)
with

Code: Select all

while True:
    aTrue        
    SleeP
I would assume that this is because in the first case you have two function calls, while in the second you have two character strings.
Also, the set-up code seems wrong ...

Code: Select all

>>> deLay = .3
>>> SleeP = "time.sleep(" + deLay + ")"
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    SleeP = "time.sleep(" + deLay + ")"
TypeError: Can't convert 'float' object to str implicitly
>>> 
If it had worked, you would have :

Code: Select all

>>> type(time.sleep)
<class 'builtin_function_or_method'>
>>> type(SleeP)
<class 'str'>
I hope you see why this does not work as you thought ...

Matt

P.S. On a stylistic note, the mixing of cases in your variable names is horrible :?

MrChips
Posts: 7
Joined: Thu Nov 22, 2012 9:23 pm

Re: Basic coding problem!

Tue Dec 11, 2012 8:34 am

Matt.

Thank you for taking the time to help me.
I have a glimmering now of why it would not work ... I will now read up and try to correct these errors.

I do agree with your stylistic statement ... it came of me thinking that perhaps I was using reserved words, and guessed that "SleeP" and "deLay" would be OK!

For the sake of completeness, I will come back with a working example .. as a way of showing my appreciation to you.
Thank again you Matt

MattCook
Posts: 16
Joined: Tue Feb 21, 2012 1:46 pm
Location: S. Manchester

Re: Basic coding problem!

Tue Dec 11, 2012 10:58 am

Hi Mr Chips.

The joy of a good forum is that there is always someone who can help you, and always someone that you can help in turn ! Also, I am really not that knowledgeable about Python - so in checking what I thought was the answer, I learned a bit more myself. Trying stuff at the interactive prompt really is a very good way of checking what goes on.

I would say that my personal view is that the best documentation that a program can have is the actual code itself. Code that is easy to read is much easier to maintain than clever code. Using your example code, I would say that :

Code: Select all

while True:
    GPIO.output(11, True) 
    time.sleep(0.3)   
    GPIO.output(11, False)
    time.sleep(0.3)
is easier to understand in terms of functionality, than :

Code: Select all

while True:
    aTrue 
    SleeP    
    aFalse
    SleeP
Comments are important, and as long as they are changed when the code is changed, they can be very helpful. Comments that don't get updated when code changes can be very confusing ... trust me on this one ! The first example is pretty much self-documenting code. While the second example is tidier to look at, you need to look at another part of the program to find what it actually does ...

I hope you find that useful, and I look forward to your finished product. Take the above styling tip with as big a pinch of salt as you wish, of course. I just find 'simple' code to be more reliable and easier to change than 'clever' code - although where space or speed issues dictate, this will priority change of course.

Goodbye Mr Chips. :roll:

Matt

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Basic coding problem!

Wed Dec 12, 2012 2:45 am

Like MattCook said, you tried to execute a variable containing a string. You probably are thinking how to encapsulate code for later use. There might be more fancy ways to do it, but the very basic way is to define a function and then later call the function. Example on how to do it:

Code: Select all

def blink():
    GPIO.output(11, True)
    time.sleep(.3)  
    GPIO.output(11, False)
    time.sleep(.3)

while True:
    blink()
Functions can take arguments that modify their behaviour. Example:

Code: Select all

def blink(sleep_time):
    GPIO.output(11, True)
    time.sleep(sleep_time)  
    GPIO.output(11, False)
    time.sleep(sleep_time)

while True:
    blink(.3)
    blink(.6)

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Basic coding problem!

Wed Dec 12, 2012 3:15 am

I know it's bad practice, but (to satisfy my own curiosity) could he just use exec? Like:

Code: Select all

while True:
    exec aTrue
    exec SleeP
    exec aFalse
    exec SleeP
?
note: I may or may not know what I'm talking about...

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Basic coding problem!

Wed Dec 12, 2012 4:10 am

toxibunny wrote:I know it's bad practice, but (to satisfy my own curiosity) could he just use exec? Like:

Code: Select all

while True:
    exec aTrue
    exec SleeP
    exec aFalse
    exec SleeP
?
Yes. (Python is cool.)

(In python3 exec is a fuction instead of a statement: exec(aTrue).)

Return to “General programming discussion”