[Tutor] Dictionary - count values where values are stored as a list

2007-09-30 Thread GTXY20
Hello,

Any way to display the count of the values in a dictionary where the values
are stored as a list? here is my dictionary:

 {'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], '4':
['a', 'c']}

I would like to display count as follows and I would not know all the value
types in the values list:

Value QTY
a   4
b   3
c   4

Also is there anyway to display the count of the values list combinations so
here again is my dictionary:
{'1': ['a', 'b', 'c'], '3': ['a', 'b', 'c'], '2': ['a', 'b', 'c'], '4':
['a', 'c']}


And I would like to display as follows

QTY Value List Combination
3  a,b,c
1  a,c

Once again all help is much appreciated.

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


Re: [Tutor] Really basic web templating

2007-09-30 Thread Steve Willoughby
wormwood_3 wrote:
> Well yes and no:-) This sort of application would fall under the 
> sprawling category of CGI, yes, and I can use Python scripts on my web 
> server, so it is supported. But nearly every tutorial I have seen 
> regarding Python and CGI only have to do with form submissions, doing 
> calculations and other things with data sent from webpages to Python 
> scripts. But that is not really what I want to do. I am wondering what a 
> script would need to do to take requests for pages on my site, and 
> generate them from templates. I am not sure if I can do this without 
> having access to Apache rewrite rules, etc.
> 
> Perhaps this is just another area of CGI that I missed and have not seen 
> tutorials on. If it is and you have seen some, please share!
> 

Yes, it's just another area of CGI that you missed.  Things like wikis 
do this, for one example.  If you request a URL you get a script which 
serves the page if it exists, or creates a blank one if it doesn't.

You can use Python's string template module as an easy way to format up 
variable text into style templates you set up as text files, or use a 
database backend, or whatever.

--steve

> -Sam
> 
> 
> - Original Message 
> From: Ian Witham <[EMAIL PROTECTED]>
> To: wormwood_3 <[EMAIL PROTECTED]>
> Cc: Python Tutorlist 
> Sent: Sunday, September 30, 2007 11:52:38 PM
> Subject: Re: [Tutor] Really basic web templating
> 
> 
> 
> On 10/1/07, *wormwood_3* <[EMAIL PROTECTED] 
> > wrote:
> 
> Hello all,
> 
> I am trying to think of a way to make this happen, but it may not be
> at all possible:-) I would like to use Python to generate pages on
> demand for my website. By this I mean, when someone hits
> www.mysite.com/pagex.html , I want
> to generate the page pagex.html via a Python script.
> 
> I have a script setup that can generate a page from a template file
> and a file with the body and title of the page in particular I want
> to generate. Now from this, I need to somehow be able to respond to
> requests, and then generate the page based on the page requested. I
> want to do this because my site is on a shared hosting account, so I
> cannot install a web framework like Django, and the site's pages
> will be rather simple. At the same time, I would like to use
> templating, so I do not have repeated identical code between the pages.
> 
> Any ideas on this?
> 
> 
> It sounds like you want to use CGI. I think virtually all web servers 
> support it and there are a ton of CGI tutorials on the web
> 
> Ian.
> 
> 
> 
> 
> ___
> 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] Really basic web templating

2007-09-30 Thread wormwood_3
Well yes and no:-) This sort of application would fall under the sprawling 
category of CGI, yes, and I can use Python scripts on my web server, so it is 
supported. But nearly every tutorial I have seen regarding Python and CGI only 
have to do with form submissions, doing calculations and other things with data 
sent from webpages to Python scripts. But that is not really what I want to do. 
I am wondering what a script would need to do to take requests for pages on my 
site, and generate them from templates. I am not sure if I can do this without 
having access to Apache rewrite rules, etc.

Perhaps this is just another area of CGI that I missed and have not seen 
tutorials on. If it is and you have seen some, please share!

-Sam


- Original Message 
From: Ian Witham <[EMAIL PROTECTED]>
To: wormwood_3 <[EMAIL PROTECTED]>
Cc: Python Tutorlist 
Sent: Sunday, September 30, 2007 11:52:38 PM
Subject: Re: [Tutor] Really basic web templating



On 10/1/07, wormwood_3 <[EMAIL PROTECTED]> wrote:
Hello all,

I am trying to think of a way to make this happen, but it may not be at all 
possible:-) I would like to use Python to generate pages on demand for my 
website. By this I mean, when someone hits 
www.mysite.com/pagex.html, I want to generate the page pagex.html via a Python 
script.

I have a script setup that can generate a page from a template file and a file 
with the body and title of the page in particular I want to generate. Now from 
this, I need to somehow be able to respond to requests, and then generate the 
page based on the page requested. I want to do this because my site is on a 
shared hosting account, so I cannot install a web framework like Django, and 
the site's pages will be rather simple. At the same time, I would like to use 
templating, so I do not have repeated identical code between the pages.


Any ideas on this?
It sounds like you want to use CGI. I think virtually all web servers support 
it and there are a ton of CGI tutorials on the web

Ian.






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


Re: [Tutor] Really basic web templating

2007-09-30 Thread Ian Witham
On 10/1/07, wormwood_3 <[EMAIL PROTECTED]> wrote:
>
> Hello all,
>
> I am trying to think of a way to make this happen, but it may not be at
> all possible:-) I would like to use Python to generate pages on demand for
> my website. By this I mean, when someone hits www.mysite.com/pagex.html, I
> want to generate the page pagex.html via a Python script.
>
> I have a script setup that can generate a page from a template file and a
> file with the body and title of the page in particular I want to generate.
> Now from this, I need to somehow be able to respond to requests, and then
> generate the page based on the page requested. I want to do this because my
> site is on a shared hosting account, so I cannot install a web framework
> like Django, and the site's pages will be rather simple. At the same time, I
> would like to use templating, so I do not have repeated identical code
> between the pages.
>
> Any ideas on this?


It sounds like you want to use CGI. I think virtually all web servers
support it and there are a ton of CGI tutorials on the web

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


[Tutor] Really basic web templating

2007-09-30 Thread wormwood_3
Hello all,

I am trying to think of a way to make this happen, but it may not be at all 
possible:-) I would like to use Python to generate pages on demand for my 
website. By this I mean, when someone hits www.mysite.com/pagex.html, I want to 
generate the page pagex.html via a Python script.

I have a script setup that can generate a page from a template file and a file 
with the body and title of the page in particular I want to generate. Now from 
this, I need to somehow be able to respond to requests, and then generate the 
page based on the page requested. I want to do this because my site is on a 
shared hosting account, so I cannot install a web framework like Django, and 
the site's pages will be rather simple. At the same time, I would like to use 
templating, so I do not have repeated identical code between the pages.

Any ideas on this?

Thanks,
Sam


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


[Tutor] How to Practice Python?(Linpeiheng)

2007-09-30 Thread 林培恒
I am learning Python. Do you know somewhere I can practice Python? I 
means some sites where have exercises I can try solving. And there should be 
standard answer or other people's answer that can be refered.


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


Re: [Tutor] [tutor] creating image from a given data in wxpython

2007-09-30 Thread Ian Witham
On 10/1/07, Varsha Purohit <[EMAIL PROTECTED]> wrote:
>
> Hi All,
> I basically wanna create a bitmap image in python. This will be
> basically in pixels. X and y coordinates will be specified in the program
> and python should create an image by matching the colours. I came across a
> function called wx.ImageFromData which book claims that it creates a
> bitmap image in pixels. But i am not getting proper implementation of this
> function on net...



Hi Varsha,

I have not delved too deeply into WX, but I have made bitmaps from data
using TKinter's canvas.
I found it to be a very slow way to do things (both in programming and in
execution speed) and I ended up using the Python Image Library (PIL)
instead.

If you don't really need the GUI capabilities of WX for this project then
PIL is probably a better tool for the job.

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


Re: [Tutor] [tutor] creating image from a given data in wxpython

2007-09-30 Thread Varsha Purohit
Hi All,
I basically wanna create a bitmap image in python. This will be
basically in pixels. X and y coordinates will be specified in the program
and python should create an image by matching the colours. I came across a
function called wx.ImageFromData which book claims that it creates a bitmap
image in pixels. But i am not getting proper implementation of this function
on net...

thanks,,
Varsha
On 9/30/07, Varsha Purohit <[EMAIL PROTECTED]> wrote:
>
> Hello All,
>
>  I want to create a wxpython program where i am reading a list having
> integer values like [1,2,3,4]. and i need to display the output value as
> bitmap image which shd be coloured after reading the values. Like 1=red,
> 2=yellow, 3=orange etc and it displays the output in colours at proper
> coordinates by matching values of the array. I need to make a script file
> for arcgis tool which converts the ascii data to a coloured bitmap image at
> given coordinates.
>
> thanks,
> --
> Varsha Purohit,
> Graduate Student,
> San Diego State University




-- 
Varsha Purohit,
Graduate Student,
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [tutor] printing bitmap image dynamically reading data in wxpython

2007-09-30 Thread Varsha Purohit
Hello All,

 I want to create a wxpython program where i am reading a list having
integer values like [1,2,3,4]. and i need to display the output value as
bitmap image which shd be coloured after reading the values. Like 1=red,
2=yellow, 3=orange etc and it displays the output in colours at proper
coordinates by matching values of the array. I need to make a script file
for arcgis tool which converts the ascii data to a coloured bitmap image at
given coordinates.

thanks,
-- 
Varsha Purohit,
Graduate Student,
San Diego State University
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Martin Walsh
wormwood_3 wrote:
> Those examples were a lot of help Martin. Turned out the only issue was that 
> I did not have this line right:
> 
> print "Content-type: text/html\n\n"

Sam,

Glad the examples helped. I should probably fess up and point out that I
mistakenly added an additional newline to that print statement. You only
need one, as the print itself will append an extra. So by adding the
addition \n, a blank line will be sent to the browser as part of the
page content, which shouldn't be a problem for text/html content-types.
But as soon as you want to serve up another content-type (eg. image/png)
you may run into odd problems.

So, the corrected print statement should look like this:

print "Content-type: text/html\n"

or as described in the docs, http://docs.python.org/lib/cgi-intro.html

print "Content-type: text/html"
print

HTH,
Marty

> 
> With that form, it loaded just fine. It had been running fine from the 
> terminal, but without this line being right, Apache did not know what to do 
> with it.
> 
> I had spoken with my web-hosting provider, but since I had a shared account I 
> was unable to view the server logs. And the person helping me knew nothing 
> about Python (he kept slipping and calling it PHP actually, to my dismay and 
> chagrin:-) ).
> 
> Thanks for all the help, Alan and Martin.
> 
> -Sam
> 
> _
> - Original Message 
> From: Martin Walsh <[EMAIL PROTECTED]>
> To: tutor@python.org
> Sent: Sunday, September 30, 2007 1:07:02 PM
> Subject: Re: [Tutor] CGI File Woes
> 
> No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
> least two instances when you will not get the pretty printed tracebacks
> in the browser when using cgitb. One is after, what I would call, a
> 'compile time' exception such as SyntaxError, in your python code. The
> other is when the python code runs without exception, but you have not
> separated the header and the document content with a newline. At least,
> I have found these to be true when using apache-cgi.
> 
> Consider the following examples:
> 
> #!/usr/bin/env python
> # raises a SyntaxError
> 
> import cgi
> import cgitb; cgitb.enable()
> 
> print "Content-type: text/html\n\n"
> # NOTE: purposeful misspelling of the print statement
> prin "Hello, world!"
> 
> # the code above will produce a server 500, with apache
> # complaining about "premature end of script headers"

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


Re: [Tutor] creating the equivalent of string.strip()

