[Tutor] Deleted dictionary length not reporting

eryksun eryksun at gmail.com
Wed Jul 17 05:11:25 CEST 2013


On Tue, Jul 16, 2013 at 9:48 PM, Jim Mooney <cybervigilante at gmail.com> wrote:
>
> Also, the printout doesn't occur until after all the sounds play, so
> maybe that has something to do with it. But I also tried saving the
> lengths in a list while the program ran, and got the same result. A
> list of all '125'

The print occurs after each sound is played, not after "all the sounds play".

winsound.PlaySound defaults to blocking mode (synchronous). To play
without blocking (asynchronous), use the flag winsound.SND_ASYNC. But
you don't want that for queuing up multiple tracks since they'd all
run simultaneously.

PlaySound
http://msdn.microsoft.com/en-us/library/dd743680
http://docs.python.org/2/library/winsound

> import winsound
> import random, os
> random.seed()

The system random is an instance of random.Random, which already calls
self.seed():

    def __init__(self, x=None):
        """Initialize an instance.

        Optional argument x controls seeding, as for Random.seed().
        """

        self.seed(x)
        self.gauss_next = None


> lenlist = []
> file_list = os.listdir('WAV')
>
> for clip in range(0,7):
>     file_dict = dict(enumerate(file_list))
>     sound_key = random.choice(range(0,len(file_list)))
>     winsound.PlaySound('WAV/' + file_dict[sound_key], winsound.SND_FILENAME)
>     del file_dict[sound_key]
>     print(len(file_dict.keys()))


I'd use random.sample:

    NUM_CLIPS = 7
    CLIP_PATH = 'WAV'  # I'd use an absolute path here

    clip_list  = random.sample(os.listdir(CLIP_PATH), NUM_CLIPS)

    for clip in clip_list:
        clip = os.path.join(CLIP_PATH, clip)
        winsound.PlaySound(clip, winsound.SND_FILENAME)


More information about the Tutor mailing list