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. 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
Re: [Tutor] create 1000 000 variables
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] 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 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] 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] 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] 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] 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
[Tutor] create 1000 000 variables
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_ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
--- ÁÕàÓöÙ [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. HTH, Erob -- Etienne Robillard [EMAIL PROTECTED] JID: incidah AT njs.netlab.cz YMS/MSN: granted14 AT yahoo.com TEL: +1 514.962.7703 __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create 1000 000 variables
Сергій 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_ Rather than creating 100 variables, create a list with 100 values. data = [] for i in range(100): data.append(i*i) # or whatever data you want in your variables This can be very nicely abbreviated with a list comprehension: data = [ i*i for i in range(100) ] In general, when you think you need to create a bunch of variables with repetive names, you should probably use a list or dictionary instead. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor