Re: [Tutor] xml question

2010-07-27 Thread Mac Ryan
On Mon, 2010-07-26 at 12:09 -0700, Albert-Jan Roskam wrote:
> I am making a data processing program that will use a configuration
> file. The file should contain information about: (1) source files
> used, (2) (intermediate) output files, (3) used parameters/estimation
> methods (4) manual data edits + datetime stamp + user name . I'd like
> to store this config file in xml. However, I've never created
> something like this before. Is this a suitable format, and, if so,
> what would the elementtree look like? Should I just use 'config'  or
> something similar as root, and the information elements 1 through 3 as
> child elements? And should the manual edits be stored as an element
> 'edit' with various attributes (the edit itself, the time stamp,
> etc.)?

I am with Steven on the fact that XML might not necessarily be the best
choice, unless you plan to use the configuration file with other
third-party programs, in which case the fact that XML has built-in
parsing libs for nearly all languages makes life of fellow developer
easier.

For the next project of mines, I am planning to use YAML (YAML Ain't a
Markup Language). I stumbled upon this format while toying around with
the google app engine, that uses it for storing your application
configuration data. IMO, YAML has the following prominent advantages:
   1) It is easy to read and edit by humans [think "markdown"]
   2) It has solid parsing libraries for Python
   3) It is far less verbose than XML
   4) It is consistent with Python "relevant whitespaces" [indentation
is used to define data structure hierarchy]

I have not yet got to the stage of writing code that use YAML (will take
two more weeks at best for me to get to that point), but should you go
down this road, I would love to hear back from you. [As I would love to
hear from anybody else who might have experience with YAML+Python]

Some links:
- http://en.wikipedia.org/wiki/YAML [good overview of the format]
- http://www.yaml.org/ [official site... when consistency with the
format makes a website hard to browse!]
- http://pyyaml.org/wiki/PyYAMLDocumentation [documentation of the
library for Python - pure Python code // can use a C library]

An example of YAML file to give you the taste of it:
> receipt: Oz-Ware Purchase Invoice
> date:2007-08-06
> 
> customer:
> given:   Dorothy
> family:  Gale
> 
> items:
> - part_no:   A4786
>   descrip:   Water Bucket (Filled)
>   price: 1.47
>   quantity:  4
> - part_no:   E1628
>   descrip:   High Heeled "Ruby" Slippers
>   price: 100.27
>   quantity:  1
> 
> bill-to:  &id001
> street: |
> 123 Tornado Alley
> Suite 16
> city:   East Westville
> state:  KS
> 
> ship-to:  *id001
> 
> specialDelivery:  >
> Follow the Yellow Brick
> Road to the Emerald City.
> Pay no attention to the
> man behind the curtain.

HTH,
Mac.


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


Re: [Tutor] django help....

2010-07-27 Thread Mac Ryan
On Mon, 2010-07-26 at 14:03 +0100, Dipo Elegbede wrote:
> what ook would you recommend for a beginner?

I am a django beginner myself. I did the tutorial first
(http://docs.djangoproject.com/en/dev/intro/tutorial01/) and now I am
very happy with "Practical Django Projects":
http://www.amazon.com/Practical-Django-Projects-Experts-Development/dp/1430219386

I assume that the fact I have a bit more than a year of "fooling around"
with Python (2.x series) makes this book easy to follow. If you
contrarily are not familiar with Python syntax then it might not be the
best choice.

As already mentioned by others, the Django book (http://djangobook.com)
is also a an excellent (and free!) alternative.

I've also begun to read "Pro Django" (http://prodjango.com/), which is
another excellent book, providing material to learn to leverage Python
itself better (beside Django). However - to put it as its author does -
"Pro Django isn’t for the faint of heart. It’s not for people who are
new to Django, and especially not those who are new to Python".

Best luck with your learning!
Mac.



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


Re: [Tutor] xml question

2010-07-27 Thread Andreas Röhler

Am 27.07.2010 02:29, schrieb Steven D'Aprano:

On Tue, 27 Jul 2010 05:09:09 am Albert-Jan Roskam wrote:
   

Hi,

I am making a data processing program that will use a configuration
file. The file should contain information about: (1) source files
used, (2) (intermediate) output files, (3) used parameters/estimation
methods (4) manual data edits + datetime stamp + user name . I'd like
to store this config file in xml.
 

Why XML?

Even though XML is plain text, it is *not* a human writable format,
except perhaps for the simplest data. XML is one of those things which
has become "the in-thing" and is used in all sorts of inappropriate
places just because "everybody else uses XML". Even *supporters* of XML
describe themselves as having "drunk the XML Kool-Aid".

(For those who are unaware, "drinking the Kool-Aid" refers to the
Reverend Jim Jones mass murder-suicide back in the 70s, when nearly a
thousand cult members drank poison-laced Kool-Aid.)
   


Be careful spreading information you didn't research thoroughly yourself.
This world is upside-down.

Andreas

--
https://code.launchpad.net/~a-roehler/python-mode
https://code.launchpad.net/s-x-emacs-werkstatt/



XML is extremely verbose and inefficient. It has its uses, but the best
advice I can give is, don't use XML unless you need to communicate with
something that expects XML, or unless your data is so complex that you
need XML.

Some alternatives:

If you're using Python 2.6 or better, you might consider the plistlib
module for a thin wrapper around XML.

JSON is often considered a more friendly format. Some people prefer YAML
over JSON, although YAML isn't in the standard library.

If your data is in the form option:value, then ConfigParser
(Windows-style ini files) may be all you need.

   

However, I've never created
something like this before. Is this a suitable format, and, if so,
what would the elementtree look like?
 

You tell us, it's your data :)


   

Should I just use 'config'  or
something similar as root, and the information elements 1 through 3
as child elements? And should the manual edits be stored as an
element 'edit' with various attributes (the edit itself, the time
stamp, etc.)?
 

How would you store the data in a Python class? Design your class first.



   


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


[Tutor] nested list help

2010-07-27 Thread Vikram K
Suppose i have this nested list:

>>> x
[['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]]
>>> for i in x:
...   print i
...
['NM100', 3, 4, 5, 6, 7]
['NM100', 10, 11, 12, 13]
['NM200', 15, 16, 17]
>>>

how do i obtain from the above the following nested list:

>>> z
[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]
>>> for i in z:
...   print i
...
['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13]
['NM200', 15, 16, 17]
>>>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] nested list help

2010-07-27 Thread Evert Rol
> Suppose i have this nested list:
> 
> >>> x
> [['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]]
> >>> for i in x:
> ...   print i
> ... 
> ['NM100', 3, 4, 5, 6, 7]
> ['NM100', 10, 11, 12, 13]
> ['NM200', 15, 16, 17]
> >>> 
> 
> how do i obtain from the above the following nested list:
> 
> >>> z
> [['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]

You're not saying exactly how you go from one list to the other (ie, what 
criterion you use to combine the inner lists).
You could remove combine the first two lists using extend(), whereby you pass 
all but the first element of the second inner list to extend(): 
list1.extend(list2[1:]). Or simply list1+list2[1:].
If you're not concerned about the order of the elements (though I guess you 
are), you could do things like list(set(list1+list2)).
On the other hand, if you want to combine lists based on their first element, 
consider using dictionaries and extend lists which have the same key. Depending 
on how you create the lists (eg, when reading columns from a file), you can 
actually do this during creationi.
Otherwise, a (double) for-loop and an if-statement would probably work and be 
relatively straightforward, but I guess that would be rather un-Pythonesque.


> >>> for i in z:
> ...   print i
> ... 
> ['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13]
> ['NM200', 15, 16, 17]
> >>> 


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


[Tutor] need help with msvcrt.getch()

2010-07-27 Thread Richard D. Moores
Python 3.1 on Vista.

Please see .

I'm trying to recall what I used to know, thus this simple script. But 'y'
or 'q' do nothing. What's wrong?

Thanks,

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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Tim Golden

On 27/07/2010 15:22, Richard D. Moores wrote:

Python 3.1 on Vista.

Please see.

I'm trying to recall what I used to know, thus this simple script. But 'y'
or 'q' do nothing. What's wrong?


msvcrt.getch returns bytes on Python 3.1; you're comparing against
string. Try

if key == b'y':
  ... etc.

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


Re: [Tutor] nested list help

2010-07-27 Thread Nick Raptis

On 07/27/2010 04:48 PM, Evert Rol wrote:


On the other hand, if you want to combine lists based on their first element, 
consider using dictionaries and extend lists which have the same key. Depending 
on how you create the lists (eg, when reading columns from a file), you can 
actually do this during creationi.



I'm very much with Everet on this one. Your data would be better 
contained in a dictionary as such:

x = {'NM100': [3, 4, 5, 6, 7, 10, 11, 12, 13],
 'NM200': [15, 16, 17]}

Choosing your data structure is, most of the time, as important as the 
rest of your code.


The rest of the time, the format of your input is not something you can 
control so:
If your data is already on a nested list, as it's on your example, you 
can convert them quite easily to a dictionary.
You then can manipulate them with ease, and if you do want them back on 
nested list format, convert them back.


Here's a very quick script I wrote on how to do it.
Please read it through and hit me with questions on what you don't 
understand.


-
input = [['NM100', 3, 4, 5, 6, 7],
 ['NM100', 10, 11, 12, 13],
 ['NM200', 15, 16, 17]]

# Convert and combine the input into a dictionary
output_dict = {}
for entry in input:
key = entry[0]
values = entry[1:]
if key in output_dict:
output_dict[key].extend(values)
else:
output_dict[key] = values
print output_dict

# Convert the dictionary back into a nested list
output = []
for key in output_dict:
entry = output_dict[key]
entry.insert(0, key)
output.append(entry)

print output


Of course, my script is very basic and it doesn't tend to a lot of 
things you'd might want, like eliminating double values or sorting, but 
it should start you on your path.


Nick

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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Nick Raptis

On 07/27/2010 05:22 PM, Richard D. Moores wrote:

Python 3.1 on Vista.

Please see .

I'm trying to recall what I used to know, thus this simple script. But 
'y' or 'q' do nothing. What's wrong?


Thanks,

Dick Moores
   

Hi Dick!
I'm not on Windows here so this might be a wild guess.

From the documentation I gather that msvcrt can work either on binary 
or text mode.
Perhaps you operate in the wrong one and need to switch with 
msvcrt.setmode(/)


Anyway, insert some print lines to see what exactly is it that /getch() 
gets and debug from there


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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Richard D. Moores
On Tue, Jul 27, 2010 at 07:36, Tim Golden  wrote:

> On 27/07/2010 15:22, Richard D. Moores wrote:
>
>> Python 3.1 on Vista.
>>
>> Please see.
>>
>> I'm trying to recall what I used to know, thus this simple script. But 'y'
>> or 'q' do nothing. What's wrong?
>>
>
> msvcrt.getch returns bytes on Python 3.1; you're comparing against
> string. Try
>
> if key == b'y':
>  ... etc.
>

Yes! That did it.

Thank you.

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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Richard D. Moores
On Tue, Jul 27, 2010 at 07:51, Nick Raptis  wrote:

> On 07/27/2010 05:22 PM, Richard D. Moores wrote:
>
>> Python 3.1 on Vista.
>>
>> Please see .
>>
>> I'm trying to recall what I used to know, thus this simple script. But 'y'
>> or 'q' do nothing. What's wrong?
>>
>> Thanks,
>>
>> Dick Moores
>>
>>
> Hi Dick!
> I'm not on Windows here so this might be a wild guess.
>
> From the documentation I gather that msvcrt can work either on binary or
> text mode.
> Perhaps you operate in the wrong one and need to switch with
> msvcrt.setmode(/)
>
> Anyway, insert some print lines to see what exactly is it that /getch()
> gets and debug from there
>

On  I find
 msvcrt.setmode(*fd*,
*flags*)¶
Set
the line-end translation mode for the file descriptor *fd*. To set it to
text mode, *flags* should be
os.O_TEXT;
for binary, it should be
os.O_BINARY
.But I don't understand how to implement this. I've tried <
http://tutoree7.pastebin.com/gYbq67Ja>, but get

C:\P31Working>test_getch1b.py
Traceback (most recent call last):
  File "C:\P31Working\test_getch1b.py", line 7, in 
msvcrt.setmode(1,os._TEXT)
AttributeError: 'module' object has no attribute '_TEXT'

Nick, could you spell out for me what you are suggesting? I like the
possibility of not having to use b'y' instead of 'y'.

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


[Tutor] append in new file??

2010-07-27 Thread Ahmed AL-Masri
hello;

I need help in append a data in new file. the problem is the new data are 
generated in every run.
so at the end I should get all the changes in one file (means for all running) 
if I run 2 times so should get couple of data set.
note all outputs in txt file.
ex:
this is the first run data 
1.0362 1.015 1.011 0.9948 1.0024 0.9936
0.9129 0.9184 0.9868 0.9682 0.975 0.8953
1.0627 1.0517 1.0306 1.0387 1.0501 1.0485
1.0467 1.0342 1.0389 1.0109 1.0107 1.0066

next run the data changes in the same output file 
1.020 1.043 1.111 0.343 1.454 1.999
0.9129 0.9184 0.9868 0.9682 0.975 0.8953
1.0555 1. 1.333 1. 1. 1.
1. 1. 1. 1. 1. 1.

the appended file should be 
1.0362 1.015 1.011 0.9948 1.0024 0.9936
0.9129 0.9184 0.9868 0.9682 0.975 0.8953
1.0627 1.0517 1.0306 1.0387 1.0501 1.0485
1.0467 1.0342 1.0389 1.0109 1.0107 1.0066
1.020 1.043 1.111 0.343 1.454 1.999
0.9129 0.9184 0.9868 0.9682 0.975 0.8953
1.0555 1. 1.333 1. 1. 1.
1. 1. 1. 1. 1. 1.

more run more data will generate.

I tried so many times without any result.
t=open('old data'.txt')
d=open('new data.txt','w')

while True:
line=t.readline()
if len(line) ==0:
  break
volt=line.split()
volt=map(float, volt)
for i in range (len(line.split())):
d.write(str(volt[i])+' ')
d.write('\n')
d.close

but this only bring as same as the old file.

also I found some codes from the committee and tried but still not working.
import sys

def check_dup(fd1):
print fd1
fd1.seek(0,0)
list1=[]
while not done:
x = fd1.readline()
if x == "":
break
else:
list1.append(x)
return list1


fname = 'new data.txt'

fd = open(fname,'a+')
print fd

t=open('old data'.txt'
while not done:
while True:
line=t.readline()
if len(line) ==0:
break
volt=line.split()
volt=map(float, volt)
for i in range (len(line.split())):
print str
flag = check_dup(fd)
print flag
fd.seek(0,2)
fd.write(str(volt[i]) + ' ')
fd.write('\n')
break

looking forward for prompt response,
best regards,

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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Alan Gauld


"Richard D. Moores"  wrote


Please see .

I'm trying to recall what I used to know, thus this simple script. 
But 'y'

or 'q' do nothing. What's wrong?


I don't think you need kbhit() or the sleep(.1)
The if kbhit test will only pass if the key is actually hit at that 
time.

Often it won't be so you will miss it.

I think getch() will wait for a key to be hit.

But thats from memory and I'm too busy to try it out right now.

Alan G. 



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


[Tutor] lambda vs list comp

2010-07-27 Thread Jon Crump
Just as a matter of curiosity piqued by having to understand someone
else's code. Is the difference here just a matter of style, or is one
better somehow than the other?

>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> ','.join([str(x) for x in l])
'0,1,2,3,4,5,6,7,8,9,10'

>>> ','.join(map(lambda x:str(x), l))
'0,1,2,3,4,5,6,7,8,9,10'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append in new file??

2010-07-27 Thread Joel Goldstick
2010/7/27 Ahmed AL-Masri 

>  hello;
>
> I need help in append a data in new file. the problem is the new data are
> generated in every run.
> so at the end I should get all the changes in one file (means for all
> running) if I run 2 times so should get couple of data set.
> note all outputs in txt file.
>

You need to use append mode when you open the file

http://docs.python.org/library/functions.html?highlight=open#open



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


Re: [Tutor] lambda vs list comp

2010-07-27 Thread Mac Ryan
On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote:
> Just as a matter of curiosity piqued by having to understand someone
> else's code. Is the difference here just a matter of style, or is one
> better somehow than the other?
> 
> >>> l
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> 
> >>> ','.join([str(x) for x in l])
> '0,1,2,3,4,5,6,7,8,9,10'
> 
> >>> ','.join(map(lambda x:str(x), l))
> '0,1,2,3,4,5,6,7,8,9,10'

Considering that "readability metters" I would go for none of the two,
but for:

','.join(map(str, l))

(Indeed defining a lambda function that simply uses "str" is redundant)

Just my personal take on this, though!
Mac.

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


Re: [Tutor] lambda vs list comp

2010-07-27 Thread Rich Lovely
On 27 July 2010 17:53, Mac Ryan  wrote:
> On Tue, 2010-07-27 at 09:44 -0700, Jon Crump wrote:
>> Just as a matter of curiosity piqued by having to understand someone
>> else's code. Is the difference here just a matter of style, or is one
>> better somehow than the other?
>>
>> >>> l
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>
>> >>> ','.join([str(x) for x in l])
>> '0,1,2,3,4,5,6,7,8,9,10'
>>
>> >>> ','.join(map(lambda x:str(x), l))
>> '0,1,2,3,4,5,6,7,8,9,10'
>
> Considering that "readability metters" I would go for none of the two,
> but for:
>
> ','.join(map(str, l))
>
> (Indeed defining a lambda function that simply uses "str" is redundant)
>
> Just my personal take on this, though!
> Mac.
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

There's very little difference between the two in the example you're
using, in python 2.x.  It's a different matter if you want to do
something slightly more complex, or of you're using Python 3.

The benefit of list comprehensions is that they can take a number of
iterables and filter clauses:

>>> [x+y for x in range(3) for y in range(3) if x!=y]
[1, 2, 1, 3, 2, 3]

I can't see any easy way of doing something like that using just map,
fliter and so on.

In python 3, map is a different sort of beast.  Instead of returning a
list, it returns an iterable called a "mapping".  It stores the
original list and the function, and only calls the function when you
ask it for the next value.  You can get similar behaviour in python2
using the imap function in the itertools module.  It's extremely
useful if you need to deal with massive lists of millions of elements
- where making the list every time would take too long.

-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Richard D. Moores
On Tue, Jul 27, 2010 at 09:09, Alan Gauld  wrote:

>
> "Richard D. Moores"  wrote
>
>
>  Please see .
>>
>> I'm trying to recall what I used to know, thus this simple script. But 'y'
>> or 'q' do nothing. What's wrong?
>>
>
> I don't think you need kbhit()


I tried taking it out. You're right, Alan, I don't need it. See <
http://tutoree7.pastebin.com/N8hdyqLZ>


> or the sleep(.1)
>

Testing with and without sleep(.001) shows that sleep(.001) cuts the CPU
usage dramatically, from ~50% to ~5%!

The if kbhit test will only pass if the key is actually hit at that time.
> Often it won't be so you will miss it.
>

I didn't see that, but with a bigger loop I may, I suppose.


> I think getch() will wait for a key to be hit.
>
> But thats from memory and I'm too busy to try it out right now.
>

So I tried putting a counter, c, in the loop. See <
http://tutoree7.pastebin.com/9Q8u3wLw>.

It seems you're correct, in that if I hit 'y' 10 times and then 'q' to
close, I'll get "c = 11". 5 'x's and 5 'y's and a 'q' also gets "c = 11", of
course. Out of curiosity, I tried holding down 'z' (i.e., neither 'y' nor
'q' for exactly 10 seconds, and then hit 'q'. Got "c = 267".

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


Re: [Tutor] xml question

2010-07-27 Thread Albert-Jan Roskam
Thank you all for your replies; they were most useful! I've never made a 
connection between xml and mass-suicide  ---interesting. ;-) Yaml seems a good 
choice (I didn't llook at config parser yet). In my office, we use Python and R 
(among other tools) and therfore it seems best not to use some pure python 
config file format. I agree that the readability/editability (by humans) of a 
yaml file is a huge advantage. Today I experimented with the yaml library of R 
and it felt very intuitive. I expect this will be the same for Python (not in 
the last place because Python is such a *beautiful*, clear language ;-)

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?

~~

--- On Tue, 7/27/10, Mac Ryan  wrote:

From: Mac Ryan 
Subject: Re: [Tutor] xml question
To: tutor@python.org
Date: Tuesday, July 27, 2010, 11:46 AM

On Mon, 2010-07-26 at 12:09 -0700, Albert-Jan Roskam wrote:
> I am making a data processing program that will use a configuration
> file. The file should contain information about: (1) source files
> used, (2) (intermediate) output files, (3) used parameters/estimation
> methods (4) manual data edits + datetime stamp + user name . I'd like
> to store this config file in xml. However, I've never created
> something like this before. Is this a suitable format, and, if so,
> what would the elementtree look like? Should I just use 'config'  or
> something similar as root, and the information elements 1 through 3 as
> child elements? And should the manual edits be stored as an element
> 'edit' with various attributes (the edit itself, the time stamp,
> etc.)?

I am with Steven on the fact that XML might not necessarily be the best
choice, unless you plan to use the configuration file with other
third-party programs, in which case the fact that XML has built-in
parsing libs for nearly all languages makes life of fellow developer
easier.

For the next project of mines, I am planning to use YAML (YAML Ain't a
Markup Language). I stumbled upon this format while toying around with
the google app engine, that uses it for storing your application
configuration data. IMO, YAML has the following prominent advantages:
   1) It is easy to read and edit by humans [think "markdown"]
   2) It has solid parsing libraries for Python
   3) It is far less verbose than XML
   4) It is consistent with Python "relevant whitespaces" [indentation
is used to define data structure hierarchy]

I have not yet got to the stage of writing code that use YAML (will take
two more weeks at best for me to get to that point), but should you go
down this road, I would love to hear back from you. [As I would love to
hear from anybody else who might have experience with YAML+Python]

Some links:
- http://en.wikipedia.org/wiki/YAML [good overview of the format]
- http://www.yaml.org/ [official site... when consistency with the
format makes a website hard to browse!]
- http://pyyaml.org/wiki/PyYAMLDocumentation [documentation of the
library for Python - pure Python code // can use a C library]

An example of YAML file to give you the taste of it:
> receipt:     Oz-Ware Purchase Invoice
> date:        2007-08-06
> 
> customer:
>     given:   Dorothy
>     family:  Gale
> 
> items:
>     - part_no:   A4786
>       descrip:   Water Bucket (Filled)
>       price:     1.47
>       quantity:  4
>     - part_no:   E1628
>       descrip:   High Heeled "Ruby" Slippers
>       price:     100.27
>       quantity:  1
> 
> bill-to:  &id001
>     street: |
>             123 Tornado Alley
>             Suite 16
>     city:   East Westville
>     state:  KS
> 
> ship-to:  *id001
> 
> specialDelivery:  >
>     Follow the Yellow Brick
>     Road to the Emerald City.
>     Pay no attention to the
>     man behind the curtain.

HTH,
Mac.


___
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] nested list help

2010-07-27 Thread Gerard Flanagan

Vikram K wrote:

Suppose i have this nested list:

 >>> x
[['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]]
 >>> for i in x:
...   print i
...
['NM100', 3, 4, 5, 6, 7]
['NM100', 10, 11, 12, 13]
['NM200', 15, 16, 17]
 >>>

how do i obtain from the above the following nested list:

 >>> z
[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]
 >>> for i in z:
...   print i
...
['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13]
['NM200', 15, 16, 17]
 >>>




z = [['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 
16, 17]]


from itertools import groupby

# assumes that the data is already sorted by the first element as it is here
# if not then you must sort, eg. with the same function you group by:
# z.sort(key=lambda X: X[0])
# also assumes that there are no empty lists

def merge(inlist):
for k, g in groupby(inlist, lambda X: X[0]):
k = [k]
for item in g:
k.extend(item[1:])
yield k

print list(merge(z))

[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]

def merge(inlist):
for k, g in groupby(inlist, lambda X: X[0]):
newlist = []
for item in g:
newlist.extend(item[1:])
yield k, newlist

print dict(merge(z))

{'NM100': [3, 4, 5, 6, 7, 10, 11, 12, 13], 'NM200': [15, 16, 17]}

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


Re: [Tutor] need help with msvcrt.getch()

2010-07-27 Thread Richard D. Moores
On Tue, Jul 27, 2010 at 07:51, Nick Raptis  wrote:

> On 07/27/2010 05:22 PM, Richard D. Moores wrote:
>
>> Python 3.1 on Vista.
>>
>> Please see .
>>
>> I'm trying to recall what I used to know, thus this simple script. But 'y'
>> or 'q' do nothing. What's wrong?
>>
>> Thanks,
>>
>> Dick Moores
>>
>>
> Hi Dick!
> I'm not on Windows here so this might be a wild guess.
>
> From the documentation I gather that msvcrt can work either on binary or
> text mode.
> Perhaps you operate in the wrong one and need to switch with
> msvcrt.setmode(/)
>
> Anyway, insert some print lines to see what exactly is it that /getch()
> gets and debug from there
>
> Nick
>

I replied to only to Nick, by mistake, and after some private back and
forth, he came up with another way to avoid having to use those b's, as in
b'y' and b'q'. It works! See ,
highlighted line 11.

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


[Tutor] Running .py files in shell

2010-07-27 Thread Kevin Rapley
 Hello all, I am new to programming and have opted to learn Python as I 
hear it is a good first language. I am currently going through the 
exercises in 'How to Think Like a Computer Scientist: Learning with 
Python 2nd Edition'.


I am running Mac OSX v10.6.4 Snow Leopard
I am running Python 2.6.1

I have a couple of questions:

1. How do I execute .py files in the command line shell? I have my files 
in /Users/Kevin/python-exercises/ and am opening python in shell from 
that directory


2. How do I install GASP which is referenced in chapter 4? The 
instructions in this book are not complete. I have researched and found 
a reference on Stack Overflow which is contributed to by GASP programmers

http://stackoverflow.com/questions/1024862/how-do-i-install-gasp-for-python-2-6-2-on-a-mac

I have MacPorts installed and have attempted to install PyObjC, which is 
referenced as a prerequisite to PyGame and GASP. I get the following 
error when attempting to install PyObjC:


sudo port install py-pyobjc
Password:
--->  Computing dependencies for py-pyobjc
--->  Fetching py-pyobjc
Error: Target org.macports.fetch returned: PyObjC 1.4 is for Mac OS X 
10.4 and lower. On 10.5, use py-pyobjc2 instead.
Log for py-pyobjc is at: 
/opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_python_py-pyobjc/main.log

Error: Status 1 encountered during processing.

It says to use py-objc2 instead, so I do so and get this error:

sudo port install py-pyobjc2
Error: Target org.macports.activate returned: Image error: 
/opt/local/lib/python2.4/site-packages/modulegraph/__init__.py is being 
used by the active py-modulegraph port.  Please deactivate this port 
first, or use 'port -f activate py-modulegraph-devel' to force the 
activation.
Log for py-modulegraph-devel is at: 
/opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_python_py-modulegraph-devel/main.log
Error: The following dependencies failed to build: py-py2app-devel 
py-modulegraph-devel

Error: Status 1 encountered during processing.
To report a bug, see 

I attempted to correct the issue by entering the following:

port -f activate py-modulegraph-devel

But it returned this error:

Warning: Failed to open Portfile from registry for py-modulegraph-devel 
@0.7.2_0

--->  Activating py-modulegraph-devel
Error: port activate failed: sqlite error: attempt to write a readonly 
database (8)


This is as far as I have got with resolving this issue so far. All help 
and guidance will be greatly appreciated.


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


[Tutor] Decorator listing

2010-07-27 Thread Mary Morris
I'm writing a program with python that compiles a list of decorators from my
company's source code.  I'm new to python and need help with the following:
I'm trying to make the program find ".com" in any lines and not include them
in the list of decorators that gets returned.  THis is because I had the
program look for the @ symbol to find the decorators and it returned email
addresses in the list.  What I was thinking was to do something like this:
k = candidate_line.find(".com")
if k != -1:
 candidate_line != candidate_line

The problem is that I know this won't work because ".com" can't be in the
last slot of the candidate line...  So far the part of my code that needs to
be changed looks like this...


  #parse out the names of the decorators from those lines
return_decorators= []
for ele in subset_lines:
candidate_line, line_number = ele
candidate_line = candidate_line.strip()
i = candidate_line.find("(")
j = candidate_line.find("#")
k = candidate_line.find(".com")
#if () is in the last spot
if i == -1:
#if () is in the last spot and the decorator is in a
comment
if j == 0:
#get rid of ( and #
candidate_line = candidate_line[2:i]
#if () is in the last spot and the decorator is not
in a comment
elif j != 0:
candidate_line = candidate_line[1:i]
#if there are not ()'s in the last spot
elif i != -1:
#if there are not ()'s, but the decorator is in a
comment
if j == 0:
candidate_line = candidate_line[2:]
#if there are not ()'s and the decorator isn't in a
comment
elif j != 0:
candidate_line = candidate_line[1:]
elif k in candidate_line:
candidate_line != candidate_line
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lambda vs list comp

2010-07-27 Thread Steven D'Aprano
On Wed, 28 Jul 2010 02:44:02 am Jon Crump wrote:
> Just as a matter of curiosity piqued by having to understand someone
> else's code. Is the difference here just a matter of style, or is one
> better somehow than the other?

Remember that list comprehensions didn't exist until a few years ago, so 
older code will use map() to implement the same functionality.

> >>> l
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using lowercase L on its own as a name is not recommended, because it 
looks too similar to 1 and hence difficult to read. For example, the 
next line reads to me like "blah blah for x in ONE", I do a 
double-take, read it again, and realise it's actually L.


> >>> ','.join([str(x) for x in l])
> '0,1,2,3,4,5,6,7,8,9,10'

This would be the preferred (or "Pythonic") way to do this for most 
people these days. That, or a generator expression:

','.join(str(x) for x in l)

Using the list comp form is a micro-optimization over the generator 
expression form (it allows join to allocate the memory needed ahead of 
time, since it has a known size).


> >>> ','.join(map(lambda x:str(x), l))
>
> '0,1,2,3,4,5,6,7,8,9,10'


That is a line of code written by somebody who doesn't quite get it.

There seems to be something about map and lambda that leads so many 
people into making that silly mistake when they wouldn't make it 
elsewhere. You (generic you, not you personally) almost certainly 
wouldn't write:

def mystr(x):
return str(x)

n = 42
s = mystr(n)


instead of the more obvious s = str(n). But bring map() into it, and 
people make that exact mistake, writing:

map(lambda x: str(x), L)

instead of the simpler and faster:

map(str, L)



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


Re: [Tutor] Decorator listing

2010-07-27 Thread Emile van Sebille

On 7/27/2010 1:11 PM Mary Morris said...

I'm writing a program with python that compiles a list of decorators from my
company's source code.  I'm new to python and need help with the following:
I'm trying to make the program find ".com" in any lines and not include them
in the list of decorators that gets returned.  THis is because I had the
program look for the @ symbol to find the decorators and it returned email
addresses in the list.


You can probably sidestep this issue by looking for the @ symbol in the 
first non-whitespace position in the line, rather than in any position 
of the line.


IE, instead of something like:

  if "@" in line:

...try something more like:

  if line.strip().startswith("@"):


This way you won't pick up the email addresses at all, and won't 
subsequently need to filter them out of your results set.


HTH,

Emile



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


[Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.

They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want the
program to show me the actual couples: 'manman', 'manbat', 'mansuper',
'manultra', 'batbat', 'batman', 'batsuper', etc.

But hey, why building up new words from just two strings? I also want to
know the possible combinations of three words, four words, and perhaps, why
not, five words.

So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
then what? I don't know how to say: take every element and paste it to
another one from the bag, and with another one, and with another one,...

If it's too complex, I dont need the whole code recipe, just need some
clues, or perhaps a useful link

Thank you very much in advance!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorator listing

2010-07-27 Thread Steven D'Aprano
Hello Mary,

On Wed, 28 Jul 2010 06:11:18 am Mary Morris wrote:
> I'm writing a program with python that compiles a list of decorators
> from my company's source code.  I'm new to python and need help with
> the following: I'm trying to make the program find ".com" in any 
> lines and not include them in the list of decorators that gets
> returned.  

What's special about .com? What about email addresses that end 
in .org, .co.uk, .net. .gov.au, .cx, .info, .biz, or any one of a 
million million other email addresses?


> THis is because I had the program look for the @ symbol to 
> find the decorators and it returned email addresses in the list. 

With respect Mary, I replied to your last question about this on 
Saturday. Did you read that? Was it useful? Did you have questions? I 
think the approach you are taking is the wrong approach and is doomed 
to failure. I fear that you are taking a problem that can be solved 
very simply, and making it much, much, much more complicated than it 
needs to be.

Please read my email sent in response to your last question about this, 
and good luck!



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


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread Nick Raptis

On 07/28/2010 01:20 AM, ZUXOXUS wrote:

Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 
'ultra'.


They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want 
the program to show me the actual couples: 'manman', 'manbat', 
'mansuper', 'manultra', 'batbat', 'batman', 'batsuper', etc.


But hey, why building up new words from just two strings? I also want 
to know the possible combinations of three words, four words, and 
perhaps, why not, five words.


So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... 
but then what? I don't know how to say: take every element and paste 
it to another one from the bag, and with another one, and with another 
one,...


If it's too complex, I dont need the whole code recipe, just need some 
clues, or perhaps a useful link


Thank you very much in advance!



Take a look in the itertools module 
http://docs.python.org/library/itertools.html
Check the section "*Combinatoric generators:" (website doesn't have an 
anchor link for that, search around a bit)


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


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread Mark Lawrence

On 27/07/2010 23:20, ZUXOXUS wrote:

Hi all pythoners

I've got a probably easy to answer question.

Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.

They are in a list, or in a sequence or whatever, say a bag of words

And now I want to know how many couples I can do with them, and I want the
program to show me the actual couples: 'manman', 'manbat', 'mansuper',
'manultra', 'batbat', 'batman', 'batsuper', etc.

But hey, why building up new words from just two strings? I also want to
know the possible combinations of three words, four words, and perhaps, why
not, five words.

So, is it easy to do?

Sorry, I'm new in programing, and am probably far from being a math-master

I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
then what? I don't know how to say: take every element and paste it to
another one from the bag, and with another one, and with another one,...

If it's too complex, I dont need the whole code recipe, just need some
clues, or perhaps a useful link

Thank you very much in advance!

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


The lazy way.
http://docs.python.org/library/itertools.html
Look for combinations().

HTH.

Mark Lawrence.


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


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread Mac Ryan
On Tue, 2010-07-27 at 23:31 +0100, Mark Lawrence wrote:
> On 27/07/2010 23:20, ZUXOXUS wrote:
> > Hi all pythoners
> >
> > I've got a probably easy to answer question.
> >
> > Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'.
> >
> > They are in a list, or in a sequence or whatever, say a bag of words
> >
> > And now I want to know how many couples I can do with them, and I want the
> > program to show me the actual couples: 'manman', 'manbat', 'mansuper',
> > 'manultra', 'batbat', 'batman', 'batsuper', etc.
> >
> > But hey, why building up new words from just two strings? I also want to
> > know the possible combinations of three words, four words, and perhaps, why
> > not, five words.
> >
> > So, is it easy to do?
> >
> > Sorry, I'm new in programing, and am probably far from being a math-master
> >
> > I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
> > then what? I don't know how to say: take every element and paste it to
> > another one from the bag, and with another one, and with another one,...
> >
> > If it's too complex, I dont need the whole code recipe, just need some
> > clues, or perhaps a useful link
> >
> > Thank you very much in advance!
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> 
> The lazy way.
> http://docs.python.org/library/itertools.html
> Look for combinations().

>From the examples listed by the OP (see: "manbat", "batman") I actually
believe he is looking for the cartesian product.

The syntax should therefore be (DISCLAIMER: I am going by memory without
having the possibility of testing right now):

import itertools
for prod in itertools.product(my_list_of_words, 2):
print prod

(The two as second parameter tells that you are wishing to have a
bi-dimensional product, with both axis having the same list of words.
See docs for more info)

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


Re: [Tutor] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Sharp thanks, but:

I try to reproduce the example from the table, but:

>>> import itertools
>>> combinations('ABC', 2)
Traceback (most recent call last):
  File "", line 1, in 
combinations('ABC', 2)
NameError: name 'combinations' is not defined
>>>

If im not mistaken, it should return AB, AC, BA, etc.

I'm using Python 3.1


2010/7/28 Mark Lawrence 

> On 27/07/2010 23:20, ZUXOXUS wrote:
>
>> Hi all pythoners
>>
>> I've got a probably easy to answer question.
>>
>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>> 'ultra'.
>>
>> They are in a list, or in a sequence or whatever, say a bag of words
>>
>> And now I want to know how many couples I can do with them, and I want the
>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>
>> But hey, why building up new words from just two strings? I also want to
>> know the possible combinations of three words, four words, and perhaps,
>> why
>> not, five words.
>>
>> So, is it easy to do?
>>
>> Sorry, I'm new in programing, and am probably far from being a math-master
>>
>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but
>> then what? I don't know how to say: take every element and paste it to
>> another one from the bag, and with another one, and with another one,...
>>
>> If it's too complex, I dont need the whole code recipe, just need some
>> clues, or perhaps a useful link
>>
>> Thank you very much in advance!
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
> The lazy way.
>
> http://docs.python.org/library/itertools.html
> Look for combinations().
>
> HTH.
>
> Mark Lawrence.
>
>
>
> ___
> 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] Running .py files in shell

2010-07-27 Thread Alan Gauld

"Kevin Rapley"  wrote


I am running Mac OSX v10.6.4 Snow Leopard
I am running Python 2.6.1


In general get the MacPython distributions of Python etc, they usually
install easier (ie from the GUI) than the "Unix" based versions.

1. How do I execute .py files in the command line shell? I have my 
files in /Users/Kevin/python-exercises/ and am opening python in 
shell from that directory


There are numerous ways, especially in MacOS.
The simplest way is to put a "shebang" line at the top of your script
and then make them executable:

$ cat > myscript.py
#! /bin/env python
# the above line must be the first line in your file and tells the
# shell where to find python
# rest of your code follows.

Then use chmod +x to make it executable

$ chmod +x myscript.py

Now you can run it:

$ myscript.py

Alternatively you can just call python explicitly:

$ python myscript.py

You can do other things with file associations and
MacOS bundles/packages etc but unless its a major app
that you use a lot the techniques above are easier.


2. How do I install GASP which is referenced in chapter 4?


Can't help you there, sorry...

I have MacPorts installed and have attempted to install PyObjC, 
which is referenced as a prerequisite to PyGame and GASP. I get the 
following error when attempting to install PyObjC:


Check the MacPython web site, I'm sure I used an install
package when I played with PyObjC...

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] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Mac,

this is what I get:

>>> for prod in itertools.product('ABC', 2):
print(prod)

Traceback (most recent call last):
  File "", line 1, in 
for prod in itertools.product('ABC', 2):
TypeError: 'int' object is not iterable


hmm, what might be that 'int' object? 2?


2010/7/28 ZUXOXUS 

> Sharp thanks, but:
>
> I try to reproduce the example from the table, but:
>
> >>> import itertools
> >>> combinations('ABC', 2)
> Traceback (most recent call last):
>   File "", line 1, in 
> combinations('ABC', 2)
> NameError: name 'combinations' is not defined
> >>>
>
> If im not mistaken, it should return AB, AC, BA, etc.
>
> I'm using Python 3.1
>
>
> 2010/7/28 Mark Lawrence 
>
> On 27/07/2010 23:20, ZUXOXUS wrote:
>>
>>> Hi all pythoners
>>>
>>> I've got a probably easy to answer question.
>>>
>>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>>> 'ultra'.
>>>
>>> They are in a list, or in a sequence or whatever, say a bag of words
>>>
>>> And now I want to know how many couples I can do with them, and I want
>>> the
>>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>>
>>> But hey, why building up new words from just two strings? I also want to
>>> know the possible combinations of three words, four words, and perhaps,
>>> why
>>> not, five words.
>>>
>>> So, is it easy to do?
>>>
>>> Sorry, I'm new in programing, and am probably far from being a
>>> math-master
>>>
>>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
>>> but
>>> then what? I don't know how to say: take every element and paste it to
>>> another one from the bag, and with another one, and with another one,...
>>>
>>> If it's too complex, I dont need the whole code recipe, just need some
>>> clues, or perhaps a useful link
>>>
>>> Thank you very much in advance!
>>>
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>> The lazy way.
>>
>> http://docs.python.org/library/itertools.html
>> Look for combinations().
>>
>> HTH.
>>
>> Mark Lawrence.
>>
>>
>>
>> ___
>> 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] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread ZUXOXUS
Oh, I think i got it:

>>> for prod in itertools.product('ABC', 'ABC'):
print(prod)

('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')

Thank you very much!!

2010/7/28 ZUXOXUS 

> Mac,
>
> this is what I get:
>
> >>> for prod in itertools.product('ABC', 2):
> print(prod)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> for prod in itertools.product('ABC', 2):
> TypeError: 'int' object is not iterable
>
>
> hmm, what might be that 'int' object? 2?
>
>
> 2010/7/28 ZUXOXUS 
>
> Sharp thanks, but:
>>
>> I try to reproduce the example from the table, but:
>>
>> >>> import itertools
>> >>> combinations('ABC', 2)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> combinations('ABC', 2)
>> NameError: name 'combinations' is not defined
>> >>>
>>
>> If im not mistaken, it should return AB, AC, BA, etc.
>>
>> I'm using Python 3.1
>>
>>
>> 2010/7/28 Mark Lawrence 
>>
>> On 27/07/2010 23:20, ZUXOXUS wrote:
>>>
 Hi all pythoners

 I've got a probably easy to answer question.

 Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
 'ultra'.

 They are in a list, or in a sequence or whatever, say a bag of words

 And now I want to know how many couples I can do with them, and I want
 the
 program to show me the actual couples: 'manman', 'manbat', 'mansuper',
 'manultra', 'batbat', 'batman', 'batsuper', etc.

 But hey, why building up new words from just two strings? I also want to
 know the possible combinations of three words, four words, and perhaps,
 why
 not, five words.

 So, is it easy to do?

 Sorry, I'm new in programing, and am probably far from being a
 math-master

 I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
 but
 then what? I don't know how to say: take every element and paste it to
 another one from the bag, and with another one, and with another one,...

 If it's too complex, I dont need the whole code recipe, just need some
 clues, or perhaps a useful link

 Thank you very much in advance!

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

>>>
>>> The lazy way.
>>>
>>> http://docs.python.org/library/itertools.html
>>> Look for combinations().
>>>
>>> HTH.
>>>
>>> Mark Lawrence.
>>>
>>>
>>>
>>> ___
>>> 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] Calculating and returning possible combinations of elements from a given set

2010-07-27 Thread Dave Angel



ZUXOXUS wrote:

Oh, I think i got it:

  

for prod in itertools.product('ABC', 'ABC'):


print(prod)

('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')

Thank you very much!!

2010/7/28 ZUXOXUS 
  

You're top-posting, which loses all the context. In this forum, put your 
comments after whatever lines you're quoting.


Your latest version gets the product of two.  But if you want an 
arbitrary number, instead of repeating the iterable ('ABC' in your 
case), you can use a repeat count.  That's presumably what you were 
trying to do in your earlier incantation.  But you forgot the 'repeat' 
keyword:


for prod in itertools.product('ABC', repeat=4):
   

will give you all the four-tuples.

DaveA




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


[Tutor] subprocess output

2010-07-27 Thread ashley
I'm using Python 2.6.5 and I've got a challenge with the subprocess
module. I'd like the output to be stored in a variable, and not sent to
the stdout. The relevant lines as they are now:

#!/usr/bin/env python
import subprocess
import sys

dom = sys.argv[1]
switch = sys.argv [2]
answer = subprocess.call("whois " + dom, shell=True)

Execute the above (whatever.py -example.com -a1) prints the entire WHOIS
output, but I just want it saved to the variable 'answer', nothing more.

Changing 'shell=True' to 'shell=False' raises an exception and removing
the 'shell=' altogether is troublesome as well.

Thoughts on how I can accomplish this?

Thanks!


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