[Tutor] updating a Csv file

2010-10-20 Thread Albert-Jan Roskam
Hi all,



How can I update a csv file? I've written the code below, but it does 
not work ("Error: line contains NULL byte"). I've never tried opening a 
file in read and write mode (r+) at the same time before, so I suspect 
that this is the culprit. Should I instead just open one file, write to 
another, throw away the original and rename the new file? That seems 
inefficient.



import csv, string



def updateLine(idVar, idValue, myCsv, newLine):

    f = open(myCsv, "r+")

    r = csv.reader(f)

    w = csv.writer(f)

    header = r.next()

    idPos = header.index(idVar)

    for row  in r:

    if row[idPos] == idValue:

    row = newLine

    w.writerow(row)

    f.close()



updateLine(idVar = "Id",

   idValue = "hawxgXvbfu",

   myCsv = "c:/temp/someCsv.csv",

   newLine = [ch for ch in string.letters[0:9]])

Cheers!!

Albert-Jan



~~

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

~~


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


Re: [Tutor] updating a Csv file

2010-10-20 Thread Steven D'Aprano
On Wed, 20 Oct 2010 08:05:06 pm Albert-Jan Roskam wrote:
> Hi all,
>
>
>
> How can I update a csv file? I've written the code below, but it does
> not work ("Error: line contains NULL byte").

Please copy and paste (DO NOT re-type, summarize, paraphrase or repeat 
from memory) the ACTUAL exception, including the traceback.

Based on the message shown completely out of context, I'm guessing that 
you're trying to stuff binary data into a CSV file, which needs plain 
text. But there doesn't seem to be anything in your code snippet that 
includes binary data, so I suspect that the code you are actually 
running is not the same as the code you think you are running.

Either that or you have modified string.letters to include things which 
are not letters. It might help if you mention what version of Python 
you're using, and the platform.



[...]
>    newLine = [ch for ch in string.letters[0:9]]

What's wrong with this?

newline = list(string.letters[0:9])



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


Re: [Tutor] Problem with python

2010-10-20 Thread Steven D'Aprano
On Wed, 20 Oct 2010 08:02:43 am Matthew Nunes wrote:

> I cannot understand the how it claimed the code executed, and
> logically it makes no sense to me. Please explain it to me, as I
> have spent hours trying to get my head around it, as the book(in my
> opinion) did not explain it well. Here it is: 

> def factorial(n):
> if n == 0:
> return 1
> else:
> recurse = factorial(n-1)
> result = n * recurse
> return result

To be pedantic, the above code will raise a SyntaxError, because you've 
ignored or lost the indentation. Fortunately in this case it's easy to 
fix:


def factorial(n):
if n == 0:
return 1
else:
recurse = factorial(n-1)
result = n * recurse
return result


Let's start with an easy example: factorial(0). When Python sees 
factorial(0), it does this:

Call factorial with argument 0:
* if n == 0 <-- this is true, so execute the "if" block: 
return 1

so the function factorial(0) returns the value 1. All that goes on 
behind the scenes. What *we* see is:

>>> factorial(0)
1

That part is easy. Now, the recursive part. What does Python do when you 
call factorial(1)?

Call factorial with argument 1:
* if n == 0 <-- this is false, so execute the "else" block: 
recurse = factorial(n-1)

Here Python is calling a function. It happens to be the same function, 
but Python doesn't care about that, and neither should you. So Python 
simply continues:

Call factorial with argument 1-1 = 0:

Now, we've already seen this above, but Python does the simplest thing 
that could work: it calculates the answer from scratch:

if n == 0 <-- this is true, so execute the "if" block:
return 1

Now Python has a value it can assign to recurse, and so it can continue:

recurse = factorial(n-1) = factorial(0) = 1
result = n*recurse = 1*1 = 1
return 1

Wait a second... a couple of lines back, I said n was 1, then I said it 
was 0, and now I'm claiming it is 1 again! What gives?

The difference is that each call to factorial() gets its own local 
variable, n. When you call factorial(1), Python ends up calling the 
function twice: once with n=1, then BEFORE that calculation is 
finished, with n=0, then it falls back to the unfinished calculation.

Here's an example that might explain things better:

def factorial(n):
print("Starting factorial calculation with n = %d" % n)
if n == 0:
print("Done with n = %d -- returning 1" % n)
return 1
else:
print("About to call factorial again...")
recurse = factorial(n-1)
result = n*recurse
print("Done with n = %d -- returning %d" % (n, result))
return result



And here it is in action:


>>> factorial(0)
Starting factorial calculation with n = 0
Done with n = 0 -- returning 1
1
>>> factorial(1)
Starting factorial calculation with n = 1
About to call factorial again...
Starting factorial calculation with n = 0
Done with n = 0 -- returning 1
Done with n = 1 -- returning 1
1
>>>
>>> factorial(2)
Starting factorial calculation with n = 2
About to call factorial again...
Starting factorial calculation with n = 1
About to call factorial again...
Starting factorial calculation with n = 0
Done with n = 0 -- returning 1
Done with n = 1 -- returning 1
Done with n = 2 -- returning 2
2
>>>
>>>
>>> factorial(5)
Starting factorial calculation with n = 5
About to call factorial again...
Starting factorial calculation with n = 4
About to call factorial again...
Starting factorial calculation with n = 3
About to call factorial again...
Starting factorial calculation with n = 2
About to call factorial again...
Starting factorial calculation with n = 1
About to call factorial again...
Starting factorial calculation with n = 0
Done with n = 0 -- returning 1
Done with n = 1 -- returning 1
Done with n = 2 -- returning 2
Done with n = 3 -- returning 6
Done with n = 4 -- returning 24
Done with n = 5 -- returning 120
120




Notice the pattern of n values: the *first* n value that is used is the 
*last* value to be finished with.


Hope this helps!



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


Re: [Tutor] updating a Csv file

2010-10-20 Thread Dave Angel

 On 2:59 PM, Albert-Jan Roskam wrote:

Hi all,



How can I update a csv file? I've written the code below, but it does
not work ("Error: line contains NULL byte"). I've never tried opening a
file in read and write mode (r+) at the same time before, so I suspect
that this is the culprit. Should I instead just open one file, write to
another, throw away the original and rename the new file? That seems
inefficient.



import csv, string

def updateLine(idVar, idValue, myCsv, newLine):
 f = open(myCsv, "r+")
 r = csv.reader(f)
 w = csv.writer(f)
 header = r.next()
 idPos = header.index(idVar)
 for row  in r:
 if row[idPos] == idValue:
 row = newLine
 w.writerow(row)
 f.close()

updateLine(idVar = "Id",
idValue = "hawxgXvbfu",
myCsv = "c:/temp/someCsv.csv",
newLine = [ch for ch in string.letters[0:9]])

Cheers!!

Albert-Jan

In general, you don't want to update anything in place, unless the new 
items are guaranteed to be the same size as the old.  So when you're 
looping through a list, if you replace one item with another, no 
problem, but if you insert or delete, then you should be doing it on a copy.


In the file, the byte is your unit of measure.  So if what you're 
writing might be a different size (smaller or larger), then don't do it 
in place.  You may not be able to anyway, if the csv module can't handle 
it.  For example, the file object 'f' has a position, which is set by 
either read or write.  csv may assume that they can count on that 
position not changing from outside influences, in which case even 
same-size updates could fail.


DaveA



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


[Tutor] get_browser() in python

2010-10-20 Thread Norman Khine
is there an equivalent module like the php's get_browser()
http://php.net/manual/en/function.get-browser.php function?

thanks

-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] updating a Csv file

2010-10-20 Thread Albert-Jan Roskam
Hi Steven and Dave,

Thanks for replying to me. Steven, I pasted the traceback below, along with 
some version info. I'm using Windows XP. I used the very same code as the one I 
posted before, including the string.letters bit. Btw, thanks for the little 
'list' tip.

Dave, what you say makes sense. I tried updating the old values with new values 
of exactly the same length (10 characters), but that didn't work either (I 
simply used [ch * 10 for ch in string.letters[0:9]] --list trick not possible 
here ;-)). I pasted the contents of my test csv file below the traceback.

Anyway, thanks again for your time!

Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on 
win32
Type "copyright", "credits" or "license()" for more information.

  
IDLE 2.6  
>>> 

Traceback (most recent call last):
  File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 18, in 

    newLine = [ch for ch in string.letters[0:9]])
  File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 10, in 
updateLine
    for row  in r:
Error: line contains NULL byte
>>> 

Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy
MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w
hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww
JSIXUYMxMt,CNebFXwmtZ,GTHQMyYUwT,XgRdYuFtfY,WyIeoiqqnC,SpbJWgDsHo,ZEuIXNujUd,www
hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww

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 Wed, 10/20/10, Dave Angel  wrote:

From: Dave Angel 
Subject: Re: [Tutor] updating a Csv file
To: "Albert-Jan Roskam" 
Cc: "Python Mailing List" 
Date: Wednesday, October 20, 2010, 12:23 PM

  On 2:59 PM, Albert-Jan Roskam wrote:
> Hi all,
>
>
>
> How can I update a csv file? I've written the code below, but it does
> not work ("Error: line contains NULL byte"). I've never tried opening a
> file in read and write mode (r+) at the same time before, so I suspect
> that this is the culprit. Should I instead just open one file, write to
> another, throw away the original and rename the new file? That seems
> inefficient.
>
>
>
> import csv, string
>
> def updateLine(idVar, idValue, myCsv, newLine):
>      f = open(myCsv, "r+")
>      r = csv.reader(f)
>      w = csv.writer(f)
>      header = r.next()
>      idPos = header.index(idVar)
>      for row  in r:
>          if row[idPos] == idValue:
>              row = newLine
>              w.writerow(row)
>      f.close()
>
> updateLine(idVar = "Id",
>             idValue = "hawxgXvbfu",
>             myCsv = "c:/temp/someCsv.csv",
>             newLine = [ch for ch in string.letters[0:9]])
>
> Cheers!!
>
> Albert-Jan
>
In general, you don't want to update anything in place, unless the new 
items are guaranteed to be the same size as the old.  So when you're 
looping through a list, if you replace one item with another, no 
problem, but if you insert or delete, then you should be doing it on a copy.

In the file, the byte is your unit of measure.  So if what you're 
writing might be a different size (smaller or larger), then don't do it 
in place.  You may not be able to anyway, if the csv module can't handle 
it.  For example, the file object 'f' has a position, which is set by 
either read or write.  csv may assume that they can count on that 
position not changing from outside influences, in which case even 
same-size updates could fail.

DaveA






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


Re: [Tutor] get_browser() in python

2010-10-20 Thread Norman Khine
thanks

On Wed, Oct 20, 2010 at 4:12 PM, Greg  wrote:
> Forgot to send to list.
>
> On Wed, Oct 20, 2010 at 10:12 AM, Greg  wrote:
>> On Wed, Oct 20, 2010 at 9:03 AM, Norman Khine  wrote:
>>> is there an equivalent module like the php's get_browser()
>>> http://php.net/manual/en/function.get-browser.php function?
>>>
>>> thanks
>>>
>>> --
>>
>> get_browser() simply gets a browser's user agent string then looks it
>> up in a file.  So if you can have your app get the http headers (the
>> User-Agent header), you can do this yourself.
>>
>> --
>> Greg Bair
>> gregb...@gmail.com
>>
>
>
>
> --
> Greg Bair
> gregb...@gmail.com
>



-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Writing Scripts.

2010-10-20 Thread Autumn Cutter
I just started learning Python last night and I'm a little stuck on how to 
write a Python script. 
I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like 
Idle, Applescript and Textmate but I'm still unable to write a script and run 
it successfully. I'm wondering if I'm using the write program or if I 
understand correctly how to write and run scripts. Help! :(

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


Re: [Tutor] Writing Scripts.

2010-10-20 Thread शंतनू
Save the file. e.g. my_test_prog.py. Open 'terminal'. Run following command

'python my_test_prog.py'

On my 10.6.4 version of Mac OSX, I get following version of python.
===
$ /usr/bin/python
Python 2.6.1 (r261:67515, Dec 17 2009, 00:59:15) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
===


On 20-Oct-2010, at 9:43 PM, Autumn Cutter wrote:

> I just started learning Python last night and I'm a little stuck on how to 
> write a Python script. 
> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like 
> Idle, Applescript and Textmate but I'm still unable to write a script and run 
> it successfully. I'm wondering if I'm using the write program or if I 
> understand correctly how to write and run scripts. Help! :(
> 
> ___
> 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] Writing Scripts.

2010-10-20 Thread Alan Gauld


"Autumn Cutter"  wrote

I just started learning Python last night and I'm a little stuck on 
how to write a Python script.

I'm using Python 2.5.0 on a Mac OS X 10.6.4.


To create the program you use any standard text editor - although one 
that supports
programming will be better. So you can use any of the programs you 
listed.
All you need to do is create a new file containg your python code and 
save

it with a sensible name and end it in .py

So you could create a file called hello.py which contains the single 
line:


print "Hello"


After you save it open the Terminal application and navigate to the 
folder
where you saved the file. ( ISTR that you can do that by dragging a 
folder
from Finder onto the Terminal... if I'm dreaming use the cd 
command...)


Now type

python hello.py

at the Unix prompt to get the code to run. You should see hello 
printed on screen.


Try that and if it doesn't work tell us what happened, including the 
exact text of

any error messages you see.

There are better ways to run the programs once you get itall set up 
properly

but for now we will be best to stick to basics!

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] updating a Csv file

2010-10-20 Thread Alan Gauld


"Albert-Jan Roskam"  wrote

I tried updating the old values with new values of exactly the same 
length (10 characters),

but that didn't work either



newLine = [ch for ch in string.letters[0:9]])
Error: line contains NULL byte



Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy


There are 8 field headingss here but newline is a list with 9 
values...



MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w


And this has 8 fields, but they are not all single characters,
nor are they all 10 characters so I'm not sure how you think
you are writing out the same number of characters?


...Should I instead just open one file, write to
another, throw away the original and rename the new file? That seems
inefficient.


This is by far the most common design pattern. It is safer and allows
you to roll back to the old version in the event of an error (if you 
save

the original as a .bak or similar).

HTH,

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


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


[Tutor] csv DictReader/Writer question

2010-10-20 Thread Ara Kooser
Hello all,

  I have a csv file (from a previous code output). It looks like this:
Species2, Protein ID, E_value, Length, Hit From, Hit to, Protein ID2, Locus
Tag, Start/Stop, Species
Streptomyces sp. AA4,  ZP_05482482,  2.82936001e-140,  5256,  1824,
2249\n, ZP_05482482,  StAA4_0101000304844,
complement(NZ_ACEV0178.1:25146..40916)4,  Streptomyces sp. AA4: 0\n
Streptomyces sp. AA4,  ZP_05482482,  8.03332997e-138,  5256,  123,
547\n, ZP_05482482,  StAA4_0101000304844,
complement(NZ_ACEV0178.1:25146..40916)4,  Streptomyces sp. AA4: 0\n
Streptomyces sp. AA4,  ZP_05482482,  1.08889e-124,  5256,  3539,  3956\n,
ZP_05482482,  StAA4_0101000304844,
complement(NZ_ACEV0178.1:25146..40916)4,  Streptomyces sp. AA4: 0\n


I want to removing certain sections in each line so I wrote this code using
csv.DictWriter:
import csv
data = csv.DictReader(open('strep_aa.txt'))

for x in data:
del x['Species2']
del x[' Protein ID2']
print x

  When it prints to the screen everything works great:
{' Hit From': '  1824', ' Hit to': '  2249\\n', ' Protein ID': '
ZP_05482482', ' Locus Tag': '  StAA4_0101000304844', ' Start/Stop': '
complement(NZ_ACEV0178.1:25146..40916)4', ' Species': '  Streptomyces
sp. AA4: 0\\n', ' Length': '  5256', ' E_value': '
2.82936001e-140'}
{' Hit From': '  123', ' Hit to': '  547\\n', ' Protein ID': '
ZP_05482482', ' Locus Tag': '  StAA4_0101000304844', ' Start/Stop': '
complement(NZ_ACEV0178.1:25146..40916)4', ' Species': '  Streptomyces
sp. AA4: 0\\n', ' Length': '  5256', ' E_value': '
8.03332997e-138'}

What I don't know how to do is the export this back out a csv file and
rearrange each key as a column header so it work look like this:
Species  Protein ID  E Value  .

I thought csv.DictWriter would be the way to go since it writes dictionaries
to text files. I was wondering how do I go about doing this? I don't really
understand the syntax.

Thank you!

Ara


-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub
cardine glacialis ursae.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with python

2010-10-20 Thread Dave Kuhlman
On Wed, Oct 20, 2010 at 03:02:43AM +0600, Matthew Nunes wrote:
> 
>To whom it may concern,
> 
>   Hi, I've just started learning how
>to program in python using Allan B. Downy's book "How to think like a
>computer scientist" and it explained something in the recursion
>chapter which have still been unable to understand. It wrote a piece
>of code for the factorial function in math for example 3! is 3 * 2 *
>1. I cannot understand the how it claimed the code executed, and
>logically it makes no sense to me. Please explain it to me, as I have
>spent hours trying to get my head around it, as the book(in my
>opinion) did not explain it well. Here it is:
> 
> 
>def factorial(n):
> 
>if n == 0:
> 
>return 1
> 
>else:
> 
>recurse = factorial(n-1)
> 
>result = n * recurse
>return result
> 
>If there is and easier piece of code that you know of that can be used
>for factorial, if you could please also tell me that.
> 
>Thank you for your time.

The other replies have helped you understand that recursive
function.  But, recursion gets especially interesting when it is
applied to recursive data structures, for example data whose
structure is the same as the data that it contains.  A tree
structure whose nodes all have the same structure is a good
example.

The code that follows constructs a tree out of objects that are
instances of class Node, then walks that tree and prints it out. 
This code provides two recursive ways to walk and display that
tree: (1) the "show" method in class Node and (2) the "shownode"
function.

Often processing recursive structures like trees is trivial with a
recursive function or method.

Keep in mind that this code is well behaved only when the data
structure we apply it to has a reasonable maximum depth.

Hope this example helps.

- Dave


# 
class Node(object):
def __init__(self, data, children=None):
self.data = data
if children is None:
self.children = []
else:
self.children = children
def show(self, level):
print '%sdata: %s' % ('' * level, self.data, )
for child in self.children:
child.show(level + 1)

def shownode(node, level):
print '%sdata: %s' % ('' * level, node.data, )
for child in node.children:
child.show(level + 1)

def test():
n1 = Node('')
n2 = Node('')
n3 = Node('')
n4 = Node('')
n5 = Node('', [n1, n2])
n6 = Node('', [n3, n4])
n7 = Node('', [n5, n6])
n7.show(0)
print '=' * 20
shownode(n7, 0)

test()
# 




-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Stopping a webservice

2010-10-20 Thread Timo
Hello,

I have written a Python script that constantly checks for incoming
connections. It is a class and runs a while loop until shutdown is set to
True. Something like:

class Connection(object):
def __init__(self):
self.shutdown = False

def mainloop(self):
while not self.shutdown:
print "We keep checking..."

I have written a GUI and when I click the Quit-button, I do
"connection.shutdown = True", which works great.

The plans are to run this on my webserver. I created a webpage with a start
and stop button, the starting works great. But how could I remember this
class to call "class.shutdown = True" any given time when I press the stop
button?

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


Re: [Tutor] Stopping a webservice

2010-10-20 Thread Alan Gauld


"Timo"  wrote


I have written a GUI and when I click the Quit-button, I do
"connection.shutdown = True", which works great.

The plans are to run this on my webserver. I created a webpage with 
a start
and stop button, the starting works great. But how could I remember 
this
class to call "class.shutdown = True" any given time when I press 
the stop

button?


There arec several ways but one would be to pass the connection
object ID as a cookie and then when Quiting usend the cookie data to 
the
server which ises it to referenbce the connection object - a 
dictionary

might be useful here...

Thats assuming you havf multiple connection objects running, maybe 
even
one per client sesssion. If its a global conmnection objerct then just 
store

it as a global object (maybe in its own module imported by all other
modules?)

There are several other options but those are probably the two 
simplest

depending on your use case.

HTH,

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


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


[Tutor] LCM

2010-10-20 Thread Jacob Bender

 Hello all,

I was wondering if you could please help me with an issue I'm having 
with my program. I'm not fond of LCM(Least Common Multiples), and I 
decided to make a program that deals with them. Here's the code:


thing = raw_input("What is the first number?\n\n")

thing2 = raw_input("What is the second number?\n\n")

int(thing)
int(thing2)

x = 1

thing3 = x/thing
thing4 = thing2/x

def calc():
while True:

if isinstance(thing3, int) == True:
if isinstance(thing4, int)== True:
print "The LCM is"
print x
raw_input()
break
else:
x + 1
calc()


else:
x + 1
calc()

calc()

The only problem is that when I tell it that thing3 = x/thing it gives 
an error. Here it is:


Traceback (most recent call last):
  File "C:/Documents and Settings/Family/My Documents/LCM.py", line 10, 
in 

thing3 = x/thing
TypeError: unsupported operand type(s) for /: 'int' and 'str'

Same with thing4...

Can you please help! Thanks in advance...



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


Re: [Tutor] LCM

2010-10-20 Thread christopher . henk
benderjaco...@gmail.com wrote on 10/20/2010 07:30:32 PM:

> 
> thing = raw_input("What is the first number?\n\n")
> 
> thing2 = raw_input("What is the second number?\n\n")
> 
> int(thing)
> int(thing2)
> 
The two lines above do the calculation of turning your input 
strings into int's but do not save the value anywhere.
You want to assign the calculation to a variable (can reuse thing 
and thing2 if you wish) and then use that calculation later on.



> 
> The only problem is that when I tell it that thing3 = x/thing it gives 
> an error. Here it is:
> 
> Traceback (most recent call last):
>File "C:/Documents and Settings/Family/My Documents/LCM.py", line 10, 

> in 
>  thing3 = x/thing
> TypeError: unsupported operand type(s) for /: 'int' and 'str'

the error here is telling you that thing is a string and python cannot 
divide an int by a string.

> 
> Same with thing4...
> 
> Can you please help! Thanks in advance...

I didn't check the rest of the program.  But that should at least get you 
moving along.

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


Re: [Tutor] LCM

2010-10-20 Thread Shashwat Anand
On Thu, Oct 21, 2010 at 5:00 AM, Jacob Bender wrote:

>  Hello all,
>
> I was wondering if you could please help me with an issue I'm having with
> my program. I'm not fond of LCM(Least Common Multiples), and I decided to
> make a program that deals with them. Here's the code:
>
> thing = raw_input("What is the first number?\n\n")
>
> thing2 = raw_input("What is the second number?\n\n")
>
> int(thing)
> int(thing2)
>
> x = 1
>
> thing3 = x/thing
> thing4 = thing2/x
>
> def calc():
>while True:
>
>if isinstance(thing3, int) == True:
>if isinstance(thing4, int)== True:
>print "The LCM is"
>print x
>raw_input()
>break
>else:
>x + 1
>calc()
>
>
>else:
>x + 1
>calc()
>
> calc()
>
>
Why this much bother.
Use:

import fractions
solution = int(thing) * int(thing2) / ( fraction.gcd(int(thing),
int(thing2)) )

The property being used is for two numbers a,b ; lcm(a, b)* gcd(a, b) = a *
b


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


[Tutor] Importing photo-python

2010-10-20 Thread steven nickerson

I would like to somehow import and output a photo to the screen via tkinter 
window

not very described sorry, I just would like to know how i can get photos into 
python ex. .gif (In photo format not video), .jpg ect.

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


[Tutor] deepak mishra has invited you to open a Google mail account

2010-10-20 Thread deepak mishra
I've been using Gmail and thought you might like to try it out. Here's an
invitation to create an account.


  You're Invited to Gmail!

deepak mishra has invited you to open a Gmail account.

Gmail is Google's free email service, built on the idea that email can be
intuitive, efficient, and fun. Gmail has:

 *Less spam*
Keep unwanted messages out of your inbox with Google's innovative
technology.

*Lots of space*
Enough storage so that you'll never have to delete another message.

*Built-in chat*
Text or video chat with deepak mishra and other friends in real time.

*Mobile access*
Get your email anywhere with Gmail on your mobile phone.

You can even import your contacts and email from Yahoo!, Hotmail, AOL, or
any other web mail or POP accounts.

Once you create your account, deepak mishra will be notified of your new
Gmail address so you can stay in touch. Learn
moreor get
started
!
Sign 
up

Google Inc. | 1600 Ampitheatre Parkway | Mountain View, California 94043
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing Scripts.

2010-10-20 Thread Connor Neblett
Alternately, I believe that MacPython comes packaged with an application called 
"Python Launher".
If you right click a python file (.py) once this is app is installed, you can 
select "Open With -> Python Launcher"
This method is more for people that are uncomfortable with the command line 
(Terminal.app)

As a side note, I would recommend installing a newer version of Python.  (3.1.2)


On Oct 20, 2010, at 6:46 PM, शंतनू wrote:

> Save the file. e.g. my_test_prog.py. Open 'terminal'. Run following command
> 
> 'python my_test_prog.py'
> 
> On my 10.6.4 version of Mac OSX, I get following version of python.
> ===
> $ /usr/bin/python
> Python 2.6.1 (r261:67515, Dec 17 2009, 00:59:15) 
> [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
> ===
> 
> 
> On 20-Oct-2010, at 9:43 PM, Autumn Cutter wrote:
> 
>> I just started learning Python last night and I'm a little stuck on how to 
>> write a Python script. 
>> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like 
>> Idle, Applescript and Textmate but I'm still unable to write a script and 
>> run it successfully. I'm wondering if I'm using the write program or if I 
>> understand correctly how to write and run scripts. Help! :(
>> 
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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