Re: [Tutor] permutations?

2010-12-02 Thread Alan Gauld


"Alex Hall"  wrote

Alright, I have it working. Now the problem is that it does not 
throw

out reversals. I tried to do this myself with a couple loops, but I
get index errors. My total list of permutations is called l.

for i in range(0, len(l)):
r=l[i]; r.reverse()


You don''t need to specify the 0, its the default.
But you don't need to use indexing either.

for r,i in enumerate(l): r.reverse()


for j in range(0, len(l)):
 print l[j], r, i, j


Here you are comparing the reversed item to every item in the
list, including the ones you have already reversed, including the
item itself. So you will always find a match when i==j...

Using the in operator and list slicing would be easier

if r in l[i:]: l.remove(r)

but...


 if r==l[j]: l.remove(r)


Its never a good idea to remove items from a list you are iterating
over, its like the old cartoon of the guy cutting down the tree branch
he is sitting on. If you must do it I'd use a while loop and manually
manage the indexing. Or operate on a copy of the list.


longer exists. However, should the loop not re-evaluate len(l) each
time,


No because the loop uses range() - a function in its own right and
range evaluates len() when first called and then return a sequence
to the loop based on that. The loop does not use len()

You can find an example of this type of thing at the end of the 
Branching

topic of my tutorial.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


HTH,


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


Re: [Tutor] data structures

2010-12-02 Thread Alan Gauld


"Dana"  wrote

I'm using Python to extract words from plain text files.  I have a 
list of words.  Now I would like to convert that list to a 
dictionary of features where the key is the word and the value the 
number of occurrences in a group of files based on the filename


I think I know what you mean but an illustration of the desired
output would be useful. Just to be sure.

correspond to different categories).  What is the best way to 
represent this data?  When I finish I expect to have about 70 unique 
dictionaries with values I plan to use in frequency distributions, 
etc.  Should I use globally defined dictionaries?


You will either need a class/object to store the data or a set of
global dictionaries (or a database/file I suppose but that only makes
sense if you have a lot of data or need persistence).

You should consider keeping the individual dictionaries in another
dictionary so you only have a single global level variable.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] a print puzzle

2010-12-02 Thread Richard D. Moores
>>> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1]
>>> sum(s)
0.
>>> print(sum(s))
1.0  Why?
>>> f = 0.
>>> f**100
0.9889
>>> print(f**100)
1.0  Why?
>>> f**1
0.99988898
>>> print(f**1)
0.
>>> from math import fsum
>>> fsum(s)
1.0
>>> from math import fsum
>>> fsum(s)
1.0See <
http://docs.python.org/py3k/library/math.html?highlight=fsum#math.fsum>

The "Why?"s mark what are puzzles for me.

I see nothing in the doc for print() that explains these: <
http://docs.python.org/py3k/library/functions.html?highlight=print#print>,
but I'm sure a Tutor can.

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


Re: [Tutor] a print puzzle

2010-12-02 Thread Alan Gauld


"Richard D. Moores"  wrote


s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1]
sum(s)

0.


This uses repr() to display the result


print(sum(s))

1.0  Why?


This uses str() to display the result.

In many cases str() calls repr() but in other cases str() is a more 
user

friendly format. repr() tends to be a developers eye view of things.
This is one reason that in my tutorial I always use print() to display
results. The output is more newbie friendly that way :-)

I suspect the details will be revealed by a search for documentation
on __repr__

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] permutations?

2010-12-02 Thread Peter Otten
Alex Hall wrote:

> 1. Order matters; I meant to say that direction does not. That is, 123
> is not the same as 213, but 123 is the same as 321 since the second
> example is simply a reversal.
> 2. I am looking for all permutations and subpermutations (if that is a
> word) of 1-n where the list must have at least 2, but no more than n,
> unique numbers (so 1 is not valid, nor is 1231 since it repeats 1 and
> is too long for n=3).

>>> from itertools import permutations, chain
>>> def hall(items):
... items = list(items)
... maxwidth = len(items)
... return chain.from_iterable(
... (p for p in permutations(items, w) if p < p[::-1])
... for w in range(2, maxwidth+1))
...
>>> map("".join, hall("123"))
['12', '13', '23', '123', '132', '213']
>>> map("".join, hall("111"))
[]

