[Tutor] batch file processing w/ python using cmd line executable?

2010-02-12 Thread kevin parks

hi

I am new territory here and not even sure where to start poking around  
other than the os module some.


Essentially i need to do something like a shell script for batch  
processing gobs of files. I am trying to use a command line tool (sox,  
an open source sound file converter that runs from the unix command  
line) and I don't want to edit the command line, run the job, edit the  
command line, etc over and over again for hundreds of small files.


I wonder if it is possible to use python to call sox and have it do  
os.mkdir, process all the input files in a particular directory and  
put the converted files into the directory it made with mkdir...


so if i had

kp/flute/ST

 kp8/flute/ST/foo01.aif
 kp8/flute/ST/foo02.aif
 kp8/flute/ST/foo03.aif
 kp8/flute/ST/foo04.aif
 kp8/flute/ST/foo05.aif

The script would call sox repeatedly and create a new dir with a  
converted file for each found in the original folder. like so:


kp/flute/STout/

 kp/flute/STout/foo01.wav
 kp/flute/STout/foo02.wav
 kp/flute/STout/foo03.wav
 kp/flute/STout/foo04.wav
 kp/flute/STout/foo05.wav

what makes this especially hairy is that sox is a monster an typically  
needs a crazy number of arguments, though these would be the same for  
the whole batch. A typical command line call i need to make to, say,  
batch convert files from one sample rate and bit depth to another  
would look like so:


% sox -V3 -D -S St.01.aif -b16  kp/flute/STout/St.01.wav rate -s -v  
44100


Is there away to do this in python, by just pointing it to a whole dir  
of files and say "do it" to all of these?


cheers,

kevin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bowing out

2010-03-03 Thread kevin parks

Dang!

I wish you were not going. But really, I have to say a HUGE thank you  
to you for all the fine teaching you have done on this list. I learned  
so much from reading your posts. Thanks for all the time and effort  
(and code) you put into this! I wish you were staying. Hats off to  
you. Wish there was some way to give a standing ovation over the  
internet. Glad to see though that the list has been handed off to 2  
fantastic folks but still sad to see you go. Wish Danny Yoo was still  
here too.


Thank you Kent. Thank you very much~

-kevin--



On Mar 3, 2010, at 10:17 PM, Kent Johnson wrote:


Hi all,

After six years of tutor posts my interest and energy have waned and
I'm ready to move on to something new. I'm planning to stop reading
and contributing to the list. I have handed over list moderation
duties to Alan Gauld and Wesley Chun.

Thanks to everyone who contributes questions and answers. I learned a
lot from my participation here.

So long and keep coding!
Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] subprocess

2010-03-27 Thread kevin parks
I tried readings some toots and tried reading alan's thing. I just still can't 
grok how to use subprocess.

I am trying to call sox (fun fact: an early contributer to sox was none other 
than Guido van Rossum)

In the old days you would just use os i guess, like:

import os
os.system('sox -V3 -D -S St.01.aif -b16 Stout-01.aif rate -s -v 44100')

to call a unix executable that you would ordinarily run from the terminal.

what would the equivalent of this in python's new subprocess be? perhaps if i 
saw an example it would click..

additionally.. sox is a sound conversion tool. I plan to batch process a bunch 
of files. Will subprocess start all the jobs as it finds the files? or will it 
process one job and que the next? If it opened a thread and started a bunch of 
jobs it would likely bog down the system no?

anyway  I have the os walk and other stuff that i am working on and i was 
hoping to get some help on subprocess. There are a few pages on subprocess but 
they all might just as well be in chinese and none of them, none that i can see 
are equivalent to what i am trying to do (they all seem to be doing os-y things)

An example might help. Not sure why i am finding this so hard to get my head 
around.






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess

2010-03-27 Thread kevin parks

Thanks David. Those are excellent short clear examples. I will look those over. 
Super! Thanks for that.

-kp

On Mar 27, 2010, at 5:30 PM, David Abbott wrote:

> On Sat, 2010-03-27 at 16:55 +0900, kevin parks wrote:
> 

> Here is an example using subprocess.call
> http://dwabbott.com/code/index8.html
> 
> and some more here with subprocess.Popen
> http://asterisklinks.com/wiki/doku.php?id=wiki:subprocess
> 
> HTH
> David
> 
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] automatic output file naming scheme

2010-03-28 Thread kevin parks
okay. I got the subprocess bit to work and i have os walk doing its walk. But 
now for something i did not think about until i started to think about how to 
fit these two bits to work together.

os walk is going to traverse my dirs from some given starting point and process 
files that it finds that fit my criteria. So i my case it will look for all 
sound files in a given directly structure and then call sox and do a 
conversion. This part i can already probably do just by combining the working 
code that i have … but now i need to have some kind of automagic naming scheme 
that creates an output file name that is based on the input name but is unique, 
either adding a number or suffix before the file extension, or even a time 
stamp. Since we want to create file names that are unique and not clobber 
existing files, but also will tells us something meaningful about how the file 
was created so that finding:

foo01.aif or foo.1.aif would yield something like foo-out01.aif or 
foo01-out.aif or something similar.

How can you do such a thing in python? is there some elegant way to take the 
input file name and combine it with some other string to create the output file 
name?

-kp


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] packing a list of lists

2009-08-28 Thread kevin parks
Back to python after a long long layoff. So i am running into some  
beginner's confusion...


I am trying to plot a list of numbers in gnuplot.py. To do that I am  
trying to pack the list with an index by iterating over the list so i  
can get something like:


foo = [12, 11, 9, 6, 2, 9, 3, 8, 12, 3, 5, 6]

[ [1, 12], [2, 11], [3, 9], [4, 6], [5, 2], [6, 9], [7, 3], [8, 8] ... ]

So that i have x, y pairs to plot. When i print in my func i get the  
right thing, for each item (note scaffolding) yet when i reurn the  
whole list i just get the last pair repeated over and over.


I am not sure why this is.


def pack(in_seq):
out_list=[]
x = 1
ll=[1, 1]
for each in in_seq:
ll[0] = x
ll[1] = each
out_list.append(ll)
#print ll
x = x + 1
print out_list


# function declarations would go here
def test():
"""test function - say what this does here and skip a line

Keyword arguments:
none
"""

print
foo = minus(200)
plot_me = pack(foo)
#print foo
print
print plot_me


if __name__ == "__main__":
test()


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] packing a list of lists

2009-08-28 Thread kevin parks


Thanks for the replies. Though the list comprehension does not work:

TypeError: enumerate() takes exactly 1 argument (2 given)


On Aug 29, 2009, at 12:20 AM, vince spicer wrote:




   #print foohough I didn't test your code, I think what you are  
trying to accomplish can be done using enumerate cleaner



def pack(foo):
out = []
for x,y in enumerate(foo, 1):
out.append((x,y))
return out




Or even cleaner with list comprehension

def pack(foo):
return [x for x in enumerate(foo, 1)]




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] packing a list of lists

2009-08-28 Thread kevin parks
I think this is b/c I am running 2.5. I also have 2.6 but i am using  
2.5 for gnupoly and an older audio lib i have.
I ran the listcom below on 2.6 and it worked, so i just have to figure  
out how that can be written for 2.5. I guess

2.6 has an update to enumerate.

-k



On Aug 29, 2009, at 2:21 AM, afit...@gmail.com wrote:


Enumerate() is returning a tuple, I haven't tested this code but try:
[[x[0],x[1]] for x in enumerate(blah,1)]
--Original Message--
Sender: tutor-bounces+afith13+python=gmail@python.org
To: tutor@python.org
Subject: Re: [Tutor] packing a list of lists
Sent: Aug 28, 2009 9:49 AM


Thanks for the replies. Though the list comprehension does not work:

TypeError: enumerate() takes exactly 1 argument (2 given)


On Aug 29, 2009, at 12:20 AM, vince spicer wrote:




  #print foohough I didn't test your code, I think what you are
trying to accomplish can be done using enumerate cleaner


def pack(foo):
   out = []
   for x,y in enumerate(foo, 1):
   out.append((x,y))
   return out




Or even cleaner with list comprehension

def pack(foo):
   return [x for x in enumerate(foo, 1)]




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] packing a list of lists

2009-08-28 Thread kevin parks

Interestingly changing:
out_list.append(ll)

to

out_list.append(list(ll))


seems to work. The part of my brain that understood why that is must  
have sleeping.


-k


On Aug 28, 2009, at 11:05 PM, i wrote:

Back to python after a long long layoff. So i am running into some  
beginner's confusion...


I am trying to plot a list of numbers in gnuplot.py. To do that I am  
trying to pack the list with an index by iterating over the list so  
i can get something like:


foo = [12, 11, 9, 6, 2, 9, 3, 8, 12, 3, 5, 6]

[ [1, 12], [2, 11], [3, 9], [4, 6], [5, 2], [6, 9], [7, 3], [8,  
8] ... ]


So that i have x, y pairs to plot. When i print in my func i get the  
right thing, for each item (note scaffolding) yet when i reurn the  
whole list i just get the last pair repeated over and over.


I am not sure why this is.


def pack(in_seq):
out_list=[]
x = 1
ll=[1, 1]
for each in in_seq:
ll[0] = x
ll[1] = each
out_list.append(ll)
#print ll
x = x + 1
print out_list


# function declarations would go here
def test():
"""test function - say what this does here and skip a line

Keyword arguments:
none
"""

print
foo = minus(200)
plot_me = pack(foo)
#print foo
print
print plot_me


if __name__ == "__main__":
test()




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] packing a list of lists

2009-08-28 Thread kevin parks


On Aug 29, 2009, at 12:23 AM, Michael M Mason wrote:


i wrote:

def pack(in_seq):
out_list=[]
x = 1
ll=[1, 1]
for each in in_seq:
ll[0] = x
ll[1] = each
out_list.append(ll)
#print ll
x = x + 1
print out_list


Variable out_list consists of list ll repeated however many times.  
Each
time you change ll you're changing it everywhere it appears in  
out_list.

That is, what's being appended to out_list isn't a copy of ll, it's a
pointer to ll.

You need something like:-

   out_list.append([ll[0],ll[1]])


Right... ugh.. Totally forgot about that. Python 101. I don't know why  
my brain resists
that idea. Every time i let python alone a while and come back to it i  
get bit by this.





And you need to add a return at the end of the function, otherwise it
returns None:

return out_list



That, of course, i know... I was messing around debugging of course.

Thanks for the response.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] mapping/filtering a sequence

2009-09-05 Thread kevin parks
I am doing some data massage, minor mapping and filtering really and i  
find that i have a lot of this kind of kludgy code:


# -- -
def filt_seq(inseq):
out_list = []
for item in inseq:
# 38 needs to be mapped on to 34 as we don't have a 38 in our 
grid
if item == 38:
item = item - 4
out_list.append(item)
# We also don't have a 40, but we do have a 39. Map that sucka
elif item == 40:
item = item - 1
out_list.append(item)
		# if we get values that fall in good places, just pass them on to  
the out seq

else:
out_list.append(item)
return out_list

# -- -

To do some basic jiggering of some out of range off grid data.

There has to be a better, more elegant, more flexible less brute force  
more pythonic way to do this kind of mapping no?


cheers,

-kp--


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] working with multiple sets

2009-09-05 Thread kevin parks
I am doing some simple things with sets and so far have had a lot of  
success with python's built-in sets, which is such a great new(ish)  
"batteries included" type python data type.


 [snip]  [snip] -- [snip]  [snip]  
--

#!/usr/bin/env python

def test():
x = range(10)
y = range(5, 15)
z = range(8, 22)
setx = set(x)
sety = set(y)
setz = set(z)
print "\n", "x = ", x, "\n", "y = ", y, "\n", "z = ", z, "\n" * 2
overlap1 = setx.intersection(sety)
overlap1 = sorted(list(overlap1))
print "overlap of x and y:", overlap1
#
overlap2 = sety.intersection(setz)
overlap2 = sorted(list(overlap2))
print "overlap of y and z:", overlap2
#
overlap3 = setx.intersection(setz)
overlap3 = sorted(list(overlap3))
print "overlap of x and z:", overlap3

if __name__ == "__main__":
test()

 [snip]  [snip] -- [snip]  [snip]  
--



so silly stuff like that works fine. But i want to do a little more  
than that. I want to be able to look at a number/item and see which  
lists it is in so that i could maybe have a master list of all the  
data, a superset, and then an indication of which lists that data  was  
in, as some items will only be in one list, some will appear in two  
lists (x & y, or x & z or y & z) and a small handful will be in all  
three lists.


0 - x
1 - x
2 - x
3 - x
4 - x
5 - x, y
6 - x, y
7 - x, y
8 - x, y, z
9 - x, y, z
10 - y, x

etc.

Of course the whole point of this is that the sets will be more  
complicated than 0-9, 5-14, and 8-21 and additionally, the sets may  
not be a list of numbers but eventually a list of strings.


So the steps would be to create the superset
then test for membership for each list?

I kinda get it, the thing that warps my brain is the idea that there  
are more than 2 lists now to test against eventually my script  
needs to accommodate 4, 5, 6 sets.. but i would just like to see if i  
can get 3 sets to work first.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help deciding between python and ruby

2009-09-05 Thread kevin parks



1. go to the book store
2. pull a copy of learning ruby by Michael Fitzgerald (ora.com)
3. pull a copy of learning python by Mark Lutz (ora.com)
4. read chapter 1 of each
5. make a decision
6. get to work

Alternately check comp.lang.python where this question comes up over  
and over and over and over

and worry your head some more.

And while i am all for python advocacy. I:

1. Could not do better than the first 20 pages of the Lutz.

2. I am (and i am sure i will be over-ruled and shouted down for  
this), not entirely sure the tutor list
is the appropriate place for this query. Places like comp.lang.python  
better serve this purpose.


The tutor list is primarily a list (cribbing from http://mail.python.org/mailman/listinfo/tutor) 
 for folks
who want to ask questions regarding how to learn computer programming  
with the Python language.


I am not entirely sure i want to hear a long drawn out thought  
experiment on what one person thinks
upon adopting a new programming language. I joined this list to learn  
the specifics of programming in python
and to get help and learn from the questions and answers posted by  
others. If this place too gets clogged with
python -v- perl, python-v-ruby, phython -v- php navelgazing it is  
going to ruin the tutor list which has a pretty
clear mission and has served that purpose very very well over the  
years i have been on it. I think we should

redirect this query and avoid "mission creep"

All IMO, YMMV, etc. & co.

If you think i am just being a jerk, at least take my initial advice.  
The ora books BOTH have
great overviews of each language. A cup of coffee and reading the  
first 20 pages of each

will tell you great overviews of Python v Ruby.

-kevin






On Sep 4, 2009, at 11:46 PM, Dan King wrote:

I think you will get at the least a slight bias toward Python.  
However,
I think you should do your own research and reach your own  
conclusions.

Simply to get you started I put the following into Google: 'Python or
Ruby: Which to learn' and got more than 1M hits.



Best of luck.



Robert


Well, the slight (at the very least) bias toward Python is expected  
as this is a python mailing list. I have already done research on  
python and ruby via Google. I've found that python is described as  
more 'explicit,'  used in multiple situations, has better libraries,  
and has unique 'spacing & underscore' syntax requirements; and ruby  
on the other is more 'implicit,' used more in web-apps (via ROR),  
emphasizes code-readability/beauty, and is more flexible (i.e. has  
more than one way of doing something).


While the preceding information is nice, the information hasn't  
helped me decide which one to learn - that's the reason for my post.  
I want to sift through the hype for each language. I want to  
understand the perspectives of people who use python - why they like/ 
dislike it, if/why the would recommend learning it, and what  
experiences they've had with python in professional contexts.


To put my programming knowledge in context, I have experience with  
php and java; I enjoy php's ease and the syntax of java (everything  
is apparent from the code, although it can be a bit verbose). Look  
forward to the responses. Thanks.


-Dan




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mapping/filtering a sequence

2009-09-05 Thread kevin parks
I can think of about 80 billion reasons why you would encounter data  
outside your grid, esp. if you aren't the original producer of the  
data. Imagine you are mapping something to color (RGB values) or you  
are doing a signification of volcanic activity or the big bang or the  
earth's magnetic field and you are getting data from an external data.  
Mapping of external data happens all the time. Like the xSonify  
project, a Java application to display numerical data as sound by  
NASA, etc.


Sometimes (to stay with this example) the data you have doesn't fit  
exactly to your grid (in this case a tuning system of musical pitches  
within the 12-tone ET system or any other form of tuning which will  
have discreet steps or a mix of RGB colors)


Then what you would want to do is map the data to the nearest value no?

>>Why is data being generated outside your grid?

Because it is data given to me by an outside source for example, not  
necessarily data i generated myself, or it is the result of some  
algorithm or natural number series or a fractal, sensor data, younameit.


>>What are the available values for the grid? Are they flexible or  
permanent? A bit more info would be helpful in finding a solution.


At this moment, i have input that represents 7-bits of sensor data  
resolution (128) so a fixed grid of integers 0-127 is how i am getting  
this data in but i don't have all 0-127 to map it to.


For this particular hack, I am mostly getting useful values in the  
range 30-85, so I am filtering out anything below 34 and above 83, but  
i don't have 34-83 contiguous. I have a gap toothed integer grid of:


[34, 39, 41, 42, 43, 44, 46, 48, 49, 50, 51, 53, 54, 55, 56, 58, 60,  
61, 62, 63, 65, 66, 67, 68, 70, 72, 73, 75, 77, 79, 80, 82]


I want to map each incoming integer to the closest one available. I  
know, it is idiosyncratic. I also need to be able to do this with  
other idiosyncratic gap toothed sets.


It's a weird, idiosyncratic, real world messy question.

If there is no easy way, it would prolly be just as easy to hack it  
all up with conditionals by hand at this point.


cheers,

k

PS. Os there some reason why it suddenly takes a day to see your posts  
to the tutor list appear? I thought this query didn't make it through  
and gave up as i sent it yesterday and it only popped up on list just  
now.



On Sep 5, 2009, at 11:47 PM, Wayne wrote:


On Fri, Sep 4, 2009 at 12:31 PM, kevin parks  wrote:
I am doing some data massage, minor mapping and filtering really and  
i find that i have a lot of this kind of kludgy code:


To do some basic jiggering of some out of range off grid data.

There has to be a better, more elegant, more flexible less brute  
force more pythonic way to do this kind of mapping no?


Why is data being generated outside your grid? That's the first  
question you should ask, and probably the better way to address the  
problem.


What are the available values for the grid? Are they flexible or  
permanent? A bit more info would be helpful in finding a solution.


-Wayne


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mapping/filtering a sequence

2009-09-05 Thread kevin parks
Yeah the list seems flaky at the moment. Additionally, my query is an  
incredibly stupid one. But what you have works and represents an  
improvement over the unreadable kludge I was doing. Thanks to all who  
responded.


cheers,

k


On Sep 6, 2009, at 12:26 AM, Douglas Philips wrote:


On or about 2009 Sep 5, at 10:45 AM, Martin A. Brown indited:

I must have missed a message or two, because I don't understand why  
math is being done on constants like that.

Given my misunderstanding, I'd go with:

my_map = { 38: 34, 40: 39 }

def filter_item(item):
   return my_map.get(item, item)

l = [ ... ]
l = map(filter_item, l)


-Doug


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help deciding between python and ruby

2009-09-06 Thread kevin parks
Well, the only thing more annoying than off topic discussion is "meta"  
discussion on lists and folks passing judgement on other people's  
posts (ie. you should have x, your should not have Y... I shouldn't  
have this second doughnut, but you know what, i am gonna eat that  
*and* have a second cup of coffee). So i'll resist the temptation to  
reply to Luke in detail, who i did not realize was a moderator here.


In any case, I happen to have right in front of me, a brandy new  
spendy (in Korea anyway) copy of the Lutz book Learning Python 3rd  
Edition O'Reilly and if you look at "Part I Getting Started," the book  
begins with a very compelling section called "Why People Use Python"  
and it outlines:


Software quality
Development productivity
Program Portability
Support libraries
Component integration
Enjoyment

and the end of this chapter, pp. 18-19 has a section called: "How does  
python stack up to language x" which gives a bunch of comparisons to  
Ruby and others. Here he says:


[Python] is more mature and has a more readable syntax than Ruby.  
Unlike Ruby and Java, OOP is an option in Python — Python does not  
impose OOP on users or projects which might not apply.


Most of this has already been touched on in this thread ... But I  
would add that that very past point is a HUGE reason why i use python.  
For little pea-shooter scripts I don't want to have to know and apply  
advanced OOP concepts. Yet if you do know them and your project  
requires it... it is there. One of the criticism of Python by Ruby  
users is that it isn't as "purely OO" as Ruby is. It is up to you  
whether that is a bug or a feature. But you can go quite far in Python  
without being an OOP guru but i am not sure that is entirely true of  
Ruby. Check with Ruby users. Personally i don't want to have to know  
how to do Schenkerian analysis and set theory just to have my first  
piano lesson and play Mary had a little Lamb, and i don't want to  
learn Norwegian just to hear a Norwegian folk tale. I like to dig  
right in and Python is good for that for sure.


-k




On Sep 6, 2009, at 5:45 AM, Luke Paireepinart wrote:



ruby on the other is more 'implicit,' used more in web-apps (via  
ROR), emphasizes code-readability/beauty, and is more flexible (i.e.  
has more than one way of doing something).
You mean people actually like ruby's syntax?  I think Python's the  
prettiest language I've worked with syntactically.
I wouldn't call having multiple ways to do something "flexible".  I  
agree more with Python's philosophy, there should be one (and  
preferably only one) obvious way to do something.


@kevin parks:
> I am (and i am sure i will be over-ruled and shouted down for  
this), not entirely sure the tutor list
> is the appropriate place for this query. Places like  
comp.lang.python better serve this purpose.

This is a good point, you probably should have stopped there.

Yes, this is probably not the best place for this discussion.  I  
would like to see how it pans out, though, now that we're already 15  
posts in.  It's not very difficult to ignore a thread in your  
favorite e-mail client, perhaps you could just do that?  If these  
debates become commonplace I will probably help you yell at everyone  
to stop flooding the list, but the amount of such questions has  
always been quite low and never bothersome, and I've been on Tutor  
since 2005.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mapping/filtering a sequence

