Re: [Tutor] Apache, CGI-BIN, Python

2007-09-10 Thread Jan Erik Moström
Ryan <[EMAIL PROTECTED]> 07-09-09 12:26

>I am running a Linux box and cannot find my Apache cgi-bin to 
>put some python scripts in. I know I probably have to create 
>one but don't know where and how.

Look in Apaches config file, you should find something like this

ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"

this is from my Mac, on my debian machine it looks like this

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

And you can of course have different catalogs for different virtual domains

jem
-- 
Jan Erik Moström, www.mostrom.pp.se

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


Re: [Tutor] Problem with while loop

2007-09-10 Thread Vishnu Mohan

> Now I just need to figure out how to only get 4 digit pin numbers :)
>
Use regular expressions
The following is the code with re.


from random import randint
import re

counter = 0
pinPattern = re.compile(r'^\d{4}$')
howmany = raw_input( "How many: " )
if pinPattern.match(howmany):
while counter < int(howmany):
pin = randint(,)
print pin
counter += 1
else:
print "%s is not valid 4 digit integer"%howmany


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


Re: [Tutor] Problem with while loop

2007-09-10 Thread Michael Connors
Hi,
I would do it as follows, adding 0s to front to make them valid PINs.

while counter < howmany:

pin = randint(,)
print "%04i" % (pin)
counter += 1

On 10/09/2007, Vishnu Mohan <[EMAIL PROTECTED]> wrote:
>
>
> > Now I just need to figure out how to only get 4 digit pin numbers :)
> >
> Use regular expressions
> The following is the code with re.
>
>
> from random import randint
> import re
>
> counter = 0
> pinPattern = re.compile(r'^\d{4}$')
> howmany = raw_input( "How many: " )
> if pinPattern.match(howmany):
> while counter < int(howmany):
> pin = randint(,)
> print pin
> counter += 1
> else:
> print "%s is not valid 4 digit integer"%howmany
>
>
> -vishnuMohan
>



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


Re: [Tutor] replacing while loop

2007-09-10 Thread Arvind Deshpande
Is this what you are looking for?

#!/usr/bin/python
'makeTextFile.py -- create text file'

import os

# get filename
#while True:
#   fname = raw_input('Enter file name: ')
#   if os.path.exists(fname):
#   print"*** ERROR: '%s' already exists" % fname
#   else:
#   break

while True:
fname = raw_input('Enter file name: ')
try:
fobj = open(fname, 'r')
except:
break

# get file content (text) lines
all = []
print "\nEnter lines ('.' by itself to quit).\n"

# loop until user terminates input
while True:
   entry = raw_input('> ')
   if entry == '.':
   break
   else:
   all.append(entry)

# write lines to file with NEWLINE line terminator
fobj = open(fname, 'w')
fobj.write('\n'.join(all))
fobj.close()
print 'DONE!'

-- 
Arvind Deshpande

On 9/8/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
> "Christopher Spears" <[EMAIL PROTECTED]> wrote
>
> > I have been asked to replace this while loop with a
> > try and except clause:
> >
> > while True:
> >fname = raw_input('Enter file name: ')
> >if os.path.exists(fname):
> >print"*** ERROR: '%s' already exists" % fname
> >else:
> >break
> >
> > I'm not sure how to do this.  I looked at the back of
> > the book, and I don't see an exception that is raised
> > when a previously existing file is found.  Any hints?
>
> The loop simply detects if the file exists *or not*
> If the file does not exist you exit the loop.
> Can you find a way using try/except to detect
> if the file does not exist?
>
> That will replace the body of the while loop,
> I can't think of any way to replace the loop itself
> with try./except...
>
> And I agree this is not an obvious place to use
> try/except. Your earlier example is more typical.
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Printing HTML files

2007-09-10 Thread Gardner, Dean
Hi 

I am currently trying to print out a html file that is essentially a
summary table and I am running into problems. From the link below it
seems that the method I am using to print the table doesn't handle
column width and wrapping but confusingly we use a similar method
elsewhere in the code and it works fine. 

This is the summary builder

class TestingSummary:
def __init__(self,records):
self.records = records

def splitRecordAndBuildSummaryText(self):

summary_dict={}
test_fields=[]
keys=[]
fields =
["TestedDate:","TestId:","Branch:","Version:","SpecId:","PassOrFail:"]


records = self.records.split("\n\n")
for record in records:

record=record.split("\n")
for item in record:
if "TestId" in item:
testid=record.pop(1)
testid = testid.replace("TestId:","")

for field in record:
for item in fields:
#print item
field = field.replace(item,"")

test_fields.append(field)
summary_dict[testid]=test_fields
test_fields = []
   # print summary_dict

summary = self.buildHtmlSummaryPage(summary_dict)
return summary