2007-09-30 Thread Bob Nienhuis
BTW, GMail brings up some interesting sponsored links when the subject line
has string.strip in it ( :-0)

On 9/30/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
>
> "wesley chun" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >> > I'm not sure how to proceed.  My biggest stumbling
> >> > block is how to detect the leading and trailing
> >> > whitespace.
> >>
> >> Use indexing.
> >> Try using a while loop.
> >> Or maybe two?
> >>
> >> And strings have an isspace method...
> >
> > unfortunately, the isspace() string method only returns True if all
> > chars in the string are whitespace chars and False otherwise, so
> > that's a bummer.
>
> But the string can be one character long:
>
> s = 'fred\n'
> print s[-1].isspace()  # --> True
>
> :-)
>
> Alan G
>
>
> ___
> 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] CGI File Woes

2007-09-30 Thread wormwood_3
Those examples were a lot of help Martin. Turned out the only issue was that I 
did not have this line right:

print "Content-type: text/html\n\n"

With that form, it loaded just fine. It had been running fine from the 
terminal, but without this line being right, Apache did not know what to do 
with it.

I had spoken with my web-hosting provider, but since I had a shared account I 
was unable to view the server logs. And the person helping me knew nothing 
about Python (he kept slipping and calling it PHP actually, to my dismay and 
chagrin:-) ).

Thanks for all the help, Alan and Martin.

-Sam

_
- Original Message 
From: Martin Walsh <[EMAIL PROTECTED]>
To: tutor@python.org
Sent: Sunday, September 30, 2007 1:07:02 PM
Subject: Re: [Tutor] CGI File Woes

No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
least two instances when you will not get the pretty printed tracebacks
in the browser when using cgitb. One is after, what I would call, a
'compile time' exception such as SyntaxError, in your python code. The
other is when the python code runs without exception, but you have not
separated the header and the document content with a newline. At least,
I have found these to be true when using apache-cgi.

Consider the following examples:

#!/usr/bin/env python
# raises a SyntaxError

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html\n\n"
# NOTE: purposeful misspelling of the print statement
prin "Hello, world!"

# the code above will produce a server 500, with apache
# complaining about "premature end of script headers"

...

#!/usr/bin/env python
# good python, bad data

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html"
print "Hello, world!"

# this is syntactically correct python, and will
# run from the command line, but the html header
# and html content have no separation, so apache
# will consider all of it to be header info, resulting
# in another server 500, "malformed header"

As others have advised, under these circumstances you can review the
apache error log (if your webhost allows it). Or roll-your-own logging
equivalent, and run your cgi from a terminal to catch SyntaxErrors and
the like.

HTH,
Marty
___
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] CGI File Woes

2007-09-30 Thread Martin Walsh
wormwood_3 wrote:
>>> I've never used cgitb (and until now didn't know it existed!)
>>> so can't comment.
> 
> I had not heard of it until this week when I started working on CGI stuff, 
> but I have found it super handy! All you have to do it "import cgitb; 
> cgitb.enable()" and all tracebacks will get printed to nicely formatted HTML 
> output on the page you were trying to load.
> 

No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
least two instances when you will not get the pretty printed tracebacks
in the browser when using cgitb. One is after, what I would call, a
'compile time' exception such as SyntaxError, in your python code. The
other is when the python code runs without exception, but you have not
separated the header and the document content with a newline. At least,
I have found these to be true when using apache-cgi.

Consider the following examples:

#!/usr/bin/env python
# raises a SyntaxError

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html\n\n"
# NOTE: purposeful misspelling of the print statement
prin "Hello, world!"

# the code above will produce a server 500, with apache
# complaining about "premature end of script headers"

...

#!/usr/bin/env python
# good python, bad data

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html"
print "Hello, world!"

# this is syntactically correct python, and will
# run from the command line, but the html header
# and html content have no separation, so apache
# will consider all of it to be header info, resulting
# in another server 500, "malformed header"

As others have advised, under these circumstances you can review the
apache error log (if your webhost allows it). Or roll-your-own logging
equivalent, and run your cgi from a terminal to catch SyntaxErrors and
the like.

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


Re: [Tutor] CGI File Woes

2007-09-30 Thread Noufal Ibrahim
wormwood_3 wrote:
> Hello all,
> I am working on a very simple CGI script. The site I want to use it
> on is a shared linux host, but I confirmed that .py files in the
> right dir with the right permissions and shebang execute just fine,
> Hello World sort of tests were successful.

Those are the first things I'd try too.


> So now something a little more involved:
> 
> 
> #!/usr/bin/python2.4
> 
> import cgitb; cgitb.enable()
> 
> thefile = open("template.html", "r")
> templatestuff = thefile.read()
> thefile.close()
> print "Content-Type: text/html"
> if templatestuff:
> print "Found it"
> title1 = "I am a title!"
> body1 = "I am some hot content"
> print templatestuff % (title1, body1)
> 
> "template.html" is in the same dir, and is simply:
> 
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> 
> 
>   
>  %s 
>   
>   
> %s
>   
> 
[..]
> Any ideas?

Here are a few ideas plus some suggestions. I've been dabbling with 
python/cgi myself and so there might be something you can take from 
these comments.

- If your webserver and the machine where you executed this from are
   different (home directories are NFS mounts etc.), then the permissions
   and directory visibilities might affect things.

- It looks fairly obvious that the problem is in those three lines. Your
   content-type header is printed only after those lines are executed so
   if there is an error there, you'll get an error 500. Try putting your
   content-type line right after your import cgitb.

- Wrap the 3 suspicious lines in a try block and print out the execption
   which you catch.

- Tail the server logs to see what happened.

Good luck.




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


Re: [Tutor] CGI File Woes

2007-09-30 Thread Alan Gauld

"wormwood_3" <[EMAIL PROTECTED]> wrote 

> I had not heard of it until this week when I started working on 
> CGI stuff, but I have found it super handy! All you have to do 
> it "import cgitb; cgitb.enable()" and all tracebacks will get 
> printed to nicely formatted HTML output on the page you 
> were trying to load.

Ok, Heres a thought.

try forcing an error after opening the file.
See what the traceback says. You can add a message 
string to an exception so that it will print...

class LogError(Exception): pass

raise LogError, "Any old text here"

So you force the cgitb to kick in and print the mesage you 
specify, use it like a print statement to debug whats happening...

Its a bit laborious but better than nothing!

See if that helps

Alan G




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


Re: [Tutor] CGI File Woes

2007-09-30 Thread wormwood_3
>>Did you mean for there to be two  tags or is it a typo?

Just a typo. Should not have a big effect, but it has been fixed.

>>I've never used cgitb (and until now didn't know it existed!)
>>so can't comment.

I had not heard of it until this week when I started working on CGI stuff, but 
I have found it super handy! All you have to do it "import cgitb; 
cgitb.enable()" and all tracebacks will get printed to nicely formatted HTML 
output on the page you were trying to load.

>>But I usually see this when the web user doesn't have
>>permission to open the html file. But...

This definitely would make the most sense in that the error is not a Python 
error, at least I am assuming so from the lack of traceback. However, the odd 
thing is that if I hit template.html directly, the page renders just fine. This 
would mean the web user can access it, right? And I was assuming the Python 
script was being run by the web user as well, but perhaps it is not.

>>For all users?
>>Remember that the web server runs as a separate user
>>and that's the user than needs to open the file.

Sorry, I stated that poorly. What I meant was that in the process of 
troubleshooting, I ended up enabling ALL permissions, so user, group, and 
others could read, write, and execute. So since this is now the case, and the 
script runs fine locally, and I can hit template.html on its own on the web 
server... I am still quite baffled!

-Sam



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


Re: [Tutor] creating the equivalent of string.strip()

2007-09-30 Thread Alan Gauld

"wesley chun" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>> > I'm not sure how to proceed.  My biggest stumbling
>> > block is how to detect the leading and trailing
>> > whitespace.
>>
>> Use indexing.
>> Try using a while loop.
>> Or maybe two?
>>
>> And strings have an isspace method...
>
> unfortunately, the isspace() string method only returns True if all
> chars in the string are whitespace chars and False otherwise, so
> that's a bummer.

But the string can be one character long:

s = 'fred\n'
print s[-1].isspace()  # --> True

:-)

Alan G 


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


Re: [Tutor] questions about tuples

2007-09-30 Thread Alan Gauld

"Fangwen Lu" <[EMAIL PROTECTED]> wrote

> I want to do some loops. Each loop will generate a tuple. Eventually 
> I want
> to put tuples together in a higher level of tuple. Do you know how 
> to do
> this?

Inaddition to other replies, you might be able to use a
generator expression to do it in one line:

>>> tuple( ( (x,x+1) for x in range(3) ) )
((0, 1), (1, 2), (2, 3))
>>>

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] CGI File Woes

2007-09-30 Thread Michael Langford
Remember, you can always add more logging that goes to your own log file.

Have it open a file in /tmp and write out a log of what's happening.
Wrap your main code block in an exception handler, etc. Have it print
out the exception to the log file.

This will allow you to look and see what the error is rather than
speculating about it.

 --Michael

On 9/30/07, wormwood_3 <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I am working on a very simple CGI script. The site I want to use it on is a 
> shared linux host, but I confirmed that .py files in the right dir with the 
> right permissions and shebang execute just fine, Hello World sort of tests 
> were successful.
>
> So now something a little more involved:
>
>
> #!/usr/bin/python2.4
>
> import cgitb; cgitb.enable()
>
> thefile = open("template.html", "r")
> templatestuff = thefile.read()
> thefile.close()
> print "Content-Type: text/html"
> if templatestuff:
> print "Found it"
> title1 = "I am a title!"
> body1 = "I am some hot content"
> print templatestuff % (title1, body1)
>
> "template.html" is in the same dir, and is simply:
>
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> 
> 
>   
>  %s 
>   
>   
> %s
>   
> 
>
> If I run this script without the 3 lines after the import line, it works fine 
> (namely I get an error that templatestuff is not defined, as would be 
> expected). With those lines however, I am getting a 500 Internal Server 
> Error. Since I am not shown an error page with cgitb, this would likely mean 
> a syntax error. However, if I run the script locally, it works just fine, 
> printing out the HTML with variables filled in. Now for the odd part: If I 
> change that open line to "thefile = open("asdas", "r")", I get "IOError: 
> [Errno 2] No such file or directory: 'asdas'
> ". So it seems the script is finding the template file when I have it as 
> above, but is throwing something when it tries to open it. I have confirmed 
> the file has the right permissions, I have even tried it with all permissions 
> set on that file.
>
> I am just totally baffled why I cannot open any files from the script.
>
> Any ideas?
> -Sam
>
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] questions about tuples

2007-09-30 Thread Noufal Ibrahim
Fangwen Lu wrote:
> Dear all-
>  
> I want to do some loops. Each loop will generate a tuple. Eventually I 
> want to put tuples together in a higher level of tuple. Do you know how 
> to do this?
>  
> Say a=(1,2,3), b=(4,3,2),c=(9,5,6). I want to get a tuple as ((1,2,3), 
> (4,3,2),(9,5,6)).
>  
> If there are just 2 tuples, I can write x=a and then x=(x,b). But for 3 
> tuples or more, I will get something like (((1,2,3), (4,3,2)),(9,5,6)).
>  

Are you sure you need tuples? Wouldn't lists be better, you can append 
generated elements to the list and finally (if you want) convert it into 
a tuple.


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


Re: [Tutor] linux terminal coloring in python

2007-09-30 Thread Noufal Ibrahim
Robert Jackson wrote:
> I'm trying to get some pretty colored output for a Linux console / terminal 
> window.  Google searches only reveal the curses module to colorize output.
> 
> Is there a simpler way?  Curses seems to be FAR too powerful for what it is I 
> want to do (simply to colorize a few 'print' outputs to the console).
> 