Is that it? Be warned though that normally you'll get duplicates if items 
repeat:

>>> map("".join, hall("112"))
['12', '12', '112', '112']

Peter

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


Re: [Tutor] permutations?

2010-12-02 Thread शंतनू
Following link could be useful:

http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python

On Thu, Dec 2, 2010 at 04:15, Alex Hall  wrote:

> Hi all,
> I am wondering if there is a python package that will find
> permutations? For example, if I have (1, 2, 3), the possibilities I
> want are:
> 12
> 13
> 23
> 123
> 132
> 231
>
> Order does not matter; 21 is the same as 12, but no numbers can
> repeat. If no package exists, does someone have a hint as to how to
> get a function to do this? The one I have right now will not find 132
> or 231, nor will it find 13. TIA.
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehg...@gmail.com; http://www.facebook.com/mehgcap
> ___
> 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] permutations?

2010-12-02 Thread Luke Paireepinart
Instead of nested fors which will grow exponentially in running time, consider 
this for removing reversals:
newlist = []
tempdict = {}
for i in oldlist:
try:
tempdict[i]
except:
tempdict[i] = 1
tempdict[i.reverse()] = 1
newlist.append(i)


That's the gneral idea. Pseudocode, so untested. You exploit the O(1) hash 
lookup of the dict to avoid the runtime cost of the nested loops, and it is 
easier to read as well. Would need to timeit to be sure it helps with your data 
sets though. They may be small enough that it doesn't matter.

-
Sent from a mobile device with a bad e-mail client.
-

On Dec 2, 2010, at 7:54 AM, शंतनू  wrote:

> Following link could be useful:
> 
> http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python
> 
> On Thu, Dec 2, 2010 at 04:15, Alex Hall  wrote:
> Hi all,
> I am wondering if there is a python package that will find
> permutations? For example, if I have (1, 2, 3), the possibilities I
> want are:
> 12
> 13
> 23
> 123
> 132
> 231
> 
> Order does not matter; 21 is the same as 12, but no numbers can
> repeat. If no package exists, does someone have a hint as to how to
> get a function to do this? The one I have right now will not find 132
> or 231, nor will it find 13. TIA.
> 
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehg...@gmail.com; http://www.facebook.com/mehgcap
> ___
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Trying To Debug Code That Runs Arbitrary Function

2010-12-02 Thread Homme, James
Hi,
If you can get away with not telling me the answer, but pointing me to where to 
look for the answer, I'd be grateful.

In my Python learning, I am just now starting to understand how to make classes 
and extend them, so I have a very long way to go.

I wrote this code because I wanted to avoid lots of if statements and having to 
maintain a bunch of code like that. Eventually, my idea is to read in strings 
from a file, look one up, and use it to execute a function. So I created the 
following code. But nothing gets printed to the screen. How do I go about 
figuring out why this isn't happening? Here's the code.

myfuncs = [ "func1",
"func2" ]

def func1():
  print "func 1"

def func2():
  print "func 2"

eval (myfuncs[0])

raw_input("Press enter to quit")

Thanks.

Jim
Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Internal recipients,  Read my accessibility 
blog. Discuss 
accessibility 
here. 
Accessibility Wiki: Breaking news and accessibility 
advice



This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying To Debug Code That Runs Arbitrary Function

2010-12-02 Thread Vern Ceder
Here's your hint... to execute a Python function, it must be followed by
parentheses otherwise you are just referring to the function object.

HTH,

Vern

On Thu, Dec 2, 2010 at 9:35 AM, Homme, James wrote:

>  Hi,
>
> If you can get away with not telling me the answer, but pointing me to
> where to look for the answer, I'd be grateful.
>
>
>
> In my Python learning, I am just now starting to understand how to make
> classes and extend them, so I have a very long way to go.
>
>
>
> I wrote this code because I wanted to avoid lots of if statements and
> having to maintain a bunch of code like that. Eventually, my idea is to read
> in strings from a file, look one up, and use it to execute a function. So I
> created the following code. But nothing gets printed to the screen. How do I
> go about figuring out why this isn't happening? Here's the code.
>
>
>
> myfuncs = [ "func1",
>
> "func2" ]
>
>
>
> def func1():
>
>   print "func 1"
>
>
>
> def func2():
>
>   print "func 2"
>
>
>
> eval (myfuncs[0])
>
>
>
> raw_input("Press enter to quit")
>
>
>
> Thanks.
>
>
>
> Jim
>
> Jim Homme,
>
> Usability Services,
>
> Phone: 412-544-1810. Skype: jim.homme
>
> Internal recipients,  Read my accessibility 
> blog.
> Discuss accessibility 
> here.
> Accessibility Wiki: Breaking news and accessibility 
> advice
>
>
>
> --
> This e-mail and any attachments to it are confidential and are intended
> solely for use of the individual or entity to whom they are addressed. If
> you have received this e-mail in error, please notify the sender immediately
> and then delete it. If you are not the intended recipient, you must not
> keep, use, disclose, copy or distribute this e-mail without the author's
> prior permission. The views expressed in this e-mail message do not
> necessarily represent the views of Highmark Inc., its subsidiaries, or
> affiliates.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Vern Ceder
vce...@gmail.com, vce...@dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying To Debug Code That Runs Arbitrary Function

2010-12-02 Thread Alan Gauld

"Homme, James"  wrote


If you can get away with not telling me the answer, but pointing
me to where to look for the answer, I'd be grateful.


OK, Don't use eval or exec they have secuity issues - especially if 
reading the

strings from a file!


idea is to read in strings from a file, look one up,
and use it to execute a function.


Functions are objects sop can be stored as data.
Dictionariers can associate strings with arbitrary data - including 
functions.


Is that enough of a hint? :-)

If not, try reading the OOP topic in my tutorial, under the section 
heading
"Collections of Objects" where I discuss the related scenario of 
managing

collections of objects with arbitrary (or user assigned) names/IDs.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] Is there any way I can use named tuples in Python 2.5?

2010-12-02 Thread Albert-Jan Roskam
Hi,

Is there any way I can use named tuples in Python 2.5? It's available since 2.6 
and I'm not sure if from "__future__" works. I can't try it here.
I cannot simply upgrade Python (alas!)

http://www.python.org/doc//current/library/collections.html#collections.namedtuple


 Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~



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


Re: [Tutor] a print puzzle

2010-12-02 Thread Richard D. Moores
On Thu, Dec 2, 2010 at 03:13, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>
> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1]
> sum(s)
>>
>> 0.
>
> This uses repr() to display the result
>
> print(sum(s))
>>
>> 1.0      Why?
>
> This uses str() to display the result.
>
> In many cases str() calls repr() but in other cases str() is a more user
> friendly format. repr() tends to be a developers eye view of things.
> This is one reason that in my tutorial I always use print() to display
> results. The output is more newbie friendly that way :-)
>
> I suspect the details will be revealed by a search for documentation
> on __repr__

I found nothing that helped as much as you have already.

Thanks,

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


Re: [Tutor] Is there any way I can use named tuples in Python 2.5?

2010-12-02 Thread Emile van Sebille

On 12/2/2010 9:52 AM Albert-Jan Roskam said...

Hi,

Is there any way I can use named tuples in Python 2.5? It's available since 2.6
and I'm not sure if from "__future__" works. I can't try it here.
I cannot simply upgrade Python (alas!)


Maybe this'll help...

http://code.activestate.com/recipes/500261/

Emile

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


Re: [Tutor] Is there any way I can use named tuples in Python 2.5?

2010-12-02 Thread Albert-Jan Roskam
Hi,

Thank you! Woaah, very cool code btw. There's much to learn from it, for me at 
least.

 Cheers!!
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~





From: Emile van Sebille 
To: tutor@python.org
Sent: Thu, December 2, 2010 8:16:19 PM
Subject: Re: [Tutor] Is there any way I can use named tuples in Python 2.5?

On 12/2/2010 9:52 AM Albert-Jan Roskam said...
> Hi,
>
> Is there any way I can use named tuples in Python 2.5? It's available since 
2.6
> and I'm not sure if from "__future__" works. I can't try it here.
> I cannot simply upgrade Python (alas!)

Maybe this'll help...

http://code.activestate.com/recipes/500261/

Emile

___
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] a print puzzle

2010-12-02 Thread bob gailer

On 12/2/2010 2:09 PM, Richard D. Moores wrote:

[snip]


I found nothing that helped as much as you have already.


In the reference (for 2.6.2) under print statement I find:

"print evaluates each expression in turn and writes the resulting object 
to standard output (see below). If an object is not a string, it is 
first converted to a string using the rules for string conversions.


This leaves the reader pondering "rules for string conversions". I 
wonder whether this is defined and why it was not cross-referenced here.


In the reference (for 3.1.3) under print function I find:

"All non-keyword arguments are converted to strings like str() does"

Under str() I find:

"returns its nicely printable representation."

OK but still kinda vague.

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

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


Re: [Tutor] a print puzzle

2010-12-02 Thread Steven D'Aprano

bob gailer wrote:

On 12/2/2010 2:09 PM, Richard D. Moores wrote:

[snip]


I found nothing that helped as much as you have already.


In the reference (for 2.6.2) under print statement I find:

"print evaluates each expression in turn and writes the resulting object 
to standard output (see below). If an object is not a string, it is 
first converted to a string using the rules for string conversions.


This leaves the reader pondering "rules for string conversions". I 
wonder whether this is defined and why it was not cross-referenced here.


I'm not sure why there's no cross-reference, or even where it should 
cross-reference to, but the rules for string conversion are that:


if the object has a method __str__, it is called and the result used;
if not, but it has a method __repr__, it is called and the result used;
if not (only relevant for old-style classes in Python 2.x), then Python 
has a default string format.



--
Steven

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


[Tutor] Scanning a file for specific text and copying it to a new file

2010-12-02 Thread Ben Ganzfried
I'm trying to build a program that reads in a file and copies specific
sections to a new file.  More specifically, every time the words
"summary on" are in the original file, I want to copy the following
text to the new file until I get to the words "summary off".

My questions are the following:
1) Once I have read in the old file, how do I copy just the parts that
I want to the new file?  What command do I give the computer to know
that "summary on" (whether capitalized or not) means start writing to
the new file-- and "summary off" means stop?  Also, I assume I'll put
all of this in a while loop based on the condition that we should read
until we are done reading the whole document?  (although, this is
somewhat inefficient because the last "summary on/off" could be way
before the end, but this is preferable to missing one that is at the
end...)

Thank you very much for any help you can provide.  Much appreciated!

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


Re: [Tutor] Scanning a file for specific text and copying it to a new file

2010-12-02 Thread Emile van Sebille

On 12/2/2010 10:27 AM Ben Ganzfried said...

I'm trying to build a program that reads in a file and copies specific
sections to a new file.  More specifically, every time the words
"summary on" are in the original file, I want to copy the following
text to the new file until I get to the words "summary off".

My questions are the following:
1) Once I have read in the old file, how do I copy just the parts that
I want to the new file?


I'd consider reading the entire file in, then use the resulting string's 
split method to break out the parts that will need to be written, and 
finally write those out.


If the file you're reading is very large (eg, several hundred MB), or 
competition is high for memory, you'll want to consider loops.


Emile





What command do I give the computer to know
that "summary on" (whether capitalized or not) means start writing to
the new file-- and "summary off" means stop?  Also, I assume I'll put
all of this in a while loop based on the condition that we should read
until we are done reading the whole document?  (although, this is
somewhat inefficient because the last "summary on/off" could be way
before the end, but this is preferable to missing one that is at the
end...)

Thank you very much for any help you can provide.  Much appreciated!

Ben


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


Re: [Tutor] Scanning a file for specific text and copying it to a new file

2010-12-02 Thread Steven D'Aprano

Ben Ganzfried wrote:

I'm trying to build a program that reads in a file and copies specific
sections to a new file.  More specifically, every time the words
"summary on" are in the original file, I want to copy the following
text to the new file until I get to the words "summary off".

My questions are the following:
1) Once I have read in the old file, how do I copy just the parts that
I want to the new file?  What command do I give the computer to know
that "summary on" (whether capitalized or not) means start writing to
the new file-- and "summary off" means stop?  


There is no such command -- you have to program it yourself. I'll give 
you an outline:


def copy(inname, outname):
infile = open(inname, 'r')
outfile = open(outname, 'w')
copying = False  # Don't copy anything yet.
for line in infile:
if copying:
outfile.write(line)
infile.close()
outfile.close()

Note:

(1) This is not how you would implement a "real" file utility. There are 
much faster methods if all you want is to copy the contents in full. But 
since the next step will be to *process* the data in the file, we need 
to do it the slow(ish) way.


(2) This has a lack of error checking. What happens if you pass the same 
file name for both arguments? What if the input file doesn't exist, or 
the output file does?


(3) This assumes that the file is a text file, and won't work on 
arbitrary binary files.



The next step is to look for the tag. You don't say whether "summary on" 
has to be in a line on its own, or if it can be in the middle of a line, 
or even whether it could be split over two lines. That third case will 
be tricky, the second case is probably a bad idea, so I'll just assume 
"summary on" must be in a line on its own:



for line in infile:
# Ignore leading & trailing whitespace, don't care about case.
s = line.strip().lower()
if s == "summary on":
copying = True  # Start copying.
continue  # But not *this* line, start with the next.
elif s == "summary off":
copying = False  # Stop copying.
if copying:
outfile.write(line)


Note that this allows a single file to have more than one summary 
section. If this is not the case, you can replace the "Stop copying" 
line with the command `break` to exist the for-loop early and avoid 
processing the rest of the file.



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


Re: [Tutor] Scanning a file for specific text and copying it to a newfile

2010-12-02 Thread Alan Gauld


"Ben Ganzfried"  wrote

I'm trying to build a program that reads in a file and copies 
specific

sections to a new file.  More specifically, every time the words
"summary on" are in the original file, I want to copy the following
text to the new file until I get to the words "summary off".


This is a very common data processing technique and is often
termed setting a "sentinel" value or flag. The generic logic looks 
like:


for line in file:
if sentinalFlag is True:
   if line is sentinel off
 sentinelFlag = False
   else
  process line
else if sentinel is in line
   sentinelFlag = True

1) Once I have read in the old file, how do I copy just the parts 
that

I want to the new file?


See above where "process line" is wrting the line to the output file

all of this in a while loop based on the condition that we should 
read

until we are done reading the whole document?


Since you don't know where the last sentinel off line is you must
process the whole file so a for loop is probably more appropriate
than a while loop.

If the data is short you can read the whole string into memory
and tokenize it instead (ie search for the first occurence of on
then find the next occurence of off and copy the slice between
start and end positions). But if you ever need to process large
files then the line by line sentinel approach is more effective
and uses a lot less memory.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



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


Re: [Tutor] data structures

2010-12-02 Thread Steven D'Aprano

Knacktus wrote:

Am 02.12.2010 02:51, schrieb Dana:

Hello,

I'm using Python to extract words from plain text files. I have a list
of words. Now I would like to convert that list to a dictionary of
features where the key is the word and the value the number of
occurrences in a group of files based on the filename (different files
correspond to different categories). What is the best way to represent
this data? When I finish I expect to have about 70 unique dictionaries
with values I plan to use in frequency distributions, etc. Should I use
globally defined dictionaries?


Depends on what else you want to do with the group of files. If you're 
expecting some operations on the group's data you should create a class 
to be able to add some more methods to the data. I would probably go 
with a class.


Unless you're planning to have multiple "file groups" at once, or 
intending to re-use this code for other modules, using a class is 
probably overkill. This isn't Java where everything has to be a class :)



But also I think dictionaries can be fine. If you really only need the 
dicts. You could create a function to create those.


Agreed.

One way or the other, a dict {word: count} is the natural data structure 
to use for a concordance. Whether you store those dicts in a class, or 
just operate directly on the dicts, is relatively unimportant.



--
Steven

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


Re: [Tutor] Pyserial and invalid handle

2010-12-02 Thread Terry Carroll

On Wed, 1 Dec 2010, Walter Prins wrote:


But whatever the case may be, suffice it to say I've reproduced your issue
on my Win7 64bit box, and then resolved it by installing the PyWin32
modules.


I'd like to put in a plug for Activestate Python here.  Activestate has a 
free distribution of Python for Windows that not only includes the basic 
Python program and libraries, but also commonly used extras including the 
Win32 modules.


I've always used Activestate Python from the start, and recommend it.

They have distributions for both Python 2 and 3, each both in 32-bit and 
64-bit.


http://www.activestate.com/activepython/downloads

I have no association with Activestate, I'm just a satisfied customer. 
Well, not even a customer.  I'm a satisfied freeloader.

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


Re: [Tutor] permutations?

2010-12-02 Thread Steven D'Aprano

Alan Gauld wrote:


"Alex Hall"  wrote


Alright, I have it working. Now the problem is that it does not throw
out reversals. I tried to do this myself with a couple loops, but I
get index errors. My total list of permutations is called l.

for i in range(0, len(l)):
r=l[i]; r.reverse()


You don''t need to specify the 0, its the default.
But you don't need to use indexing either.

for r,i in enumerate(l): r.reverse()


for j in range(0, len(l)):
 print l[j], r, i, j


Here you are comparing the reversed item to every item in the
list, including the ones you have already reversed, including the
item itself. So you will always find a match when i==j...

Using the in operator and list slicing would be easier

if r in l[i:]: l.remove(r)

but...


 if r==l[j]: l.remove(r)


Its never a good idea to remove items from a list you are iterating
over, its like the old cartoon of the guy cutting down the tree branch
he is sitting on. 


You can cut off the branch you're sitting on so long as you remember to 
cut on the correct side :)


Back in Ancient Days when dinosaurs walked the earth, and I programmed 
in Pascal, computers didn't have much memory, and were slow. 
Consequently it wasn't practical to make a copy of a list if you wanted 
to delete a few items. The only practical way to modify lists was to 
modify them in place, and if you needed to delete items, you had to work 
backwards. It took me a while to break myself of the habit of doing this:


for i in range(len(mylist)-1, -1, -1):
if mylist[i] == "something":
 del mylist[i]

(Note that you only need to work backwards if you're *deleting* entries, 
not if you replace them with something else.)


This is still a useful technique to have in your toolbox, but generally 
speaking the above is better written as:


mylist = [x for x in mylist if x != "something"]


If you really need to modify the list in place, and not just re-bind the 
name "mylist" to the new list, then one tiny change will do it:


mylist[:] = [x for x in mylist if x != "something"]



--
Steven

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


[Tutor] printing in python 3.x

2010-12-02 Thread Rance Hall
I've been working on this cli based python 3.x app for a friends shop.
 So far, everything is working well.  We are now ready to start
development on a new module of my code.

The shop is a repair shop, and I've already done the code for client
management and employee management and all the framework and
supporting stuff, but now its time to check  in broken items for
repair and print a reciept for the customer and a shop ticket for the
item.

shop tickets are easy to do *we think*, we think all we need to do is
create a "template" text file, and open that text file, sub markers
for real data, and create a temp file then send that to the printer
with the functions of the underlying OS.  In my tests on windows this
worked fine.

But shop owner wants to do something nicer with the customer receipts.
 He is talking shop logos and pdf files.

I have several issues.

Google keeps bringing up ReportLib and the python imaging library, but
neither have a 3.x compatible version yet.

Shop owner is talking about eventually being able to save customer
check in tickets and check out tickets on a web site where clients can
log in and check on their work.

My thought is that this is perfect for PDF files, but they aren't
available on 3.x yet.

What other options do I have?

I looked at word integration and openoffice integration and they dont
look ready for 3.x yet either.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor