[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 Danny Yoo


On Tue, 14 Mar 2006, kevin parks wrote:

 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)

Hi Kevin,

I'm still confused by the problem.  Let's try small examples.


Let's say we had this:

##
x = [13]
scaledX = scale(x, 0, 1.0)
##

What would 'scaledX' have?  I'm asking because I have no clue from the
problem description!  *grin*

(There's some hidden knowledge that you have about the problem, so I'm
trying to make sure it's out in the open.)

We can ask the same question with a slightly larger (but still small)
example:

x = [13, 71]

What should we expect from things like:

 scale(x, 0.0, 1.0)
 scale(x, 0.0, 2.0)
 scale(x, 1.0, 2.0)

The results of small examples will help clarify what we'd need to do to
make scale() work.


Good luck to you!


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


Re: [Tutor] Dynamically naming functions

2006-03-14 Thread Ed Singleton
On 13/03/06, Alan Gauld [EMAIL PROTECTED] wrote:
  For website, I can't really see how I can not have a dynamic
  structure.  There's no way I'm writing a function for each folder.

 Hmm, this may be a CherryPie concept thing but the vast majority
 of websites do not have dynamic structures. It really shouldn't be
 necessary. Why would you need to write a function per folder,
 simply write one function that takes the folder as an argument.

  I do take your point though, however you often find that it's easy to
  maintain something dynamic than huge amounts of more static stuff

 Very rarely, its easy to do backups of huge amounts of data if you
 know where to find it, its hard to trawl all over a changing structure
 looking for the things that need backing up. Particularly if, when you
 try to restore it, it needs to go in a different place to where you found
 it!

Backing up is generally one of the lesser of my worries.  Maintaining
content is primary.  Unless you are dynamically generating static
pages, they are a nightmare to maintain.  Particularly as it tends to
be quite repetitive.  A large amount of the code on any page is the
same as on any other page (page structure, boilerplate, etc).

I think it's a generally accepted principle that computers are better
at handling thousands of files like that better than humans are.  The
more of the repetitive stuff that can be removed and given to a
computer, the better.

  in the difference between hundreds of static web pages and using
  a cms of some kind).

 Yes but a CMS normally uses a static structure with dynamic content.
 The templates stored in one place and the content in another. The
 templates know where to look for the content and the content doesn't
 care where the templates are.

I think we might be using different meanings of structure.  I'm
referring to the site structure, as in which page is a parent of which
other page.  The site structure is just another aspect of the content.
 A reference to the parent/child pages is just another attribute like
content and title.

A (good) CMS would create a dynamic structure for the user to browse,
with folders and pages easily creatable and movable, and references to
pages would be dynamic so that if yo0u move a page internal links to
it still work.

 Dynamic content is 'A Good Thing', dynamic structure is usually bad.

But structure is just an aspect of content.

 BTW On the concept of loading your entire site at startup; if you are
 sharing a server you will make yourself very unpopular since you will
 be a huge resource hog. That's why ASP, JSP and other frameworks
 go to a lot of trouble to manage session lengths etc - to free up any
 unused resources and keep server speed up. Loading the structure
 of the site in the form of links might be reasonable, but only load content
 when you absolutely must. This also helps the maintainers update the
 site without restarting it.

I have my own dedicated server, but point taken.  Though I will still
need to reload the structure of the site when the maintainers change
it (adding/removing pages etc).

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


[Tutor] ldap client

2006-03-14 Thread Cedric BRINER
debian/sarge

hello,

I'm wanting to interogate an ldap server to fetch informations. so I'm looking 
for
a python2.4 ldap client. And the only I've found is python2.4-ldaptor. Does 
some of you
knows an good ldap client which use SSL ?

Ced.
-- 

Cedric BRINER
Geneva - Switzerland
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using Beautiful Soup to extract tag names

2006-03-14 Thread Ed Singleton
I have (unfortunately) received some data in XML format.  I need to
use it in Python, preferably as a list of dictionaries.  The data is a
flat representation of a table, in the style:

tablename
fieldname1Some Data/fieldname1
fieldname2Some Data/fieldname
...
/tablename
tablename
fieldname1Some Data/fieldname1
fieldname2Some Data/fieldname
...

and so on (where tablename is always the same in one file).

It looks like Beautiful Soup would be a good option to quickly change
it into a usable format, but I need to extract the field name (there's
lots and lots of fields) as well as the data, and I can't work out how
to do that in Beautiful Soup.

If anyone can give me some help I'd be grateful, or if they can point
me in the direction of a better solution.

Thanks

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


[Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
Hello,

I'm trying to work on some programs to help me understand ciphers and
ultimately cryptography.  I've understood so far, that a simple form
of bit-level cryptography is to split the original message into chunks
the same length as a 'key' and then do an xor.  I'm trying to keep
this really simple so I can understand from first principles - so eg:

Hello Tutors! could be split into:

Hell o Tut ors!

and xor'd with beer

I think I understand how xor works (thanks to an earlier post) but I'm
not sure how to iterate over each letter in a string.  What is the
recommended way to do this?

Thanks,

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


Re: [Tutor] Using Beautiful Soup to extract tag names

2006-03-14 Thread Kent Johnson
Ed Singleton wrote:
 I have (unfortunately) received some data in XML format.  I need to
 use it in Python, preferably as a list of dictionaries.  The data is a
 flat representation of a table, in the style:
 
 tablename
 fieldname1Some Data/fieldname1
 fieldname2Some Data/fieldname
 ...
 /tablename
 tablename
 fieldname1Some Data/fieldname1
 fieldname2Some Data/fieldname
 ...
 
 and so on (where tablename is always the same in one file).

ElementTree makes short work of this:

from elementtree import ElementTree

xml = '''
datatablename
fieldname1Some Data1/fieldname1
fieldname2Some Data2/fieldname2
/tablename
tablename
fieldname3Some Data3/fieldname3
fieldname4Some Data4/fieldname4
/tablename
/data'''

doc = ElementTree.fromstring(xml)
# use ElementTree.parse() to parse a file

for table in doc.findall('tablename'):
 for field in table.getchildren():
 print field.tag, field.text


prints:
fieldname1 Some Data1
fieldname2 Some Data2
fieldname3 Some Data3
fieldname4 Some Data4

If speed is an issue then look at cElementTree which has the same 
interface and is blazingly fast.
http://effbot.org/zone/element.htm

Kent

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


Re: [Tutor] scaling values

2006-03-14 Thread Bob Gailer
kevin parks wrote:
 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?
   
Perhaps someone on this list knows what you want, but I don't. What is 
scaling? What does foo look like?
 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)

   

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


Re: [Tutor] scaling values

2006-03-14 Thread David Heiser

Is this what you're asking for?


# Scaler.py
#

def scale(OldList, NewMin, NewMax):
NewRange = float(NewMax - NewMin)
OldMin = min(x)
OldMax = max(x)
OldRange = float(OldMax - OldMin)
ScaleFactor = NewRange / OldRange
print '\nEquasion:  NewValue = ((OldValue - ' + str(OldMin) + ') x '
+ str(ScaleFactor) + ') + ' + str(NewMin) + '\n'
NewList = []
for OldValue in OldList:
NewValue = ((OldValue - OldMin) * ScaleFactor) + NewMin
NewList.append(NewValue)
return NewList

x = [13, 71, 120, 88, 82, 100, 10, 65, 101, 45, 26]
foo = scale(x, 0, 1.0)

##x = [.12789, .982779, .19798198, .266796, .656527, .257877091]
##foo = scale(x, 0, 127)

##x = [0, 50, 100]
##foo = scale(x, 32, 212)

##x = [32, 122, 212]
##foo = scale(x, 0, 100)

print 'New List = ' + str(foo)
Print


 

Equasion:  NewValue = ((OldValue - 10) x 0.00909090909091) + 0

New List = [0.027272727272727271, 0.55454545454545456, 1.0,
0.70909090909090911, 0.65454545454545454, 0.81818181818181812, 0.0, 0.5,
0.82727272727272727, 0.31818181818181818, 0.14545454545454545]

 



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of kevin parks
Sent: Tuesday, March 14, 2006 1:03 AM
To: tutor@python.org
Subject: [Tutor] scaling values


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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using Beautiful Soup to extract tag names

2006-03-14 Thread Ed Singleton
As always Kent, you're amazing.

That will do perfectly.  (Though the ElementTree documentation seems a
bit difficult to get through.  I'm sure I'll get through it
eventually).

Thanks

Ed

On 14/03/06, Kent Johnson [EMAIL PROTECTED] wrote:
 Ed Singleton wrote:
  I have (unfortunately) received some data in XML format.  I need to
  use it in Python, preferably as a list of dictionaries.  The data is a
  flat representation of a table, in the style:
 
  tablename
  fieldname1Some Data/fieldname1
  fieldname2Some Data/fieldname
  ...
  /tablename
  tablename
  fieldname1Some Data/fieldname1
  fieldname2Some Data/fieldname
  ...
 
  and so on (where tablename is always the same in one file).

 ElementTree makes short work of this:

 from elementtree import ElementTree

 xml = '''
 datatablename
 fieldname1Some Data1/fieldname1
 fieldname2Some Data2/fieldname2
 /tablename
 tablename
 fieldname3Some Data3/fieldname3
 fieldname4Some Data4/fieldname4
 /tablename
 /data'''

 doc = ElementTree.fromstring(xml)
 # use ElementTree.parse() to parse a file

 for table in doc.findall('tablename'):
  for field in table.getchildren():
  print field.tag, field.text


 prints:
 fieldname1 Some Data1
 fieldname2 Some Data2
 fieldname3 Some Data3
 fieldname4 Some Data4

 If speed is an issue then look at cElementTree which has the same
 interface and is blazingly fast.
 http://effbot.org/zone/element.htm

 Kent

 ___
 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] Using Beautiful Soup to extract tag names

2006-03-14 Thread Kent Johnson
Ed Singleton wrote:
 As always Kent, you're amazing.

Thank you!
 
 That will do perfectly.  (Though the ElementTree documentation seems a
 bit difficult to get through.  I'm sure I'll get through it
 eventually).

Unfortunately I have to agree with you on this one. ET is going to be 
part of the standard lib in Python 2.5, I hope the docs get a bit of work.

Kent

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


Re: [Tutor] scaling values

2006-03-14 Thread Hugo González Monteverde
Hi Kevin,

Do you mean:?

1)take the highest value in the list hval, take the lowest value in the 
list lval

2) pass top and bottom NEW values for the list: ntop nbot

3) then build another list where hval is replaced by ntop, lval is 
replaced by nbot, and everything else is geometrically scaled in bewteen?

AFAIK, there is no such function in the standard lib (maybe someone 
knows better) but it is not hard to build such a function. Have you tried?

It would be something like:

def scaled(x, ntop, nbot)

See that you can use min(L) and max(L) to get lval and hval

Try it, and if you run into problems or have questions, don't hesitate 
to ask.

Hope that helps,

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo
 Hello Tutors! could be split into:

 Hell o Tut ors!

 and xor'd with beer

 I think I understand how xor works (thanks to an earlier post) but I'm
 not sure how to iterate over each letter in a string.  What is the
 recommended way to do this?

The xor bitwise operator works with numbers --- not directly with strings
--- so one of your tasks will probably be to take a chunk like:

beer

and turn it into some numeric bit pattern.


It turns out that this isn't too bad if we abuse the 'struct' module:

http://www.python.org/doc/lib/module-struct.html


The idea is to unpack four single characters as a single 4-byte integer.
For example:

##
 struct.unpack('i', 'food')
(1685024614,)
##


This kind of transformation is reversable:

##
 struct.pack('i', 1685024614)
'food'
##


Does this make sense?

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Danny Yoo [EMAIL PROTECTED] wrote:

 The idea is to unpack four single characters as a single 4-byte integer.

That's really useful, thanks, as I was planning to iterate over each
letter and call ord()

 This kind of transformation is reversable:

Highly useful.  Thanks very much indeed.

 Does this make sense?

Absolutely.

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Matthew Webber
As a side note, remember that that xor-ing a key with a message is trivial
to break (it's just a variation on the Vigenere cipher first published in
1568). So don't use if for any real applications.

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Steve Nelson
Sent: 14 March 2006 15:29
To: tutor@python.org
Subject: [Tutor] Iterate over letters in a word

Hello,

I'm trying to work on some programs to help me understand ciphers and
ultimately cryptography.  I've understood so far, that a simple form
of bit-level cryptography is to split the original message into chunks
the same length as a 'key' and then do an xor.  I'm trying to keep
this really simple so I can understand from first principles - so eg:

Hello Tutors! could be split into:

Hell o Tut ors!

and xor'd with beer

I think I understand how xor works (thanks to an earlier post) but I'm
not sure how to iterate over each letter in a string.  What is the
recommended way to do this?

Thanks,

S.

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Matthew Webber [EMAIL PROTECTED] wrote:
 As a side note, remember that that xor-ing a key with a message is trivial
 to break (it's just a variation on the Vigenere cipher first published in
 1568). So don't use if for any real applications.

Yes - at the moment this is just a way for me to begin to get my head
around how cryptography works from anabsolutely ludicrously basic
position.  This all started because I couldn't get my head around the
difference between an encryption algorithm and the key.  I thought
that by writing my own, I would work it out!

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Steve Nelson [EMAIL PROTECTED] wrote:
 On 3/14/06, Danny Yoo [EMAIL PROTECTED] wrote:

  The idea is to unpack four single characters as a single 4-byte integer.

 That's really useful, thanks, as I was planning to iterate over each
 letter and call ord()

Ok, so experimenting a little further, and looking at the
documentation, it seems that len(string) and calcsize (i) must be the
same.  Is there a reason why 'i' is a 4 byte integer?  Doesn't this
mean that this method wouldn't scale if I then decided I wanted to
use, eg, a 6 byte key instead of a four?  Or do I misunderstand?

I am also struggling to understand why a 4 byte integer is so large?

 mystring = Hello I am Steve
 import struct
 struct.unpack('i', mystring[0:4])
(1819043144,)

I can see that the largest number I can generate in a 1 byte integer
is 255 - which is (2^8)-1.  Is the point that with a 4 byte number I
can actually get (2^32)-1 , ie 4294967295?  This just seems like a
huge number!

I suppose I've answered my question... but any comments or
clarifications would help a lot.

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo


 As a side note, remember that that xor-ing a key with a message is
 trivial to break (it's just a variation on the Vigenere cipher first
 published in 1568). So don't use if for any real applications.

Hi Matthew,

Counterpoint: think of one-time pads.

http://en.wikipedia.org/wiki/One-time_pad

XOR itself is just a technique --- it's just a binary operation between
two bit patterns --- but the use of one-time pads provides unbreakable
encryption.  As long as the xor-ing key is protected, and as long as the
encrypting key pattern is as long as the message, it's theoretically
unbreakable.

If we reuse the same encrypting bit-pattern over and over, or make the
XORing key less than random, then all bets are off, of course.  *grin*

There are different encryption schemes that, at its heart, use XOR.  But
XOR itself is not inherently insecure: it's our use of it that determines
the quality of the result.

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo

 Yes - at the moment this is just a way for me to begin to get my head
 around how cryptography works from anabsolutely ludicrously basic
 position.  This all started because I couldn't get my head around the
 difference between an encryption algorithm and the key.  I thought that
 by writing my own, I would work it out!

Hi Steve,

Yup; it really has to do with numbers.  Once we manage that leap, then
it's just a matter of writing functions that transform numbers to other
numbers.

When you want to get more in depth, you may find:

http://mitpress.mit.edu/sicp/psets/ps3/readme.html

useful; it's a fun homework assignment on the RSA cryptosystem.  The
support functions are in Scheme, but converting it to Python isn't too
bad.


Best of wishes!

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


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Adam
Here's something you might find useful they've just started a series
on cryptography on this site, you can read them or listen to a
podcast.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Adam
On 14/03/06, Adam [EMAIL PROTECTED] wrote:
 Here's something you might find useful they've just started a series
 on cryptography on this site, you can read them or listen to a
 podcast.

D'oh! Would help if I actually stuck the link in
http://www.grc.com/SecurityNow.htm#30
___
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


[Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
Hello all,

Further to my previous puzzling, I've been working out the best way to
chop a string up into n-sized words:

I'm aware that I can use a slice of the string, with, eg, myWord[0:4].

I am also aware that I can do blob = myWord.pop(0) to take (and
remove) the first character.  I can botch these together, eg
myFourLetterWord =
myWord.pop(0)+myWord.pop(0)+myWord.pop(0)+myWord.pop(0) but that is
really horrid.

I then tried doing something suitably silly, as follows:

1) Find the length of string.
2) Create a list using range(0, length, 4)
3) We now have the end points for each 'word', eg 4, 8, 12.
4) Now create a list of tuples that represent the slices we need, ie
(0,4), (5,8) etc
5) Iterate over this list, grabbing the slices as depicted in the tuples.
6) Use these tuples to grab slices.

