Re: [Tutor] exit handler for C extension module

2010-01-17 Thread Stefan Behnel
Shuying Wang, 18.01.2010 03:13:
> What would be an atexit equivalent for a C extension module? When my
> extension module is unloaded, I would like some clean up functions to
> be called from an external c library. I've had a look at the C
> extension guide but I can't find anything like that,

I'm not aware of anything special here. Just use the "atexit" module itself
and register a Python function. At least, that's what we do in Cython for
any module level cleanup.

You may also consider adding some hints on what you want to use it for,
maybe there is some support for your actual use case.

Stefan

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


Re: [Tutor] Book for Python and XML

2010-01-17 Thread Stefan Behnel
Григор, 15.01.2010 08:28:
> Someone to have good E-book for python and XMl

The existing books are rather old. You might find these interesting, though:

http://effbot.org/zone/element.htm
http://codespeak.net/lxml/tutorial.html
http://www.nmt.edu/tcc/help/pubs/pylxml

Stefan

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


Re: [Tutor] Confirmation about __init__()

2010-01-17 Thread Lie Ryan
On 01/17/10 16:42, Robert wrote:
> I have read quite a bit in the past 2 months, ( I have also looked at codes)
> At this point, I think I understand well what __init__() is and does -
> But, I have yet to see this *specifically* spelled out about the the
> __init__ method for a Class;
> 
> It is OPTIONAL, correct ?
> 
> if I have NO start values/variables to set, no Base Class __init__ to
> call --- the __init__ method is NOT required, correct ?

No, __init__ is always required for new-style classes; however due to
object inheritance (all new-style classes inherits from `object`), all
new-style classes inherits the do-nothing __init__ from `object`.
Essentially, it means __init__ can be omitted if it does the same as the
super class' __init__.

More generally, a method (e.g. MyClass.__init__) that does not override
the superclass' method (e.g. MySuperClass.__init__) will use the
superclass' definition of the method (i.e. when MyClass inherits from
MySuperClass, and __init__ is not overrided in MyClass, MyClass.__init__
== MySuperClass.__init__).

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


[Tutor] exit handler for C extension module

2010-01-17 Thread Shuying Wang
Hi,

What would be an atexit equivalent for a C extension module? When my
extension module is unloaded, I would like some clean up functions to
be called from an external c library. I've had a look at the C
extension guide but I can't find anything like that,

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


Re: [Tutor] adding more text to a file

2010-01-17 Thread Alan Gauld


"Magnus Kriel"  wrote

It might be that when you create a file for the first time with 'a', that 
it
will through an exception. So you will have to try with 'w', and if there 
is
an exception, you know the file does not exist and you will have to 
create

the file for the first time with 'w'.


That should not happen. If there is no file 'a' will act like 'w'

Alan G.


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


Re: [Tutor] adding more text to a file

2010-01-17 Thread Alan Gauld


"Shizuha Aki"  wrote


i need it to be able to add more entries instead of just one.

my code is like this:

while True:
  name = raw_input("what is the name ")
  age = raw_input("what is the age ")

  out_file = open("persons.txt", "w")


It would work if you moved the open() call outside the loop 
so the file is only opened once. Every time you open the file 
you create a new file on top of the old one. You need to 
move the close outside the loop too of course. That way 
you don't need append mode unless you want to run the 
program multiple times to keep adding data.



  out_file.write("name: ")
  out_file.write(name)
  out_file.write("\n")


You should join the strings together and write a full line out at once. 
It will be more efficient and possibly more resilient too.


--
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] Subclassing object

2010-01-17 Thread Alan Gauld


"Robert"  wrote

I have been wondering about this "New-style Class" subject along this 
line:


so, *theoretically speaking*, in EXISTING, pre-P3K code,
if one changes everywhere where it's coded "class someClass()" to
"class someClass(object)",
it should not break the programs, right ?


More correctly replacing old style

class SomeClass:

with new style

class SomeClass(object):

should not break anything.
You cannot of course reliably go the other way!

Alan G 



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


Re: [Tutor] smtp project

2010-01-17 Thread Alan Plum
On So, 2010-01-17 at 15:45 -0500, Kirk Z Bailey wrote:
> I am writing a script that will send an email message. This will 
> run in a windows XP box. The box does not have a smtp server, so 
> the script must crete not merely a smtp client to talk to a MTA, 
> it must BE one for the duration of sending the message- then shut 
> off, we don't need no bloody op0en relays here!

Twisted is the obvious choice for a server daemon, but it should be good
enough for what you do, too.

What you need isn't a "SMTP server", it's a "SMTP sender", which is more
like a client than a server in the usual model.

As you don't want it to run as a daemon, first consider how you want it
to behave if the server (SMTP receiver, the destination or a server that
is willing to relay the message) is not responding or responds with an
error.

A message queue (in terms of days not seconds) is out of the question
for a fire-and-forget service like the one you want, so you probably
want the transfer synchronously, i.e. somewhere between receiving the
user's input (or whatever triggers the script) and the output (or the
end of the lifetime of whatever script was triggered). If you're dealing
with real users, though, this can mean an unexpected pause of several
seconds, depending on how long the delivery takes (or how long it takes
to notice it's not going anywhere). This is bad if the user isn't
expecting it (zero progress can be interpreted as a freeze).

Anyway. Take a look at Twisted or other libraries for SMTP senders or
"clients". Actual SMTP servers are mostly concerned with accepting mail
via SMTP and doing something with them (if they are capable of relaying,
they can also act as SMTP senders, otherwise they probably just deliver
the mail locally or forward them to a local message queue for a separate
SMTP sender).


Hope that helped a bit,

Alan Plum

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


Re: [Tutor] smtp project

2010-01-17 Thread Steve Willoughby
Kent Johnson wrote:
> On Sun, Jan 17, 2010 at 3:45 PM, Kirk Z Bailey  
> wrote:
>> I am writing a script that will send an email message. This will run in a
>> windows XP box. The box does not have a smtp server, so the script must
>> crete not merely a smtp client to talk to a MTA, it must BE one for the
>> duration of sending the message- then shut off, we don't need no bloody
>> op0en relays here!
> 
> Is there no SMTP server available on another machine? Can you send
> email from a regular email client (e.g. Thunderbird)?

Unless I'm missing something, you're probably overly complicating your
solution.  You don't need to open a local relay just to *send* a message
from the same script.  Your script can connect to whatever upstream SMTP
server your organization uses and send mail to it directly.

>> I am RTFM and having some heavy sledding, can I get an Elmer on this?
> 
> An Elmer?

Old Amateur Radio term.  An "Elmer" is someone who mentors new people
and shows them the ropes.  Which is essentially what the list is for.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] smtp project

2010-01-17 Thread Kent Johnson
On Sun, Jan 17, 2010 at 3:45 PM, Kirk Z Bailey  wrote:
> I am writing a script that will send an email message. This will run in a
> windows XP box. The box does not have a smtp server, so the script must
> crete not merely a smtp client to talk to a MTA, it must BE one for the
> duration of sending the message- then shut off, we don't need no bloody
> op0en relays here!

Is there no SMTP server available on another machine? Can you send
email from a regular email client (e.g. Thunderbird)?

> I am RTFM and having some heavy sledding, can I get an Elmer on this?

An Elmer?

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


Re: [Tutor] adding more text to a file

2010-01-17 Thread wesley chun
> while True:
>    name = raw_input("what is the name ")
>    age = raw_input("what is the age ")
>
>    out_file = open("persons.txt", "w")
>    out_file.write("name: ")
>    out_file.write(name)
>    out_file.write("\n")
>    out_file.write("age: ")
>    out_file.write(age)
>    out_file.write("\n")
>    out_file.write("\n")
>    out_file.close


while everyone is helping you with your original inquiry, i'm going to
inject some "Pythonic" style guidelines here.

1. i would strongly suggest calling the close() method instead of
merely providing the object. in other words, change your last line to
"out_file.close()" so you execute it. otherwise your file contents are
not guaranteed.

2. try to minimize the total number of calls to write() to make things
run faster. for example, your code only really needs one call:

out_file.write("name: %s\nage: %s\n\n" % (name, age))

the reason why it's faster is because you pay a little bit of overhead
with every function call you make. of course, since your example
requires human input each time, it doesn't make as much of a
difference in this example, but this is mostly a general suggestion.

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

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


Re: [Tutor] what is dynamic about "dynamic name resolution" ?

2010-01-17 Thread wesley chun
On Sun, Jan 17, 2010 at 12:10 PM, Robert  wrote:
> In the Python tutorial (highlighted here http://awurl.com/N1XvzIo2Q)
>
> What exactly is "dynamic name resolution" ? specifically what is
> "dynamic" about it ?


what this means is that as your code is executing and the interpreter
encounters an identifier/variable, it "dynamically" looks up the
variable at that point -- it checks at that point to see if it's a
local variable; if not, global; finally it checks if it's a built-in.

to rephrase: this check is *not* done during compile time (static);
instead, it is done during runtime (dynamic). if it were static, it
would've already been checked *before* your code ever started running.

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

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


[Tutor] smtp project

2010-01-17 Thread Kirk Z Bailey
I am writing a script that will send an email message. This will 
run in a windows XP box. The box does not have a smtp server, so 
the script must crete not merely a smtp client to talk to a MTA, 
it must BE one for the duration of sending the message- then shut 
off, we don't need no bloody op0en relays here!


I am RTFM and having some heavy sledding, can I get an Elmer on this?

--
end

Very Truly yours,
 - Kirk Bailey,
   Largo Florida

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


[Tutor] what is dynamic about "dynamic name resolution" ?

2010-01-17 Thread Robert
In the Python tutorial (highlighted here http://awurl.com/N1XvzIo2Q)

What exactly is "dynamic name resolution" ? specifically what is
"dynamic" about it ?

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


Re: [Tutor] adding more text to a file

2010-01-17 Thread Magnus Kriel
Thanx, you are correct. And now I know that 'a' will create a file as well.

Magnus

On Sun, Jan 17, 2010 at 10:34 AM, Steve Willoughby wrote:

> Magnus Kriel wrote:
> > It might be that when you create a file for the first time with 'a',
> > that it will through an exception. So you will have to try with 'w', and
> > if there is an exception, you know the file does not exist and you will
> > have to create the file for the first time with 'w'.
>
> That's a little confusing, I think.  I think you meant "try with 'a' and
> if there is an exception  ... create ... with 'w'" but that shouldn't be
> necessary anyway.  Opening a file with 'a' will append to an existing
> file, or create a new file if one doesn't exist already.
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding more text to a file

2010-01-17 Thread Steve Willoughby
Magnus Kriel wrote:
> It might be that when you create a file for the first time with 'a',
> that it will through an exception. So you will have to try with 'w', and
> if there is an exception, you know the file does not exist and you will
> have to create the file for the first time with 'w'.

That's a little confusing, I think.  I think you meant "try with 'a' and
if there is an exception  ... create ... with 'w'" but that shouldn't be
necessary anyway.  Opening a file with 'a' will append to an existing
file, or create a new file if one doesn't exist already.

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


Re: [Tutor] Subclassing object

2010-01-17 Thread Robert
On Sun, Jan 17, 2010 at 12:47 PM, Robert  wrote:
> On Sun, Jan 17, 2010 at 11:25 AM, Alan Gauld  
> wrote:
>> In older versions of Python it made a difference whether you used object
>> or not. Using object gave you a "new style" class which has several extra
>> features, without you got an "old style class" without the features.


I have been wondering about this "New-style Class" subject along this line:

so, *theoretically speaking*, in EXISTING, pre-P3K code,
if one changes everywhere where it's coded "class someClass()" to
"class someClass(object)",
it should not break the programs, right ?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] adding more text to a file

2010-01-17 Thread Magnus Kriel
Hi,

It is the way you open the text file in the beginning.

This is from the python manual:

open() returns a file object, and is most commonly used with two arguments:
"open(filename, mode)".

>>> f=open('/tmp/workfile', 'w')
>>> print f


The first argument is a string containing the filename. The second argument
is another string containing a few characters describing the way in which
the file will be used. mode can be 'r' when the file will only be
read, 'w'for only writing (an existing file with the same name will be
erased), and
'a' opens the file for appending; any data written to the file is
automatically added to the end. 'r+' opens the file for both reading and
writing. The mode argument is optional; 'r' will be assumed if it's omitted.

You should use 'a' and not 'w'.

It might be that when you create a file for the first time with 'a', that it
will through an exception. So you will have to try with 'w', and if there is
an exception, you know the file does not exist and you will have to create
the file for the first time with 'w'.

Hope it helps.

Buy the way, why don't you try sqlite for your database structure? sqlite is
still a single file, not like MySQL. You get a pysqilte at :
http://pypi.python.org/pypi/pysqlite/

Magnus Kriel



The 'W' means write. An 'A' means append

On Sun, Jan 17, 2010 at 9:40 AM, Shizuha Aki  wrote:

> Hi,
>
> I am new to python, and i am trying to make a program that makes like a txt
> file database. so i have my whole program in a while True loop, it asks for
> information about the person and then writes the information about the
> person to a txt file, but i need it to be able to add more entries instead
> of just one.
>
> my code is like this:
>
> while True:
>name = raw_input("what is the name ")
>age = raw_input("what is the age ")
>
>out_file = open("persons.txt", "w")
>out_file.write("name: ")
>out_file.write(name)
>out_file.write("\n")
>out_file.write("age: ")
>out_file.write(age)
>out_file.write("\n")
>out_file.write("\n")
>out_file.close
>
> now i want to add like say 5 persons to the txt file, but whenever i enter
> a new person it just overwrites the previous, im sure its because of the "w"
> but how exactly do i need to change my code to add more text instead of
> overwriting?
>
>
> ___
> 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] adding more text to a file

2010-01-17 Thread Hugo Arts
On Sun, Jan 17, 2010 at 6:40 PM, Shizuha Aki  wrote:
> Hi,
>
> I am new to python, and i am trying to make a program that makes like a txt
> file database. so i have my whole program in a while True loop, it asks for
> information about the person and then writes the information about the
> person to a txt file, but i need it to be able to add more entries instead
> of just one.
>
> my code is like this:
>
> while True:
>    name = raw_input("what is the name ")
>    age = raw_input("what is the age ")
>
>    out_file = open("persons.txt", "w")
>    out_file.write("name: ")
>    out_file.write(name)
>    out_file.write("\n")
>    out_file.write("age: ")
>    out_file.write(age)
>    out_file.write("\n")
>    out_file.write("\n")
>    out_file.close
>
> now i want to add like say 5 persons to the txt file, but whenever i enter a
> new person it just overwrites the previous, im sure its because of the "w"
> but how exactly do i need to change my code to add more text instead of
> overwriting?
>

you should check the python documentation for the 'open' function.
help() is your friend, works on any interactive session. On linux, you
could also check out the pydoc program. Or you could check the website
docs.python.org.

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


[Tutor] adding more text to a file

2010-01-17 Thread Shizuha Aki
Hi,

I am new to python, and i am trying to make a program that makes like a txt 
file database. so i have my whole program in a while True loop, it asks for 
information about the person and then writes the information about the person 
to a txt file, but i need it to be able to add more entries instead of just one.

my code is like this:

while True:
   name = raw_input("what is the name ")
   age = raw_input("what is the age ")

   out_file = open("persons.txt", "w")
   out_file.write("name: ")
   out_file.write(name)
   out_file.write("\n")
   out_file.write("age: ")
   out_file.write(age)
   out_file.write("\n")
   out_file.write("\n")
   out_file.close

now i want to add like say 5 persons to the txt file, but whenever i enter a 
new person it just overwrites the previous, im sure its because of the "w" but 
how exactly do i need to change my code to add more text instead of overwriting?



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


Re: [Tutor] Subclassing object

2010-01-17 Thread Alan Gauld


"Timo Vanwynsberghe"  wrote


I read that it is advised to subclass object.

Is it really necessary? I mean, everything works, why should I add it to 
my

code?


In older versions of Python it made a difference whether you used object
or not. Using object gave you a "new style" class which has several extra
features, without you got an "old style class" without the features.

In Python v3 you only get new style classes and I think you can omit
the object without detriment.

For comparison here are Python v2.5 definitions


class New(object): pass

...

dir(New)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash_
_', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr_

_', '__setattr__', '__str__', '__weakref__']

class Old: pass

...

dir(Old)

['__doc__', '__module__']

As you can see there is quite a lot of extra "stuff" in the New class.


In Python 3:


class New(object): pass



dir(New)

['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__']

class Old: pass



dir(Old)

['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__']


There is even more "stuff" but it is the same with/without the explicit 
'object'.


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] Subclassing object

2010-01-17 Thread Timo Vanwynsberghe
I read that it is advised to subclass object.

Is it really necessary? I mean, everything works, why should I add it to my
code?

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


[Tutor] Confirmation about __init__()

2010-01-17 Thread Robert
I have read quite a bit in the past 2 months, ( I have also looked at codes)
At this point, I think I understand well what __init__() is and does -
But, I have yet to see this *specifically* spelled out about the the
__init__ method for a Class;

It is OPTIONAL, correct ?

if I have NO start values/variables to set, no Base Class __init__ to
call --- the __init__ method is NOT required, correct ?

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


Re: [Tutor] command line invocation

2010-01-17 Thread Alan Gauld


"Robert"  wrote 


in shell, my current directory is "/tmp",
are line#3 and #4 --- http://paste.pocoo.org/show/166268/ -
"equivalent" --- i.e. yielding the same result ?


No need to use pastebin when its only 2 lines!

Since -m runs a module as a script I think the answer is yes. 

Where they would be different is if you were not in the same folder 
as the module. Then the -m option would search the PYTHONPATH 
whereas without -m I think it would not search for the module, you 
would need to specify the location.


I think...

--
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] Confirmation about __init__()

2010-01-17 Thread Alan Gauld


"Robert"  wrote

I have read quite a bit in the past 2 months, ( I have also looked at 
codes)

At this point, I think I understand well what __init__() is and does -
But, I have yet to see this *specifically* spelled out about the the
__init__ method for a Class;

It is OPTIONAL, correct ?


Yes as are all metods. There is nothing very special about __init__
except that it is not called explicitly by users of the class. It could 
have

been done as an explicit method as in Smalltalk or Objective C.
In these the instance creation paradigm is a two stage process

obj = [[class new] init]

In Python we get the init call for "free"

Alan G.


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


[Tutor] command line invocation

2010-01-17 Thread Robert
Say that I have module "spam.py"  in  "/tmp" which is in PYTHONPATH.

in shell, my current directory is "/tmp",
are line#3 and #4 --- http://paste.pocoo.org/show/166268/ -
"equivalent" --- i.e. yielding the same result ?

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