2009-09-06 Thread kevin parks
I actually find the map biz easier to get my head around than the list  
comp. I guess this makes it another good reason for me to be happy  
that map is apparently staying in after nearly being depreciated. I  
generally prefer list comp in every instance, but the idea of an if  
else construct within the conditional of a list comp really hurts my  
brain, but it is nice to see that, had map(), filter() and reduce()  
all gotten the boot, there is a way to do it with a list comp, opaque  
as it is.


Thanks to all who responded this.

-kp--





On Sep 6, 2009, at 5:29 AM, Mark Tolonen wrote:



"Douglas Philips"  wrote in message news:9ee00578-6af7-4c6c-9968-af5f25a00...@mac.com 
...

On 2009 Sep 5, at 12:22 PM, Mark Tolonen wrote:

As a list comp:


L=range(30,41)
[{38:34,40:39}.get(n,n) for n in L]

[30, 31, 32, 33, 34, 35, 36, 37, 34, 39, 39]



True, that is terse, but IMHO has maintainability issues. The  
mapping data structure and the method of transformation (.get())  
are tangled  in with the transformation itself.
Again, IMHO, unless this is a very short script, the maintenance   
should outweigh raw terseness.


This *was* a very short script.  My point was using a list comp.  If  
you want to nitpick, don't use lower-case L for variables either :^)


my_map = { 38: 34, 40: 39 }

def filter_item(item):
   return my_map.get(item, item)

L = [filteritem(n) for n in L]

Happy?

-Mark


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-08 Thread kevin parks



I am looking at this and wondering:

Why does this use collections.defaultdict ?

In fact i guess since collections.defaultdict is new to me i am not  
even sure why it exists
and why someone would use this as opposed to using Python's built-in  
dictionary? and why was it

used in this instance?

cheers,

-kp--




On Sep 6, 2009, at 3:06 AM, bob gailer wrote:




I want to be able to look at a number/item and see which lists it is  
in so that i could maybe have a master list of all the data, a  
superset, and then an indication of which lists that data  was in,  
as some items will only be in one list, some will appear in two  
lists (x & y, or x & z or y & z) and a small handful will be in all  
three lists.


I think you mean "set" rather than "list"

To enable processing of an arbitrary number of sets, put them in a  
collection (list or dictionary). Use a list if it is sufficient to  
identify sets by number, else use a dictionary.


Use a dictionary to relate items to their set(s).

import collections
lookup = collections.defaultdict(list)
sets = {'x': set((1,2,3)), 'y': set((2,3))}
for key, value in sets.items():
for element in value:
 lookup[element].append(key)
print lookup


0 - x
1 - x
2 - x
3 - x
4 - x
5 - x, y
6 - x, y
7 - x, y
8 - x, y, z
9 - x, y, z
10 - y, x

etc.

Of course the whole point of this is that the sets will be more  
complicated than 0-9, 5-14, and 8-21 and additionally, the sets may  
not be a list of numbers but eventually a list of strings.


So the steps would be to create the superset
then test for membership for each list?

I kinda get it, the thing that warps my brain is the idea that there  
are more than 2 lists now to test against eventually my script  
needs to accommodate 4, 5, 6 sets.. but i would just like to see if  
i can get 3 sets to work first.

--
Bob Gailer
Chapel Hill NC
919-636-4239


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-08 Thread kevin parks

I also notice that if i do:


def foo():
lookup = collections.defaultdict(list)
x = range(10)
y = range(5, 15)
z = range(8, 22)
sets = {'x': set(x), 'y': set(y), 'z': set(z)}
for key, value in sets.items():
for element in value:
lookup[element].append(key)
for x in lookup:
print x, lookup[x]
print

in oder to print more clearly what I want to see, the sets (as usual  
for a mapping type) are not always in order. Note that from 5 to 7 for  
example 'y' is listed in front of 'x' and 8 & 9 have 'y', 'x', 'z' and  
not 'x', 'y', 'z' ... I am not clear on how to sort that as the  
dictionary 	method lookup.sort() either doesn't work or i have tried  
it in all the wrong places.



0 ['x']
1 ['x']
2 ['x']
3 ['x']
4 ['x']
5 ['y', 'x']
6 ['y', 'x']
7 ['y', 'x']
8 ['y', 'x', 'z']
9 ['y', 'x', 'z']
10 ['y', 'z']
11 ['y', 'z']
12 ['y', 'z']
13 ['y', 'z']
14 ['y', 'z']
15 ['z']
16 ['z']
17 ['z']
18 ['z']
19 ['z']
20 ['z']
21 ['z']

On Sep 8, 2009, at 10:52 PM, bob gailer wrote:


kevin parks wrote:



I am looking at this and wondering:

Why does this use collections.defaultdict ?

In fact i guess since collections.defaultdict is new to me i am not  
even sure why it exists
and why someone would use this as opposed to using Python's built- 
in dictionary? and why was it

used in this instance?


It simplifies coding, as it takes care of initializing each new  
entry to a list.



On Sep 6, 2009, at 3:06 AM, bob gailer wrote:




I want to be able to look at a number/item and see which lists it  
is in so that i could maybe have a master list of all the data, a  
superset, and then an indication of which lists that data  was in,  
as some items will only be in one list, some will appear in two  
lists (x & y, or x & z or y & z) and a small handful will be in  
all three lists.


I think you mean "set" rather than "list"

To enable processing of an arbitrary number of sets, put them in a  
collection (list or dictionary). Use a list if it is sufficient to  
identify sets by number, else use a dictionary.


Use a dictionary to relate items to their set(s).

import collections
lookup = collections.defaultdict(list)
sets = {'x': set((1,2,3)), 'y': set((2,3))}
for key, value in sets.items():
 for element in value:
   lookup[element].append(key)
print lookup



--
Bob Gailer
Chapel Hill NC
919-636-4239


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-08 Thread kevin parks

Actually,

This seems like it works:

lookup[x].sort()


in like so:

def foo():
lookup = collections.defaultdict(list)
x = range(10)
y = range(5, 15)
z = range(8, 22)
sets = {'x': set(x), 'y': set(y), 'z': set(z)}
for key, value in sets.items():
for element in value:
lookup[element].append(key)
print
print lookup, "\n\n"
for x in lookup:
lookup[x].sort()
print x, lookup[x]
#print type(lookup)
print
#print sets

I realized that i was trying to apply the standard:

k = lookup.keys()
k.sort()

in the wrong way and in the wrong place (i accidentally cut it out in  
my message when i removed the scaffolding)


I might try the tuple things to for educational purposes. I am  
somewhat nervous about using something other than
a built in type as i am not familiar with collections and it isn't  
well covered in beginner books or the docs.




On Sep 9, 2009, at 12:44 AM, Kent Johnson wrote:


On Tue, Sep 8, 2009 at 10:07 AM, kevin parks wrote:

I also notice that if i do:


def foo():
   lookup = collections.defaultdict(list)
   x = range(10)
   y = range(5, 15)
   z = range(8, 22)
   sets = {'x': set(x), 'y': set(y), 'z': set(z)}
   for key, value in sets.items():
   for element in value:
   lookup[element].append(key)
   for x in lookup:
   print x, lookup[x]
   print

in oder to print more clearly what I want to see, the sets (as  
usual for a
mapping type) are not always in order. Note that from 5 to 7 for  
example 'y'
is listed in front of 'x' and 8 & 9 have 'y', 'x', 'z' and not 'x',  
'y', 'z'


Dictionaries and sets are not ordered. The order of items in
sets.items() is an implementation detail, not something you can depend
on.


... I am not clear on how to sort that as the dictionary  method
lookup.sort() either doesn't work or i have tried it in all the wrong
places.


lookup can't be sorted directly as it is a (default)dict. Anyway it is
lookup[x] that you want to sort. Try
 print x, sorted(lookup[x])

or
for x in lookup:
 lookup[x].sort() # list.sort() sorts the list in place and does not
return a value.
 print x, lookup[x]

Another alternative would be to use a list of tuples instead of a dict
for sets, then the lookup values would be created in the desired
order, e.g.
sets = [ (x', set(x)), ('y', set(y)), ('z', set(z)) ]
for key, value in sets:
 # etc

Kent


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-08 Thread kevin parks


I guess what i honestly want to asks, but am hesitant to since it  
makes me look like a dork is:


What would this look like if i want to use a straight up built-in  
dictionary type and not the collections.defaultdict.



import collections

def foo():
lookup = collections.defaultdict(list)
x = range(10)
y = range(5, 15)
z = range(8, 22)
sets = {'x': set(x), 'y': set(y), 'z': set(z)}
for key, value in sets.items():
for element in value:
lookup[element].append(key)
print "\n", lookup, "\n\n"
for x in lookup:
lookup[x].sort()
print x, lookup[x]
print "\n"


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-09 Thread kevin parks

This discussion is making my brain melt.

It is also showing how clever Bob was to do it the way he did... I  
found a solution that i think works, and think has not yet been  
suggested. I quarantined Bob's code into a black box ... and then cast  
the output as a plain old fashioned python built in dictionary on  
output. So now instead of printing the code Bob gave the collection is  
returned by the func.


Then i can cast it as a dict and pick over that dictionary as i wish.  
Here (as a bonus) I can transverse a range of keys that is inclusive  
of all my keys and also use python's get() dict method to also  
indicate index points (keys) that are empty.. which by default returns  
'None', which is also useful in this case to show me what is missing.  
But I also have to do some type testing tomfoolery since missing keys  
return None, which is a special type (and not a list like the  
others)... I wanted the value list sorted so... i did  if type(item)  
== type(foo):  not sure if there is a betterererer way.


Anyway, this woiks.

--


#!/usr/bin/env python

import collections

def pscape():
lookup = collections.defaultdict(list)
k1 = [34, 39, 41, 42, 43, 44, 46, 48, 49, 50, 51, 54, 55, 56, 58,  
60, 61, 62, 63, 65, 66, 67, 68]

k2 = [51, 56, 58, 63, 65, 68, 70, 72, 75, 77, 80, 82]
y1 = [51, 53, 54, 56, 58, 60, 61, 63, 65, 66, 68, 70, 72, 73, 70,  
72, 73, 75, 77, 79, 80]

sets = {'k1': set(k1), 'k2': set(k2), 'y1': set(y1)}
for key, value in sets.items():
for element in value:
lookup[element].append(key)
return lookup

def test():
gamut = dict(pscape())
# -- scaffolding
#print "\n\n", type(gamut), "\n\n", gamut, "\n\n", gamut.keys()
print "\n\n"
foo = [1, 2, 3]
for x in range(30, 85):
item = gamut.get(x)
if type(item) == type(foo):
item.sort()
print x, item
print "\n\n"

if __name__ == "__main__":
test()


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mapping/filtering a sequence

2009-09-09 Thread kevin parks
Prolly good to post final solutions for future goog'lerz (like when i  
forget) or anyone who was following along.


Here's where i ended up with this... shows both ways.
--

#!/usr/bin/env python

my_map = { 38:34, 40:39, 45:44, 47:46, 52:51, 59:58, 55:56 }

def filter_item(item):
return my_map.get(item, item)

# you can do it old skool with map()
def filter_test1():
foo = range(60)
mappedfoo = map(filter_item, foo)
for item in foo:
print foo[item], mappedfoo[item]

# you can also do it with a list comp
def filter_test2():
foo = range(60)
mappedfoo = [filter_item(n) for n in foo]
for item in foo:
print foo[item], mappedfoo[item]

if __name__ == "__main__":
filter_test1()
print "\n","--" * 8, "\n"
filter_test2()

#--EOF--
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] working with multiple sets

2009-09-09 Thread kevin parks

Are any of these methods better than another for some reason?


On Sep 9, 2009, at 10:12 PM, Lie Ryan wrote:


kevin parks wrote:

This discussion is making my brain melt.
It is also showing how clever Bob was to do it the way he did... I  
found a solution that i think works, and think has not yet been  
suggested. I quarantined Bob's code into a black box ... and then  
cast the output as a plain old fashioned python built in dictionary  
on output. So now instead of printing the code Bob gave the  
collection is returned by the func.
Then i can cast it as a dict and pick over that dictionary as i  
wish. Here (as a bonus) I can transverse a range of keys that is  
inclusive of all my keys and also use python's get() dict method to  
also indicate index points (keys) that are empty.. which by default  
returns 'None', which is also useful in this case to show me what  
is missing. But I also have to do some type testing tomfoolery  
since missing keys return None, which is a special type (and not a  
list like the others)... I wanted the value list sorted so...



1
i did  if type(item) == type(foo):  not sure if there is a  
betterererer way.




2

You can use:
alist = [1, 2, 3]
if isinstance(alist, list):
   ...



3

or alternatively check for the None case:
if alist is not None:
   ...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is this an example of (and how can i use it?)

2009-09-20 Thread kevin parks
I am afraid that in the long layoff in python has meant some new  
constructs have passed me by. In googling around I found some nice  
little code I want to use, but i don't quite understand it, how it is  
called, and what it is an example of. I guess there are generators and  
iterators now and it seems this might be an example of one of those  
new constructs. Can anyone explain what it is i am looking at, how it  
is called, and what it is an example of so that I can look it up:


def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is this an example of (and how can i use it?)

2009-09-20 Thread kevin parks


On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote:


kevin parks wrote:

called, and what it is an example of. I guess there are generators  
and iterators now and it seems this might be an example of one of  
those new


This is a generator expression.


That's unfortunate news for me.


It is like a list comprehension (you know about those right?)


Yes. I know and use and love them daily. Even if there were  
implemented backwards :)
[for x in range(10) x**2] would have been easier than: [x**2 for x in  
range(10)] But i am used to it now.



except it doesn't create the list it just returns each item on  
demand. You could think of a list as a list constructed using a  
generator expression.



def roundrobin(*iterables):
   "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
   # Recipe credited to George Sakkis
   pending = len(iterables)
   nexts = cycle(iter(it).next for it in iterables)


note this is storing the next methods not the results of them.


   while pending:
   try:
   for next in nexts:
   yield next()


So the yield calls the stored method and returns the result.



So... then to call (by call i mean use/execute/doit) i would do, what?  
foo.next()


I kinda understand conceptually what iterators and generators do and  
why they are "a honking good idea" (why create 100 of x when we just  
want the 100th, etc.) what i don't get is the syntax and how they are  
used in real life. How generator and iterators behave in the wild. I  
am also bummed since generators have methods, which means they are OO  
which means i am i'd be in for 16 years of computer science study and  
super arcane, obscure and opaque concepts like what to do with  
__self__ and all that junk before i can use them.


Anyway i needed a pea shooter that does a round robin. This one does  
it, but i don't know how to use it.


I read up on gennies and itties and see if i can get my head around  
it. They are really poorly addressed nearly everywhere i look. They  
are explained so that really smart folks who know a lot of CS and are  
fluent in 15 computer languages can understand them, but not us  
mortals. Even the Lutz is too terse and generally poor on these two  
complex and relatively new constructs. They are a dark and obscure  
magic.  I'll try the links Kent pointed me to first and see how that  
goes.


thanks,

-kp--




















___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is this an example of (and how can i use it?)

2009-09-22 Thread kevin parks


On Sep 21, 2009, at 9:52 AM, Kent Johnson wrote:




Calling a generator function gives you something that can be iterated.
You can create a list out of it (by passing it to the list() function)
or you can iterate the items in it directly with a for loop. Using the
example above, you could say
for item in roundrobin('abc', [], range(4),  (True,False)):
 print item

I kinda understand conceptually what iterators and generators do  
and why
they are "a honking good idea" (why create 100 of x when we just  
want the
100th, etc.) what i don't get is the syntax and how they are used  
in real

life. How generator and iterators behave in the wild.


It's really not that bad. They are just a generalization of what you
have already been doing with lists.


Even the Lutz is too
terse and generally poor on these two complex and relatively new  
constructs.

They are a dark and obscure magic.


No, really they are not difficult. Read my essay and ask questions if
you don't understand.



Thanks. I have some time today and will read up on what you sent me  
and revisit
the lutz and other docs. It appears it is not so impenetrable as i  
initially though. Well iterators
aren't maybe, but generator do look tricky. So interators iterate over  
lists, tuples, strings, dictionaries
and any data type that is iterable, and generators are ways to make  
new iterables? Anyway, i will

brew some coffee and hit those links. Thanks,

-kevin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help with conditionals

2009-09-26 Thread kevin parks


On Sep 26, 2009, at 11:42 PM, Alan Gauld wrote:



"Kent Johnson"  wrote


It appears to be
http://openbookproject.net/thinkCSpy/ch04.html


So it is, Thats a shame CSpy is one of my favourite "competitors" :-)

Pity it's apparently encouraging the use of eval like this with no  
caveat.


But to the OP, keep with it, its not a bad tutorial, shame about  
this exercise!





Perhaps worth alerting the author? He's a nice guy and i e-mailed him  
many moons ago when i was working through that book with some concerns  
i had. He was really receptive and grateful to get feed back and was  
quick to make changes if he thought they would improv the text. A  
super good dude and it seems, a top notch educator. He tends to credit  
*everyone* who sends in a suggestion, no matter how minor.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Automaton/transitional grammar query

2009-10-12 Thread kevin parks
I posted about this a couple weeks back, but then got horribly ill and  
dropped the ball so i was hoping to revisit.


I am not sure if this is and example of Finite Automaton or a Finite  
State Machine or perhaps it is related to a transition table or markov  
process. I think some one here told me it is called a transitional  
grammar? I am not sure. I am not a math person but i would love to  
know exactly what this is. I googled around and got lots of super  
complicated gobbledegoo all with knotty regex stuff, but what i want  
to do is much more simple. I am trying to use a table to define a  
bunch of moves like so:


1 --> 2 5
2 --> 1 4
3 --> 3
4 --> 1
5 --> 4 3

so that i can generate a sequence that, given an initial value, that  
will continue to grow according to these rules. Starting with 1 we  
then go to 2 & 5, 2 leads us too 1 & 4, the 5 leads us to 4 & 3, then  
we iterate over 1 4 4 and 3 to get 2 5 1 1 and 3 Like so:


1
2 5
1 4 4 3
2 5 1 1 3
1 4 4 3 2 5 2 5 3

. etc.

Essentially, iterating over the last added items to the list, applying  
the table, appending those new items to the list, applying the table
again... etc, until the sequence reaches some predetermined number of  
iterations and quits.


[ [1], [2, 5], [1, 4] , [4, 3], [2, 5], [1], [1], [3], [1, 4], [4, 3],  
[2, 5], [2, 5], [3] ]


First, as i mentioned I would like to know what, precisely, this kind  
of process is called so that i can look it up. Second, i would l like  
to add to what i have, which seems to work. But first here is the  
code, where we left off below:


#!/usr/bin/env python

rules = {1: (2, 5), 2: (1, 4), 3: (3,), 4: (1,), 5: (4, 3)}

def apply_rules(sequence):
for element in sequence:
# look it up in the global rules
values = rules[element]
# yield each of those in turn
for value in values:
yield value

def flatten(l, ltypes=(list, tuple)):
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)

def test():
data = [1]
outlist = []
for i in range(10):
outlist.append(data)
gen = apply_rules(data)
data = list(gen)
outlist.append(data)  # one more to get the final result
print '\n\n', outlist, '\n\n'
flat = flatten(outlist)
count = 0
for item in flat:
print count, ',', item, ';'
count += 1
print '\n\n'

if __name__ == "__main__":
test()


This all appears to work. I am not sure if this is the best way to do  
it, but for the size lists i have been generating it seems zippy.


So what? You are asking a question you already know the answer to?  
Well now I would like to have this set of rules contain some  
probabilistic branching. Instead of having my "go to" rules or grammar  
hard wired it would be good if some steps could also have weighted  
choices. So that maybe 1 --> 2 5 70% of the time but maybe it goes 1 -- 
> 2 4  every once in a while (as in 30%). So i am not sure how to do  
that... also, it occurs to me that i could define a grammar that got  
stuck in an infinite loop if not careful. I wonder if i should add  
some mechanism to check the dictionary defined rules before  
execution or if that is just too hard to do and i should just be  
careful.


Meanwhile I have my trusty old weighted random func all ready to go:

import random

def windex(lst):
'''an attempt to make a random.choose() function that makes  
weighted choices


accepts a list of tuples with the item and probability as a pair'''

wtotal = sum([x[1] for x in lst])
n = random.uniform(0, wtotal)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item

My question is how to apply this since i am setting up my rules in a  
dictionary, so I am confused as to how all these pieces, which work in  
isolation, would fit together. Lastly, if i add the probabilities...  
is this just a super roundabout way to make a quasi markov table? i  
dunno. But it seems like a cool way to make patterns.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Automaton/transitional grammar query

2009-10-12 Thread kevin parks



> You might be interested in Steven Wolfram's book, "A New Kind of
> Science" and the many examples on his web site:
> http://www.wolframscience.com/ See Wikipedia as well. This is a very
> rich area.


Thanks. That was just the kind of reference I was looking for.  
Fantastic.
I am sure i wont be able to grok the math bits but maybe i can begin  
to understand
thsese ideas on a conceptual level. I will look for this in the book  
store.


> When you introduce the random element you are generating Markov  
chains.


That's what i thought. I would be interested in playing with some  
simple, 1st and 2nd order markov chains too, but i just want to define  
the transition tables with the elements and their probabilities and  
generate. Most of the Markov stuff i see out there is big and knotty  
and way more than i need.




> I don't understand why you want to flatten outlist; when I run your
> program I get one number per line, not one generation per line as  
you

> show above.



That's odd. Anyway in my program I am printing the list twice. The  
first time outlist is printed it is nested one level deep. That is  
just scaffolding. Then i pick through it one item per line having  
flattened it and print it in a format that my other program can read.


I been using that flatten function since 1970. Prolly pilfered from  
Tim Peters or Effbot. Remember them guys? Awesome dudes. I wonder if  
they even use python anymore. Anyway that is from way before itertools  
was even a glimmer. Additionally, your flatten function doesn't work  
for me actually. I get:


NameError: global name 'chain' is not defined


>> enumerate() is simpler:


Thanks. enumerate is also still somewhat new to me.



> I don't think you will get an infinite loop. You may have a grammar
> that generates a stable or repeating pattern but I don't think you
> will be able to detect that without trying it.


Yeah i don't mean an infinite loop, but more like a perpetual dance  
back and forth between to items
that point to each other. I think I need to be careful when i define  
the rules that i don't get something like that... say if 1 goes to 4  
but 4's rule is go to 1, for example.



> our rules, instead of being just a list of numbers, become a list of
> probability mappings. I think you want to apply the probabilities to
> the whole sequence, so a single rule might be (using your example)
> 1: [ ((2. 5), .7), ((2. 4), .3) ]
>
> Then change the apply_rules function to choose one of the  
possibilites

> using your windex() function.



I'll roll this around in my pea sized brain and see if i can put this  
suggestion to work.


Thanks Kent!

Hopefully I will get this going and my psudo-markov thingy happening  
too. These things are fun for making patterns. I like these powerful  
little pea shooters.


-kevin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Automaton/transitional grammar query

2009-10-12 Thread kevin parks


On Oct 12, 2009, at 8:02 PM, Dave Angel wrote:

Often, when a combination of existing stdlib collection types gets  
too confusing, it's time to consider classes and objects.  Not  
necessarily to make the program "object oriented," but to make the  
program data structure understandable.



That's sage advice I just dislike and resist OO. But i do  
eventually need to make steps in that direction.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Time script help sought!

2005-01-11 Thread kevin parks
I am kind of in a bit of a jam  (okay a big jam) and i was hoping that 
someone here could give me a quick hand. I had a few pages of time 
calculations to do. So, i just started in on them typing them in my 
time calculator and writing them in by hand. Now i realize, that i 
really need a script to do this because:

1. It turns out there are hundreds of pages of this stuff.
2. I have to do something similar in again soon.
3. By doing it by hand i am introducing wonderful new errors!
4. It all has to be typed up anyway (which means weeks of work and even 
more typos!)

The input would like so:
Item_1TAPE_1100:238:23
Item_2TAPE_128:239:41
Item_3TAPE_139:4110:41
Item_3TAPE_1410:4711:19
Item_3TAPE_1511:2111:55
Item_3TAPE_1611:5812:10
Item_3TAPE_1712:1512:45Defect in analog tape sound.
Item_3TAPE_1812:5824:20Defect in analog tape sound.
Item_4TAPE_1924:33
Item_4TAPE_11025:48
Item_4TAPE_11129:48
Item_4TAPE_11231:46
Item_4TAPE_11334:17Electronic sounds.
Item_4TAPE_11435:21
Item_4TAPE_11536:06
Item_4TAPE_11637:0137:38
These are analog tapes that were digitized (on to CD or a digital tape) 
that have now been exported as individual files that are meant to be 
part of an on-line audio archive. The timings refer to the time display 
on the CD or digital tape. The now all have to adjusted so that each 
item starts at 0.00 since they have all been edited out of their 
context and are now all individual items that start at 00:00. So Item_1 
which was started at 00:23 on the tape and ended at 8:23 needs to have 
23 seconds subtracted to it so that it says:

Item_1TAPE_1100:0008:00
Item_2TAPE_1208:2309:41
would change to:
Item_2TAPE_1200:0001:18
etc.
but as always you may notice a wrinkle some items have many times 
(here 6) indicated:

Item_3TAPE_139:4110:41
Item_3TAPE_1410:4711:19
Item_3TAPE_1511:2111:55
Item_3TAPE_1611:5812:10
Item_3TAPE_1712:1512:45Defect in analog tape sound.
Item_3TAPE_1812:5824:20Defect in analog tape sound.
This is all a single sound file and these separate times mark where 
there was a break, defect, or edit in the individual item. These have 
to be adjusted as well to show where these events would appear in the 
new sound file which now starts at 00:00.

Item_3TAPE_1300:0001:00
Item_3TAPE_1401:0001:38
Item_3TAPE_1501:3802:14
Item_3TAPE_1602:1402:29
Item_3TAPE_1702:2903:04Defect in analog tape sound.
Item_3TAPE_1803:0414:39Defect in analog tape sound.
Further wrinkles: Some have start and end times indicated, some only 
start times. I suppose that the output would ideally have both some 
have comments and others don't ... and I need these comments echo-ed or
since i probably need to make a database or table eventually non 
comments just have some place holder.

I'd have a lot of similar type calculations to do... I was hoping and 
praying that some one here was feeling generous and show me the way and 
then, of course i could modify that to do other tasks... Usually i am 
happy to take the long road and all but i'll be honest, i am in a big 
jam here and this huge task was just dumped on me. I am frankly a 
little desperate for help on this and hoping someone is feeling up to 
spoon feeding me a clear modifiable example that works. Sorry.

cheers,
kevin
k p 8 'at ' m a c 'dot' c o m

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Time script help sought!

2005-01-11 Thread kevin parks
Thanks for this Everyone!
Trying to work with all the stuff folks are giving me on this i a have 
come across a problem... down
the line i notice that some of the times will also have an hour as well 
as in H:M:S (e.g. 1:22:40)

so in some cases i would need to convert H:M:S to sec and some just M:S
or should there just be a fun that passes all the times and converts 
them to H:M:S first and
just appends a 00:  ((e.g. 00:02:07) if there is no hour value?

cheers,
kevin
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Time script help sought!

2005-01-11 Thread kevin parks
I am still working on it and also fixing the input data. I think for 
simplicity and consistency's sake i will have *all* time values input 
and output as hh:mm:ss maybe that would be easier for now.. so i am 
editing the input stuff now...

so it should all look like:
Item_1, DAT_1, 1, 00:00:23, 00:08:23
Item_2, DAT_1, 2, 00:08:23, 00:09:41
Item_3, DAT_1, 3, 00:09:41, 00:10:41
Item_3, DAT_1, 4, 00:10:47, 00:11:19
Item_3, DAT_1, 5, 00:11:21, 00:11:55
Item_3, DAT_1, 6, 00:11:58, 00:12:10
Item_3, DAT_1, 7, 00:12:15, 00:12:45
Item_3, DAT_1, 8, 00:12:58, 00:24:20
Item_4, DAT_1, 9, 00:24:33
Item_4, DAT_1, 10, 00:25:48
Item_4, DAT_1, 11, 00:29:48
Item_4, DAT_1, 12, 00:31:46
Item_4, DAT_1, 13, 00:34:17
Item_4, DAT_1, 14, 00:35:21
Item_4, DAT_1, 15, 00:36:06
Item_4, DAT_1, 16, 00:37:01, 00:37:38
no comments either i can copy them later...
this is kind of hard...
-k
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Time script help sought!

2005-01-11 Thread kevin parks
I also notice that there is the is the 'datetime' module, which is new 
to version 2.3, which i now have access to. My feeling is that this 
will do much of what i want, but i can't get my head round the standard 
library reference stuff

http://www.python.org/doc/lib/module-datetime.html
I don't have any texts with me either and it probably is too new to be 
in the Python Standard Library book by Fredrik Lundh or the Python 
Essential Reference by  David Beazley

-kevin-

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Time script help sought!

2005-01-12 Thread kevin parks
thanks everyone... I will look at all the various appraoches folks came 
up with and see what i can learnn from them. I ended doing something 
lame -- a brute force method. I formmatted and reformatted my input 
data and stuffed it in a HUGE dictionary it was stupid and 
kludgy i hope to study all these approaches and learn something 
that may help me do something that is more flexible about the input 
data and is more elegant. here's what i
came up with ... with my pea sized brain...


#!/usr/bin/env python
# New in version 2.3 is the 'datetime' module (see standard library 
reference)
# http://www.python.org/doc/lib/module-datetime.html

import datetime
inseqs = { (1) : ['DAT_1', '01', '00:00:23', '00:08:23'],
(2) : ['DAT_1', '02', '00:08:23', '00:09:41'],
(513) : ['DAT_75', '10', '00:59:55', '01:11:05'],
(514) : ['DAT_75', '11', '01:11:05', '01:16:15'],
(515) : ['DAT_75', '12', '01:16:15', '01:34:15'],
(516) : ['DAT_75', '13', '01:34:15', '01:45:15'],
(517) : ['DAT_75', '14', '01:45:15', '01:48:00'] }
mykeys = inseqs.keys()  # first make a copy of the keys
mykeys.sort()   # now sort that copy in place
for key in mykeys:
event = inseqs[key]
print '\n','Item #', key, event
TD = datetime.timedelta
h, m, s = event[2].split(':')
zero_adjust = TD(hours=int(h), minutes=int(m),seconds=int(s))
#
print ' Q___ ', key, event[:2], ': ',
for item in event[2:]:
hrs, mins, secs, = item.split(':')
time1 = TD(hours=int(hrs), minutes=int(mins),seconds=int(secs))
print time1 - zero_adjust,
print
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] spaces in print

2005-01-12 Thread kevin parks

when i print:
print '\n','siday_q', key, '.wav'# event
i get:
siday_q 515 .wav
how can you eliminate the spaces to get:
siday_q515.wav
?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] permutations, patterns, and probability

2005-02-01 Thread kevin parks
Greetings,
I am working on a program to produce patterns. What would like is for 
it to  exhaustively produce all possible permutations of a sequence of 
items but for each permutation produce variations, and also a sort of 
stutter based on probability / weighted randomess.

Let us say we have tiles of four primary colors: ['Red', 'Blue', 
'Green', 'Yellow']. Now we also have 4 alternatives or substitutes for 
each color ['Maroon', 'Navy_Blue', 'Forest_Green', 'Dark_Brown']

We pick a unique permutation, say: ['Red', 'Blue', 'Yellow', 'Green']
Now I would like to pick the primary colors substitute (say 30% chance 
for each element) so instead of our plain

['Red', 'Blue', 'Yellow', 'Green']
we might end up with:
['Red', 'Navy_Blue', 'Yellow', 'Forest_Green']
or
['Maroon', 'Navy_Blue', 'Yellow', 'Green']
Whatever... The main point is that sometimes the original color is 
retained and sometimes the dark color is substituted.

Now I want to take this list and sometimes stutter an element so that 
there is, let us say a 50% chance for each element, that it is 
stuttered, and it may be repeated 1 (34%), 2(66%), or 3(33%) times. So 
that we could get:

['Maroon','Maroon','Navy_Blue', 'Yellow','Yellow','Yellow','Yellow', 
'Green']

The program would quit when all 24 (in the case of 4 elements) was 
exhausted.

I have code that makes weighted randomness. I have code that makes 
permutations, but I am having trouble putting this all together... 
While i work on it though that i might ask for help... I'd like for the 
code to be reusable and am building a library of functions for 
patterns.

cheers,
kevin
### This is not mine, it is from a python book... I believe the Lutz 
book

def permute(list):
if not list:# shuffle any 
sequence
return [list]   # empty sequence
else:
res = []
for i in range(len(list)):
rest = list[:i] + list[i+1:]# delete 
current node
for x in permute(rest): # permute the 
others
res.append(list[i:i+1] + x) # add node at 
front
return res

mport random
### This this is mine, but seems to work anyway hee hee
def windex(lst):
'''an attempt to make a random.choose() function that makes 
weighted choices

accepts a list of tuples with the item and probability as a pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x)'''
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] permutations, patterns, and probability

2005-02-01 Thread kevin parks
Tremendously helpful One question though. How can i pluck a unique 
item from my exhaustive list of permutations without repeats making 
sure that each one is used once? Like filling a bag, shaking it, and 
then picking from the bag and removing that item from the bag so it 
isn't used again

-k
On Feb 1, 2005, at 8:58 PM, [EMAIL PROTECTED] wrote:
f you had a randomizeList function and a stutterList function then 
your top-level function would
look like this:

permutations = permute(['Red', 'Blue', 'Yellow', 'Green'])
permutations = [ randomizeList(list) for list in permutations ]
permutations = [ stutterList(list) for list in permutations ]
In other words you start with the basic permutations, then apply the 
randomize function to each
permutation, then apply the stutter function.

The randomizeList function should walk through the list, find the 
right randomize list for that list
element (a dict could help with that - look up the list element and 
get the randomize list), and
build a new list with the randomized values.

The stutterList function walks through the list building a new list 
with possibly repeated elements.

HTH,
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cyclically rotate a seq

2005-03-10 Thread kevin parks
Hi folks,
I am trying to cyclically rotate a seq until it reached the beginning 
stage again.
I would like to be able to rotate in both directions and using any 
arbitrary interval.
I think that I have this correct, but would be happy for someone to 
check it and also
i am interested in any improvements or enhancements. It is important 
that this
work correctly or the whole rest of my code will be in barf *^-^* hee 
hee. So
any help would be appreciated.

#!/usr/bin/env python
import sys
import random
# cyclically rotate a sequence
# -- 
# should work on any sequence type
# should work with any hop(n) interval
# should also work in both directions (left or right)
# -- 
def rotate(seq, n=1):
if len(seq) == 0:
return seq
# Normalize n, using modulo - even works for negative n
n = n % len(seq)
return seq[n:] + seq[:n]

def test():
start = 1
x = [7, 2, 1, 0, 11, 6, 5, 4]
print; print x; print '--' * 8
for i in range(len(x)):
out = rotate(x, start)
print out
start = start + 1
if __name__ == "__main__":
test()
#  EOF 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Module imports

2007-07-11 Thread kevin parks
With sincere apologies for such a basic question, and one i will  
admit that i asked once before, moons ago. But even after googling  
around a bit i don't understand what the right answer is, or if i  
once did, can't recall it now..

I have a script, let's call it foo.py

This script loads several modules from the standard library and a  
home brewed module that has a bunch code that i often reuse in it.

For example it might have:

# -- foo.py --

# -- batteries included
import random
import sys
import time

# -- homebrewed
import kp

[... code here ..]

That is fine and in this script i will call and use various things
from the random, sys, and time modules.

but my kp module also uses happens to call on certain things from the
random, time, and sys modules and so kp.py also has

import random
import sys
import time

Now so far this seems to be working fine and without error (as far as  
i can tell). However, shouldn't i only be importing random, sys and  
time once? and if so, where? in foo.py or kp.py?

It was explained to me that it is fine to import random, sys and time  
in both, and that only the first import uses up memory, and  
subsequent attempts to import the same module don't really cost  
anything and just add a reference in the namespace. but isn't loading  
it in both modules confusing and bad  additionally in this case  
which import is actually being used (or does this not even matter?)  
the one in kp.py or in foo.py?

For some reason i feel like i should understand how and why this  
works a little better in order to avoid overlap and conflict in what  
is becoming a bit more involved intermingling of modules and scripts.

or alternately you all can chime in and say "dude, get over it,  
multiple and overlapping imports are not a big deal in python, you  
are worrying about nothing,  and making a problem where there is  
none! Get on with your life." haha

best,

kevin




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] printing the random seed?

2006-02-01 Thread kevin parks
hi.

I am having some fun with python and making multiple runs on an 
algorhythm and sometimes getting some fun stuff that i would like to be 
able to reproduce, but there are some random elements in it. I wonder 
is there a way to see the random seed, and make note of it so that you 
could then set the seed for a subsequent run to get the same 
(initially) random results?

cheers,

kevin

  (Hi Danny, if you are still here!)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] printing the random seed?

2006-02-01 Thread kevin parks
Danny (hope you are good!) & co,

I see that biz about random.seed()... but in the absence of setting 
that ... does it
just grab a value from the system clock?

Is there a way to just let it generate it's usual, known seed... but 
then observe
what that is in case you get an especially good run of data?

like i clearly can't just go:

zeed = random.seed()
print "zeed = ", zeed

hmm...

cheers,

-[kp]--

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Gaussian integer values

2006-02-08 Thread kevin parks
hi all.

I am working with a list and would like to choose from the list  
randomly but not uniformly. I am interested in several distributions  
but the first one i looked at is Gaussian.

which you call like so:

random.gauss(mean, dev)

You can try this code courtesy of effbot (http://effbot.org/ 
librarybook/random.htm)


# File: random-example-3.py

import random

histogram = [0] * 20

# calculate histogram for gaussian
# noise, using average=5, stddev=1
for i in range(1000):
 i = int(random.gauss(5, 1) * 2)
 histogram[i] = histogram[i] + 1

# print the histogram
m = max(histogram)
for v in histogram:
 print "*" * (v * 50 / m)

fine this works and works well...


one problem with this... if you are using this to create an index to  
a list you might be in trouble as each time you run it you get a  
slightly different min and max value.


so... how does one safely pick from a list of say 15 elements with a  
large bias towards the center values without over or under-running  
the bounds of your list?

cheers,

kevin

ps. what would be the opposite of the Gaussian distro the scoop like  
one, the one where the outer elements are favored. Is that in the  
python standard lib? (or numpy?)







___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] soundfile picker & rating system

2006-02-08 Thread kevin parks

I am a little bit stuck 

I want to play a bunch of soundfiles randomly, but i want to give each 
soundfile a rating (say 0-100) and have the likelihood that the file be 
chosen be tied to its rating so that the higher the rating the more 
likely a file is to be chosen.  Then i need some additional flags for 
repetition, and some other business. I am guessing a dictionary would 
be a great way to do this, with the key being the soundfile name and 
the values being my ratings and other flags & associated data.

#soundfile name : [rating, %chance it will repeat/loop]
sfiles = { ("sf001") : [85, 15],
  ("sf002") : [25, 75],
  ("sf003") : [95, 45],
  ("sf004") : [35, 95] }


But i am stuck on how to do a random chooser that works according to my 
idea of choosing according to rating system. It seems to me to be a bit 
different that just choosing a weighted choice like so:

def windex(lst):
'''an attempt to make a random.choose() function that makes weighted 
choices

accepts a list of tuples with the item and probability as a pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x)'''
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item


And i am not sure i want to have to go through what will be hundreds of 
sound files and scale their ratings by hand so that they all add up to 
100%. I just want to have a long list that i can add too whenever i 
want, and assign it a grade/rating according to my whims!

cheers,

kevin




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] list packing

2006-02-26 Thread kevin parks
howdy,

I am using the os module to do some of my heavy lifting for me. I am 
tried of building lists
by hand so i decided that i would get python to look in a bunch of 
directories and stuff all the things it
find there into a list depending on it's extension.

Works great ... one problem sometimes i need just the filenames and 
that is fine, but more often i need
to feed the full path to my other functions yet i don't see *any* 
documentation on os.listdir() at all. I don't
know how to give me the full path ...

snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')]


cheers,

kevin

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] list packing

2006-02-27 Thread kevin parks
John,

Thanks... i am liking this variation a tad more since it means i only
have to type the path in one place  but it is akin to your second
one... i was (still am really) having a hard time understanding
how to apply path.join _and_ listdir  sometimes list comprehensions
twist my brain but this one is worth studying ...

nice! two little lines that do a boatload of work! hee hee


pth = '/Users/kpp9c/snd/01'
samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith 
('.aif')]


i could even add:
samples = [os.path.join(pth, f) for f in os.listdir(pth) if f.endswith 
('.aif' & f.startswith('t')]

to make sublists in guess

What i like about this is that i can chance my directory structure  
(add/delete/rename/etc)
and shape it any way that i want and then Python reflects all my  
restructuring... lovely...
Exactly the type of tedious thing i want Python to do for me *^-^*

cheers,

kevin



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] module imports

2006-03-08 Thread kevin parks
i have a module called foo.py

foo.py imports random

kp.py imports foo.py ... but in kp.py i also need to use stuff from 
random...

so kp.py also imports random

but i prolly shouldn't do that right?

Cause now, haven't i needlessly copied the same 
names/attributes/methods/functions to 2 namespaces...

I get very confused about imports... and accessing names from other 
spaces and such...
and efficiency.

i have 2 or more modules all of which need to access a given function 
or method, what is the best way to do that.

i am so cornfused

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module imports

2006-03-08 Thread kevin parks

On Mar 8, 2006, at 7:09 PM, Bob Gailer wrote:

> kevin parks wrote:
>> i have a module called foo.py
>>
>> foo.py imports random
>>
>> kp.py imports foo.py ... but in kp.py i also need to use stuff from 
>> random...
>>
>> so kp.py also imports random
>>
>> but i prolly shouldn't do that right?
>>
> Wrong.

so it is okay to do that?

>> Cause now, haven't i needlessly copied the same 
>> names/attributes/methods/functions to 2 namespaces...
>>
> The first import of a module runs its top level code, and creates a 
> module object. Subsequent imports simply place a reference to that 
> object in the current namespace.
>> I get very confused about imports... and accessing names from other 
>> spaces and such...
>> and efficiency.
>>
>> i have 2 or more modules all of which need to access a given function 
>> or method, what is the best way to do that.
>>
> Put the function in a module and import it as needed.

but i mean a function from a standard module.

>> i am so cornfused
>>
> Apparently. Even your spell checker is addled.
[that was a joke]


so let's say i have a script called foo.py.
foo.py uses some things from the random module and therefore has

import random
import kp
import sys
import time

etc. & co.  in it

but foo.py also imports a module kp which also
happens to import random.

Should i only be importing random once or are you saying it
is just fine to import a module that imports another module that
you actually have already imported.


-kp--





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] weighted choices from among many lists

2006-03-10 Thread kevin parks
I have several lists... and i would like to some times chose from one 
list and for a while choose from a different list, etc... so i cooked 
this up and it almost works so that i can get colors 50% of the time, 
doggies 25%, beer 10%, and guitars 100% (if i was real smart i would 
make my index thingy check to make sure my wieghets added up to 100% or 
scaled them to be. ) ... meanwhile, as you can see i am 90% of the 
way there, can anyone figure out what i got wrong or suggest 
improvements to the code...

best,

-kevin--

#!/usr/bin/env python

import random

def windex(lst):
'''a random.choose() function that makes weighted choices

accepts a list of tuples with the item and probability as a pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x)'''
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item


def test():

lst_a = [ 'red', 'green', 'blue', 'orange', 'violet', 'yellow', 
'black', 'white' ]
lst_b = ['Rottweiler', 'Beagle', 'Sheepdog', 'Collie', 'Boxer', 
'Terrier', 'Bulldog', 'Chihuahua', 'Retriever', 'Collie', 'Dachshund', 
'Doberman', 'Greyhound', 'Pug', 'Spaniel']
lst_c = ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
lst_d = ['fender', 'gibson', 'rickenbacker', 'guild', 'danelectro', 
'gretsch', 'martin', 'ibanez']
x = [('lst_a', .50), ('lst_b', .25), ('lst_c', .10),('lst_d', .15)]
i = 1
while i < 100:
lst = windex(x)
print i, lst,
pick = random.choice(lst)
print pick
i = i + 1

if __name__ == "__main__":
test()

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] activestate

2006-03-10 Thread kevin parks
I noticed a couple days ago that the active state archive seems to have 
ceased.
Is it going away?

cheers,

kevin

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread kevin parks

On Mar 11, 2006, at 3:24 PM, [EMAIL PROTECTED] wrote:

>
> Message: 1
> Date: Sat, 11 Mar 2006 08:34:49 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] weighted choices from among many lists
> Cc: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> kevin parks wrote:
>> I have several lists... and i would like to some times chose from one
>> list and for a while choose from a different list, etc.
>
> You don't say what isn't working but I have a guess. The actual 
> windex()
> function looks OK to me.

yes, that part always worked fine... not the most robust thing, but it 
works.


>
>> def test():
>>
>>  lst_a = [ 'red', 'green', 'blue', 'orange', 'violet', 'yellow',
>> 'black', 'white' ]
>>  lst_b = ['Rottweiler', 'Beagle', 'Sheepdog', 'Collie', 'Boxer',
>> 'Terrier', 'Bulldog', 'Chihuahua', 'Retriever', 'Collie', 'Dachshund',
>> 'Doberman', 'Greyhound', 'Pug', 'Spaniel']
>>  lst_c = ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
>>  lst_d = ['fender', 'gibson', 'rickenbacker', 'guild', 'danelectro',
>> 'gretsch', 'martin', 'ibanez']
>>  x = [('lst_a', .50), ('lst_b', .25), ('lst_c', .10),('lst_d', .15)]
>
> x is list containing the *names* of the other lists. You need to keep
> references to the lists so you can pick from them.
>   x = [(lst_a, .50), (lst_b, .25), (lst_c, .10),(lst_d, .15)]

i am an idiot... someone shoot me.. i guess i got so carried away with 
typing
the single quotes for the above lists that when i went to pack those up 
for windex
i didn't realize that i had needlessly typed 'lst_a', etc. Sometimes 
you just need a
fresh pair of eyes to pick up on that type of thing


>>  i = 1
>>  while i < 100:
>>  lst = windex(x)
>>  print i, lst,
>
> with the change above this will print the list, not its name
>>  pick = random.choice(lst)
> but this will work.
>
> If you want to be able to print the name of the list then you could
> include both the name and the actual list in x:
>
>   x = [(('lst_a', lst_a), .50), etc...]


That produces:
('lst_c', ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout'])
['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']

i am actually trying to get name of list, the choice like so:

lst_c
Bock

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] weighted choices from among many lists

2006-03-11 Thread kevin parks
> Yes, you need to unpack the result of calling windex(). You so ably
> demonstrated unpacking the list in your for loop I thought you would
> figure it out :-)
>
> Try
>lst_name, lst = windex(x)
>
> then random.choice() and print.
>

Thanks Kent i got it, just another brain-fart on my side...

I was already doing something like:

lst_name, lst = windex(x)
pick = random.choice(lst)

But instead of:
print i, lst_name, pick

I was idiotically still printing:
print i, lst, pick

now matter how you unpack it all... you are still gonna get lst if that 
is what
is in your print statement... grrr

of course the another way i would be a use a dictionary and us the list 
name as
a key... the list as a value...

Thanks,

kevin

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] scaling values

2006-03-14 Thread kevin parks
i have various functions (that i didn't write) that put out data in 
lists of various types. But some functions (which i didn't write) that 
expect the data to be scaled, sometimes 0-1 sometimes 1-2, sometimes 
0-127..., sometimes 0 - 32768... gosh you name it. In other words i 
have a bunch of black boxes that don't speak the same language  is 
there a scaling function in python (or numeric or scipy) that can scale 
a list of values to a high precision?

x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26]

foo = scale(x, 0, 1.0)

and get that list scaled 0 to 1, or if i had:

x = [.12789, .982779, .19798198, .266796, .656527, .257877091]

foo = scale(x, 0, 127)

cheers,

-kp--

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scaling values

2006-03-14 Thread kevin parks
hi,

Seems my post added much confusion. Sorry... I was hoping not to have 
to post my code since it is really wrong and slightly embarrassing.. 
what i am trying to do is map an input range of values to output range. 
I was hoping to make it a bit of an all purpose utility that would map 
pretty much any input range to an output range, also do inverted 
mapping... and also handle negative numbers and perhaps even a flag for 
exponential mapping.

import random

def scaleX(in_seq, low, hi):
range1 = max(in_seq) - min(in_seq)
#range2 = max(out_seq) - min(outseq)
range2 = hi - low
ratio = range1/range2
return [(x * ratio) for x in in_seq]

def test():
# Create a list of 15 random integers in the range 0 to 127
# see if we can map it to 0 -> 1
inseq = random.sample(xrange(128), 25)
print
print scaleX(inseq, 0.0, 1.0)
print

if __name__ == "__main__":
test()




  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scaling values

2006-03-14 Thread kevin parks
Thanks to Kent Johnson, & David Heiser and everyone else. Looks like i 
was most of the way there...hehe... David Heiser gets special bonus 
points for actually understanding my initial mysterious query.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Alternating patterns

2006-03-28 Thread kevin parks
I have a set that i iterate over... but each time through it i would 
like to alternate between the original set and a variation of the set 
that has one of the members of the set altered (by + or - 1)

So if my original set is:

[0, 2, 4, 5, 7, 9, 11]

I would use that the first pass but on the second pass i might like  
the third member (4,) to become 3, (-1) resulting in : [0, 2, 3, 5, 7, 
9, 11]

But then back again to the original  on the next pass (+1 back to 4,): 
[0, 2, 4, 5, 7, 9, 11]

and then back: [0, 2, 3, 5, 7, 9, 11] again, etc.

in other words i would like to alternate members of the set back and 
forth. Usually only 1 (or sometimes 2,) member at time. i could also 
imagine a needing(alter one, alter another, undo that, undo the first 
back to the original set):

[0, 2, 4, 5, 7, 9, 11] --> [0, 2, 3, 5, 7, 9, 11] --> [0, 2, 3, 5, 7, 
8, 10] --> [0, 2, 3, 5, 7, 9, 11] --> [0, 2, 4, 5, 7, 9, 11]

or:

original --> [0, 2, 4, 5, 7, 9, 11]
altered --> [0, 2, 3, 5, 7, 9, 11]
now back to 4, but change something else (like 11, is now 10):
[0, 2, 4, 5, 7, 9, 10]

etc...

How can one make such alternating patterns?

-kp--

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternating patterns

2006-03-28 Thread kevin parks
>
>
>
> --
>
> Message: 10
> Date: Tue, 28 Mar 2006 22:43:38 -0500
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Alternating patterns
> Cc: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> kevin parks wrote:
>> I have a set that i iterate over... but each time through it i would
>> like to alternate between the original set and a variation of the set
>> that has one of the members of the set altered (by + or - 1)
>>
>> So if my original set is:
>>
>> [0, 2, 4, 5, 7, 9, 11]
>>
>> I would use that the first pass but on the second pass i might like
>> the third member (4,) to become 3, (-1) resulting in : [0, 2, 3, 5, 7,
>> 9, 11]
>>
>> But then back again to the original  on the next pass (+1 back to 4,):
>> [0, 2, 4, 5, 7, 9, 11]
>>
>> and then back: [0, 2, 3, 5, 7, 9, 11] again, etc.
>
>> How can one make such alternating patterns?
>
> itertools.cycle() will repeat a sequence indefinitely:
> In [2]: from itertools import cycle
>
> In [3]: i=cycle([1,2])
>
> In [5]: for j in range(6):
> ...: print i.next()
> ...:
> ...:
> 1
> 2
> 1
> 2
> 1
> 2
>
> For non-repeating sequences I would look at writing a generator 
> function
> for the sequences.
>
> Kent



okay.. i am painfully unaware of generators, iterators, sets, genexes 
and a lot of the new stuff post 2.3 & 2.4 stuffs... my problem is that 
i find the online docs kind of terse and few of the Python books yet 
cover these newer constructs in detail

itertools looks very cool are there any toots on the above and on 
Sets & itertools? It really seems like it would help to know these for 
my work... That liddo bit right up there with itertools.cycle already 
has me a drooling... (so helpful that would be!)

-kp--





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] seq looping

2006-04-25 Thread kevin parks
I have a loop that process each item in a sequence and after each item 
some updating is done to some variables. However i don't what these 
variable updated if we are processing the last item in the list. i 
could put in a conditional and test if the list is now empty after 
popping items from the list... but testing for an empty list and the 
bookkeeping of maintaining the popped list seems horribly inefficient 
and this is for a real time multimedia playback type situation so 
learning a more efficient idiom for this seems worth while:

You won't have all the dependancies... including the player (STEREO) 
and some tables of durations.. but you get the gist:

def playall(startime, amp, wet_percent, rest_percent, duty_factor, 
smpl_lst):
''' a play-loop that plays all samples in a directory, just once with 
some
temporal padding and also returns the end of the last duration so
that the begining of the next section can be determined'''
event = 1; inskip = 0; inchan = 0; incr = 0
for sample in smpl_lst:
splt = os.path.split(sample)
rtinput(sample)
loc = random.random()
dur = DUR()
STEREO(startime, inskip, dur, amp, loc)
print "event no. %d @ %.2f (dur: %.2f, end: %.2f) --> sf: %s : 
[flag: 
%.2f]" % (event, startime, dur, startime+dur, splt[1], dry)
incr = (dur * duty_factor) + kptools.windex(kptools.durations)
startime = startime + incr
restflag = random.random()
if (restflag < rest_percent):
rest = kptools.windex(kptools.rest)
print "\n", "<>-" * 8, "[ rest : ", rest, "]", "-<>" * 
8, "\n"
startime = startime + rest
event = event + 1
print '\n', 'Next start = ', startime, '\n\n'

so what i am trying to do its skip that 

if (restflag < rest_percent):

biz on the last item if we are on our last sequence item. The rests 
(which are random) is for padding between events
and since we have just played our last event, we don't want any extra 
padding so that our next call of the loop
starts at the proper time (just at the last event of this loop is 
over). The loop function will calculate the
next start time, and return it so that we can use it as the start time 
argument for our next call of the loop.

so if i want to play 7 items i might get something like:

loop_call_01:
item_1
item_2
rest
item_3
rest
item_4
item_5
item_6
rest
item_7 (but we don't want any rest here ever! cause we might want our 
next loop to start w/o any pause)


gosh.. i hope this is clear

anyway that's my query .. hehe  ...

cheers,

kevin


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] seq looping

2006-04-26 Thread kevin parks
Bob ... i used my kludge which defines some bounds and tests for  
it... but your
idea of stuffing that into a seperate black box as golden in that it  
made me think
of the problem as input --> doit --> result.
and made my loops simpler and easier to read... and i use that same  
gap making
typing thing in several loops, so encapsulating that meant that i  
could make
it general purpose, and re-usable and i can add to it later more easily.

Thanks for that suggestion. I ended up with this:


# if the flag (random float) is lower than our percentage of rest, we  
pick
# a rest duration and add that to our start time creating a gap  
between events.
def make_rest(event, lowerbound, upperbound, start, rest_percent):
restflag = random.random()
if (restflag < rest_percent):
# This kludge makes sure our last event does not get a rest, 
and we  
can
# prevent the first few events from getting rests as well to 
insure
# our transitions between sections are seamless & sufficiently 
busy
# NOTE LOWER bound is included
if event >= lowerbound and event < upperbound:
rest_dur = windex(rest)
print "\n", "<>-" * 8, "[ rest : ", rest_dur, "]", 
"-<>" * 8, "\n"
start = start + rest_dur
else:
print "[bang]"  # debug
return start


then in the master bedroom i just call it:

startime = foo.make_rest(event, 1, upperbound, startime, 
rest_percent)

cheers,

kevin



On Apr 25, 2006, at 11:27 PM, Bob Gailer wrote:

> How about separating the body into 2 functions, calling both for  
> all but the last list element, then calling just the first for the  
> last element:
> :
> def step1(sample):
>  global incr
>  splt = os.path.split(sample)
>  rtinput(sample)
>  loc = random.random()
>  dur = DUR()
>  STEREO(startime, inskip, dur, amp, loc)
>  print "event no. %d @ %.2f (dur: %.2f, end: %.2f) --> sf: %s :  
> [flag: %.2f]" % (event, startime, dur, startime+dur, splt[1], dry)
>  incr = (dur * duty_factor) + kptools.windex(kptools.durations)
>  startime = startime + incr
>
> def rest():
>  global event
>  restflag = random.random()
>  if (restflag < rest_percent):
>rest = kptools.windex(kptools.rest)
>print "\n", "<>-" * 8, "[ rest : ", rest, "]", "-<>" * 8, "\n"
>startime = startime + rest
>event = event + 1
>
> for sample in smpl_lst[:-1]:
>  step1(sample)
>  rest()
> step1(smpl_lst[-1])
>
>
> so what i am trying to do its skip that
>> if (restflag < rest_percent):
>>
>> biz on the last item if we are on our last sequence item. The  
>> rests (which are random) is for padding between events
>> and since we have just played our last event, we don't want any  
>> extra padding so that our next call of the loop
>> starts at the proper time (just at the last event of this loop is  
>> over). The loop function will calculate the
>> next start time, and return it so that we can use it as the start  
>> time argument for our next call of the loop.
>>
>> so if i want to play 7 items i might get something like:
>>
>> loop_call_01:
>> item_1
>> item_2
>> rest
>> item_3
>> rest
>> item_4
>> item_5
>> item_6
>> rest
>> item_7 (but we don't want any rest here ever! cause we might want  
>> our next loop to start w/o any pause)
>>
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cycle w/ shuffle

2006-04-26 Thread kevin parks
I am trying to write a loop that iterates over a sequence and do 
something x number of times. But sometimes i will need more events 
(larger number x) than i have have items in the sequence, so if i need 
more events that i have stuff in my sequence i would like to have the 
loop reload and shuffle the deck and start all over again, reloading as 
many times as necessary to get the number of events needed. I suppose i 
could go around cyclically modulo the list size but i would want to 
shuffle the deck before doing that again...

it seems to me that i need something like itertools cycle, except that 
i need to keep track of when i have exhausted my list and then call 
random.shuffle() on my sequence.



def cycle(iterable):
saved = []
for element in iterable:
yield element
saved.append(element)
while saved:
for element in saved:
yield element

def test():
seq = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 
'eight', 'nine', 'ten')
loop = cycle(seq)
count = 1
for item in range(25):
print count, loop.next()
count = count + 1

if __name__ == '__main__':
test()


here i have gone through my list 2.5 times .. and somewhere after the 
second and third(tho incomplete) rounds i need to shuffle... but i am 
not sure how to fix that.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cycle w/ shuffle

2006-04-26 Thread kevin parks
I am trying to write a loop that iterates over a sequence and do 
something x number of times. But sometimes i will need more events 
(larger number x) than i have have items in the sequence, so if i need 
more events that i have stuff in my sequence i would like to have the 
loop reload and shuffle the deck and start all over again, reloading as 
many times as necessary to get the number of events needed. I suppose i 
could go around cyclically modulo the list size but i would want to 
shuffle the deck before doing that again...

it seems to me that i need something like itertools cycle, except that 
i need to keep track of when i have exhausted my list and then call 
random.shuffle() on my sequence.

def cycle(iterable):
saved = []
for element in iterable:
yield element
saved.append(element)
while saved:
for element in saved:
yield element

def test():
seq = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 
'eight', 'nine', 'ten')
loop = cycle(seq)
count = 1
for item in range(25):
print count, loop.next()
count = count + 1

if __name__ == '__main__':
test()


here i have gone through my list 2.5 times .. and somewhere after the 
second and third(tho incomplete) rounds i need to shuffle... but i am 
not sure how to fix that.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to *really* copy a list

2006-04-27 Thread kevin parks




I know there is an answer to this somewhere. it is prolly the biggest 
stumbling
block to all python n00bs, but it hasn't been an issue for me in a 
while.
Suddenly i am getting bit by it and can't for the life of me keep 
straight the
two way of opperating on lists.

In most case you are fine operating on the list in place and altering 
the
existing list. In some cases you want your code to stop molesting your 
poor
mutables and really honestly sincerly copy the dang thing. In this case 
i am
making a function that does odd smmetry mirroring. But i want my 
orginal list
to remain intact

 >>> a = [1, 2, 3, 4]
 >>> mirror(a)
[1, 2, 3, 4, 3, 2, 1]
 >>> a
[1, 2, 3, 4, 3, 2, 1]

clearly this is not happening. believe it or not i googled around 
figuring the
answer would be posted someplace... but if it is i haven't found it. 
Every thing
i land on says copy a list by [:] slicing like i have below...

how to i really (not kidding) copy a list? I swear i used to know this, 
but i haven't had
to do it in a long long long time.


# ===

dumb code:

def mirror(seq):
"""odd symmetry mirroring  [1, 2, 3, 4] --> [1, 2, 3, 4, 3, 2, 1]"""
foo=seq[:-1]# copy list, excluding last 
element for odd symetry
foo.reverse()   # flip it
seq.extend(foo)
return seq


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cycle w/ shuffle

2006-04-29 Thread kevin parks
Thanks Kent. That is what i did the first time around but it didn't 
work ...
but that was do to something else that was not working in the script
hehe of course, this just shuffles the saved copy of the list which 
is
egg-zactly what i needed "duh" ... sorry...  gosh... 

-kevin--

On Apr 27, 2006, at 6:00 AM, [EMAIL PROTECTED] wrote:

> kevin parks wrote:
>> it seems to me that i need something like itertools cycle, except that
>> i need to keep track of when i have exhausted my list and then call
>> random.shuffle() on my sequence.
>>
>> def cycle(iterable):
>>  saved = []
>>  for element in iterable:
>>  yield element
>>  saved.append(element)
>>  while saved:
>  random.shuffle(saved) ###
>>  for element in saved:
>>  yield element
>
> Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to *really* copy a list

2006-04-29 Thread kevin parks
John,

Thanks. Your message was very helpful. I will tattoo it to my forehead.
hehe... i notice that the "learning python" book also explains so of 
this
and i shall study that as well

cheers,

kevin

On Apr 27, 2006, at 10:14 PM, [EMAIL PROTECTED] wrote:
>
> On 28/04/06, kevin parks <[EMAIL PROTECTED]> wrote:
>> In most case you are fine operating on the list in place and altering 
>> the
>> existing list. In some cases you want your code to stop molesting 
>> your poor
>> mutables and really honestly sincerly copy the dang thing. In this 
>> case i am
>> making a function that does odd smmetry mirroring. But i want my 
>> orginal list
>> to remain intact
>>
>> def mirror(seq):
>> """odd symmetry mirroring  [1, 2, 3, 4] --> [1, 2, 3, 4, 3, 
>> 2, 1]"""
>> foo=seq[:-1]# copy list, 
>> excluding last element for odd symetry
>> foo.reverse()   # flip it
>> seq.extend(foo)
>> return seq
>
> Hi Kevin,
>
> Your problem is this line:
> seq.extend(foo)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to *really* copy a list

2006-04-29 Thread kevin parks
Ed,

I should have realized that the nesting would create the problem, but i 
didn't have
that in mind... i always thought that the difference between extend and 
append
was that extend did not yield a nested list.

I really need to revisit this issue and get it right in my mind. It is 
a 'gotcha'
that i remember reading about often but, now that it has bit me a few 
times
hehe  so much to know...

-kevin--



On Apr 29, 2006, at 6:00 AM, [EMAIL PROTECTED] wrote:

>>
>> Hi Kevin,
>>
>> Your problem is this line:
>> seq.extend(foo)
>>
>> This is the line that mutates your original list.
>>
>> There are a few ways you could procede here.  One way is to make a
>> copy of the argument, like this:
>>
>> def mirror(seq):
>> start = list(seq)
>> end = seq[:-1]
>> end.reverse()
>> start.extend(end)
>> return start
>>
>> Notice that we've not calling any methods on seq, so seq won't be
>> changed.  The first line, "start = list(seq)", instructs python to
>> build a new list out of the elements of seq.  You could also write
>> "start = seq[:]" here --- I'm not sure which is the preferred way.
>
> A little 'gotcha' with this is that if you have nested lists, these
> methods don't copy the nested lists, only the outer list (which makes
> sense, but can be surprising the first time you encounter it).  If for
> some reason you want to copy nested lists, look into deepcopy(),
> otherwise you'll be fine.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] time(duration) formats & basic time math query

2006-07-05 Thread kevin parks
I have been handed a huge number of documents which have hundreds of  
pages of times and durations, all calculated and notated by several  
different people over the course of many years. Sadly, no made any  
guidelines at all about how this work would proceed and all the  
documenters had their own ideas about how times/durations would be  
specified so the doc are a mess. Furthermore the person i work for  
changes her mind every 15 minutes so i have no idea what she will  
want at any given moment. This sounds to me like a job for Python  


Essentially, I am trying to do 2 things:

move fluidly between different duration formats (take any, specify  
and display any). Such as:

pure miliseconds
seconds. msec
mm:ss.msec
hh:mm:ss.msec

So the input doc would be grepped for times and i could just  
uncomment the line i need and get he format my boss wants at this  
particular moment.
So a recording that is 71 minutes and 33 seconds could be printed as:
4293 seconds
71:33.
01:11.33.  or whatever

also i need to be able to adjust start times, durations, and end  
times which means doing a little time math.
Like if a recording is 71 minute and 33 seconds.. but there are  
several sonic events in that recording and i
need to specify the duration, or start time etc of those individual  
events... then i need to subtract... Additionally i
know that i will need to subtract pure minutes from an hh:mm format

I having a real hard time getting my head round the labyrinthian  
datetime module in the docs (i am guessing
that this is what i should use). I wonder if anyone could give me a  
hand and point me in the right direction.

cheers,

kevin


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor