Re: [Tutor] Dynamically named objects

2007-12-28 Thread Marc Tompkins
Sorry, meant to respond to the list, not just the OP...

-- Forwarded message --
From: Marc Tompkins [EMAIL PROTECTED]
Date: Dec 28, 2007 12:13 AM
Subject: Re: [Tutor] Dynamically named objects
To: Michael Bernhard Arp Sørensen [EMAIL PROTECTED]


I don't think there's any need for eval - I'll leave the sermon about why
eval is a Bad Thing for someone else - and if your objects are going to
spend most of their lives in a list or dictionary, they don't really need
meaningful individual names.  (I mean, instead of naming each one class1,
class2, etc. you can simply refer to them as classDict[1], classDict[2],
etc. or classList[1], etc.)

How's this:
#
class thingy():
example = Just testing - 
def __init__(self, num):
self.example = self.example + str(num)

thang = {}
for x in range(1,4):
thang[x] = thingy(x)

for item, value in thang.iteritems():
print item, value.example
#=

On Dec 27, 2007 11:44 PM, Michael Bernhard Arp Sørensen 
[EMAIL PROTECTED] wrote:

 Hi there.

 I need to instantiate objects on the fly and put the in a list/dict for
 later use. I was thinking of this:

 objectlist = []
 newobjectname = object1
 classname = class1 + ()
 objectlist.append(newobjectname = eval(classname) )
 objectlist[0].method(hello world)

 Can this be done? If not, is there a work around or some design pattern
 for this situation?

 --
 Venlig hilsen/Kind regards

 Michael B. Arp Sørensen
 Programmør / BOFH

 I am /root and if you see me laughing you better have a backup.
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
www.fsrtechnologies.com



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


Re: [Tutor] Dynamically named objects

2007-12-28 Thread Marc Tompkins
Here's the same thing with a list instead of a dictionary:
#===
class thingy():
example = Just testing - 
def __init__(self, num):
self.example = self.example + str(num)

thang = []
for x in range(1,4):
thang.append(thingy(x))

for item, value in enumerate(thang):
print item, value.example
#


On Dec 27, 2007 11:44 PM, Michael Bernhard Arp Sørensen 
[EMAIL PROTECTED] wrote:

 Hi there.

 I need to instantiate objects on the fly and put the in a list/dict for
 later use. I was thinking of this:

 objectlist = []
 newobjectname = object1
 classname = class1 + ()
 objectlist.append(newobjectname = eval(classname) )
 objectlist[0].method(hello world)

 Can this be done? If not, is there a work around or some design pattern
 for this situation?

 --
 Venlig hilsen/Kind regards

 Michael B. Arp Sørensen
 Programmør / BOFH

 I am /root and if you see me laughing you better have a backup.
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




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


Re: [Tutor] Dynamically named objects

2007-12-28 Thread Chris Fuller

You might find a dictionary useful. Each element in a dictionary is associated 
with a key, which can be a string.

objectlist = {}
o = eval(class1 + ())
objectlist[object1] = o
o.method(hello world)

Also, try to avoid using eval(), it usually makes troubleshooting and 
maintenance harder and can lead to security problems in some cases. You could 
have a dictionary of classes and look them up on the fly:

classlist = { classone : classone, classtwo : classtwo }

objectlist = {}
cls = classlist[user_input]
o = cls()
objectlist[object1] = o
o.method(hello world)

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


Re: [Tutor] Microsoft Access

2007-12-28 Thread Alan Gauld
Jim Morcombe [EMAIL PROTECTED] wrote 

 This tutorial seems very specific to PythonWin IDE.  
 I haven't tried it, but it seems to imply that it uses stuff 
 from PythonWin IDE that may not be available in IDLE.  

The IDE is just an IDE - editor and debugger etc.
But the winall package is how you get access to the 
Windows libraries for COM/ADO etc You can download 
winall on top of standard python. You then have the choice 
of IDLE or Pythonwin as an IDE - I prefer Pythonwin.

However if you don;t want to do that you can access Access 
via ODBC using the standard DBAPI for Python using SQL.
That hasthe advantage that you get familiar with DBAPI 
which also works across Oracle, Informix, DB2 etc etc

But if you want to do any kind of complex stuff with 
Windows then the winall package is pretty essential.
And if you get seruious about programming Windows 
from Python the O'Reilly book by Mark Hammond is 
strongly recommended too - it's old now but still the 
best reference on Windows and Python.

HTH,

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

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


[Tutor] Internet Bandwidth Management

2007-12-28 Thread Sewqyne Olpo
Hello. I want to restrict internet conncection on windows xp
machine,sometimes to cut off the connection or to reduce the bandwidth
.. But I dont know exactly where to start and what to learn. Could you
give some hints about this?
Thanks in advance.



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python CLI parser

2007-12-28 Thread Chris Fuller

TUGZip (www.tugzip.com) is another free (as in speech) alternative.  It has a 
distinct interface from 7-zip that some may prefer.

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


Re: [Tutor] Internet Bandwidth Management

2007-12-28 Thread Tim Golden
Sewqyne Olpo wrote:
 Hello. I want to restrict internet conncection on windows xp
 machine,sometimes to cut off the connection or to reduce the bandwidth
 .. But I dont know exactly where to start and what to learn. Could you
 give some hints about this?
 Thanks in advance.

Couple of things: this is really a Windows question first,
although I assume you're (implictly) asking: How do I do
this from within Python? So, if you haven't already, start
searching Windows newsgroups and so on to get some pointers
that way and come back to ask for help on implementing things
in Python.

Secondly, the only way I can think of... although I'm sure
there are others... is to fiddle with the Windows ICF: the
Firewall. I think it can be adjusted via WMI and presumably
by other means as well, so you might try including ICF or
Firewall in your search terms if nothing else shows. I'm
afraid I've never actually tried to do it myself, merely
helped someone out a while back with the WMI mechanics of it.

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


Re: [Tutor] Closing GUI program

2007-12-28 Thread Michael H. Goldwasser

Hi Jim,

  The problem with your code is that Tk's mainloop continues to run
  even though you have closed the window.  The cleaner way to close
  such a GUI program is to explicitly stop the main loop and the
  Python interpreter at the appropriate time.

  In this particular case, you may provide a callback function that is
  triggered on the event of a window being manually closed by the user.

  Here's a revised version of your program that should work better,
  even within IDLE.  Of course, there is still some system dependency
  among the program, IDLE, and the underlying operating system.

  Some interesting reading is at  http://www.3dartist.com/WP/python/tknotes.htm

With regard,
Michael

---
from Tkinter import Tk,Label

def onClose():
root.destroy()   # stops the main loop and interpreter

root = Tk()
root.protocol(WM_DELETE_WINDOW, onClose)  # handle event when window is 
closed by user
z = Label(root, text=Hello World!)
z.grid()
root.mainloop()
---

On Friday December 28, 2007, Jim Morcombe wrote: 

Oops!  Here's the program:
---
from Tkinter import * 

root = Tk()
z = Label(root, text=Hello World!)
z.grid()
root.mainloop()
--

Jim

  - Original Message - 
  From: Jim Morcombe 
  To: python tutor mailing list 
  Sent: Friday, December 28, 2007 3:51 PM
  Subject: [Tutor] Closing GUI program


  I have copied the following program.  When I run it, (By pressing F5 
 from IDLE), it displays the Hello world message.

  When I close the window, the Hello world message disappears, but it 
 seems that the program is still running, because when I close the shell, i 
 get the message The program is still running.  Do you want to kill it?

  How do I get the program to terminate when the window is closed?

  Jim

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


[Tutor] send file/mail to imap

2007-12-28 Thread Tim Michelsen
Hello,
I have a mbox file locally on my notebook.

I would like to send this file to my IMAP account using python.

Does anyone know a module or tutorial which does this?

I tried
* IMAPClient 0.3 - http://pypi.python.org/pypi/IMAPClient/0.3
but it doesn't contain a send function.

Thanks in advance for any hints,
Timmie

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


Re: [Tutor] send file/mail to imap

2007-12-28 Thread Tim Golden
Tim Michelsen wrote:
 I have a mbox file locally on my notebook.
 
 I would like to send this file to my IMAP account using python.

Ummm. There seem to be two possible sources of confusion here.

Number one is that you don't normally send things via IMAP,
you use the IMAP protocol to *read* messages from a server-based
mail store of some sort.

Number two is that it's not clear whether the mbox file is
merely coincidentally a mbox file (ie it might as well be a
text file or a Word doc) or is -- as seems most likely -- a
real mailbox which you want to make available to normal email
clients via your IMAP server.

So... reading way too much between the lines, I deduce that you
want to have the mails in your mbox file available to your
mail clients via your IMAP server. Am I right?

If so, you don't want to use an IMAP client, you want some
other kind of file transfer mechanism, such as FTP or an scp
command to get the file into the appropriate place on your
mail server where the IMAP server will see it. You might
then have to subscribe to it from your clients; depends on
your setup.

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


Re: [Tutor] send file/mail to imap

2007-12-28 Thread Chris Fuller
On Friday 28 December 2007 09:31, Tim Michelsen wrote:
 Hello,
 I have a mbox file locally on my notebook.

 I would like to send this file to my IMAP account using python.

 Does anyone know a module or tutorial which does this?

 I tried
 * IMAPClient 0.3 - http://pypi.python.org/pypi/IMAPClient/0.3
 but it doesn't contain a send function.

 Thanks in advance for any hints,
 Timmie

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

Batteries included!

http://docs.python.org/lib/module-imaplib.html

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


[Tutor] Careful Dictionary Building

2007-12-28 Thread doug shawhan
I'm building a dictionary from a list with ~ 1M records.

Each record in the list is itself a list.
Each record in the list has a line number, (index 0) which I wish to use as
a dictionary key.

The problem: It is possible for two different records in the list to share
this line number. If they do, I want to append the record to the value in
the dictionary.

The obvious (lazy) method of searching for doubled lines requires building
and parsing a key list for every record. There must be a better way!

dict = {}
for record in list
if record[0] in dict.keys():
dict[ record[0] ].append( record )
else:
dict[ record[0] ] = [record]

Once you get ~ 80,000 records it starts slowing down pretty badly (I would
too ...).

Here's hoping there is a really fast, pythonic way of doing this!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Careful Dictionary Building

2007-12-28 Thread doug shawhan
*sigh* Ignore folks. I had forgotten about .has_key().



On Dec 28, 2007 11:22 AM, doug shawhan [EMAIL PROTECTED] wrote:

 I'm building a dictionary from a list with ~ 1M records.

 Each record in the list is itself a list.
 Each record in the list has a line number, (index 0) which I wish to use
 as a dictionary key.

 The problem: It is possible for two different records in the list to share
 this line number. If they do, I want to append the record to the value in
 the dictionary.

 The obvious (lazy) method of searching for doubled lines requires building
 and parsing a key list for every record. There must be a better way!

 dict = {}
 for record in list
 if record[0] in dict.keys ():
 dict[ record[0] ].append( record )
 else:
 dict[ record[0] ] = [record]

 Once you get ~ 80,000 records it starts slowing down pretty badly (I would
 too ...).

 Here's hoping there is a really fast, pythonic way of doing this!

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


Re: [Tutor] send file/mail to imap

2007-12-28 Thread Eric Brunson

Tim's pretty spot on in his ruminations below, I just wanted to add a 
few comments.  Read inline.

Tim Golden wrote:
 Tim Michelsen wrote:
   
 I have a mbox file locally on my notebook.

 I would like to send this file to my IMAP account using python.
 

 Ummm. There seem to be two possible sources of confusion here.

 Number one is that you don't normally send things via IMAP,
 you use the IMAP protocol to *read* messages from a server-based
 mail store of some sort.

 Number two is that it's not clear whether the mbox file is
 merely coincidentally a mbox file (ie it might as well be a
 text file or a Word doc) or is -- as seems most likely -- a
 real mailbox which you want to make available to normal email
 clients via your IMAP server.

 So... reading way too much between the lines, I deduce that you
 want to have the mails in your mbox file available to your
 mail clients via your IMAP server. Am I right?
   

In order to use IMAP, the key word would not be to send the messages, 
as Tim points out, which IMAP does not do, but do copy the messages 
from one IMAP repository to another.  However, (reading between the 
lines) in order to do that you would need both repositories (mbox and 
your IMAP server) available via the IMAP protocol and I doubt you have 
IMAP access to the local mbox file.

 If so, you don't want to use an IMAP client, you want some
 other kind of file transfer mechanism, such as FTP or an scp
 command to get the file into the appropriate place on your
 mail server where the IMAP server will see it. You might
 then have to subscribe to it from your clients; depends on
 your setup.
   

This would definitely be the correct approach if your IMAP server uses 
an mbox store and you have the ability to directly manipulate those 
stores outside of the IMAP interface, i.e. root privs on the mail 
server.  However, knowing that all but the most rudimentary IMAP servers 
do *not* use mbox and that there's a good possibility (based on your 
initial question) that you do *not* have direct access to the mail 
backend, I think your best solution would be to resend the emails via 
SMTP to your mail server where they would be delivered as if they had 
been sent there initially.

This can definitely be done via smtplib in Python by opening the mbox 
file and iterating over the messages, but you may want to look at the 
fetchmail utility and see if that has the capabilities (on your local 
platform) required to access the mbox file directly.  It's a mighty 
capable utility and may be able to take care of this in a single shot.

Hope that helps,
e.

 TJG
 ___
 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] How to stop a script.

2007-12-28 Thread Ian Egland
Hi everyone... I litterally just started python and did the following 'hello
world' related tutorial.

http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro
http://hkn.eecs.berkeley.edu/%7Edyoo/python/idle_intro

Being the kind of person who like to experiment, I tried changing the
following script from:

print Hello World!
print Here are the ten numbers from 0 to 9, just in case you can't count.
for i in range(10):
print i,

print I'm done!

to:

print Hello World!
print Here are the ten numbers from 0 to 9, just in case you can't count.
for i in range(1000):
print i,

print I'm done!


Now I am stuck staring at IDLE as it prints out all 10million numbers.
Is there a way other than closing the shell to stop a script midway through
execution?

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


Re: [Tutor] Careful Dictionary Building

2007-12-28 Thread Eric Brunson
doug shawhan wrote:
 *sigh* Ignore folks. I had forgotten about .has_key().

Actually, you don't even need that, simply write:

if record[0] in dict:
   # Do your thing.

But don't use dict as a variable name, it's a builtin function name.




 On Dec 28, 2007 11:22 AM, doug shawhan [EMAIL PROTECTED] 
 mailto:[EMAIL PROTECTED] wrote:

 I'm building a dictionary from a list with ~ 1M records.

 Each record in the list is itself a list.
 Each record in the list has a line number, (index 0) which I wish
 to use as a dictionary key.

 The problem: It is possible for two different records in the list
 to share this line number. If they do, I want to append the record
 to the value in the dictionary.

 The obvious (lazy) method of searching for doubled lines requires
 building and parsing a key list for every record. There must be a
 better way!

 dict = {}
 for record in list
 if record[0] in dict.keys ():
 dict[ record[0] ].append( record )
 else:
 dict[ record[0] ] = [record]

 Once you get ~ 80,000 records it starts slowing down pretty badly
 (I would too ...).

 Here's hoping there is a really fast, pythonic way of doing this!


 

 ___
 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] Careful Dictionary Building

2007-12-28 Thread Chris Fuller
first thing.. you are duplicating a lot of effort inside your loops. getting 
rid of that will speed things up:

dict = {}
for record in list:
rid = record[0]
    if rid in dict:
    dict[ rid ].append( record )
    else:
    dict[ rid ] = [record]

The other thing I see isn't a speed problem. You are overwriting two python 
built-in types when you use variables named list and dict. You probably 
want to avoid doing this, because someday yuou want to use the built-ins, and 
you code wil exhibit goofy errors when you try.

The only thing I can suggest that might speed it up more is to use a set type 
instead of a list, if the ordering doesn't matter to you. I've seen that 
change result in 20-30 percent speedups, but YMMV. If you can't start with a 
set, you can create a new one by passing the list to the set constructor. 
This may or may not help you speedwise, but try it out.

You could play around with psyco, but I don't know if it would help much in 
this case.

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


Re: [Tutor] Careful Dictionary Building

2007-12-28 Thread Michael Langford
This functionality already exists in the ever so useful defaultdict object.
You pass a factory method to the constructor of defaultdict for an object,
and it returns a new object when there is no key:

from collections import defaultdict
mydict = defaultdict(list)
for record in mylist:
mydict[ record[0] ].append( record )

defaultdict is usually good enough for datasets I've used it for.

 --Michael



On 12/28/07, doug shawhan [EMAIL PROTECTED] wrote:

 *sigh* Ignore folks. I had forgotten about .has_key().



 On Dec 28, 2007 11:22 AM, doug shawhan [EMAIL PROTECTED] wrote:

  I'm building a dictionary from a list with ~ 1M records.
 
  Each record in the list is itself a list.
  Each record in the list has a line number, (index 0) which I wish to use
  as a dictionary key.
 
  The problem: It is possible for two different records in the list to
  share this line number. If they do, I want to append the record to the value
  in the dictionary.
 
  The obvious (lazy) method of searching for doubled lines requires
  building and parsing a key list for every record. There must be a better
  way!
 
  dict = {}
  for record in list
  if record[0] in dict.keys ():
  dict[ record[0] ].append( record )
  else:
  dict[ record[0] ] = [record]
 
  Once you get ~ 80,000 records it starts slowing down pretty badly (I
  would too ...).
 
  Here's hoping there is a really fast, pythonic way of doing this!
 


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




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


Re: [Tutor] Tutor] Careful Dictionary Building

2007-12-28 Thread Kent Johnson
doug shawhan wrote:

 To help performance a bit, don't call dict.keys() on every iteration,
 since you're invoking a function.
 
 dict = {}
 allKeys =dict.Keys

Should be dict.keys()

 for record in list
   if record[0] in allKeys:
   dict[ record[0] .append( record ) ]

Should be
   dict[record[0]].append(record)

   else:
   dict[ record[0] ] = [record]
   allKeys = dict.Keys()# this function is
 only invoked some of the time, not on every iteration

So you only create the list of keys when it changes; that may speed up 
the loop, depending on how many duplicates there are. But there are two 
big problems with the original program - it creates a new list of keys 
each time through the loop, and it searches for a key in the list 
instead of looking it up directly in the dict. Your change may cut down 
on the number of times the key list is created but it will not improve 
the search time at all.

In other words, you are making modest improvements to a broken 
algorithm; a better fix is to use a better algorithm.

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


Re: [Tutor] How to stop a script.

2007-12-28 Thread Tiger12506
Ctrl+c will issue a KeyboardInterrupt which breaks out of programs such as 
the one you described. (The only situation it doesn't is when the program 
catches that exception. You won't see that 'til you get your sea legs ;-) 

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


[Tutor] Fwd: How to stop a script.

2007-12-28 Thread Ian Egland
Thanks, I will keep that in mind the next time I  experiment. :-P

-Ian

-- Forwarded message --
From: Tiger12506 [EMAIL PROTECTED]
Date: Dec 28, 2007 5:42 PM
Subject: Re: [Tutor] How to stop a script.
To: tutor@python.org


Ctrl+c will issue a KeyboardInterrupt which breaks out of programs such as
the one you described. (The only situation it doesn't is when the program
catches that exception. You won't see that 'til you get your sea legs ;-)

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