Re: [Tutor] Python Programming Books
On 7/14/06, wesley chun [EMAIL PROTECTED] wrote: (LONG... you've been warned ;-) ) snip Heh, that was pretty long. I bought the first edition of Core Python and thought that it was well-written, but I didn't quite get it (stay with me, this gets better). It wasn't until after I had taken quite a few courses in C++ that I realized 1) that python was s much nicer to work with and 2) Wesley's book made a lot more sense. It's probably not a good one for someone new to programming, but I find that I pick it up when I need to see an example of how something is done in python. As for an absolute beginner, Alan's tutorial and How To Think Like a Computer Scientist are both pretty good. The latter had my daughter doing a fair bit of programming in a day. So Wesley's Big Book was a huge help in a project I did for work that involved socket programming, pickling, and interfacing with MySQL. Showing how particular things were done in python in clear, concise examples is it's big strength. Thanks for not getting sucked in to using lots of source code :) -- Matt Waiting for the second edition ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
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 ??? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
Сергій wrote: 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 ??? Why would you possibly need to do this? I think whatever you're trying to do could be done a different way much easier. Can you tell us what you're trying to accomplish? Or are you just seeing if this is possible? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
Hi! I don't know why you want to do that but actually it is possibble. for i in xrange(1): ...exec( 'var_%s = var_%s' %(i,i), locals() ) var_100 'var_100' Regards. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi, need help on zip commands for windows
Can somebody explain what I'm doing wrong? The problem should be in the string passed to os.system try to print the complete string that you are passing to os.system. then run a shell and paste the complete string on the shell, I am sure that the shell will complain about something. Fabrizio ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
Fabrizio Milo aka misto wrote: Hi! I don't know why you want to do that but actually it is possibble. for i in xrange(1): ...exec( 'var_%s = var_%s' %(i,i), locals() ) var_100 'var_100' It is possible but it's unlikely that it is the best solution to whatever problem the OP is trying to solve. Any reference to the variables created will be similarly roundabout. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
Сергій wrote: 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 ??? Why do you want to do this? What can you do with the named variables that you can't do with the original list? Please tell us more about the problem you are trying to solve. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Needing to create a program that will search my hard drive for certain folders
Bobby J. Howerton Jr. wrote: Hello, I am new to programming in Python, but I am very excited about the possibilities that it (Python) has. I maybe jumping the gun a little bit here, but this is what I would like to do: I would like to create an executable program that when ran it will search my hard drive for certain folders that contain different files. Once the program finds these folders, I would like the program to zip each of the folders up and then e-mail each of the folders to a certain e-mail address. Is this possible??? I have been told by someone else that it is…I just want to make sure. Yes, it's possible. Python includes modules that can help you search the file system, create zip files and send emails with attachments. (The os, zipfile, email and smtplib modules.) If this is possible…How would I do this (please remember that I am new to Programming in Python). You need to walk before you can run. Pick a Python tutorial and work through it to learn the basics of the language. Then work on each piece of the problem. Searching for files is pretty easy, zipping folders is a little harder, creating and sending the email a little more complex. None of them are really hard but you need to understand the language first. You don't say if you have any programming background. Take a look at one of these pages for a suitable tutorial: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers http://wiki.python.org/moin/BeginnersGuide/Programmers Try stuff out. Ask here when you get stuck. And welcome to Python! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
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 "??? Lets back up a stage. Why do you think you need the variables? Variables in Python are just a reference to an object/value. Lists also contain references to objects/values. So if you have the list why would you also need variables to reference the same objects? What do you think you could do differently with the variables that you can't do with the list reference? Here is an ASCII art picture (pick your font carefully!) index onject variable myList = [ 0 = A = var_0 1 = B = var_1 2 = C = var_2 3= D = var_3 4 = E = var_4 ] What difference do you perceive between var_2 and myList[2] Clue - There isn't any... Alan GauldAuthor of the Learn to Program web sitehttp://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
On Fri, Jul 14, 2006 at 09:25:57PM -0400, Etienne Robillard wrote: --- ÁÕàÓöÙ [EMAIL PROTECTED] wrote: suppose I need to create 1000 000 variables var_1, var_2, var_100 how to do this using for? (something like for i in range(100): ___var_ How would you consider NOT creating 100,000 variables ?? Answer that question and you should be close of knowing the answer to your own question. I'm with Etienne. You probably need analysis, not a mechanism for a solution that you've picked too quickly. However, if a look-up by name *is* what you need, also consider dictionaries. After all, in Python, variables are just a dictionary look-up in a namespace. In [1]: d1 = {} In [2]: for idx in range(10): ...: key = 'k%d' % idx ...: d1[key] = idx * 10 ...: ...: In [3]: d1 Out[3]: {'k0': 0, 'k1': 10, 'k2': 20, 'k3': 30, 'k4': 40, 'k5': 50, 'k6': 60, 'k7': 70, 'k8': 80, 'k9': 90} HTH, Me, too. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
On 7/14/06, Сергій [EMAIL PROTECTED] wrote: supposeI need to create 1000 000 variables var_1, var_2, var_100 how to do this using for? (something like for i in range(100): ___var_ You should think about not creating variables like this, it is bad programming and continuing to use similar techniques leads you down the path of buggy code and hours of trying to trace your code. That being said, here's a nice, safe solution:# get the current moduleimport syscur_module = sys.module[__name__]for i in range(100): cur_module['var_%s' % i] = i # var_i = i print var_46889 # in the current namespaceBut again, like others have suggested, you should rethink your problem and your solution before starting down your path. What are you really capturing? -Arcege -- 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
Re: [Tutor] create 1000 000 variables
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'vecreated 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 = Falseself.in_h2 = Falseself.in_h3 = Falseself.in_h4 = Falseself.in_h5 = Falseself.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, listerf = 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 parser.h1:print i Show_step('h2')for i in parser.h2:print i Show_step('h3')for i in parser.h3:print i Show_step('h4')for i in parser.h4:print i Show_step('h5')for i in parser.h5:print i Show_step('h6')for i in parser.h6:print i And I don't like this BAD CODE1 and BAD CODE2 How to rewrite bad codes??? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
And I don't like this BAD CODE1 and BAD CODE2 How to rewrite bad codes??? about BAD CODE2 -I've fount a good solution (or it seems to me to be good :) ): tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] for i in tags:Show_step(i)for j in getattr(parser, i):print j ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
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 h N), 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_h N 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() 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'vecreated 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 = Falseself.in_h2 = Falseself.in_h3 = Falseself.in_h4 = Falseself.in_h5 = Falseself.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, listerf = 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 parser.h1:print i Show_step('h2')for i in parser.h2:print i Show_step('h3')for i in parser.h3:print i Show_step('h4')for i in parser.h4:print i Show_step('h5')for i in parser.h5:print i Show_step('h6')for i in parser.h6:print i And I don't like this BAD CODE1 and BAD CODE2 How to rewrite bad codes??? ___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
Re: [Tutor] [SOLVED] Basic QT query #2
On Friday 14 July 2006 23:21, Dave S wrote: On Friday 14 July 2006 19:21, Dave S wrote: Hi all, I am trying to get to grips with QT, putting a friendly face on some of my apps :) Its early days and my first attempt but I expected the following to print 'hi it works' every second. There problem I am stuck with is ... The debugged program raised the exception unhandled RuntimeError underlying C/C++ object has been deleted At line 24 ie self.timer = self.startTimer(1000). First of all I did not have an assignment ie I just had self.startTimer(1000) realising Python would garbage collect I added an assignment but still the same problem. Also I am struggling with QObject.connect(self.tickerevent, PYSIGNAL(ticker), self.hello) I am unsure of what the source of the connect is - I suspect my guess of self.tickerevent is wrong. The source of the event should be PYSIGNAL(ticker) ? Any ideas or suggestions much appreciated Cheers Dave SOLVED - Sometimes trying trying is a good way to learn :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How do you implement a config file?
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? Saying: foo1 = bar foo2 = baz foo3 = bah is not scalable as the list can be arbitrarily long. It might also suffice if I could just put the actual python code where these variables are defined into a config file and have it read into the main file. But I don't see any good way to do that either without actually making my config file a module and calling it config.py instead of application.config. Suggestions? -- Tracy R Reed http://ultraviolet.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do you implement a config file?
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: Hi Tracy, [config parser approach cut] I don't see any good way to do that either without actually making my config file a module and calling it config.py instead of application.config. This second approach --- using a module as a configuration file --- is the programmer-friendly one. *grin* If you can get away with this, it's probably the simplest to implement. It also seems to be the approach that most Python programs use, bar more sophisticated approaches like XML or some other structured data format. We had some discussion about this earlier the last few weeks (and months! Someone should put this on the FAQ!), and the concensus seems to be that ConfigParser is a bit limiting: http://aspn.activestate.com/ASPN/Mail/Message/python-Tutor/3116794 http://mail.python.org/pipermail/tutor/2006-June/047557.html Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] error: (10054, 'Connection reset by peer')
Here is a program that I wrote using one of the python.org's examples at 12.2.13 (first example listed): # 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.connect()__init__(self, host='', port=0, local_hostname=None)s.sendmail('[EMAIL PROTECTED]', ['[EMAIL PROTECTED]'], msg.as_string())s.close() And this is what I get when I run the program using IDLE: Traceback (most recent call last): File "C:\Documents and Settings\User\Desktop\textsender.py", line 23, in ? s.connect() 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') Anybody have any suggestions? Grady Henry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor