[Tutor] walking directories

2005-01-07 Thread Nick Lunt
Hi folks,

Im running python2.4 on linux.

I have many python scripts in various directories of varying depth under my 
home directory, and I need to change one line in each of these files.

I thought about using os.walk with os.path.join eg

>>> for r,d,f in os.walk('.'):
... print os.path.join(r,d,f)

but I get this error
Traceback (most recent call last):
  File "", line 2, in ?
  File "/usr/local/lib/python2.4/posixpath.py", line 60, in join
if b.startswith('/'):
AttributeError: 'list' object has no attribute 'startswith'

which makes sense.

I know I can use glob.glob on the current directory, but Im struggling to see 
how I can access these files, change them and save the changes.

Im also a little annoyed with myself cos I feel that I really should know how 
to do this by now.

If anyone can just give me a pointer in the right direction I would be very 
grateful.

Many thanks
Nick .

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


RE: [Tutor] walking directories

2005-01-08 Thread Nick Lunt
>>From: Ryan Davis [mailto:[EMAIL PROTECTED]
??
>>I think you want to be doing something like:
>>
>>>for r,d,f in os.walk('.'):
>>...   for filename in f:
>>...   print os.path.join(r,filename)
>>
>>I think that would give you the full path of every file, and then you can
open it, do a regex substitution or whatever close it and
>>keep going.
>>
>>From: Jacob Schmidt
>>
>>for root, dirs, files in os.walk(directory):
>>for x in files:
>>print os.path.join(root,x)

Thankyou Ryan and Jacob, I obviously did not read the docs for os.walk
correctly, many thanks.

>>Alan G.
>>personally I'd use find and sed in Linux, right tool
>>for the job etc..

Yes I could use *nix tools for the job, but I want to do it in python to see
if I can :)

Thanks again everyone for your help.

Nick .



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005

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


FW: [Tutor] walking directories

2005-01-09 Thread Nick Lunt
Hello,

I sent this reply a few days ago, but it doesn't seem to have appeared on
the list, so Im resending it.

Many thanks
Nick .


-Original Message-
From: Nick Lunt [mailto:[EMAIL PROTECTED]
Sent: 08 January 2005 19:44
To: 'python tutor'
Subject: RE: [Tutor] walking directories


>>From: Ryan Davis [mailto:[EMAIL PROTECTED]
??
>>I think you want to be doing something like:
>>
>>>for r,d,f in os.walk('.'):
>>...   for filename in f:
>>...   print os.path.join(r,filename)
>>
>>I think that would give you the full path of every file, and then you can
open it, do a regex substitution or whatever close it and
>>keep going.
>>
>>From: Jacob Schmidt
>>
>>for root, dirs, files in os.walk(directory):
>>for x in files:
>>print os.path.join(root,x)

Thankyou Ryan and Jacob, I obviously did not read the docs for os.walk
correctly, many thanks.

>>Alan G.
>>personally I'd use find and sed in Linux, right tool
>>for the job etc..

Yes I could use *nix tools for the job, but I want to do it in python to see
if I can :)

Thanks again everyone for your help.

Nick .



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.9 - Release Date: 06/01/2005

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


Re: [Tutor] 3 questions for my port scanner project

2005-02-26 Thread Nick Lunt
Nr 3. If your using python sockets try socket.settimeout(x) 

Nick .


On Sat, 2005-02-26 at 15:12 +0200, Mark Kels wrote:
> Hi list.
> 
> Here are the questions (-:
> 1. How do I make a progress bar in Tkinter ?
> 2. I got a while loop which does the port scan itself. How can I end
> it while its working ?
> 3. For some reason the scan is too slow (2-3 seconds for a port). Is
> there a way to make it faster (other port scanner work allot faster...
> ) ?
> 
> Thanks in advance !! 
> 

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


Re: [Tutor] sys.argv[1: ] help

2005-02-27 Thread Nick Lunt
Richard,

if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0]
then you are bound to get an empty list returned, [] .

Im not sure I understand the problem you think you've got but here's
what happens with sys.argv for me, and it's correct.

[argl.py]

$ cat argl.py
#!/usr/bin/python

import sys
print sys.argv[1:]


./argl.py
[]

./argl.py a b c
['a', 'b', 'c']

Is that what your getting ? 



> Sorry for the late response, I tried all of the the suggestions, 
> including correcting my typo of print sys[1:] and tried print 
> sys,argv[1:], this does now work as long as I run 'python test.py fred 
> joe' it returns all the arguments. If I try just test.py all I get is 
> '[]' . Is there something wrong with my environmental variables in 
> Windows XP, I would like to be able to just use the file name rather 
> than having to type python each time. Any help would be gratefully received.
> 
> Richard 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


[Tutor] reading from stdin

2005-03-01 Thread Nick Lunt
Hi folks,

I've been pondering how to get python to read from a pipe outside of
itself, sort of.

For example I tried a simple python prog to do a grep, eg

# ps -e | myprog.py cron

would give this output

3778 ?00:00:00 crond

same as 
# ps -e | grep cron

The way I did this was to use sys.stdin.readlines() to get the output
from the pipe.

Here is the program:

[code]
import sys, glob
args = sys.stdin.readlines() # found on the net
pat = sys.argv[1]
for i in args:
if (i.find(pat) != -1):
print i,
[/code]

My question is am I getting the output from the pipe in the correct
way ? The way Im doing it works (so far) but should I be doing it
another way ?

Many thanks
Nick .


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


Re: [Tutor] reading from stdin

2005-03-01 Thread Nick Lunt
On Tue, 2005-03-01 at 14:14 -0800, Sean Perry wrote:

> 
> unless you want the output for some other reason, a more idiomatic way
> is:
> 
> for line in sys.stdin.readlines():
>  # handle the line
> 
> I tend to use xreadlines() which does not read the entire input at once. 
>   For stdin this make sense, you have no idea how much data will be 
> piped in.

Thanks Sean, I agree with you on both accounts there.

Cheers
Nick .


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


Re: [Tutor] reading from stdin

2005-03-01 Thread Nick Lunt
On Tue, 2005-03-01 at 22:20 +, Max Noel wrote:

>   I don't think you are. You're using readlines(), which means your 
> program won't execute until ps terminates.
>   UNIX philosophy is to have programs start acting as soon as possible 
> -- in that case, as soon as the first line is available. You should be 
> reading sys.stdin as an iterator (same thing you'd do for a file):
> 
> import sys
> for line in sys.stdin:
>   # do stuff with that line of input

Aha, that makes sense. Thanks very much.

Nick .


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


Re: [Tutor] reading from stdin

2005-03-01 Thread Nick Lunt
On Wed, 2005-03-02 at 11:22 +1300, [EMAIL PROTECTED] wrote:
> Quoting Sean Perry <[EMAIL PROTECTED]>:
> 
> > for line in sys.stdin.readlines():
> >  # handle the line
> > 
> > I tend to use xreadlines() which does not read the entire input at once.
> 
> xreadlines() these days just does 'return self', I believe.  File objects are
> their own iterators; you can just do:
> 
> for line in sys.stdin:
> # do stuff
> 

Same as Max Noel said, must be a good idea ;)

Thankyou
Nick .

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


Re: [Tutor] reading from stdin

2005-03-02 Thread Nick Lunt
Thanks to everyone who helped me with this.
It's certainly given me something to think about :)

Cheers
Nick .


On Tue, 2005-03-01 at 23:13 -0600, David Rock wrote:
> * Nick Lunt <[EMAIL PROTECTED]> [2005-03-01 22:23]:
> > On Tue, 2005-03-01 at 14:14 -0800, Sean Perry wrote:
> > 
> > > 
> > > unless you want the output for some other reason, a more idiomatic way
> > > is:
> > > 
> > > for line in sys.stdin.readlines():
> > >  # handle the line
> > > 
> > > I tend to use xreadlines() which does not read the entire input at once. 
> > >   For stdin this make sense, you have no idea how much data will be 
> > > piped in.
> > 
> > Thanks Sean, I agree with you on both accounts there.
> 
> For another approach to this, I like to use the fileinput module. In the
> case of the original example of mimicing grep, it allows you to easily
> handle both a list of files passed to the command or use it as a pipe.
> The default action if there are no files given is to use stdin.
> 
> http://www.python.org/doc/2.4/lib/module-fileinput.html
> 
> ___
> 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] reading from stdin

2005-03-02 Thread Nick Lunt
Hi Hugo,

many thanks for pointing that out. It all helps :)

Thanks again,
Nick .


On Tue, 2005-03-01 at 17:35 -0600, Hugo GonzÃlez Monteverde wrote:
> Everypne else has answered pretty muh about this. I just want to add 
> that if you want to read noncanonically (less thana line ending in "\n" 
> you'll have to do it char by char =( AFAIK, there's no way to redefine a 
> separator por readlines() (other than \n..)
> 
> Hugo
> 
> Nick Lunt wrote:
> > Hi folks,
> > 
> > I've been pondering how to get python to read from a pipe outside of
> > itself, sort of.
> > 
> > For example I tried a simple python prog to do a grep, eg
> > 
> > # ps -e | myprog.py cron
> > 
> > would give this output
> > 
> > 3778 ?00:00:00 crond
> > 
> > same as 
> > # ps -e | grep cron
> > 
> > The way I did this was to use sys.stdin.readlines() to get the output
> > from the pipe.
> > 
> > Here is the program:
> > 
> > [code]
> > import sys, glob
> > args = sys.stdin.readlines() # found on the net
> > pat = sys.argv[1]
> > for i in args:
> > if (i.find(pat) != -1):
> > print i,
> > [/code]
> > 
> > My question is am I getting the output from the pipe in the correct
> > way ? The way Im doing it works (so far) but should I be doing it
> > another way ?
> > 
> > Many thanks
> > Nick .
> > 
> > 
> > ___
> > 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] [HELP]how to test properties of a file

2005-04-04 Thread Nick Lunt
I've gotten into the habit of just using the os.?_OK stuff.
eg
>>> import os
>>> os.access('/', os.W_OK)
False
>>> os.access('/tmp', os.W_OK)
True
Thats gotta be simple if I understand it lol :)
Nick .
Alan Gauld wrote:
The simplest, IMHO, is :
try:
  f = file(filename, "w")
  [...]
except IOError:
  print "The file is not writable"
Of course, not that this method empty the file if it is writable !
   

The
 

best is to just put your IO code in such a try block ... That way,
you're sure the file has the right mode.
   

Its not really the simplest, its not efficient and it might be
dangerous
if the file is not empty. At the very least open using 'a' to avoid
obliterating the file!!
However the os.stat function and stat module do what you want safely
and more comprehensively:
---
Help on module stat:
NAME
   stat - Constants/functions for interpreting results of
   os.stat() and os.lstat().
FILE
   /usr/lib/python2.3/stat.py
DESCRIPTION
   Suggested usage: from stat import *
DATA
   ST_ATIME = 7
   ST_CTIME = 9
   ST_DEV = 2
   ST_GID = 5
   ST_INO = 1
   ST_MODE = 0
   ST_MTIME = 8
   ST_NLINK = 3
   ST_SIZE = 6
   ST_UID = 4
   S_ENFMT = 1024
-
The constants above are the indices into the tuple returned by
os.stat()
That will tell you most of the things you need to know,
check the docs to find out what they all mean.
For your purposes the important one is ST_MODE.
So
import os
from stat import *
status = os.stat('somefile.txt')[ST_MODE]
if status & S_IWRITE: print 'writable'
elif staus &  S_IREAD: print 'readable'
else: print 'status is ', status
Notice you have to use bitwise AND (&) to extract the status bits.
HTH
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] [HELP]how to test properties of a file

2005-04-05 Thread Nick Lunt
Shidai Liu wrote:
   

I found out in the following situation, it fails to work.
Say, 'somefile.csv' is opened by EXCEL,
 

os.access('somefile.csv', os.W_OK)
   

True
 

file('somefile.csv', 'w')
   

IOError: [Errno 13] Permission denied: 'somefile.csv'
By the way, using os.stat & stat as suggested by Alan doesn't work either.
Any idea how solve it?
 

I havent got access to a windows box unfortunately at the moment. And I 
have no idea about permissions on windows boxes anyway :)
However, did you already have the .csv file open in Excel or some other 
app ? Or is is password protected maybe ?

Sorry not much help.
Nick .
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] clock.py

2005-05-23 Thread Nick Lunt
Feziwe Mpondo wrote:

>clock.py,problem is to get the last two digits to be random.her's what i 
>tried
>from time import time,ctime
>prev_time = ""
>while(1):
>the_time = ctime()
>if (prev_time != the_time):
> print "The time is :",ctime(time())
>prev_time = the_time
>guess = 0
>number = 1-60
>while guess != number:
>guess = input ("Guess the last two digits:")
>if guess > number:
>print "Too high"
>elif guess < number:
>print "Too low"
>print "Just right"
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>

Hi Feziwe,

you might want to look at the random module to generate 'guess'
ramdon.randrange() more specifically.

Nick .

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


Re: [Tutor] Are Empty "Placeholder" Functions possible?

2005-05-26 Thread Nick Lunt
Hi John,

you can use 'pass' . This works the same as with exceptions, eg

try:
open('somefile')
except IOError:
pass

so we can have

class doNothing:
def __init__(self):
pass
def boring(self, other):
pass

Hope that helps :)
Nick

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Gooch, John
Sent: 26 May 2005 16:43
To: tutor@python.org
Subject: [Tutor] Are Empty "Placeholder" Functions possible?


Is there a way to create an empty function definition with no lines of code
in it? In my coding style I will often do this ( in other languages ) for
RAD just to remind myself that I will need to implement the function later.

Example: For a file handling class, I may need functions such as
copy,delete,move,etc so I want to start off with:

class FileHandler:
def __init__(self):

def copy(self):

def delete(self):
.
.
.


then in my driver/aka testing file:

import FileHandler

def main()
MyHandler = FileHandler()
print "Handler Created."

main()




Right now, this won't work as the compliler will error on functions with
nothing in them. Is there a "no-op", "do nothing", or some similar command I
can put in the empty functions to keep the compiler/interpreter from
erroring on them? This would really help speed up my development so that I
can create the class skeleton quickly and then implement each function and
test it as I go.


Thank You,


John A. Gooch
Systems Administrator
IT - Tools
EchoStar Satellite L.L.C.
9601 S. Meridian Blvd.
Englewood, CO  80112
Desk: 720-514-5708


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


--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 266.11.17 - Release Date: 25/05/2005


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


[Tutor] Should I be thinking of threads for this ?

2005-08-25 Thread Nick Lunt
Hello folks,

I have the following code taken from the Twisted examples -

[code]
# filewatcher.py
from twisted.application import internet

def watch(fp):
fp.seek(fp.tell())
for line in fp.readlines():
sys.stdout.write(line)

import sys
from twisted.internet import reactor
s = internet.TimerService(1.0, watch, file(sys.argv[1]))
s.startService()
reactor.run()
s.stopService()
[/code]

I find this piece of code amazing and I am keen to put it to use.
If I run './filewatcher.py myfile' it will print out any changes made to 
'myfile', very similar to 'tail -f' .

Now if I want to have this program monitor several files at once I could 
run './filewatcher.py file1 file2 filex' or './filewatcher.py file1 & 
./filewatcher file2 & etc' both with minor modifications to the code,  
but I think that could cause performance problems relating to the OS.
So I'm thinking I will need to learn python threads (no bad thing) 
instead, but Im hoping that someone could tell me if that seems the best 
way to go ?
I will be getting to grips with python threads anyway but I'd appreciate 
any input on this.

Many thanks,
Nick .
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should I be thinking of threads for this ?

2005-08-27 Thread Nick Lunt
Hi Kent,


> >
> > [code]
> > # filewatcher.py
> > from twisted.application import internet
> >
> > def watch(fp):
> > fp.seek(fp.tell())
> > for line in fp.readlines():
> > sys.stdout.write(line)
> >
> > import sys
> > from twisted.internet import reactor
> > s = internet.TimerService(1.0, watch, file(sys.argv[1]))
> > s.startService()
> > reactor.run()
> > s.stopService()
> > [/code]
> >

>
> What performance problems you you anticipate? I don't know much
> about Twisted but my understanding is that tasks are run in a
> single thread when they are ready. In your case you are
> scheduling a simple task to run every second. I would think that
> you could schedule several such tasks and they would each run
> every second. If your task were time-consuming you might have to
> worry about doing something different but in this case I think it
> will be fine. Just try something like
>
> for name in sys.argv[1:]:
>   s = internet.TimerService(1.0, watch, file(name))
>   s.startService()
>

That's one way I was thinking of doing it. I'll run it like that on about 10
active files and see how it stacks up.

> twisted.protocols.basic.FileSender and
> twisted.internet.stdio.StandardIO look like they may be starting points.

Thanks for the twisted pointers. I've been using twisted for a little while
but it's such a massive thing that it can be difficult to fully understand
whats happening.

Thanks for you help,
Nick .

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


Re: [Tutor] Tail -f problem

2005-08-31 Thread Nick Lunt
Hi Alberto,


> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of
> Alberto Troiano
> Sent: 31 August 2005 22:01
> To: tutor@python.org
> Subject: [Tutor] Tail -f problem
>
>
> Hey
>
I thought about tail-f
> /var/log/radacct/max/detail but this thing opens a console and I
> won't end
> but how can I get the output of the command to python..

Have a look here for a possible solution to your tail -f problem
http://twistedmatrix.com/projects/core/documentation/examples/filewatch.py


> I know the second option has nothing to do with this forum but if anyone
> knows of any manual to upgrade MySQL over Linux Red HAt I would appreciate

If your using a recent Redhat then try this as root (im on a windows box at
the moment so this is untested)

$ yum upgrade mysql mysql-server

Naturally this will only work if your current mysql install is an RPM.

Cheers
Nick .


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


Re: [Tutor] how to create GUI for Python progs

2005-10-13 Thread Nick Lunt
Hi Olexiy,


> I'm really confused on the issue how to create windows, forms, etc. in
> Python & can't find any manual for that.
> Could you possibly advise me smth useful?
> --
> Best regards,
>
> Olexiy Kharchyshyn

this has come up quite a bit recently on the list.
I would recommend that you go here
http://pythoncard.sourceforge.net/index.html to get PythonCard and then go
here  http://pythoncard.sourceforge.net/walkthrough1.html to read the docs.
It will take all of 10 minutes to be able to create a decent GUI.

Hth,
Nick .

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


Re: [Tutor] how to alter list content

2005-10-13 Thread Nick Lunt
Hi marc,

> 
> i create a list of all JPG files with:
> >>> list = glob.glob('*.JPG')
> the content of 'list' is now:
> >>> print list
> ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
> 
> but what i want is this type of list:
> ['DSC1', 'DSC2', 'DSC3']
> i.e. the names w/o the file extension.
> 
> what's the easiest way of doing this?
> 
> marc

You need the split() method.
ie 
>> l = ['DSC1.JPG', 'DSC2.JPG', 'DSC3.JPG']
>> print [i.split('.')[0] for i in l] 

Hth,
Nick .

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


Re: [Tutor] Python 2.4.1 on Mndrk Linux 10.1 - path

2005-10-14 Thread Nick Lunt
Hi Andy,


> Behalf Of Andy Dani
>
> Python 2.3.2 came with the linux distribution which is located in
> /usr/lib.
>
> Installed 2.4.1 in /usr/local/Python-2.4.1
>
> updated /etc/profile path so IDLE or "python" would point to the
> latest version (2.4.1).
>
> PATH="$PATH:/usr/local/Python-2.4.1/:."
> export PATH
>
> But it is still pointing to the old one unless I go
> /use/local/Python-2.4.1/python.
>
> I want to be able to launch and run python 2.4.1, from the user
> account so I do not need to be "root" every time.

The default install of python on Mandrake probably install the python
executable in /usr/bin/python.
/usr/bin/ is in your PATH, so if you put PATH=$PATH:/usr/local/Python-2.4.1
in /etc/profile it will still pick up the default python install.

Try typing 'which python' at the command line and see what it picks up.
Chances are it will be /usr/bin/python.

You could solve this by either putting
PATH=/path/to/my/wanted/python/dir:$PATH in /etc/profile (or
~/.bash_profile) or by putting an alias in ~/.bashrc, such as 'alias
python='path/to/my/wanted/python/dir/python'.

What I must stress is that you do not overwrite your default python
installation, leave it as it is, otherwise many of mandrakes GUI tools will
no work.

I hope that helps, feel free to ask more questions about this as I have had
to overcome the same issue on RHEL serveral times.

Hth,
Nick .


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


Re: [Tutor] Python 2.4.1 on Mndrk Linux 10.1 - path

2005-10-15 Thread Nick Lunt
Hi Andy,


Andy Dani wrote:

>Thanks  Nick,
>
>Any idea about the second question? When I python from 
>/usr/loca/Python-2.4.1/bin, it does launch the interpreter but with an 
>traceback error saying there is some conflict with "import readline" line on 
>pythonrc.py file located somewhere in /etc/ directory. Is there any harm If I 
>comment the it out (import readline)?
>
>Thanks - Andy
>  
>
Im sorry I have no idea what problem your having with "import readline", 
I have never come across that error.

However, Im certain that one of the python tutors will be able to help 
you out.

Good Luck,
Nick .

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


[Tutor] Global var problem

2005-10-28 Thread Nick Lunt
Hi Folks,

messing about with classes I've come across something basic that I don't 
understand.

Take this class

class T:
def p(self):
print x

if __name__ == '__main__':
x = 1
t = T()
t.p()

This outputs 1

Now this
class T:
def p(self):
x += 1
print x

if __name__ == '__main__':
x = 1
t = T()
t.p()

This outputs
UnboundLocalError: local variable 'x' referenced before assignment

So I tried this

class T:
def p(self):
x += 1
print x

if __name__ == '__main__':
global x
x = 1
t = T()
t.p()

but that gives me the same UnboundLocalError exception.

This has got me confused. Why does the first example work ok, but not 
the second ?
And in the third example I get the same error even after declaring x to 
be global.

No doubt the answer is staring me in the face ... but I still can't see it.

Cheers
Nick .

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


Re: [Tutor] Global var problem

2005-10-29 Thread Nick Lunt
Alan Gauld wrote:

>> messing about with classes I've come across something basic that I 
>> don't understand.
>
>
> As you say this has nothing to do with classes its more basic. Its 
> about namespaces. Try reading the namespaces topic in my tutor for 
> more info.
>
> Meanwhile lets simplify by removing the class bit
>  
>
> def h():
>   global x
>   x += 1
>  print x
>
> h()   # prints 43


Many thanks to Alan, Andrei, Hugo and Welsey for helping me to 
understand where I was going wrong.
I thought I understood namespaces but _obviously_ not .

Thanks again,
Nick .

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


Re: [Tutor] new topic draft

2005-11-14 Thread Nick Lunt


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Alan Gauld

>
>
> I've just added an incomplete draft copy of my latest tutorial topic
> on using the Operating System from Python. The material that's
> there discusses the role of the OS and looks at file handling
> usng os/os.path/shutil etc.
>
> http://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm
>
> If anyone would like to take a look and provide feedback on
> general direction/depth etc that'd be greatly appreciated.

Thanks for that Alan. I always seem to get myself into difficulty with the
os.walk routine, but you explained it brilliantly there.

Have to admit, I couldn't be bothered to read the "So What is the Operating
System" bit tho cos as you say on the page, we don't need to know it. Now if
we were C programmers maybe .. :)

Thanks again,
Nick .

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


Re: [Tutor] smtplib alternative???

2005-11-20 Thread Nick Lunt
Hi Adisegna,

Lets say that you are [EMAIL PROTECTED] and you want to send email to
[EMAIL PROTECTED], [EMAIL PROTECTED] and [EMAIL PROTECTED] .

You setup SMTPLib to talk to the MX server for abc.com which all users on
abc.com use to send email.
SMTP connects to that email server and you can send emails to anyone you
like (unless theyre blacklisted).

So by telling python to use [EMAIL PROTECTED] (your ISP's mail server) you
can send email to [EMAIL PROTECTED] (obviously), [EMAIL PROTECTED] (because 
your a member of
abc.com) and [EMAIL PROTECTED] (again cos your a member of abc.com).

However, if you want your emails to be received with a different address, ie
your using the isp abc.com and you want to send an email to [EMAIL PROTECTED] 
and it
to appear to have come from [EMAIL PROTECTED] then you will need to tell
pythons smptlib to connect to the MX server for pinkstuff.com to send the
email, and you'll probably have to be dialled in to pinkstuff.com.

Webmail MX servers differ in that they require authentication so it doesnt
matter which ISP your dialled in from.

Generally smtp servers are only configured to send to or from domains in
which they belong.

Hope that helps,
Nick .


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of
Adisegna
Sent: 20 November 2005 14:26
To: Danny Yoo
Cc: tutor@python.org
Subject: Re: [Tutor] smtplib alternative???


Hi Danny,

Yes, when sending an email your mail client will always send the email to
the mail server specified by the MX record in the authoritive domain.
Usually the domain specificed after the @ symbol. The problem with smtplib
is that I have to specify the mail server I'm sending email too. What if I
wanted to send an email to 3 different people on three different domains
hosted by 3 different mail servers? Smtlib prohibits this functionality. Do
you see what I mean now...?

Thanks for replying...


On 11/20/05, Danny Yoo < [EMAIL PROTECTED]> wrote:


On Sat, 19 Nov 2005, Adisegna wrote:

> I found this script to send mail online. It works fine but requires me
> to enter an mail server. I'm looking for something else that doesn't
> require and SMTP server. Having to specify a mail server prohibits me
> from sending to alternate domains.

Hi Adisegna,

I've always assumed that emails have to talk with some SMTP server.
RFC821 seems to confirm this:

 http://www.faqs.org/rfcs/rfc821.html

so what you're asking, to be able to send mail without an SMTP server, may
not be possible.  There is an 'smtpd' module that comes with Python:

 http://www.python.org/doc/lib/module-smtpd.html

I'm also not sure I understand the reason you're trying to avoid talking
to an outside smtp server.  But again, I'm unfamiliar enough with how the
SMTP email protocol really works that perhaps I'm just overlooking
something.





--
Arthur DiSegna
Network Operations Center
Authentium, Inc.

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


Re: [Tutor] reduce with comprehension

2005-11-24 Thread Nick Lunt


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> >>> def f(x, y):
> ...  return x + y
> ...
> >>> arr = range(10)
> >>> sum(arr)  # Our target
> 45
> >>> tmp = [0]
> >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, 
> y)) or True][-1]
> 45
> 
> Let's try some more...
> 
> >>> def f(x, y):
> ...  return x*y
> ...
> >>> arr = range(1, 10)# Don't want to include 0!
> >>> reduce(f, arr)   # Our target
> 362880
> >>> tmp = [1]
> >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, 
> y)) or True][-1]
> 362880
> >>> print 'Magic!'
> Magic!

LOL I feel like I've accidentally signed up for the perl-tutor list ;)
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Books

2005-12-22 Thread Nick Lunt
Web/Network programming here
http://www.amazon.com/gp/product/0596100329/qid=1135198935/sr=2-1/ref=pd_bbs
_b_2_1/103-4720029-4050242?s=books&v=glance&n=283155

Not CGI specific, and I haven't bought it yet, but I bet it's a blinder :)



> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Johan Geldenhuys
> Sent: 21 December 2005 20:59
> To: David Holland
> Cc: tutor@python.org
> Subject: Re: [Tutor] Books
>
>
> Are here any new books on web programming with Python?
>
> Johan
>
> David Holland wrote:
>
> >I would recommend python programming for the absolute beginner.
> >
> >
> >
> >
> >
> >___
> >Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide
> with voicemail http://uk.messenger.yahoo.com
> >___
> >Tutor maillist  -  Tutor@python.org
> >http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.371 / Virus Database: 267.14.3/209 - Release Date: 21/12/2005
>
>

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


Re: [Tutor] Learning books

2005-12-23 Thread Nick Lunt
Hi Richard,

I myself just about know enough python to help me in my job occasionally and
to do some cool little hobby projects.
When it comes to creating GUI's for my programs I always use pythoncard, see
here www.pythoncard.org .

It's very simple to use, and the tutorials on their site will have you up
and running in no time.
It's based on wxpython but you don't need to know wxpython to use it at all,
unless pythoncard itself does not implement some functionality that you
desire.

The pythoncard mailing list is also very low volume and very helpful.

As a side note, I am the senior unix admin for a uk financial corp and I
know very well that learning perl would be the best 'prospective' language
for me to get to grips with, but no matter what issue I come up with at work
(or home) python is always, and I truly mean always, the better option.

I wish you good luck with your python learning experience, and I'm sure that
you will find it's also a very cool language to use. I can't help but feel
that it is inevitable that it will eventually overtake perl as the scripting
language of choice.

I hope none of that went too far off topic :)

Nick .


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Richard
> Sent: 23 December 2005 20:05
> Cc: tutor
> Subject: Re: [Tutor] Learning books
>
>
> First of thanks for all the great input from everyone!!!
>
> Okay, so I have been reading some of the tutorials around the net on
> Python. great stuff I might add but I am getting all confused with the
> TCL, xwwidgets etc. I want to be able to program and I am just using the
> standard IDE that comes with Python. Am I on the right track? Or I am
> better off using a different one or ??? I see that some are someting
> with  C++ but heck, I just want to learn Python for now. I do want the
> widgets to look nice sure. HELP!
>
> Thanks in advance and Happy Holidays!!
>
> Richard
>
> P.S. I think I am getting one or two of the recomended books for
> Christmas! (cant wait!)
>
> nephish wrote:
>
> >Learning Python by O'Reilly,
> >got me started after realizing that Programming Python by O'Reilly was a
> >tad over me head.
> >
> >i am new here too.
> >
> >On Tue, 2005-12-20 at 14:46 -0600, Richard wrote:
> >
> >
> >>Afternoon all, My son asked me what books I would like for Christmas
> >>this year. So what would you recommend?
> >>
> >>I am a beginner here.
> >>
> >>
> >>Thanks
> >>
> >>
> >>Richard
> >>___
> >>Tutor maillist  -  Tutor@python.org
> >>http://mail.python.org/mailman/listinfo/tutor
> >>
> >>
> >
> >
> >
> >
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.371 / Virus Database: 267.14.5/212 - Release Date: 23/12/2005
>
>

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


Re: [Tutor] problems with the shebang line and linux

2006-02-16 Thread Nick Lunt
Hi Brian,

it's the ^M characters that are catching you out here.

Often files from a windows PC will have ^M as the newline char when viewed
in linux/unix.

On linux you should be able to check if a file has ^M in by running either
'vi -b filename' or 'sed -n l filename'.

Try this with the file

vi filename
escape
:%s/^M//

That should get rid of all the ^M chars. (not on linux at the mo so not 100%
sure).
The ^M is done in vi by actually typing in 'Ctl-V M' not shift6 M.

What I wrote above may not sound too clear, but feel free to email me off
list if you need any vi or linux commandline help (altho ive never used
ubuntu so anything ubuntu specific I wont be much use with).

Good luck,
Nick .


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Brian van den Broek
> Sent: 16 February 2006 13:36
> To: Tutor
> Subject: [Tutor] problems with the shebang line and linux
>
>
> Hi all,
>
> I've switched to Linux fairly recently and am still at the fumbling
> about stage :-)  I'm having a devil of a time with the shebang line
> and running a py file from a command line.
>
> I wrote the following little test script with IDLE 1.1.2 under Python
> 2.4.2 on Ubuntu 5.10:
>
> 
> #!/usr/bin/python
> print "Working!"
> 
>
> I then C & P'ed it to another .py file. testerlyfoo.py is the
> original, testerlybar.py is the pasted copy.
>
> Here's my command line results:
>
> [EMAIL PROTECTED]:~$ which python
> /usr/bin/python
> [EMAIL PROTECTED]:~$ cd /media/windata/
> [EMAIL PROTECTED]:/media/windata$ ./testerlyfoo.py
> Working!
> [EMAIL PROTECTED]:/media/windata$ ./testerlybar.py
> bash: ./testerlybar.py: /usr/bin/python^M: bad interpreter: No such
> file or directory
> [EMAIL PROTECTED]:/media/windata$
>
> I even retyped the testerlybar.py file, but I end up with the same
> results as when the small script was copied and pasted.
>
> Likewise, I got the same results after saving the two files to my Home
> directory on the hail mary thought that perhaps the fact I'd save the
> originals on a FAT32 mounted drive might be making things goofy.
>
> I'm stumped. Any steps I can take to work out what's going on?
>
> Best to all,
>
> Brian vdB
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.375 / Virus Database: 267.15.9/261 - Release Date: 15/02/2006
>

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


Re: [Tutor] problems with the shebang line and linux

2006-02-16 Thread Nick Lunt
Take no notice of this, others have already answered.

My emails to the tutor list seem to take about 5 hours to get thru for some
reason.

Nick .

> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Nick Lunt
> Sent: 16 February 2006 14:27
> To: Brian van den Broek; Tutor
> Subject: Re: [Tutor] problems with the shebang line and linux
>
>
> Hi Brian,
>
> it's the ^M characters that are catching you out here.
>
> Often files from a windows PC will have ^M as the newline char when viewed
> in linux/unix.
>
> On linux you should be able to check if a file has ^M in by running either
> 'vi -b filename' or 'sed -n l filename'.
>
> Try this with the file
>
> vi filename
> escape
> :%s/^M//
>
> That should get rid of all the ^M chars. (not on linux at the mo
> so not 100%
> sure).
> The ^M is done in vi by actually typing in 'Ctl-V M' not shift6 M.
>
> What I wrote above may not sound too clear, but feel free to email me off
> list if you need any vi or linux commandline help (altho ive never used
> ubuntu so anything ubuntu specific I wont be much use with).
>
> Good luck,
> Nick .
>
>
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > Behalf Of Brian van den Broek
> > Sent: 16 February 2006 13:36
> > To: Tutor
> > Subject: [Tutor] problems with the shebang line and linux
> >
> >
> > Hi all,
> >
> > I've switched to Linux fairly recently and am still at the fumbling
> > about stage :-)  I'm having a devil of a time with the shebang line
> > and running a py file from a command line.
> >
> > I wrote the following little test script with IDLE 1.1.2 under Python
> > 2.4.2 on Ubuntu 5.10:
> >
> > 
> > #!/usr/bin/python
> > print "Working!"
> > 
> >
> > I then C & P'ed it to another .py file. testerlyfoo.py is the
> > original, testerlybar.py is the pasted copy.
> >
> > Here's my command line results:
> >
> > [EMAIL PROTECTED]:~$ which python
> > /usr/bin/python
> > [EMAIL PROTECTED]:~$ cd /media/windata/
> > [EMAIL PROTECTED]:/media/windata$ ./testerlyfoo.py
> > Working!
> > [EMAIL PROTECTED]:/media/windata$ ./testerlybar.py
> > bash: ./testerlybar.py: /usr/bin/python^M: bad interpreter: No such
> > file or directory
> > [EMAIL PROTECTED]:/media/windata$
> >
> > I even retyped the testerlybar.py file, but I end up with the same
> > results as when the small script was copied and pasted.
> >
> > Likewise, I got the same results after saving the two files to my Home
> > directory on the hail mary thought that perhaps the fact I'd save the
> > originals on a FAT32 mounted drive might be making things goofy.
> >
> > I'm stumped. Any steps I can take to work out what's going on?
> >
> > Best to all,
> >
> > Brian vdB
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> > --
> > No virus found in this incoming message.
> > Checked by AVG Free Edition.
> > Version: 7.1.375 / Virus Database: 267.15.9/261 - Release Date:
> 15/02/2006
> >
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.375 / Virus Database: 267.15.9/261 - Release Date: 15/02/2006
>
>

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