The code looked like this:
#!/usr/bin/env python
myString = Sir, you are an egotistical rhetorician!!!
length=len(myString)
extra=length%4
if extra  0:
  myString = myString+(#*extra)+#
r = range(0, len(myString), 4)
wordRange=[]
for i in r:
  if i1:
wordRange.append((int(i-4),i))
for t in wordRange:
  print myString[t[0]:t[1]]

Surely there's an easier way?

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


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Adam
On 14/03/06, Steve Nelson [EMAIL PROTECTED] wrote:
 Hello all,

 Further to my previous puzzling, I've been working out the best way to
 chop a string up into n-sized words:

 I'm aware that I can use a slice of the string, with, eg, myWord[0:4].

 I am also aware that I can do blob = myWord.pop(0) to take (and
 remove) the first character.  I can botch these together, eg
 myFourLetterWord =
 myWord.pop(0)+myWord.pop(0)+myWord.pop(0)+myWord.pop(0) but that is
 really horrid.

 I then tried doing something suitably silly, as follows:

 1) Find the length of string.
 2) Create a list using range(0, length, 4)
 3) We now have the end points for each 'word', eg 4, 8, 12.
 4) Now create a list of tuples that represent the slices we need, ie
 (0,4), (5,8) etc
 5) Iterate over this list, grabbing the slices as depicted in the tuples.
 6) Use these tuples to grab slices.

 The code looked like this:
 #!/usr/bin/env python
 myString = Sir, you are an egotistical rhetorician!!!
 length=len(myString)
 extra=length%4
 if extra  0:
   myString = myString+(#*extra)+#
 r = range(0, len(myString), 4)
 wordRange=[]
 for i in r:
   if i1:
 wordRange.append((int(i-4),i))
 for t in wordRange:
   print myString[t[0]:t[1]]

 Surely there's an easier way?

 S.

How's this?:

 myString = Sir, you are an egotistical rhetorician!!!
 [myString[i:i+4] for i in range(0, len(myString), 4)]
['Sir,', ' you', ' are', ' an ', 'egot', 'isti', 'cal ', 'rhet',
'oric', 'ian!', '!!']

Hopefully that should point you in the right direction to do n-sized
words as well.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scaling values

2006-03-14 Thread Kent Johnson
kevin parks wrote:
 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.. 

I think the confusion was about what input range to use. From your code 
it looks like you want to use just the actual range of input values.

 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]

This is actually pretty close. You have ratio backwards and you need to 
account for the offsets min(in_seq) and low. Try
   in_low = min(in_seq)
   ratio = range2/range1
   return [ ((x-in_low) * ratio + low) for x in in_seq ]

Kent

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


[Tutor] Tix: binding

2006-03-14 Thread hk
I am looking at Tix and I am stuck with what seems to be a simple problem:

I want to bind the cancel buttom of a ExFileSelectBox to a routine in my class, 
but can not make it work.
I guess the problem is the same for any binding of tix sub-widgets, so if 
someone could send some sample code.
 import Tix
 root = Tix.Tk()
 box = Tix.ExFileSelectBox(root)
 print box.subwidget(name='cancel')
.11827520.bf.cancel#so the sub-widget cancel buttom 
is there

But how to bind it ?

 box.cancel(command = lambda a,b : a+b )
Traceback (most recent call last):
  File pyshell#12, line 1, in ?
box.cancel(command = lambda a,b : a+b )
AttributeError: _dummyButton instance has no __call__ method

Or

 box.subwidget(name='cancel' , command = lambda a,b : a+b )
Traceback (most recent call last):
  File pyshell#13, line 1, in ?
box.subwidget(name='cancel' , command = lambda a,b : a+b )
TypeError: subwidget() got an unexpected keyword argument 'command'

So, it seems I can not bind to command, but what then ?
From the Tix docu I thought the buttoms are normal Tkinter widgets ?
-- 
Harm 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Fwd: Re: Indoor ultimate]

2006-03-14 Thread John Fouhy
Sorry guys, total brain fart there :-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Fwd: Re: Indoor ultimate]

2006-03-14 Thread John Fouhy
On 15/03/06, Sam B [EMAIL PROTECTED] wrote:
 I'm keen!

 what shoe type do you reccomend?

Non-marking cross trainers will do.

If you want to spend more money, go into shoe clinic and tell them you
want shoes for playing indoor ultimate.  I ended up with a pair of
Adidas handball shoes.  Expect to pay $200 or more, though.

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


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Smith
| From: Steve Nelson 
| 
| Further to my previous puzzling, I've been working out the best way to
| chop a string up into n-sized words:
| 

I think the follow use of groupby is from Raymond Hettinger from ASPN recipes. 
The batch() function will return an iterable to you in user-definable sized 
sets.


from itertools import groupby
def batch(iterable, size):
def ticker(x, s=size, a=[-1]):
r = a[0] = a[0] + 1
return r // s
for k, g in groupby(iterable, ticker):
 yield g
s='this is my string to parse up.'
for i in batch(s,4):
print list(i)


The output is lists of (in this case) 4 characters; the last group is shorter, 
but you already know how to fix that.


['t', 'h', 'i', 's']
[' ', 'i', 's', ' ']
['m', 'y', ' ', 's']
['t', 'r', 'i', 'n']
['g', ' ', 't', 'o']
[' ', 'p', 'a', 'r']
['s', 'e', ' ', 'u']
['p', '.']


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


[Tutor] Help me please

2006-03-14 Thread yogeshwarran Nadeson
Hello, I am a newbie in python programming without previous programming experience.Here i would like to ask a question based on the exercises given by Josh Cogliatti in his tutorial.I'll paste the question.Hope you can help me to solve it.I'd like to end it by saying thanks for all yourr help. :Modify the password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print ``That must have been complicated.'' 
This is the previous password program:
Password.py # Waits until a password has been entered.  Use control-C to break out with out
# the password

#Note that this must not be the password so that the 
# while loop runs at least once.
password = "foobar"

#note that != means not equal
while password != "unicorn":
password = raw_input("Password:")
print "Welcome in"

Password.py # Waits until a password has been entered.  Use control-C to break out with out
# the password

#Note that this must not be the password so that the 
# while loop runs at least once.
password = "foobar"

#note that != means not equal
while password != "unicorn":
password = raw_input("Password:")
print "Welcome in"



-- 
___
Search for businesses by name, location, or phone number.  -Lycos Yellow Pages
http://yp.lycos.com

___
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] OSError

2006-03-14 Thread Christopher Spears
I am trying to write a function that takes a directory
name and describes the contents of the directory (file
name and size) recursively.  Here is what I have
written so far:

import os, os.path

def describeDirectory(directory):
if os.listdir(directory) == []:
print Empty directory!
else:
for files in os.listdir(directory):
if os.path.isdir(files):
print files, ' is a directory!'
else:
print files, 'SIZE: ',
os.path.getsize(files)

print 'Current Directory: \n'
describeDirectory('.')
print '\n'
print './testFiles: \n'
describeDirectory('./testFiles')

Here is the result:

Current Directory: 

changePeppers.py SIZE:  915
describeDirectory.py SIZE:  481
describeDirectory.pyc SIZE:  514
error_log SIZE:  0
makezeros.py SIZE:  147
makezeros.pyc SIZE:  481
output SIZE:  387
pepper.txt SIZE:  601
testFiles  is a directory!
textWrapper.py SIZE:  619
textWrapper.pyc SIZE:  1092
timings.py SIZE:  567
timings.pyc SIZE:  733


./testFiles: 

renameFiles.py SIZE: 
Traceback (most recent call last):
  File describeDirectory.py, line 17, in ?
describeDirectory('./testFiles')
  File describeDirectory.py, line 11, in
describeDirectory
print files, 'SIZE: ', os.path.getsize(files)
  File /usr/lib/python2.4/posixpath.py, line 139, in
getsize
return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory:
'renameFiles.py'

I don't understand the OSError.  The file
'renameFiles.py' is in './testFiles' directory.  Can
someone clarify this for me?

-Chris

I'm the last person to pretend that I'm a radio.  I'd rather go out and be a 
color television set.
-David Bowie

Who dares wins
-British military motto

I generally know what I'm doing.
-Buster Keaton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
On 3/14/06, Adam [EMAIL PROTECTED] wrote:

 Hopefully that should point you in the right direction to do n-sized
 words as well.

Indeed - as I now have a function:

def nsplit(s, n):
  return [s[i:i+n] for i in range(0, len(s), n)]

Incidentally I am currently going with:

def nsplit(s, n):
  while s:
   yield s[:n]
   s = s[n:]

As my friend just showed me how generators work, and also that the
beginning and end of lists are implicit.

Very cool.

Thanks for all your help!

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