Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > iterating initalizations

Reply
Thread Tools

iterating initalizations

 
 
Aaron Stepp
Guest
Posts: n/a
 
      12-22-2008
Hi all:

I'm new to python and trying to save time and code by iterating
through list initializations as well as the assignments. I have the
following code:

import random
from rtcmix import *
from chimes_source import *
from rhythmblock import *
from pitchblock import *

indexrand = random.Random()
indexrand.seed(2)

rhythm = rhythmBlock()
pitch = pitchBlock()

class pitchAndRhythm:

def __init__self:

self.__abet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


def listCreate(self, num):

if num > 25:

print "Oops. This won't work"

else:

for a in range(num):

b = indexrand.randint(0, 3)

c = indexrand.randint(0, 7)

index = self.__abet[a]

index = [ ]

index = index.append(rhythm.rhythmTwist(b, c))


This doesn't do what I expect (probably because I don't have a clue
what I'm doing!): initalizing, then filling new arrays, each new one
called A[ ], then B[ ], etc.

This seems very un-pythonic, and I'm sure there is a right way to do
it. I'm just lost!

Thanks

Aaron Stepp
 
Reply With Quote
 
 
 
 
r
Guest
Posts: n/a
 
      12-23-2008
I can't check you code because i don't have these modules but here is
the code with proper indention


import random
from rtcmix import *
from chimes_source import *
from rhythmblock import *
from pitchblock import *
indexrand = random.Random()
indexrand.seed(2)
rhythm = rhythmBlock()
pitch = pitchBlock()

class pitchAndRhythm:
def __init__self:
self.__abet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def listCreate(self, num):
if num > 25:
print "Oops. This won't work"
else:
for a in range(num):
b = indexrand.randint(0, 3)
c = indexrand.randint(0, 7)
index = self.__abet[a]
index = [ ]
index = index.append(rhythm.rhythmTwist(b, c))
 
Reply With Quote
 
 
 
 
r
Guest
Posts: n/a
 
      12-23-2008
I can't check you code because i don't have these modules but here is
the code with proper indention

import random
from rtcmix import *
from chimes_source import *
from rhythmblock import *
from pitchblock import *
indexrand = random.Random()
indexrand.seed(2)
rhythm = rhythmBlock()
pitch = pitchBlock()

class pitchAndRhythm:
def __init__(self):
self.__abet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def listCreate(self, num):
if num > 25:
print "Oops. This won't work"
else:
for a in range(num):
b = indexrand.randint(0, 3)
c = indexrand.randint(0, 7)
index = self.__abet[a]
index = [ ]
index = index.append(rhythm.rhythmTwist(b, c))

take 2: notice the "(" and ")" around the arg to __init__
 
Reply With Quote
 
Aaron Stepp
Guest
Posts: n/a
 
      12-23-2008

On Dec 22, 2008, at 10:15 PM, r wrote:

> I can't check you code because i don't have these modules but here is
> the code with proper indention
>
> import random
> from rtcmix import *
> from chimes_source import *
> from rhythmblock import *
> from pitchblock import *
> indexrand = random.Random()
> indexrand.seed(2)
> rhythm = rhythmBlock()
> pitch = pitchBlock()
>
> class pitchAndRhythm:
> def __init__(self):
> self.__abet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
> def listCreate(self, num):
> if num > 25:
> print "Oops. This won't work"
> else:
> for a in range(num):
> b = indexrand.randint(0, 3)
> c = indexrand.randint(0, 7)
> index = self.__abet[a]
> index = [ ]
> index = index.append(rhythm.rhythmTwist(b, c))
>
> take 2: notice the "(" and ")" around the arg to __init__



Thanks for the help so far, I think I'm starting to get a hang of the
syntax.

I think I need to state my goal more clearly.

Instead of writing a long list of initializations like so:

A = [ ]
B = [ ]
....
Y = [ ]
Z = [ ]

I'd like to save space by more elegantly turning this into a loop. If
I need to just write it out, I guess that's ok... but it would be much
cleaner. I'm a composer, not a programmer, so some of this is quite
above me.

I usually ask as a last resort, but I've been through the tutorial and
didn't find this. I've got a couple python books, but I'd like to
finish this piece sooner than later.

Thanks!

AS

 
Reply With Quote
 
r
Guest
Posts: n/a
 
      12-23-2008
>>> class test():
def __init__(self, name):
self.name = 'My name is %d' %name


>>> l = []
>>> for name in range(10):

l.append(test(name))


>>> l

[<__main__.test instance at 0x02852E18>, <__main__.test instance at
0x02852C38>, <__main__.test instance at 0x028528A0>, <__main__.test
instance at 0x02852E90>, <__main__.test instance at 0x02852EE0>,
<__main__.test instance at 0x02852F30>, <__main__.test instance at
0x02852F80>, <__main__.test instance at 0x02852FD0>, <__main__.test
instance at 0x023AFC60>, <__main__.test instance at 0x02855030>]
>>> l[0].name

'My name is 0'
>>> l[1].name

'My name is 1'
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      12-23-2008
On Mon, Dec 22, 2008 at 7:32 PM, Aaron Stepp <(E-Mail Removed)> wrote:
<snip>
>
> Thanks for the help so far, I think I'm starting to get a hang of the
> syntax.
>
> I think I need to state my goal more clearly.
>
> Instead of writing a long list of initializations like so:
>
> A = [ ]
> B = [ ]
> ...
> Y = [ ]
> Z = [ ]
>
> I'd like to save space by more elegantly turning this into a loop. If I
> need to just write it out, I guess that's ok... but it would be much
> cleaner. I'm a composer, not a programmer, so some of this is quite above
> me.
>


So, are these variables supposed to be module-level, or attributes of
class pitchAndRhythm, or what?
Also, are you going to use the variables normally or are you going to
need "variable variables" (e.g. like $$var in PHP, which gives the
value of the variable with the name of the string stored in $var)?

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
Aaron Stepp
Guest
Posts: n/a
 
      12-23-2008

On Dec 22, 2008, at 10:43 PM, Chris Rebert wrote:

> On Mon, Dec 22, 2008 at 7:32 PM, Aaron Stepp <(E-Mail Removed)>
> wrote:
> <snip>
>>
>> Thanks for the help so far, I think I'm starting to get a hang of the
>> syntax.
>>
>> I think I need to state my goal more clearly.
>>
>> Instead of writing a long list of initializations like so:
>>
>> A = [ ]
>> B = [ ]
>> ...
>> Y = [ ]
>> Z = [ ]
>>
>> I'd like to save space by more elegantly turning this into a loop.
>> If I
>> need to just write it out, I guess that's ok... but it would be much
>> cleaner. I'm a composer, not a programmer, so some of this is
>> quite above
>> me.
>>

>
> So, are these variables supposed to be module-level, or attributes of
> class pitchAndRhythm, or what?
> Also, are you going to use the variables normally or are you going to
> need "variable variables" (e.g. like $$var in PHP, which gives the
> value of the variable with the name of the string stored in $var)?
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...
> http://rebertia.com



The're going to only be part of the pitchAndRhythm class. Simply put,
I just need enough arrays to hold a list of pitches/rhythms. Then
I'll have each list member returned to an instrument defined in
another module.

As I'm hacking away at the code, I'm realizing that maybe I can do
this with just and A = [] and B = []. But I'm not sure...

Thanks again.

AS

 
Reply With Quote
 
Rhodri James
Guest
Posts: n/a
 
      12-23-2008
On Tue, 23 Dec 2008 03:52:35 -0000, Aaron Stepp <(E-Mail Removed)>
wrote:

> Simply put, I just need enough arrays to hold a list of
> pitches/rhythms. Then I'll have each list member returned to an
> instrument defined in another module.


One "array" can hold a list of pitches/rhythms. I'm still not terribly
clear as to why you need so many. Is each list intended for a different
instrument, so you're more looking at:

violin_1 = [ ...stuff... ]
violin_2 = [ ...other stuff...]
viola = [ ...really sweet stuff... ]
cello = [ ...really boring stuff... ]

--
Rhodri James *-* Wildebeeste Herder to the Masses
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      12-23-2008
On Mon, Dec 22, 2008 at 7:52 PM, Aaron Stepp <(E-Mail Removed)> wrote:
>
> On Dec 22, 2008, at 10:43 PM, Chris Rebert wrote:
>
>> On Mon, Dec 22, 2008 at 7:32 PM, Aaron Stepp <(E-Mail Removed)>
>> wrote:
>> <snip>
>>>
>>> Thanks for the help so far, I think I'm starting to get a hang of the
>>> syntax.
>>>
>>> I think I need to state my goal more clearly.
>>>
>>> Instead of writing a long list of initializations like so:
>>>
>>> A = [ ]
>>> B = [ ]
>>> ...
>>> Y = [ ]
>>> Z = [ ]
>>>
>>> I'd like to save space by more elegantly turning this into a loop. If I
>>> need to just write it out, I guess that's ok... but it would be much
>>> cleaner. I'm a composer, not a programmer, so some of this is quite
>>> above
>>> me.
>>>

>>
>> So, are these variables supposed to be module-level, or attributes of
>> class pitchAndRhythm, or what?
>> Also, are you going to use the variables normally or are you going to
>> need "variable variables" (e.g. like $$var in PHP, which gives the
>> value of the variable with the name of the string stored in $var)?
>>
>> Cheers,
>> Chris
>>
>> --
>> Follow the path of the Iguana...
>> http://rebertia.com

>
>
> The're going to only be part of the pitchAndRhythm class. Simply put, I
> just need enough arrays to hold a list of pitches/rhythms. Then I'll have
> each list member returned to an instrument defined in another module.
>
> As I'm hacking away at the code, I'm realizing that maybe I can do this with
> just and A = [] and B = []. But I'm not sure...
>


Do you really need to name them, or are the names arbitrary and you
only really care about having N distinct lists?

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      12-23-2008
On Mon, 22 Dec 2008 22:32:17 -0500
Aaron Stepp <(E-Mail Removed)> wrote:
> Instead of writing a long list of initializations like so:
>
> A = [ ]
> B = [ ]
> ...
> Y = [ ]
> Z = [ ]
>
> I'd like to save space by more elegantly turning this into a loop. If


Well, if all you want is a loop:

for v in vars:
locals()[v] = []

It's hard to tell if that's what you actually need though without
deeper analysis of your requirements.

--
D'Arcy J.M. Cain <(E-Mail Removed)> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
iterating over arrays with map - problem Mothra Perl 1 05-27-2004 03:37 PM
Iterating through specific rows of DataReader... Jacko ASP .Net 8 10-23-2003 10:59 PM
Iterating through controls (VB) Jeremy ASP .Net 1 07-31-2003 04:27 PM
CheckBoxList -- Iterating Through Items Ron ASP .Net 0 07-15-2003 06:51 PM



Advertisments