Re: [Tutor] create 1000 000 variables

2006-07-16 Thread Сергій
 In fact I want to create a list of variables from the list of strings Example: ['var1', 'var2', 'var3'] - list of strings
Ok, let's stop for the moment.Do you know about dictionaries yet? If not, we should point this out to you, because they solve the problem you describe.


Yes. I know about dictionaries.
But I wanted to 






In fact I want to create a list of variables from the list of strings

Example: ['var1', 'var2', 'var3'] - list of strings
And I need to create variables var1, var2, var3 named as strings in the list, and:
var1 == 'var1'
var2 == 'var2'
var3 == 'var3'

How to do this using only my list and for i in ???


Can you show me how to solve this using your dictionaries?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] So close! But ... error: (10054, 'Connection reset by peer')

2006-07-16 Thread Grady Henry



I think that I am so close to getting this 
simple program to run correctly:

# Import smtplib for the actual sending 
functionimport smtplib

# Import the email modules we'll needfrom 
email.MIMEText import MIMEText

# Open a plain text file for reading. For 
this example, assume that# the text file contains only ASCII 
characters.fp = open(r'C:\Documents and 
Settings\User\Desktop\\text3.txt')# Create a text/plain messagemsg = 
MIMEText(fp.read())fp.close()

# me == the sender's email address# you == the 
recipient's email addressmsg['Subject'] = 'The contents of %s' % 
'C:\Documents and Settings\User\Desktop\\text3.txt'msg['From'] = '[EMAIL PROTECTED]'msg['To'] = '[EMAIL PROTECTED]'

# Send the message via our own SMTP server, but 
don't include the# envelope header.s = 
smtplib.SMTP()s.set_debuglevel(1)s.connect(host='', 
port=25)__init__(self, host='', port=25, 
local_hostname=None)s.sendmail('[EMAIL PROTECTED]', ['[EMAIL PROTECTED]'], 
msg.as_string())s.quit()s.close()

But when I run it using IDLE, I get the 
following:


IDLE 1.1.3  No 
Subprocess  connect: ('', 25)connect: ('', 
25)Traceback (most recent call last): File "C:\Documents and 
Settings\User\Desktop\textsender.py", line 24, in ? 
s.connect(host='', port=25) File "C:\Python24\lib\smtplib.py", line 
307, in connect (code, msg) = self.getreply() 
File "C:\Python24\lib\smtplib.py", line 348, in getreply 
line = self.file.readline() File "C:\Python24\lib\socket.py", line 
340, in readline data = 
"">error: (10054, 'Connection reset by 
peer') 

Can anybody help?

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


Re: [Tutor] How do you implement a config file?

2006-07-16 Thread Kent Johnson
Tracy R Reed wrote:
 I am writing a small python application that needs a few variables to be
 end user configurable. Right now I just have the variables right up
 front where the user can tweak them in the program code and a big
 commented line that says Nothing editable past this point. But I would
 like to be able to break this out into a separate config file. There are
 two ways I see to do this:

 1. Use ConfigParser. But from all of the docs and examples I see
 ConfigParser only accepts name=value type pairs. How can I implement a
 list or a dictionary of configs in ConfigParser?

 Part of my config involves things like:

 foo = ['bar','baz','bah']

 How would I represent this cleanly in config parser? 

ConfigObj supports list data.
http://www.voidspace.org.uk/python/configobj.html

There is a list of alternative config file parsers here:
http://wiki.python.org/moin/ConfigParserShootout

To import a file in Python syntax whose name doesn't end in .py, I think 
you can use the imp module though I'm not certain of the details. This 
post might give you some clues:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/775dabe0a5e63f4f/f3bb17d8c7377aad?q=import+imprnum=3#f3bb17d8c7377aad

Kent

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


Re: [Tutor] create 1000 000 variables

2006-07-16 Thread Kent Johnson
Michael P. Reilly wrote:
 Good.  But one VERY important point to note is that that you are not working
 with variables here.  You are working with members of a class instance.
 This is a very different beast.  You could just use getattr(), setattr() and
 delattr() for these.

 But continuing... you might want to think about this in a step back.  Each
 of the self.hN and self.in_hN have something in common and they all have the
 same behavior.  That sounds a lot like a job for object oriented
 programming, no?  We can create a class that look and acts like a list
 (like hN), but is only active if we have set it (if in_hN is True).

 Actually, because of the structure of the SGML code, BAD CODE1 isn't quite
 the bad code, the handle_data code is actually worse.  The reason BAD
 CODE1 looks bad is not because of your code, but because SGMLParser forces
 you to create so many methods in the subclass.  There are no start_hN and
 end_hN catch-all methods available.  For this reason, I made only a minor
 change to the start_hN and end_hN methods, but changed the reset and
 handle_data methods quite a bit.

 class HeaderCapture:
 def __init__(self, contents=[]):
 self.contents = contents[:] # copy
 self.deactivate()
 def append(self, item):
 # could raise an exception, but for now, ignore
 if self.active:
 self.contents.append(item)
 def __len__(self):
 return len(self.contents)
 def __getitem__(self, idx):
 return self.contents[idx]
 def activate(self):
 self.active = True
 def deactivate(self):
 self.active = False
 ...
 class Lister(SGMLParser):

 def reset(self):
 SGMLParser.reset(self)
 self.headers = {
 'h1': HeaderCapture(),
 'h2': HeaderCapture(),
 'h3': HeaderCapture(),
 'h4': HeaderCapture(),
 'h5': HeaderCapture(),
 'h6': HeaderCapture(),
 }

 def handle_data(self, text):
 # only one would be active, but legally, two could
 for hc in self.headers.values():
 hc.append(text)  # if not active, ignore

 def start_h1(self, attrs):
 self.headers['h1'].activate()
 def end_h1(self):
 self.headers['h1'].deactivate()
 def start_h2(self, attrs):
 self.headers['h2'].activate()
 def end_h2(self):
 self.headers['h2'].deactivate()
 def start_h3(self, attrs):
 self.headers['h3'].activate()
 def end_h3(self):
 self.headers['h3'].deactivate()
 def start_h4(self, attrs):
 self.headers['h4'].activate()
 def end_h4(self):
 self.headers['h4'].deactivate()
 def start_h5(self, attrs):
 self.headers['h5'].activate()
 def end_h5(self):
 self.headers['h5'].deactivate()
 def start_h6(self, attrs):
 self.headers['h6'].activate()
 def end_h6(self):
 self.headers['h6'].deactivate()
   
To continue this, your BAD CODE2 becomes

for tag in 'h1 h2 h3 h4 h5 h6'.split():
  Show_step(tag)
  for i in parser.headers[tag]:
print i

Kent

 On 7/15/06, Сергій [EMAIL PROTECTED] wrote:
   
  But again, like others have suggested, you should rethink your problem
 
 and your solution before starting down your path.  What are you really
 capturing?

   
 Rethink problem...
 I try to use sgmllib - get all info tagged in h1... h6
 I've created file lister.py:

 from sgmllib import SGMLParser

 class Lister(SGMLParser):

  def reset(self):
   SGMLParser.reset(self)
   self.h1 = []
   self.h2 = []
   self.h3 = []
   self.h4 = []
   self.h5 = []
   self.h6 = []

   self.in_h1 = False
   self.in_h2 = False
   self.in_h3 = False
   self.in_h4 = False
   self.in_h5 = False
   self.in_h6 = False

  def handle_data(self, text):
   if self.in_h1 == True:
self.h1.append(text)
   elif self.in_h2 == True:
self.h2.append(text)
   elif self.in_h3 == True:
self.h3.append(text)
   elif self.in_h4 == True:
self.h4.append(text)
   elif self.in_h5 == True:
self.h5.append(text)
   elif self.in_h6 == True:
self.h6.append(text)

 #AND NOW BAD CODE1:

  def start_h1(self, attrs):
   self.in_h1 = True

  def end_h1(self):
   self.in_h1 = False

  def start_h2(self, attrs):
   self.in_h2 = True

  def end_h2(self):
   self.in_h2 = False

  def start_h3(self, attrs):
   self.in_h3 = True

  def end_h3(self):
   self.in_h3 = False

  def start_h4(self, attrs):
   self.in_h4 = True

  def end_h4(self):
   self.in_h4 = False

  def start_h5(self, attrs):
   self.in_h5 = True

  def end_h5(self):
   self.in_h5 = False

  def start_h6(self, attrs):
   self.in_h6 = True

  def end_h6(self):
   self.in_h6 = False

 

 And now I want to print all text in this tags.

 file use_lister.py:

 

 import urllib, lister

 f = open('_1.html', 'r')
 text = f.read()
 f.close()

 parser = urllister.Lister()
 parser.feed(text)
 parser.close()

 #AND NOW BAD CODE2:

 Show_step('h1')
 for i in 

Re: [Tutor] How do you implement a config file?

2006-07-16 Thread Adam
On 16/07/06, Kent Johnson [EMAIL PROTECTED] wrote:
Tracy R Reed wrote: I am writing a small python application that needs a few variables to be end user configurable. Right now I just have the variables right up front where the user can tweak them in the program code and a big
 commented line that says Nothing editable past this point. But I would like to be able to break this out into a separate config file. There are two ways I see to do this: 1. Use ConfigParser. But from all of the docs and examples I see
 ConfigParser only accepts name=value type pairs. How can I implement a list or a dictionary of configs in ConfigParser? Part of my config involves things like: foo = ['bar','baz','bah']
 How would I represent this cleanly in config parser?ConfigObj supports list data.http://www.voidspace.org.uk/python/configobj.html
There is a list of alternative config file parsers here:http://wiki.python.org/moin/ConfigParserShootoutTo import a file in Python syntax whose name doesn't end in .py, I think
you can use the imp module though I'm not certain of the details. Thispost might give you some clues:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/775dabe0a5e63f4f/f3bb17d8c7377aad?q=import+imprnum=3#f3bb17d8c7377aadKentI tried this out and the simplest way seems to be something like this: 
import impconfig = imp.load_source('config', '/path/application.config')HTH,Adam
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] create 1000 000 variables

2006-07-16 Thread Danny Yoo
  In fact I want to create a list of variables from the list of strings
 
  Example: ['var1', 'var2', 'var3'] - list of strings
 
 Do you know about dictionaries yet?

 Yes. I know about dictionaries.


Hello,

Can you show us an example of the kind of dictionary usage you've used 
before?

I'm skirting really indirectly on this one, because I'm still not sure 
where the difficulty is in applying dictionaries to this problem.  Just to 
hint at this: the dictionary my_vars shown below:

###
my_vars = {}
my_vars['v1'] = 'tesuji'
my_vars['v2'] = ['anti-suji']
my_vars['v3'] = {'atari' : 1 }
###

is one that directs string names 'v1', 'v2', and 'v3' to some arbitrary 
values.  We can later look things up in the dictionary by providing the 
name of the key:

##
print I'm reading James Davies' %s % my_vars['v1']
print I don't yet have %s % my_vars['v2'][0]
print %s! % my_vars['v3'].keys()[0]
##
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] So close! But ... error: (10054, 'Connection reset by peer')

2006-07-16 Thread Michael P. Reilly
You are running on a PC, which doesn't have a SMTP server running on it. The default hostname for smtplib.SMTP().connect() is to localhost (your own machine). You will need to find out the hostname of the mail server that your ISP provides. You probably set it when you set up your email (based on a little research, it looks to be 
smtp.cox.net). -ArcegeOn 7/16/06, Grady Henry [EMAIL PROTECTED] wrote:







I think that I am so close to getting this 
simple program to run correctly:

# Import smtplib for the actual sending 
functionimport smtplib

# Import the email modules we'll needfrom 
email.MIMEText import MIMEText

# Open a plain text file for reading. For 
this example, assume that# the text file contains only ASCII 
characters.fp = open(r'C:\Documents and 
Settings\User\Desktop\\text3.txt')# Create a text/plain messagemsg = 
MIMEText(fp.read())fp.close()

# me == the sender's email address# you == the 
recipient's email addressmsg['Subject'] = 'The contents of %s' % 
'C:\Documents and Settings\User\Desktop\\text3.txt'msg['From'] = '[EMAIL PROTECTED]'
msg['To'] = '[EMAIL PROTECTED]'

# Send the message via our own SMTP server, but 
don't include the# envelope header.s = 
smtplib.SMTP()s.set_debuglevel(1)s.connect(host='', 
port=25)__init__(self, host='', port=25, 
local_hostname=None)s.sendmail('[EMAIL PROTECTED]', ['
[EMAIL PROTECTED]'], 
msg.as_string())s.quit()s.close()

But when I run it using IDLE, I get the 
following:


IDLE 1.1.3  No 
Subprocess  connect: ('', 25)connect: ('', 
25)Traceback (most recent call last): File C:\Documents and 
Settings\User\Desktop\textsender.py, line 24, in ? 
s.connect(host='', port=25) File C:\Python24\lib\smtplib.py, line 
307, in connect (code, msg) = self.getreply() 
File C:\Python24\lib\smtplib.py, line 348, in getreply 
line = self.file.readline() File C:\Python24\lib\socket.py, line 
340, in readline data = 
"">error: (10054, 'Connection reset by 
peer') 

Can anybody help?


___Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor