• Python

    From Mortifis@VERT/EMPTYKEG to All on Sun Dec 9 13:40:34 2018
    Anyone into Python? I have a few questions relating to nested loops

    Thanks

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Nightfox@VERT/DIGDIST to Mortifis on Sun Dec 9 18:11:39 2018
    Re: Python
    By: Mortifis to All on Sun Dec 09 2018 01:40 pm

    Anyone into Python? I have a few questions relating to nested loops

    I've done a bit of Python for work.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Minex@VERT/TDOD to Mortifis on Sun Dec 9 15:19:24 2018
    Re: Python
    By: Mortifis to All on Sun Dec 09 2018 01:40 pm

    Anyone into Python? I have a few questions relating to nested loops

    Ask away. I'm curious what your question is.

    The Dawn of Demise BBS (tdod.org)

    ---
    þ Synchronet þ The Dawn of Demise (tdod.org:5000)
  • From Mortifis@VERT/EMPTYKEG to Nightfox on Mon Dec 10 10:35:13 2018
    Re: Python
    By: Mortifis to All on Sun Dec 09 2018 01:40 pm

    Anyone into Python? I have a few questions relating to nested loops

    I've done a bit of Python for work.

    Seems Python is very finicky about use of proper indentation :/ I am (trying) to code some random light sequences for use with my Raspberry Pi, mostly works, however, when I try to set a while ... for ... try while ... blah blah blah it complains about indentation. when I remove the first x = 0 ... while x < cycle: x = x + 1 it seems to work correctly.

    here is my spaghetti code:

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    import random
    import mutagen.mp3
    import sys

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    # init list with pin numbers
    seq0 = [2, 3, 4, 17, 27, 22, 10, 9] # RPI GPIO PINS
    seq1 = [3, 17, 22, 9]
    seq2 = [2, 4, 27, 10]
    seq3 = [2, 9, 3, 10, 4, 22, 17, 27, 27, 17, 22, 4, 10, 3, 9, 2]

    from random import randint
    rand = random.randint(0, 3)

    mP3 = sys.argv[1]

    from mutagen.mp3 import MP3
    audio = MP3(mP3)
    cycle = int(audio.info.length)
    print ' Playing ', mP3, ' for ', cycle, 'seconds'

    if mP3 == "letitgo.mp3":
    rand = 1 # speeds timing up
    SleepTimeS = 0.2
    SleepTimeL = 0.1
    else:
    SleepTimeS = 1.2 # normal speed
    SleepTimeL = 0.2

    if rand == 0: seq = seq0
    if rand == 1: seq = seq1
    if rand == 2: seq = seq2
    if rand == 3: seq = seq3

    x = 0
    while x < cycle:
    x = x + 1

    for i in seq:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)

    # main loop

    try:
    while True:

    for i in seq:
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeL);

    except KeyboardInterrupt:
    print " Quit"

    GPIO.cleanup()

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Mortifis@VERT/EMPTYKEG to Minex on Mon Dec 10 11:37:55 2018
    Re: Python
    By: Mortifis to All on Sun Dec 09 2018 01:40 pm

    Anyone into Python? I have a few questions relating to nested loops

    Ask away. I'm curious what your question is.

    My question is about indentation and nested loops ... seems at times I get syntax error as well (x += 1) . I can paste the code if you are a good reader :-P the code I pasted to Nightfox works except that it does loop through the while x < cycle: as a main counter.

    What is need is a counter so that the GPIO pins blink only for the length of an mp3 then automagically shutdown when the song is over

    In the code I pasted to Nightfox the lights blink until ^C is hit

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From echicken@VERT/ECBBS to Mortifis on Mon Dec 10 11:12:41 2018
    Re: Re: Python
    By: Mortifis to Minex on Mon Dec 10 2018 11:37:55

    What is need is a counter so that the GPIO pins blink only for the length of an mp3 then automagically shutdown when the song is over

    What value does audio.info.length represent? Duration of the song in seconds / milliseconds / something else? File size?

    I doubt if a counter is the right way to go. Seems like you could store the start time prior to entering the loop, and then make your While condition something like 'current_time - start_time < song_duration', so that the loop breaks once that amount of time has passed.

    In the code I pasted to Nightfox the lights blink until ^C is hit

    As expected, I assume.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-425-5435
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Mortifis@VERT/EMPTYKEG to Nightfox on Mon Dec 10 12:16:31 2018
    Re: Python
    By: Mortifis to All on Sun Dec 09 2018 01:40 pm

    Anyone into Python? I have a few questions relating to nested loops

    I've done a bit of Python for work.

    I appreciate you looking in this for me, I think I figured out the correct loop usage I need, python is soooo F$%&'n finicky -;P:

    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    import random
    import mutagen.mp3
    import sys

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    # init list with pin numbers
    seq0 = [2, 3, 4, 17, 27, 22, 10, 9] # RPI GPIO PINS
    seq1 = [3, 17, 22, 9]
    seq2 = [2, 4, 27, 10]
    seq3 = [2, 9, 3, 10, 4, 22, 17, 27, 27, 17, 22, 4, 10, 3, 9, 2]

    from random import randint
    rand = random.randint(0, 3)

    mP3 = sys.argv[1]

    from mutagen.mp3 import MP3
    audio = MP3(mP3)
    cycle = int(audio.info.length)
    print ' Playing ', mP3, ' for ', cycle, 'seconds'

    if mP3 == "letitgo.mp3":
    rand = 1 # speeds timing up
    SleepTimeS = 0.2
    SleepTimeL = 0.1
    else:
    SleepTimeS = 1.2 # normal speed
    SleepTimeL = 0.2

    if rand == 0: seq = seq0
    if rand == 1: seq = seq1
    if rand == 2: seq = seq2
    if rand == 3: seq = seq3

    x = 0
    while x < cycle:
    x = x + 1

    for i in seq:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)

    # main loop

    try:
    while True and x < cycle:

    for i in seq:
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeL);
    # 4 seconds to cycle loop
    x = int(x + ((SleepTimeS + SleepTimeL) * 3))

    except KeyboardInterrupt:
    print " Quit"

    GPIO.cleanup()

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Mortifis@VERT/EMPTYKEG to echicken on Mon Dec 10 15:13:06 2018
    Re: Re: Python
    By: Mortifis to Minex on Mon Dec 10 2018 11:37:55

    What is need is a counter so that the GPIO pins blink only for the length of an mp3 then automagically shutdown when the song is over

    What value does audio.info.length represent? Duration of the song in seconds / milliseconds / something else? File size?

    yes, audo.info.length is the time of the mp3 in seconds, int(audio.info.length) makes it a whole number


    I doubt if a counter is the right way to go. Seems like you could store the start time prior to entering the loop, and then make your While condition something like 'current_time - start_time < song_duration', so that the loop breaks once that amount of time has passed.

    isn't that just a counter of sorts as well? I discover the error of my ways, I was putting an unnecessary while: ahead of everything, the easy-for-me solution to self-terminate when the song finished playing was

    mP3 = sys.argv[1]

    from mutagen.mp3 import MP3
    audio = MP3(mP3)
    cycle = int(audio.info.length)

    x = 0

    for i in seq:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)
    seq.reverse()

    try:
    while True and x < cycle:

    for i in seq:
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.LOW)
    time.sleep(SleepTimeS);
    GPIO.output(i, GPIO.HIGH)
    time.sleep(SleepTimeL);
    # 4 seconds to cycle loop
    x = int(x + ((SleepTimeS + SleepTimeL) * 3.5))

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From echicken@VERT/ECBBS to Mortifis on Mon Dec 10 15:32:43 2018
    Re: Re: Python
    By: Mortifis to echicken on Mon Dec 10 2018 15:13:06

    isn't that just a counter of sorts as well?

    Sure, as much as any clock is a counter incrementing with the passage of time. I don't mean that "using a counter" is inherently bad, just that there's a more straightforward approach that also lets you ditch a magic number.

    Doesn't matter, you'll achieve the same result in the end.

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-425-5435
    þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.com
  • From Nightfox@VERT/DIGDIST to Mortifis on Mon Dec 10 19:13:08 2018
    Re: Re: Python
    By: Mortifis to Nightfox on Mon Dec 10 2018 12:16 pm

    I appreciate you looking in this for me, I think I figured out the correct loop usage I need, python is soooo F$%&'n finicky -;P:

    Good to hear you may have figured it out.

    Yeah, Python has strict rules about indentation. Instead of using curly braces to mark blocks of code like other languages, all the lines in a block of code in Python must be indented the same way (same number of spaces or tabs).

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Mortifis@VERT/EMPTYKEG to echicken on Tue Dec 11 10:59:11 2018
    Re: Re: Python
    By: Mortifis to echicken on Mon Dec 10 2018 15:13:06

    isn't that just a counter of sorts as well?

    Sure, as much as any clock is a counter incrementing with
    the passage of
    time. I don't mean that "using a counter" is inherently
    bad, just that
    there's a more straightforward approach that also lets you
    ditch a magic
    number.

    Doesn't matter, you'll achieve the same result in the end.

    I prefer straightforward approaches, I just started looking
    at Python and am not very familiar with the structure and
    functions.

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Mortifis@VERT/EMPTYKEG to Nightfox on Tue Dec 11 11:01:11 2018
    Re: Re: Python
    By: Mortifis to Nightfox on Mon Dec 10 2018 12:16 pm

    I appreciate you looking in this for me, I think I
    figured out the
    correct loop usage I need, python is soooo F$%&'n
    finicky -;P:

    Good to hear you may have figured it out.

    Yeah, Python has strict rules about indentation. Instead
    of using curly
    braces to mark blocks of code like other languages, all the
    lines in a block
    of code in Python must be indented the same way (same
    number of spaces or
    tabs).

    So much for being a lazy spaghetti coder with python lol

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Kirkman@VERT/GUARDIAN to Nightfox on Tue Dec 11 21:26:40 2018
    Re: Re: Python
    By: Nightfox to Mortifis on Mon Dec 10 2018 07:13 pm

    Yeah, Python has strict rules about indentation. Instead of using curly braces to mark blocks of code like other languages, all the lines in a block of code in Python must be indented the same way (same number of spaces or tabs).

    This is something I love about Python, personally.

    Make sure you don't mix spaces and tabs for indentation in your code, or Python will likely complain.

    --Josh

    ////--------------------------------------------------
    BiC -=- http://breakintochat.com -=- bbs wiki and blog

    ---
    þ Synchronet
  • From Nightfox@VERT/DIGDIST to Kirkman on Fri Dec 14 17:30:46 2018
    Re: Re: Python
    By: Kirkman to Nightfox on Tue Dec 11 2018 09:26 pm

    This is something I love about Python, personally.

    Make sure you don't mix spaces and tabs for indentation in your code, or Python will likely complain.

    I like that it helps keep code consistent, but one downside that bugs me is that text editors often assume you want to continue your indented code block in Python, and Python doesn't use curly braces, which text editors often use with other languages to know when you're done with a code block. In other languages, text editors can even know you're done with a one-line block of code without curly braces since that's how other programming languages work.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Mortifis@VERT/EMPTYKEG to Nightfox on Sat Dec 15 11:44:34 2018
    Re: Re: Python
    By: Kirkman to Nightfox on Tue Dec 11 2018 09:26 pm

    This is something I love about Python, personally.

    Make sure you don't mix spaces and tabs for indentation in your code, or Python will likely complain.

    I like that it helps keep code consistent, but one downside that bugs me is that text editors often assume you want to continue your indented code block in Python, and Python doesn't use curly braces, which text editors often use with other languages to know when you're done with a code block. In other languages, text editors can even know you're done with a one-line block of code without curly braces since that's how other programming languages work.

    I keep running into unexpected indent or unexpected unindent when using nested loops and such, which is obviously my lack of understanding of Python. Another problem I keep running into is, for example, x = x + 1 or x += 1 ... syntax error. I won't paste my troubled code but Python is definitely a finicky language

    ---
    þ Synchronet þ The Empty Keg BBS emptykeg.synchro.net:82 Lake Echo NS Canada
  • From Fireball@VERT/FBEX to Mortifis on Mon Jan 28 23:40:12 2019
    I keep running into unexpected indent or unexpected unindent when using nested loops and such, which is obviously my lack of understanding of Python. Another problem I keep running into is, for example, x = x + 1 or x += 1 ... syntax error. I won't paste my troubled code but Python is definitely a finicky language

    I don't know what you're using for an editor, but it's not working for you. NotePad++, Visual Studio Code, PyCharm, and Sublime Text will all help with syntax, formatting, and beautification. You will most likely need to install plugins to make them work with python code, but a good editor always helps.

    Fireball

    ---
    þ Synchronet þ My Brand-New BBS