def buildHtmlSummaryPage(self,dict_of_summary):
#print list_of_ids
test_summary_details=""
for key, value in dict_of_summary.items():
#print value
#print details
test_summary_details+="""%s%s%s%s\n""" %
(key,value[3],value[-1],"".join(value[1]+value[2]))

summary =
"".join(["""\n""",
"""Testing
Summary  \n""",
"""Tested
by:  \n""",
"""Machine
Name: \n""",
"""   \n""",
"""   \n""",
"""TestIDSpecification\n""",
"""ResultBuildID\n"""])


summary+=test_summary_details
summary+=""


return summary

and the mechanism for printing

def printSummary(self,summary):

print_dialog = wx.PrintDialog(self)

if print_dialog.ShowModal() == wx.ID_CANCEL:
return

print_dialog_data = print_dialog.GetPrintDialogData()
printer = wx.Printer(print_dialog_data)
printout = wx.html.HtmlPrintout("Printing Test Summary")
# margins (top, bottom, left, right)
printout.SetMargins(15, 15, 20, 20)
#REMOVE
#-This was for testing purposes only
htmlOutput = open("TestingSummary.html","w")
htmlOutput.write(summary)
htmlOutput.close()
#-
printout.SetHtmlText(summary)
printout.SetFooter(self.HtmlFooterForPrint(1, 1))

printer.Print(self, printout, False)


When I save the file as html the browser will open it fine and it is I
expect but if I print it I end up with 


   Testing Summary  Tested by:  Machine Name:   
   TestIDSpecificationResultBuildID 03 --0009
Pass   02 Specification--0009 Pass   
   01 Specification--0009 Pass  


http://archives.devshed.com/forums/python-122/printing-problem-1843805.h
tml

Am I doing something silly? 

Thanks
Dean Gardner





DISCLAIMER:
Unless indicated otherwise, the information contained in this message is 
privileged and confidential, and is intended only for the use of the 
addressee(s) named above and others who have been specifically authorized to 
receive it. If you are not the intended recipient, you are hereby notified that 
any dissemination, distribution or copying of this message and/or attachments 
is strictly prohibited. The company accepts no liability for any damage caused 
by any virus transmitted by this email. Furthermore, the company does not 
warrant a proper and complete transmission of this information, nor does it 
accept liability for any delays. If you have received this message in error, 
please contact the sender and delete the message. Thank you.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Livewires

2007-09-10 Thread christopher . henk
[EMAIL PROTECTED] wrote on 09/10/2007 07:29:24 AM:

> Hi all
> 
> just learning python really and been using the livewires tutorial / 
worksheets to get some experience.
> 
> I have hit an issue which is just my lack of understanding around 
looping concepts and execution.
> 
> My issue:
> 
> in worksheet 5-robots.pdf attached, page 4 the challenge
> 
> "Challenge: Write a loop that makes the circle move smoothly from (0,0) 
to (640,480): in other words, from the bottom left
> to the top right of the screen."
> 
> this has got me a bit stumped because its an (x,y) co-ordinate pair that 
I want to update.
> I think in a loop i need to draw a circle, move a circle, remove the 
circle.
> 
> I thought I needed to for loops to iterate through two ranges but this 
is wrong, here is my code though!
> 
> from livewires import *
> begin_graphics()
> 
> allow_moveables()
> x=range(10,640,10)
> y=range(10,480,10)
> for xco in x:
> for yco in y:
> c = circle(xco,yco,5)
> move_to(c, xco,yco)
> #remove_from_screen(c) /*commented this out to see output on 
graphics window */
> end_graphics()
> 


If I understand the requirements correctly: you are moving along the 
diagonal of the map.
So say 100 time steps to move the distance.

time step: 0position:  (0,0)
time step: 1position: (64,48)
time step: 2position: (128,96)
...

so the new x and y move together with a different delta so that they reach 
their max at the same time.
Your only loop would be what time step you are on.

timesteps=100
x,y =0,0
deltax=640/timesteps
deltay=480/timesteps
for time in range (timesteps):
c=circle(x,y,5)
x+=deltax
y+=deltay
move_to(c,x,y)
remove_from_screen(c)

I would think that would do the trick.
But I haven't had any coffee yet this morning, so if I missed something, 
let me know.

Chris


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


Re: [Tutor] Livewires

2007-09-10 Thread Tonu Mikk
Hi Sacha,  I am very much a beginner to Python myself, but I tried my 
hand on the Livewires modules.  Here is how I solved the challenge. 

from livewires import *
begin_graphics()
allow_moveables()
x=0
y=0
c = circle(x, y,5)
while x < 640:
x=x+5
y=y+3.822
move_to (c, x, y)
time.sleep(0.1)

Tonu
sacha rook wrote:
> Hi all
>  
> just learning python really and been using the livewires tutorial / 
> worksheets to get some experience.
>  
> I have hit an issue which is just my lack of understanding around 
> looping concepts and execution.
>  
> My issue:
>  
> in worksheet 5-robots.pdf attached, page 4 the challenge
>  
> "Challenge: Write a loop that makes the circle move smoothly from 
> (0,0) to (640,480): in other words, from the bottom left
> to the top right of the screen."
>  
> this has got me a bit stumped because its an (x,y) co-ordinate pair 
> that I want to update.
> I think in a loop i need to draw a circle, move a circle, remove the 
> circle.
>  
> I thought I needed to for loops to iterate through two ranges but this 
> is wrong, here is my code though!
>  
> from livewires import *
> begin_graphics()
>  
> allow_moveables()
> x=range(10,640,10)
> y=range(10,480,10)
> for xco in x:
> for yco in y:
> c = circle(xco,yco,5)
> move_to(c, xco,yco)
> #remove_from_screen(c) /*commented this out to see output on 
> graphics window */
> end_graphics()
>  
> Can anyone look at the worksheet challenge and my poor code and show 
> me the error of my ways? :)
> I appreciate it may be my inexperience in program flow/logic which is 
> the problem also, I don't my help or suggestion to improve in any area.
> Thanks for your help in advance
> Sacha
>  
>  
>
> 
> Play Movie Mash-up and win BIG prizes! 
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

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


[Tutor] Running other files

2007-09-10 Thread Lawrence Barrott
is it possible to run other non-python files using python such as .exe or other 
files.

Thanks,

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


Re: [Tutor] Running other files

2007-09-10 Thread Rikard Bosnjakovic
On 10/09/2007, Lawrence Barrott <[EMAIL PROTECTED]> wrote:

> is it possible to run other non-python files using python such as .exe or
> other files.

Have a look at os.system().


-- 
- Rikard - http://bos.hack.org/cv/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] making math problems mmmm fun

2007-09-10 Thread max baseman
hello all this is a homework in math i dont need to program it but i  
would like to :)  so like any other time pleas  dont just give a  
answer tutorials or a explanation. i dont like to use script  
something i dont understand :)

thanks

basically the problem is to find a bunch of ways to put 1,2,3,4,5  
into different math problems to that equal 1-25, i haven't spent to  
much time thinking about how to do this but i cant think of a way to  
do it it without writing making the program rather long here is the  
page from the book for the rules i will be working on this for the  
next week or so thanks for any help :)




  . you may use any of the four basic arithmetic operations-  
addition, subtraction, multiplication, and division (according to the  
order of operations rules). for example, 2+1x3-4 is a 1-2-3-4  
expression for the number 1.

. you may use exponents. for example, 2² - 4 - 1 is a 1234 expression  
for the number 3

. you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is  
a 1234 expression for 6

. you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a  
1234 expression for the number 26


. you  may juxtapose two or more digits (that is put them next to  
each other) to form a number such as 12. for example 43-12 is a 1234  
expression for 31

. you may use parentheses and brackets to change the meaning of a  
expression for example according to the rules of order of operations 1 
+4x3² is a 1234 expression for 37. you can add parentheses and  
brackets to get [(1+4)x3]² which is a 1234 expression for 225

. must use 1,2,3,4 exactly once



thanks for the help ill post if i find anything 
  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] making math problems mmmm fun

2007-09-10 Thread max baseman
hello all this is a homework in math i dont need to program it but i  
would like to :)  so like any other time pleas  dont just give a  
answer tutorials or a explanation. i dont like to use script  
something i dont understand :)

thanks

basically the problem is to find a bunch of ways to put 1,2,3,4,5  
into different math problems to that equal 1-25, i haven't spent to  
much time thinking about how to do this but i cant think of a way to  
do it it without writing making the program rather long here is the  
page from the book for the rules i will be working on this for the  
next week or so thanks for any help :)




  . you may use any of the four basic arithmetic operations-  
addition, subtraction, multiplication, and division (according to the  
order of operations rules). for example, 2+1x3-4 is a 1-2-3-4  
expression for the number 1.

. you may use exponents. for example, 2² - 4 - 1 is a 1234 expression  
for the number 3

. you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is  
a 1234 expression for 6

. you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a  
1234 expression for the number 26


. you  may juxtapose two or more digits (that is put them next to  
each other) to form a number such as 12. for example 43-12 is a 1234  
expression for 31

. you may use parentheses and brackets to change the meaning of a  
expression for example according to the rules of order of operations 1 
+4x3² is a 1234 expression for 37. you can add parentheses and  
brackets to get [(1+4)x3]² which is a 1234 expression for 225

. must use 1,2,3,4 exactly once



thanks for the help ill post if i find anything
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Making a python script to feed files into another python script

2007-09-10 Thread Ashley Booth
I am trying to create a script that will get files from a directory
that the user specifies, then feeds the relevant input and output
(which are input and output paths) into another python script whose
path is also given by the user. I'm pretty lazy after all and I would
rather spend my time making tools than doing the busy-work myself.

So far I can get it to read the directory fine but it ends up just
opening the script I want the files to be fed into instead of giving
it the input and output to run and running it. Any ideas?

Here is what I have so far:

import sys, os

indir = raw_input('input directory path ') # input directory
script1 = os.system(raw_input('python script path '))

for j in os.listdir(indir):
   input = os.path.join(indir,j)
   output = os.path.join(indir,j.split('rovctd')[0]+'.txt')
   script1(input,output)


If this is going to be too complicated, I would at least be happy with
how to tack on the list directory stuff to then of the script1- it
would not be a stand alone script but at least it would work. I tired
to do that too but it only processed the first file in the directory
instead of all of them.


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


Re: [Tutor] Making a python script to feed files into another python script

2007-09-10 Thread John Fouhy
On 11/09/2007, Ashley Booth <[EMAIL PROTECTED]> wrote:
> I am trying to create a script that will get files from a directory
> that the user specifies, then feeds the relevant input and output
> (which are input and output paths) into another python script whose
> path is also given by the user. I'm pretty lazy after all and I would
> rather spend my time making tools than doing the busy-work myself.

Hi Ashley,

You've basically got two options:

1. You can build a command line and then use os.system to call it:
   script2 = raw_input('python script path:')
   input, output = # whatever
   os.system('python %s %s %s' % (script2, input, output))

2. You can import your second script and call its main function
directly.  How well this works will depend on the structure of the
second script.

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


Re: [Tutor] Socket Timeout Handling

2007-09-10 Thread wormwood_3
Have not gotten any responses on this, nor very much by way of searching, which 
is strange and a little disappointing for such a seemingly basic thing. (May 
just be too obvious of a thing, so no one wanted to post the solution:-). )

But, I did find a decent recipe on ASPN that serves the purpose, so I will 
share in case others needed to do the same check as I did.

Before I needed to make the network call in my program, I have the following:

if checkURL('http://www.google.com/'):
networkup = True
else:
networkup = False
if networkup:
print "Internet connection seems to be up."
else:
print "Internet connection seems to be down. Please check it",
print "and retry."
sys.exit()

Then I continue with my network calls. The function I use is:

def checkURL(url):
"""For checking internet connection. Taken from recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/101276""";
try:
p = urlparse(url)
h = HTTP(p[1])
h.putrequest('HEAD', p[2])
h.endheaders()
if h.getreply()[0] == 200: return 1
else: return 0
except:
return 0

The nice thing about this check is that is just looks at the head of the site, 
and so is rather fast.

One other consideration: While it is a rare day indeed that Google.com is ever 
down, to be even safer, would could check against several reliable sites, such 
as Amazon, Yahoo, w3c.org, etc. The status of each check could be put in a 
list, and if any list item was networkup, then the internet connection may be 
considered up.

Hope someone finds this useful.

-Sam

___
- Original Message 
From: wormwood_3 <[EMAIL PROTECTED]>
To: Python Tutorlist 
Sent: Thursday, September 6, 2007 4:46:08 PM
Subject: Re: [Tutor] Socket Timeout Handling

Since no one bit on this yet, let me simplify to the core issue I am having:

What is the best practice for checking for network connectivity errors when 
making network calls? Is it better to wrap the functions that make said calls 
in threads and time them? Or to use timeout variables for modules like socket? 
Something else?

I found some good general info here: 
http://www.onlamp.com/pub/a/python/2003/11/06/python_nio.html

But I have had a hard time finding info on network error handling specifically.

Thoughts?

__
- Original Message 
From: wormwood_3 <[EMAIL PROTECTED]>
To: Python Tutorlist 
Sent: Thursday, September 6, 2007 9:40:21 AM
Subject: [Tutor] Socket Timeout Handling

I am trying to figure out the optimal way to make socket connections (INET) and 
check for timeouts. The socket module has settimeout(timeout) and 
setdefaulttimeout(timeout). However, so far as I can tell, these apply to 
socket objects. The type of socket connection I want to make is 
getfqdn(address). So I can set the default timeout for socket, but not a socket 
object (makes sense so far). I cannot use the getfqdn(address) method on a 
socket object, I have to use it on socket. This means (as I understand it thus 
far), that while I can set a timeout value for socket objects, this will not 
apply to when I use the getfqdn() method, which is where I need a timeout 
check! Some example code for the steps so far:

>>> import socket
>>> conn = socket.socket()
>>> conn.setdefaulttimeout(2.0)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'setdefaulttimeout'
>>> socket.setdefaulttimeout(2.0)
>>> conn.getfqdn("64.33.212.2")
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: '_socketobject' object has no attribute 'getfqdn'
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'
>>> # Disconnected network connection here
... 
>>> socket.getfqdn("64.33.212.2")
'64.33.212.2'
>>> # Reconnected network connection here
>>> socket.getfqdn("64.33.212.2")
'64-33-212-2.customers.pingtone.net'

After I disconnected my network connection and called getfqdn(), it returned 
the IP address I called it with after about 25 seconds. So the default timeout 
was ignored? Is there some other way to call this function so that I can check 
for timeouts? Should I instead just put my network calls in a thread and see 
how long they take, stopping them after a certain period?

Thanks for any help.

-Sam


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



___
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] making math problems mmmm fun

2007-09-10 Thread wormwood_3
Don't have any ideas to Pythonize this problem for you, but I must say that I 
hope this problem was listed in a chapter entitled "Cruel and Unusual"!

-Sam

- Original Message 
From: max baseman <[EMAIL PROTECTED]>
To: tutor@python.org
Sent: Monday, September 10, 2007 6:28:23 PM
Subject: [Tutor] making math problems  fun

hello all this is a homework in math i dont need to program it but i  
would like to :)  so like any other time pleas  dont just give a  
answer tutorials or a explanation. i dont like to use script  
something i dont understand :)

thanks

basically the problem is to find a bunch of ways to put 1,2,3,4,5  
into different math problems to that equal 1-25, i haven't spent to  
much time thinking about how to do this but i cant think of a way to  
do it it without writing making the program rather long here is the  
page from the book for the rules i will be working on this for the  
next week or so thanks for any help :)




  . you may use any of the four basic arithmetic operations-  
addition, subtraction, multiplication, and division (according to the  
order of operations rules). for example, 2+1x3-4 is a 1-2-3-4  
expression for the number 1.

. you may use exponents. for example, 2² - 4 - 1 is a 1234 expression  
for the number 3

. you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is  
a 1234 expression for 6

. you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a  
1234 expression for the number 26


. you  may juxtapose two or more digits (that is put them next to  
each other) to form a number such as 12. for example 43-12 is a 1234  
expression for 31

. you may use parentheses and brackets to change the meaning of a  
expression for example according to the rules of order of operations 1 
+4x3² is a 1234 expression for 37. you can add parentheses and  
brackets to get [(1+4)x3]² which is a 1234 expression for 225

. must use 1,2,3,4 exactly once



thanks for the help ill post if i find anything
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



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


[Tutor] Image Analysis

2007-09-10 Thread wormwood_3
I have thought up a project for myself that is rather beyond my current 
knowledge, but I think it will be fun and very informative. I'll leave out the 
details right now, but the task that will be the hardest is that I need some 
way to analyze an image for color patterns. I would like to be able to load an 
image file of some format, and be able to determine what color the pixels are, 
in short. From this, I need to generate statistics, such as what color is most 
common, find patterns in the image, etc.

If anyone knows ANYTHING about this sort of analysis in Python, or even any 
helpful tutorials on image analysis that are more general, I would greatly 
appreciate it. 

My only lead right now is PIL, and I am not sure if it will meet my needs.

Thanks,
Sam


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


Re: [Tutor] making math problems mmmm fun

2007-09-10 Thread max baseman
haha :) yeah it's the new imp stuff i like parts of the idea but  
other parts i really dislike basically it TRIES   to make math more  
interactive and world like i really enjoy how most of it is really  
programable stuff :) where compared to normal math books it's a bit  
harder to program just a problem instead of a story but imp needs  
help with it's grading and teaching the grading is terrible i can get  
a A as long as i can explain and know how my way of doing it wrong  
"works" but will fail if i just write the write answer without  
explanation i dont mind the explanations bit but that what i write  
matters more than if i can do the work  is odd
adn i just haven't learned anything new yet :)

On Sep 10, 2007, at 7:16 PM, wormwood_3 wrote:

> Don't have any ideas to Pythonize this problem for you, but I must  
> say that I hope this problem was listed in a chapter entitled  
> "Cruel and Unusual"!
>
> -Sam
> 
> - Original Message 
> From: max baseman <[EMAIL PROTECTED]>
> To: tutor@python.org
> Sent: Monday, September 10, 2007 6:28:23 PM
> Subject: [Tutor] making math problems  fun
>
> hello all this is a homework in math i dont need to program it but i
> would like to :)  so like any other time pleas  dont just give a
> answer tutorials or a explanation. i dont like to use script
> something i dont understand :)
>
> thanks
>
> basically the problem is to find a bunch of ways to put 1,2,3,4,5
> into different math problems to that equal 1-25, i haven't spent to
> much time thinking about how to do this but i cant think of a way to
> do it it without writing making the program rather long here is the
> page from the book for the rules i will be working on this for the
> next week or so thanks for any help :)
>
>
>
>
>   . you may use any of the four basic arithmetic operations-
> addition, subtraction, multiplication, and division (according to the
> order of operations rules). for example, 2+1x3-4 is a 1-2-3-4
> expression for the number 1.
>
> . you may use exponents. for example, 2² - 4 - 1 is a 1234 expression
> for the number 3
>
> . you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is
> a 1234 expression for 6
>
> . you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a
> 1234 expression for the number 26
>
>
> . you  may juxtapose two or more digits (that is put them next to
> each other) to form a number such as 12. for example 43-12 is a 1234
> expression for 31
>
> . you may use parentheses and brackets to change the meaning of a
> expression for example according to the rules of order of operations 1
> +4x3² is a 1234 expression for 37. you can add parentheses and
> brackets to get [(1+4)x3]² which is a 1234 expression for 225
>
> . must use 1,2,3,4 exactly once
>
>
>
> thanks for the help ill post if i find anything
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> ___
> 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] making math problems mmmm fun

2007-09-10 Thread John Fouhy
On 11/09/2007, max baseman <[EMAIL PROTECTED]> wrote:
> basically the problem is to find a bunch of ways to put 1,2,3,4,5
> into different math problems to that equal 1-25, i haven't spent to
> much time thinking about how to do this but i cant think of a way to
> do it it without writing making the program rather long here is the
> page from the book for the rules i will be working on this for the
> next week or so thanks for any help :)

I've seen this kind of problem before.. though not usually with quite
as much freedom as this one :-)  You could brute force it, by trying
all possible combinations of numbers and operators.  The only problem
is that the freedom they give you means you get horrible combinatorial
explosion.

I'll give you my attempt at brute-force code.  Basically, my thought
process was:
   1. Solve the problem for N digits by solving it for N-1 digits,
then combining those solutions with the last remaining digit.

   2. Because of non-commutivity and non-associativity, we need to
pick do this for each possible digit we could remove, and for both
directions.

   3. If I stick to binary operators, I guarantee that the number of
digits always reduces.  So I will forget about factorial and square
root for now.

Anyway, here's my code.  I'm happy to answer questions about it if you
like.  I guess I could be doing your homework for you, but only if
you've got access to some pretty staunch hardware -- it's pretty
snappy on [1, 2, 3], but if I try it on [1, 2, 3, 4] on my machine,
python gets up to about 1.3GB memory usage before crashing with a
MemoryError :-)

###
import operator

binops = { 'add':operator.add,
   'sub':operator.sub,
   'mul':operator.mul,
   'div':operator.truediv,
   'pow':operator.pow,
   'join':lambda x, y: int(str(x)+str(y))
   }

patterns = { 'add':'(%s) + (%s)',
 'sub':'(%s) - (%s)',
 'mul':'(%s) * (%s)',
 'div':'(%s) / (%s)',
 'pow':'(%s)^(%s)',
 'join':'%s%s'
 }

def combine(digits):
""" digits :: set(int)

output :: [ (value, expression) ]
  value :: int
  expression :: str -- string representation of math expression
"""

# We're going to solve this instance in terms of the solution
# for a list with one fewer digit.

# Because of non-commutativity, we have to do this twice for each
# possible start digit.

res = []

# Base case.
if len(digits) == 1:
return [(digit, str(digit)) for digit in digits]

# Otherwise..
for digit in digits:
remainder = digits - set([digit])

for val, exp in combine(remainder):
for binop in binops:
if binop == 'join':
# Ensure we only join numbers, not expressions.
try:
int(exp)
except ValueError:
continue

try:
newval1 = binops[binop](digit, val)
pattern1 = patterns[binop] % (str(digit), exp)
res.append((newval1, pattern1))
except ZeroDivisionError:
pass

try:
newval2 = binops[binop](val, digit)
pattern2 = patterns[binop] % (exp, str(digit))
res.append((newval2, pattern2))
except ZeroDivisionError:
pass

return res

if __name__ == '__main__':
res = combine(set(range(1, 4)))
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making math problems mmmm fun

2007-09-10 Thread max baseman
wow this is a bit over my range of knowledge im impressed :) ill be  
happy to look at it but i think  i will see if i can end up writing  
my own :) worse case ill do it by hand will not take long hmm wow  
thanks :)



On Sep 10, 2007, at 8:47 PM, John Fouhy wrote:


###
import operator

binops = { 'add':operator.add,
   'sub':operator.sub,
   'mul':operator.mul,
   'div':operator.truediv,
   'pow':operator.pow,
   'join':lambda x, y: int(str(x)+str(y))
   }

patterns = { 'add':'(%s) + (%s)',
 'sub':'(%s) - (%s)',
 'mul':'(%s) * (%s)',
 'div':'(%s) / (%s)',
 'pow':'(%s)^(%s)',
 'join':'%s%s'
 }

def combine(digits):
""" digits :: set(int)

output :: [ (value, expression) ]
  value :: int
  expression :: str -- string representation of math expression
"""

# We're going to solve this instance in terms of the solution
# for a list with one fewer digit.

# Because of non-commutativity, we have to do this twice for each
# possible start digit.

res = []

# Base case.
if len(digits) == 1:
return [(digit, str(digit)) for digit in digits]

# Otherwise..
for digit in digits:
remainder = digits - set([digit])

for val, exp in combine(remainder):
for binop in binops:
if binop == 'join':
# Ensure we only join numbers, not expressions.
try:
int(exp)
except ValueError:
continue

try:
newval1 = binops[binop](digit, val)
pattern1 = patterns[binop] % (str(digit), exp)
res.append((newval1, pattern1))
except ZeroDivisionError:
pass

try:
newval2 = binops[binop](val, digit)
pattern2 = patterns[binop] % (exp, str(digit))
res.append((newval2, pattern2))
except ZeroDivisionError:
pass

return res

if __name__ == '__main__':
res = combine(set(range(1, 4)))


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


Re: [Tutor] inserting csv file into a sqlite3 memory connected table

2007-09-10 Thread Alan Gauld

<[EMAIL PROTECTED]> wrote 

>  I am basically just a visual basic person trying to learn python.
> 
> Anyway I have spent hours experimenting with  CSV and sqllite3 
> tutorial samples and misc snippets, Does anyone have any 
> code samples that might get me going?

Doing what?

Where are you having problems? Most tutorials will have code 
samples and snippets but unless we know what you are trying 
to do or not understanding we can't be more precise than point 
you at what you have probavbly already found.

My tutorial has a database topic that uses SQLite. 
I donlt have anything on CSV but the module does have 
reasonable documentation. But are you trying to store a CSV 
file content in your database? Or convert a data table to a CSV 
format? or use a CSV to traslate between databases?

We need a more specific question to direct our responses 
to your needs.

-- 
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] Apache, CGI-BIN, Python

2007-09-10 Thread Alan Gauld

"Ryan" <[EMAIL PROTECTED]> wrote

>I am running a Linux box and cannot find my Apache cgi-bin to put 
>some
> python scripts in. I know I probably have to create one but don't 
> know
> where and how.

I think there are several places that you can create cgi-bin depending
on how you configure apache. But the place I usually see it is 
directly
under the apache root directory - ie the one that is equivalent to / 
in
the url.

> everything works fine except:
>
> form = cgi.FieldStorage()
>
> #This is where everything goes wrong
> #I get error messages for either the lastname line or firstname

Can you tell us
a) exactly what error messages and
b) How you are invoking the script - where are the values
supposed to be coming from?

> lastname = form['lastname'].value
> firstname =form['firstname'].value
> message = firstname + " " + lastname
> print reshtml % message

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


[Tutor] input file encoding

2007-09-10 Thread Tim Michelsen
Hello,
I want to process some files encoded in latin-1 (iso-8859-1) in my 
python script that I write on Ubuntu which has UTF-8 as standard encoding.

When I use the "print lines_in_myfile" is get some wired symbols.

How shold I read those files in or convert their encoding to utf-8?

Thanks in advance,
Tim

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


Re: [Tutor] Suggested books for Agile Programming & Testing?

2007-09-10 Thread Kent Johnson
Stephen McInerney wrote:
> 
> Can anyone recommend me the best single must-read book for Agile 
> Programming?

Quoting myself from the archives:

I recommend Robert Martin's "Agile Software Development: Principles, 
Patterns, and Practices"
http://www.objectmentor.com/resources/bookstore/books/AgileSoftwareDevelopmentPPP

This is the best book I know for learning object-oriented design the way 
I do it ;) - a very agile, pragmatic approach. It shows you the 
nitty-gritty details of how to create classes as you develop a solution. 
Also a good introduction to the agile development style.

Much of the content of the book is available as essays on the 
ObjectMentor website: http://w
ww.objectmentor.com/resources/listArticles?key=author&author=Robert%20C.%20Martin

---

Some of the highlights from my bookshelf:

Martin, Agile Software Development
http://www.objectmentor.com/resources/bookstore/books/AgileSoftwareDevelopmentPPP

Fowler, Refactoring: Improving the Design of Existing Code
http://martinfowler.com/books.html#refactoring

Beck, Extreme Programming Explained
http://www.amazon.com/exec/obidos/ASIN/0201616416/ref%3Dnosim/armaties/102-7636110-6481700


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


Re: [Tutor] Running other files

2007-09-10 Thread Alan Gauld
"Rikard Bosnjakovic" <[EMAIL PROTECTED]> wrote

>> is it possible to run other non-python files using python such as 
>> .exe or
>> other files.
>
> Have a look at os.system().

Or the more recent subprocess module which supercedes os.system etc.

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] making math problems mmmm fun

2007-09-10 Thread Eric Brunson

When you get done with this math problem you should consider a book on 
punctuation. Not using it makes your sentences run together and 
difficult to read. :-) Honestly, I just gave up after the first two lines.

max baseman wrote:
> haha :) yeah it's the new imp stuff i like parts of the idea but  
> other parts i really dislike basically it TRIES   to make math more  
> interactive and world like i really enjoy how most of it is really  
> programable stuff :) where compared to normal math books it's a bit  
> harder to program just a problem instead of a story but imp needs  
> help with it's grading and teaching the grading is terrible i can get  
> a A as long as i can explain and know how my way of doing it wrong  
> "works" but will fail if i just write the write answer without  
> explanation i dont mind the explanations bit but that what i write  
> matters more than if i can do the work  is odd
> adn i just haven't learned anything new yet :)
>
> On Sep 10, 2007, at 7:16 PM, wormwood_3 wrote:
>
>   
>> Don't have any ideas to Pythonize this problem for you, but I must  
>> say that I hope this problem was listed in a chapter entitled  
>> "Cruel and Unusual"!
>>
>> -Sam
>> 
>> - Original Message 
>> From: max baseman <[EMAIL PROTECTED]>
>> To: tutor@python.org
>> Sent: Monday, September 10, 2007 6:28:23 PM
>> Subject: [Tutor] making math problems  fun
>>
>> hello all this is a homework in math i dont need to program it but i
>> would like to :)  so like any other time pleas  dont just give a
>> answer tutorials or a explanation. i dont like to use script
>> something i dont understand :)
>>
>> thanks
>>
>> basically the problem is to find a bunch of ways to put 1,2,3,4,5
>> into different math problems to that equal 1-25, i haven't spent to
>> much time thinking about how to do this but i cant think of a way to
>> do it it without writing making the program rather long here is the
>> page from the book for the rules i will be working on this for the
>> next week or so thanks for any help :)
>>
>>
>>
>>
>>   . you may use any of the four basic arithmetic operations-
>> addition, subtraction, multiplication, and division (according to the
>> order of operations rules). for example, 2+1x3-4 is a 1-2-3-4
>> expression for the number 1.
>>
>> . you may use exponents. for example, 2² - 4 - 1 is a 1234 expression
>> for the number 3
>>
>> . you may use radicals for EX: √4x2+1 is equal to 3 so 3+√4x2+1 is
>> a 1234 expression for 6
>>
>> . you may use factorials for EX: 4! means 4x3x2x1 so 3+4!+1-2 is a
>> 1234 expression for the number 26
>>
>>
>> . you  may juxtapose two or more digits (that is put them next to
>> each other) to form a number such as 12. for example 43-12 is a 1234
>> expression for 31
>>
>> . you may use parentheses and brackets to change the meaning of a
>> expression for example according to the rules of order of operations 1
>> +4x3² is a 1234 expression for 37. you can add parentheses and
>> brackets to get [(1+4)x3]² which is a 1234 expression for 225
>>
>> . must use 1,2,3,4 exactly once
>>
>>
>>
>> thanks for the help ill post if i find anything
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>
> ___
> 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] Apache, CGI-BIN, Python

2007-09-10 Thread Eric Brunson
Alan Gauld wrote:
> "Ryan" <[EMAIL PROTECTED]> wrote
>
>   
>> I am running a Linux box and cannot find my Apache cgi-bin to put 
>> some
>> python scripts in. I know I probably have to create one but don't 
>> know
>> where and how.
>> 
>
> I think there are several places that you can create cgi-bin depending
> on how you configure apache. But the place I usually see it is 
> directly
> under the apache root directory - ie the one that is equivalent to / 
> in
> the url.
>   

But, if you read the apache configuration files, it will tell you were 
it is.  :-)

>   
>> everything works fine except:
>>
>> form = cgi.FieldStorage()
>>
>> #This is where everything goes wrong
>> #I get error messages for either the lastname line or firstname
>> 
>
> Can you tell us
> a) exactly what error messages and
> b) How you are invoking the script - where are the values
> supposed to be coming from?
>
>   
>> lastname = form['lastname'].value
>> firstname =form['firstname'].value
>> message = firstname + " " + lastname
>> print reshtml % message
>> 
>
> HTH,
>
>
>   

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