• 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ãã Mo> 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ãã > Mo> 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ãã Mo> What is need is a counter so that the GPIO pins blink only for the lengthã Mo> 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.ãã Mo> 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ãã > Mo> 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ãã > Mo> What is need is a counter so that the GPIO pins blink only for theã > Mo> 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ãã Mo> 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ãã Mo> I appreciate you looking in this for me, I think I figured out the correctã Mo> 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ãã > Mo> 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ãã > Mo> I appreciate you looking in this for me, I think I ãfigured out theã > Mo> 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ãã Ni> Yeah, Python has strict rules about indentation. Instead of using curlyã Ni> braces to mark blocks of code like other languages, all the lines in aã Ni> block of code in Python must be indented the same way (same number ofã Ni> 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ãã Ki> This is something I love about Python, personally.ãã Ki> Make sure you don't mix spaces and tabs for indentation in your code, orã Ki> 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ãã > Ki> This is something I love about Python, personally.ãã > Ki> Make sure you don't mix spaces and tabs for indentation in your code,ã > Ki> 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ã