I stumbled across a recipe for a portable terminal colouring scheme here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475116

You need to use the curses module but don't need to put the terminal 
into raw mode etc.

Maybe it will suit your needs.

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


Re: [Tutor] CGI File Woes

2007-09-30 Thread Alan Gauld

"wormwood_3" <[EMAIL PROTECTED]> wrote

> #!/usr/bin/python2.4
>
> import cgitb; cgitb.enable()
>
> thefile = open("template.html", "r")
> templatestuff = thefile.read()
> thefile.close()
> print "Content-Type: text/html"
> if templatestuff:
>print "Found it"
> title1 = "I am a title!"
> body1 = "I am some hot content"
> print templatestuff % (title1, body1)
>
> "template.html" is in the same dir, and is simply:
>
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> 
> 

Did you mean for there to be two  tags or is it a typo?

>  
> %s 
>  
>  
>%s
>  
> 
>
> If I run this script without the 3 lines after the import line,
> it works fine (namely I get an error that templatestuff is not 
> defined,

> I am getting a 500 Internal Server Error.

> Since I am not shown an error page with cgitb, this would likely
> mean a syntax error.

I've never used gcitb (and until now didn't know it existed!)
so can't comment.

But I usually see this when the web user doesn't have
permission to open the html file. But...

>  I have confirmed the file has the right permissions,
> I have even tried it with all permissions set on that file.

For all users?
Remember that the web server runs as a separate user
and that's the user than needs to open the file.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] creating the equivalent of string.strip()

2007-09-30 Thread wesley chun
> > I'm not sure how to proceed.  My biggest stumbling
> > block is how to detect the leading and trailing
> > whitespace.
>
> Use indexing.
> Try using a while loop.
> Or maybe two?
>
> And strings have an isspace method...

unfortunately, the isspace() string method only returns True if all
chars in the string are whitespace chars and False otherwise, so
that's a bummer.

on the other hand, the string module also has an attribute called
string.whitespace that contains all (valid/recognized) whitespace
characters with which you can compare individual characters of your
string with.

an alternative (yet possibly more complex) solution can use regular expressions.

you can also "cheat" and read the source code for the *strip() methods
and just duplicate them in Python. ;-)

regardless of what method you use, you should recognize that strings
cannot be modified (immutable), meaning that you will have to create a
new string (with all the whitespace removed).

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] detecing palindromic strings

2007-09-30 Thread wesley chun
greetings, and welcome to Python!

there were some good solutions using the extended slice operator
([::-1]), but that may be an obscure notation to many newbies to
Python, as powerful as it can be.

i'm going to pick up terry's response and further clarify here (i hope!):

> string_list_orig = string_list

this is a critical assignment.  and yes, you already figured out that
you are just "copying pointers" here.  in Python, we say that you are
creating an alias or another reference to the same object.  you are
not creating a new one which is a copy.  there are a couple of ways to
do the copy.

1. terry already mentioned string_list[:] takes an improper (or
complete) slice of the existing string
2. kent also chimed in with list(string_list), which will do exactly the same
3. more off the beaten path, you can also using copy.copy():

import copy
string_list_orig = copy.copy(string_list)

all 3 do exactly the same thing: create a new list but copy the
*references* to the internal objects.  these are known as "shallow
copies." they don't *copy* the contained objects themselves.  this is
another place that newbies go HUH?!?
the bottom line is that if the contained objects are mutable (meaning
their values can be altered), changing them from one reference will
affect the outcome via an alias.

in order to copy the container as well as its contents, you need to
use copy.deepcopy().  see the man page there for more details.
there's also a section in the book on this.


> string_list.reverse()

because both variable names reference the same object, doing this [or
string_list_orig.reverse()] will have the same effect.


> print string_list_orig
> print string_list

both "point" to the exact same object, hence the duplicate output.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor