resume picking items from a previous list

Discussion in 'Python' started by kpp9c, Apr 30, 2006.

  1. kpp9c

    kpp9c Guest

    I have a several list of songs that i pick from, lets, say that there
    are 10 songs in each list and there are 2 lists.

    For a time i pick from my songs, but i only play a few of the songs in
    that list... now my wife, Jessica Alba, comes home, and i start playing
    from Jessica's list of songs. After playing a few songs, Jessica, who
    needs her beauty sleep, goes to bed, and i start my play loop which
    starts picking from my songs again...

    The wrinkle:
    only now i want it to pick first from among the 6 songs yet not played
    from the first time around, and *then* when the list is exhausted,
    shuffle the whole original list of songs and start again.

    Here is some working but hilariously bad code that does most of this
    funny biz... I've gotten this far, but can't figure out how to get the
    loops to keep track of what was played and what wasn't and how to
    pick-up the list where it left off.

    I know this is a dumb thing to want to do, but you know, being married
    to bona-fide star is not easy.

    # -----------------------------------------------------------
    #!/usr/bin/env python

    import random
    import os

    def shuffleloop(iterable):
    """An iterator like itertools cycle, which returns elements from the
    iterable and saves a copy of each. When the iterable is
    exhausted, it return elements from the saved copy.

    The added wrinkle here is that the saved copy is randomly shuffled.
    Repeats indefinitely."""
    saved = []
    for element in iterable:
    yield element
    saved.append(element)
    while saved:
    random.shuffle(saved)
    for element in saved:
    yield element

    def playall_reload(startime, playdur, smpl_lst):
    '''a loop that shuffles and plays all sounds in a list. If the
    sequence is exhausted the list is reloaded, re-shuffled, and plyed
    through
    again. It does this as many times as needed to fill the time
    specified.

    Also returns the end of the last duration so that the begining of the
    next
    section can be fed to the next function or loop.
    '''
    event = 0; incr = 0; lst_len = len(smpl_lst)
    random.shuffle(smpl_lst)
    smpl_loop = shuffleloop(smpl_lst)
    endpoint = startime + playdur
    while startime < endpoint:
    sample = smpl_loop.next()
    splt = os.path.split(sample)
    # get the duration of the current soundfile
    # (hard wire it for now)
    #incr = DUR()
    dur = 10
    #load the sample & play it
    #
    # input(sample)
    # PLAY(startime, dur)
    #
    print "event %d @ %.4f --> [%s] dur: %.4f" % (event+1, startime,
    splt[1], dur)
    incr = dur
    startime = startime + incr
    event = event + 1
    if (event < lst_len):
    print "\n\n*** Heads-up yo: <the input sequence was not exhausted>
    ***\n\n"
    return startime


    def test():
    kevins = ['/Users/kevin/snd/songs/loveisintheair.aif',
    '/Users/kevin/snd/songs/boymeetsgirl.aif',
    '/Users/kevin/snd/songs/yourcheatingheart.aif',
    '/Users/kevin/snd/songs/kindletheflame.aif',
    '/Users/kevin/snd/songs/mywifeissohot.aif',
    '/Users/kevin/snd/songs/haha.aif',
    '/Users/kevin/snd/songs/blueberryorstrawberry.aif',
    '/Users/kevin/snd/songs/didyoupaytheelectricbill.aif',
    '/Users/kevin/snd/songs/whereistheremote.aif',
    '/Users/kevin/snd/songs/youspenthowmuchforthoseshoes.aif']

    jessicas = ['/Users/kevin/snd/quiet_songs/iloveu.aif',
    '/Users/kevin/snd/quiet_songs/uloveme.aif',
    '/Users/kevin/snd/quiet_songs/wearehappy.aif',
    '/Users/kevin/snd/quiet_songs/wearesad.aif',
    '/Users/kevin/snd/quiet_songs/letsbreakup.aif',
    '/Users/kevin/snd/quiet_songs/letsgetbacktogether.aif',
    '/Users/kevin/snd/quiet_songs/walkinthesunshine.aif',
    '/Users/kevin/snd/quiet_songs/iloveutruly.aif',
    '/Users/kevin/snd/quiet_songs/whosefootisthat.aif',
    '/Users/kevin/snd/quiet_songs/ohbaby.aif']

    print
    one = playall_reload(1.00, 20.00, kevins)
    print
    two = playall_reload(one, 180, jessicas)
    print
    three = playall_reload(two, 40.00, kevins)

    if __name__ == '__main__':
    test()
    kpp9c, Apr 30, 2006
    #1
    1. Advertising

  2. kpp9c

    James Stroud Guest

    kpp9c wrote:
    > I have a several list of songs that i pick from, lets, say that there
    > are 10 songs in each list and there are 2 lists.
    >
    > For a time i pick from my songs, but i only play a few of the songs in
    > that list... now my wife, Jessica Alba, comes home, and i start playing
    > from Jessica's list of songs. After playing a few songs, Jessica, who
    > needs her beauty sleep, goes to bed, and i start my play loop which
    > starts picking from my songs again...
    >
    > The wrinkle:
    > only now i want it to pick first from among the 6 songs yet not played
    > from the first time around, and *then* when the list is exhausted,
    > shuffle the whole original list of songs and start again.
    >
    > Here is some working but hilariously bad code that does most of this
    > funny biz... I've gotten this far, but can't figure out how to get the
    > loops to keep track of what was played and what wasn't and how to
    > pick-up the list where it left off.
    >
    > I know this is a dumb thing to want to do, but you know, being married
    > to bona-fide star is not easy.
    >
    > # -----------------------------------------------------------
    > #!/usr/bin/env python
    >
    > import random
    > import os
    >
    > def shuffleloop(iterable):
    > """An iterator like itertools cycle, which returns elements from the
    > iterable and saves a copy of each. When the iterable is
    > exhausted, it return elements from the saved copy.
    >
    > The added wrinkle here is that the saved copy is randomly shuffled.
    > Repeats indefinitely."""
    > saved = []
    > for element in iterable:
    > yield element
    > saved.append(element)
    > while saved:
    > random.shuffle(saved)
    > for element in saved:
    > yield element
    >
    > def playall_reload(startime, playdur, smpl_lst):
    > '''a loop that shuffles and plays all sounds in a list. If the
    > sequence is exhausted the list is reloaded, re-shuffled, and plyed
    > through
    > again. It does this as many times as needed to fill the time
    > specified.
    >
    > Also returns the end of the last duration so that the begining of the
    > next
    > section can be fed to the next function or loop.
    > '''
    > event = 0; incr = 0; lst_len = len(smpl_lst)
    > random.shuffle(smpl_lst)
    > smpl_loop = shuffleloop(smpl_lst)
    > endpoint = startime + playdur
    > while startime < endpoint:
    > sample = smpl_loop.next()
    > splt = os.path.split(sample)
    > # get the duration of the current soundfile
    > # (hard wire it for now)
    > #incr = DUR()
    > dur = 10
    > #load the sample & play it
    > #
    > # input(sample)
    > # PLAY(startime, dur)
    > #
    > print "event %d @ %.4f --> [%s] dur: %.4f" % (event+1, startime,
    > splt[1], dur)
    > incr = dur
    > startime = startime + incr
    > event = event + 1
    > if (event < lst_len):
    > print "\n\n*** Heads-up yo: <the input sequence was not exhausted>
    > ***\n\n"
    > return startime
    >
    >
    > def test():
    > kevins = ['/Users/kevin/snd/songs/loveisintheair.aif',
    > '/Users/kevin/snd/songs/boymeetsgirl.aif',
    > '/Users/kevin/snd/songs/yourcheatingheart.aif',
    > '/Users/kevin/snd/songs/kindletheflame.aif',
    > '/Users/kevin/snd/songs/mywifeissohot.aif',
    > '/Users/kevin/snd/songs/haha.aif',
    > '/Users/kevin/snd/songs/blueberryorstrawberry.aif',
    > '/Users/kevin/snd/songs/didyoupaytheelectricbill.aif',
    > '/Users/kevin/snd/songs/whereistheremote.aif',
    > '/Users/kevin/snd/songs/youspenthowmuchforthoseshoes.aif']
    >
    > jessicas = ['/Users/kevin/snd/quiet_songs/iloveu.aif',
    > '/Users/kevin/snd/quiet_songs/uloveme.aif',
    > '/Users/kevin/snd/quiet_songs/wearehappy.aif',
    > '/Users/kevin/snd/quiet_songs/wearesad.aif',
    > '/Users/kevin/snd/quiet_songs/letsbreakup.aif',
    > '/Users/kevin/snd/quiet_songs/letsgetbacktogether.aif',
    > '/Users/kevin/snd/quiet_songs/walkinthesunshine.aif',
    > '/Users/kevin/snd/quiet_songs/iloveutruly.aif',
    > '/Users/kevin/snd/quiet_songs/whosefootisthat.aif',
    > '/Users/kevin/snd/quiet_songs/ohbaby.aif']
    >
    > print
    > one = playall_reload(1.00, 20.00, kevins)
    > print
    > two = playall_reload(one, 180, jessicas)
    > print
    > three = playall_reload(two, 40.00, kevins)
    >
    > if __name__ == '__main__':
    > test()
    >


    I didn't read your code, but a class might make it simpler:

    class Playah(object):
    def __init__(self, playlist):
    self.playlist = playlist
    self.reset()
    def reset(self):
    self._order = randrange(len(self.playlist))
    self._i = 0
    def next():
    song = self.playlist(self._i)
    self._i += 1
    if self._i > len(self.playlist):
    self.reset()
    return song

    You could probably make it an iterable if you tried.

    James


    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
    James Stroud, Apr 30, 2006
    #2
    1. Advertising

  3. kpp9c

    James Stroud Guest

    James Stroud wrote:
    > kpp9c wrote:
    >
    >> I have a several list of songs that i pick from, lets, say that there
    >> are 10 songs in each list and there are 2 lists.
    >>
    >> For a time i pick from my songs, but i only play a few of the songs in
    >> that list... now my wife, Jessica Alba, comes home, and i start playing
    >> from Jessica's list of songs. After playing a few songs, Jessica, who
    >> needs her beauty sleep, goes to bed, and i start my play loop which
    >> starts picking from my songs again...
    >>
    >> The wrinkle:
    >> only now i want it to pick first from among the 6 songs yet not played
    >> from the first time around, and *then* when the list is exhausted,
    >> shuffle the whole original list of songs and start again.
    >>
    >> Here is some working but hilariously bad code that does most of this
    >> funny biz... I've gotten this far, but can't figure out how to get the
    >> loops to keep track of what was played and what wasn't and how to
    >> pick-up the list where it left off.
    >>
    >> I know this is a dumb thing to want to do, but you know, being married
    >> to bona-fide star is not easy.
    >>
    >> # -----------------------------------------------------------
    >> #!/usr/bin/env python
    >>
    >> import random
    >> import os
    >>
    >> def shuffleloop(iterable):
    >> """An iterator like itertools cycle, which returns elements from the
    >> iterable and saves a copy of each. When the iterable is
    >> exhausted, it return elements from the saved copy.
    >>
    >> The added wrinkle here is that the saved copy is randomly shuffled.
    >> Repeats indefinitely."""
    >> saved = []
    >> for element in iterable:
    >> yield element
    >> saved.append(element)
    >> while saved:
    >> random.shuffle(saved)
    >> for element in saved:
    >> yield element
    >>
    >> def playall_reload(startime, playdur, smpl_lst):
    >> '''a loop that shuffles and plays all sounds in a list. If the
    >> sequence is exhausted the list is reloaded, re-shuffled, and plyed
    >> through
    >> again. It does this as many times as needed to fill the time
    >> specified.
    >>
    >> Also returns the end of the last duration so that the begining of the
    >> next
    >> section can be fed to the next function or loop.
    >> '''
    >> event = 0; incr = 0; lst_len = len(smpl_lst)
    >> random.shuffle(smpl_lst)
    >> smpl_loop = shuffleloop(smpl_lst)
    >> endpoint = startime + playdur
    >> while startime < endpoint:
    >> sample = smpl_loop.next()
    >> splt = os.path.split(sample)
    >> # get the duration of the current soundfile
    >> # (hard wire it for now)
    >> #incr = DUR()
    >> dur = 10
    >> #load the sample & play it
    >> #
    >> # input(sample)
    >> # PLAY(startime, dur)
    >> #
    >> print "event %d @ %.4f --> [%s] dur: %.4f" % (event+1, startime,
    >> splt[1], dur)
    >> incr = dur
    >> startime = startime + incr
    >> event = event + 1
    >> if (event < lst_len):
    >> print "\n\n*** Heads-up yo: <the input sequence was not
    >> exhausted>
    >> ***\n\n"
    >> return startime
    >>
    >>
    >> def test():
    >> kevins = ['/Users/kevin/snd/songs/loveisintheair.aif',
    >> '/Users/kevin/snd/songs/boymeetsgirl.aif',
    >> '/Users/kevin/snd/songs/yourcheatingheart.aif',
    >> '/Users/kevin/snd/songs/kindletheflame.aif',
    >> '/Users/kevin/snd/songs/mywifeissohot.aif',
    >> '/Users/kevin/snd/songs/haha.aif',
    >> '/Users/kevin/snd/songs/blueberryorstrawberry.aif',
    >> '/Users/kevin/snd/songs/didyoupaytheelectricbill.aif',
    >> '/Users/kevin/snd/songs/whereistheremote.aif',
    >> '/Users/kevin/snd/songs/youspenthowmuchforthoseshoes.aif']
    >>
    >> jessicas = ['/Users/kevin/snd/quiet_songs/iloveu.aif',
    >> '/Users/kevin/snd/quiet_songs/uloveme.aif',
    >> '/Users/kevin/snd/quiet_songs/wearehappy.aif',
    >> '/Users/kevin/snd/quiet_songs/wearesad.aif',
    >> '/Users/kevin/snd/quiet_songs/letsbreakup.aif',
    >> '/Users/kevin/snd/quiet_songs/letsgetbacktogether.aif',
    >> '/Users/kevin/snd/quiet_songs/walkinthesunshine.aif',
    >> '/Users/kevin/snd/quiet_songs/iloveutruly.aif',
    >> '/Users/kevin/snd/quiet_songs/whosefootisthat.aif',
    >> '/Users/kevin/snd/quiet_songs/ohbaby.aif']
    >>
    >> print
    >> one = playall_reload(1.00, 20.00, kevins)
    >> print
    >> two = playall_reload(one, 180, jessicas)
    >> print
    >> three = playall_reload(two, 40.00, kevins)
    >>
    >> if __name__ == '__main__':
    >> test()
    >>

    >
    > I didn't read your code, but a class might make it simpler:
    >
    > class Playah(object):
    > def __init__(self, playlist):
    > self.playlist = playlist
    > self.reset()
    > def reset(self):
    > self._order = randrange(len(self.playlist))
    > self._i = 0
    > def next():
    > song = self.playlist(self._i)
    > self._i += 1
    > if self._i > len(self.playlist):
    > self.reset()
    > return song
    >
    > You could probably make it an iterable if you tried.
    >
    > James
    >
    >


    Oops, I meant that

    self._order = randrange(len(self.playlist))

    should be

    self._order = range(len(self.playlist))
    random.shuffle(self._order)

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
    James Stroud, Apr 30, 2006
    #3
  4. Re: how not use memmove when insert a object in the list

    "kyo guan" wrote:

    > for (i = n; --i >= where; ) /// here, why not use memmove? it would be more speed then this loop.
    > items[i+1] = items;


    have you benchmarked this on a wide variety of platforms, or are
    you just guessing?

    </F>
    Fredrik Lundh, Apr 30, 2006
    #4
  5. kpp9c

    Guest

    kpp9c wrote:
    > I have a several list of songs that i pick from, lets, say that there
    > are 10 songs in each list and there are 2 lists.
    >
    > For a time i pick from my songs, but i only play a few of the songs in
    > that list... now my wife, Jessica Alba, comes home, and i start playing
    > from Jessica's list of songs. After playing a few songs, Jessica, who
    > needs her beauty sleep, goes to bed, and i start my play loop which
    > starts picking from my songs again...
    >


    Not sure I follow. Jessica goes to bed, and you... _listen to music_??
    , Apr 30, 2006
    #5
  6. On 30 Apr 2006 05:33:39 -0700, rumours say that ""
    <> might have written:

    >
    >kpp9c wrote:


    >> For a time i pick from my songs, but i only play a few of the songs in
    >> that list... now my wife, Jessica Alba, comes home, and i start playing
    >> from Jessica's list of songs. After playing a few songs, Jessica, who
    >> needs her beauty sleep, goes to bed, and i start my play loop which
    >> starts picking from my songs again...


    >Not sure I follow. Jessica goes to bed, and you... _listen to music_??


    He said his *wife*, Jessica Alba. There's *no* way you marry *anyone* you
    won't get bored of eventually.
    --
    TZOTZIOY, I speak England very best.
    "Dear Paul,
    please stop spamming us."
    The Corinthians
    Christos Georgiou, May 2, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHBlcnJp?=

    DataGrid: Paging Items (Next, Previous) Question

    =?Utf-8?B?dHBlcnJp?=, Jul 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    590
    =?Utf-8?B?Sm9zZXBoIE0uIFdhaGJh?=
    Jul 17, 2005
  2. Gilles Kuhn
    Replies:
    0
    Views:
    424
    Gilles Kuhn
    Sep 15, 2003
  3. Anjan Bhowmik
    Replies:
    1
    Views:
    458
    Misbah Arefin
    Feb 14, 2008
  4. divya
    Replies:
    1
    Views:
    1,072
    Munna
    May 28, 2008
  5. Don Bruder
    Replies:
    3
    Views:
    950
    spikeysnack
    Aug 3, 2010
Loading...

Share This Page