iterating initalizations

A

Aaron Stepp

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
 
R

r

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))
 
R

r

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__
 
A

Aaron Stepp

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
 
R

r

class test():
def __init__(self, name):
self.name = 'My name is %d' %name

l = []
for name in range(10):
l.append(test(name))

[<__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
<__main__.test said:
l[0].name 'My name is 0'
l[1].name
'My name is 1'
 
C

Chris Rebert

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
 
A

Aaron Stepp

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


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
 
R

Rhodri James

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... ]
 
C

Chris Rebert

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


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
 
D

D'Arcy J.M. Cain

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.
 
S

Steve Holden

D'Arcy J.M. Cain said:
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] = []
Note that this isn't guaranteed to work. While locals() will return a
dict containing the names and values from the local namespace, you won't
affect the local namespace by assigning values to the appropriate keys:
.... a = "hello"
.... locals()["a"] = "goodbye"
.... print a
....
If you look at the function's code you will see that the local "a" is
accessed using the LOAD_FAST and STORE_FAST opcodes, which take
advantage of the knowledge that the name is local - the interpreter
analyzed the function body looking for assignments to non-globals, and
optimizes its treatment of such names.
2 0 LOAD_CONST 1 ('hello')
3 STORE_FAST 0 (a)

3 6 LOAD_CONST 2 ('goodbye')
9 LOAD_GLOBAL 0 (locals)
12 CALL_FUNCTION 0
15 LOAD_CONST 3 ('a')
18 STORE_SUBSCR

4 19 LOAD_FAST 0 (a)
22 PRINT_ITEM
23 PRINT_NEWLINE
24 LOAD_CONST 0 (None)
27 RETURN_VALUE
It's hard to tell if that's what you actually need though without
deeper analysis of your requirements.
I think it's unlikely that the OP really does need to create names
dynamically, and should look at using either a dict indexed by the
letters of self.__abet, or a list indexed from 0 to 24 instead. But you
*are* correct about the need for a little more information ;-)

regards
Steve
 
A

Aaron Stepp

import random
from rtcmix import *
from chimes_source import * # Chime.play()
from rhythmblock import * # rhythmBlock.rhythmTwist() and
rhythmBlock.printStuff()
from pitchblock import * # pitchBlock.pitchTwist() and
pitchBlock.printStuff()
from lenEval import * #greaterThan.sovler()

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

chime = Chime()
notes = pitchBlock()
rhythm = rhythmBlock()
solve = greaterThan()

class arrayBlock:

def __init__(self, theTempo, start):
self.__A = []
self.__B = []
self.__start = start
self.__tempo = theTempo

def player(self, length, tempo, octave, pan, seed):

tempo = (120, self.__tempo)

for a in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__A = self.__A + notes.pitchTwist(one , two)

for b in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__B = self.__B + rhythm.rhythmTwist(one , two)

lenA = len(self.__A)
lenB = len(self.__B)

var = solve.solver(lenA, lenB)


for c in range(var):

print self.__A[c]

self.__start = self.__start + tb(self.__B[var])

chime.play(self.__start, self.__A[var], octave, pan, seed)

This almost does exactly what I want, and is far cleaner than my
previous attempts.

The only problem is that now all my arguments are being passed as zeros!

I assume this has to do with WHEN I'm referencing self.__A and self.__B?

AS


D'Arcy J.M. Cain said:
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] = []
Note that this isn't guaranteed to work. While locals() will return a
dict containing the names and values from the local namespace, you
won't
affect the local namespace by assigning values to the appropriate
keys:
... a = "hello"
... locals()["a"] = "goodbye"
... print a
...
If you look at the function's code you will see that the local "a" is
accessed using the LOAD_FAST and STORE_FAST opcodes, which take
advantage of the knowledge that the name is local - the interpreter
analyzed the function body looking for assignments to non-globals, and
optimizes its treatment of such names.
2 0 LOAD_CONST 1 ('hello')
3 STORE_FAST 0 (a)

3 6 LOAD_CONST 2 ('goodbye')
9 LOAD_GLOBAL 0 (locals)
12 CALL_FUNCTION 0
15 LOAD_CONST 3 ('a')
18 STORE_SUBSCR

4 19 LOAD_FAST 0 (a)
22 PRINT_ITEM
23 PRINT_NEWLINE
24 LOAD_CONST 0 (None)
27 RETURN_VALUE
It's hard to tell if that's what you actually need though without
deeper analysis of your requirements.
I think it's unlikely that the OP really does need to create names
dynamically, and should look at using either a dict indexed by the
letters of self.__abet, or a list indexed from 0 to 24 instead. But
you
*are* correct about the need for a little more information ;-)

regards
Steve
 
D

D'Arcy J.M. Cain

D'Arcy J.M. Cain said:
Well, if all you want is a loop:

for v in vars:
locals()[v] = []
Note that this isn't guaranteed to work. While locals() will return a
dict containing the names and values from the local namespace, you won't
affect the local namespace by assigning values to the appropriate keys:
... a = "hello"
... locals()["a"] = "goodbye"
... print a

This was my test:
locals()['x'] = "hello"
x 'hello'
locals()['x'] = "goodbye"
x
'goodbye'

Just didn't want people to think that I post without testing.

In any case, even if that worked as expected I am pretty sure that it
is the wrong solution but without knowing more about what the OP is
doing it is impossible to know what the right answer is.
 
S

Steve Holden

D'Arcy J.M. Cain said:
D'Arcy J.M. Cain said:
Well, if all you want is a loop:

for v in vars:
locals()[v] = []
Note that this isn't guaranteed to work. While locals() will return a
dict containing the names and values from the local namespace, you won't
affect the local namespace by assigning values to the appropriate keys:
... a = "hello"
... locals()["a"] = "goodbye"
... print a

This was my test:
locals()['x'] = "hello"
x 'hello'
locals()['x'] = "goodbye"
x
'goodbye'

Just didn't want people to think that I post without testing.

In any case, even if that worked as expected I am pretty sure that it
is the wrong solution but without knowing more about what the OP is
doing it is impossible to know what the right answer is.
The thing you overlooked was that the locals of a function are special.
The locals of a module are the globals!
.... return locals() is globals()
....
regards
Steve
 
R

Rhodri James

import random
from rtcmix import *
from chimes_source import * # Chime.play()
from rhythmblock import * # rhythmBlock.rhythmTwist() and
rhythmBlock.printStuff()
from pitchblock import * # pitchBlock.pitchTwist() and
pitchBlock.printStuff()
from lenEval import * #greaterThan.sovler()

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

chime = Chime()
notes = pitchBlock()
rhythm = rhythmBlock()
solve = greaterThan()

class arrayBlock:

def __init__(self, theTempo, start):
self.__A = []
self.__B = []
self.__start = start
self.__tempo = theTempo

def player(self, length, tempo, octave, pan, seed):

tempo = (120, self.__tempo)

for a in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__A = self.__A + notes.pitchTwist(one , two)

for b in range(length):

one = indexrand.randint(0, 3)

two = indexrand.randint(0, 7)

self.__B = self.__B + rhythm.rhythmTwist(one , two)

lenA = len(self.__A)
lenB = len(self.__B)

var = solve.solver(lenA, lenB)


for c in range(var):

print self.__A[c]

self.__start = self.__start + tb(self.__B[var])

chime.play(self.__start, self.__A[var], octave, pan, seed)

This almost does exactly what I want, and is far cleaner than my
previous attempts.

The only problem is that now all my arguments are being passed as zeros!

Which "all" your arguments? There are an awful lot there; what *exactly*
do
you mean?
I assume this has to do with WHEN I'm referencing self.__A and self.__B?

If you mean __A and __B are full of zeroes, then you should suspect your
notes.pitchTwist() and rhythm.rhythmTwist() methods of returning zeroes.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top