Re: The Samurai Principle
On Tue, Sep 7, 2010 at 9:38 PM, Phlip wrote: > Everyone gets defensive about the design flaws in their own language. > But the django.db situation is not even a design flaw; just a > misinterpretation of the Samurai Principle. int('yo') shall throw an > exception, but a missing record could be the result you were looking > for, so it's not exceptional. I consider it exceptional because it breaks the rule that Model.objects.get(...) returns an instance of Model. If it can also return None, then this needs to be checked for every time the method is called, because None does not implement the Model interface and will break if you try to use it like that. You're still doing exception handling; you're just doing it with an if instead of a try-except. I prefer to do exception handling in a block clearly marked as an exception handler. Out of curiosity, would you also regard the case where two matching records are found instead of one as not exceptional? After all, it could be the result you were looking for. What should QuerySet.get return in that case, if it doesn't raise an exception? -- http://mail.python.org/mailman/listinfo/python-list
Re: include a file in a python program
Thanks all for your response i will try out this week, you have give me sufficient hints. Thanks again. Bussiere -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
sajuptpm wrote: > Now output is similar to first elements asc and second elements > desc.But the problem is the -ve sign, i dont need that. > > Have any other method to do it?? Sort twice: >>> for item in items: ... print item ... ('adams', 'anne') ('miller', 'arnold') ('miller', 'bill') ('adams', 'berta') ('adams', 'charlotte') ('miller', 'thomas') >>> items.sort(key=lambda x: x[1], reverse=True) >>> items.sort(key=lambda x: x[0]) >>> for item in items: ... print item ... ('adams', 'charlotte') ('adams', 'berta') ('adams', 'anne') ('miller', 'thomas') ('miller', 'bill') ('miller', 'arnold') See? First column ascending, second column descending within groups of rows with equal first column. Peter -- http://mail.python.org/mailman/listinfo/python-list
console-editor written in python
hi there, greetings. i am looking for a small, console-based (opensource) texteditor, written in python (or as shellscript!), but i'm not finding something usable. very find would be emacs-keybindings ... dank u well, tanja -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
On 7 sep, 16:46, Ben Finney wrote: > de...@web.de writes: > > Objects can be mutable or immutable. For example, in Python, integers, > > strings, floats and tuples are immutable. That means that you can't > > change their value. > > Yes. Importantly, wherever you see code that you *think* is changing the > value of an immutable object, you're thinking incorrectly. (There's no > shame in that; other languages give us preconceptions that can be hard > to shake off.) > > The only way to get a different value from an integer object is to ask > Python for a different integer object; the original is unchanged. The > same goes for tuples, strings, and all the other immutable types. > > > Mutable objects OTOH can be changed. > > […] > > Some good articles to explain Python's object model: > > http://effbot.org/zone/python-objects.htm> > > http://docs.python.org/reference/datamodel.html#objects-values-and-types> > > -- > \ “We can't depend for the long run on distinguishing one | > `\ bitstream from another in order to figure out which rules | > _o__) apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 | > Ben Finney Thanks all for feedback! Baba -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 8 sep, 02:07, Ben Finney wrote: > Baba writes: > > However the following Wiki excerpt seems to go in my direction: > > No, it doesn't. It advises that people show kindness; as I've been > arguing, that's exactly what you were shown. You haven't shown how the > information being imparted could have been fully imparted in a way > that's kinder, nor that it would be reasonable to do so. > > To put it another way: if you feel offended by an utterance, then > insufficient kindness on the part of the speaker is not the only > explanation. > > -- > \ “Software patents provide one more means of controlling access | > `\ to information. They are the tool of choice for the internet | > _o__) highwayman.” —Anthony Taylor | > Ben Finney Hi Ben, Thanks for your feedback. My question is: Who owns this forum? If we all do then we are allowed to post questions that are simple and that could otherwise be answered by doing research. It is just unfriendly to tell someone to go and look it up by themselves. Who is licensed to judge what can and cannot be posted as a question? A teacher of mine used to say: "There are no stupid questions, there are only stupid answers." It is arrogant to tell someone in a Forum to "look it up yourself ,this question is too basic". Can you not understand that accessing a file can seem daunting at first? Believe me, documentation can be offputting too when you only start with programming. Per se the beauty of forums like these is that there are human beings willing to make such tasks as finding out how to access a text file less 'scary'. Whoever thinks he or she has a license to tell someone to look up the answer by themselves should think again. I reckon the only license we have is not to answer at all. I acknowledge that Benjamin pointed me to the right place to find an answer but somehow the "Please do us a favour" sounded bit arrogant, as if he owned the place (same goes for all those who sided against me here: do you own this forum?)...i'd be glad if there was a python for beginners forum but in the meantime i have to say i find it awesome to have this forum to get help. It makes a BIG difference. So sorry if i upset anyone, we all have our opinions and get carried away and can be stubborn, i DO respect everyone in this forum and i think it goes bothways. I've learned a few things in this post... So now, to use Benjamin words: Please do me a favour and let's move on :) Baba Baba -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
Thanks everyone for all the suggestions! New to python, so reading up on the struct module & Construct library now, hope to get my problem fixed soon. - Original Message - Bitfields are most commonly used for extreme space optimization - i.e. shoving several variables and flags with carefully limited ranges into a single work. In Python you rarely work this way (where such an optimization is warranted, Python isn't the best tool for the job). However, as in your use case, it is sometimes needed in Python in order to communicate with other devices over the network or some other link. In my work with Python and embedded devices I've found the construct library (http://construct.wikispaces.com/) very useful. It allows to you very easily define complex formats for frames/messages on the bit and byte level. The idea is to use construct to encode and decode messages being sent to an embedded device. It works great. If you have further questions about this approach, feel free to ask. Eli -- -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba writes: > It is just unfriendly > to tell someone to go and look it up by themselves. Someone seeing too many unthoughtful questions from you might tell you to look it up yourself, in the hopes of getting you to change your questioning style, so that your future questions will be more thoughtful and worth answering. But according to you that would be unfriendly. Another thing they could do is say nothing, but quietly configure their news-reading software to ignore your questions completely. Then none of your future questions would have any hope of being answered. Would that be less unfriendly, or more unfriendly? -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
On Tue, Sep 7, 2010 at 9:53 PM, Eli Bendersky wrote: >> >> I'm trying to rewrite a c program in python & encountered several >> problems. I have some data structures in my c program like below: >> >> typedef struct >> { >> unsigned short size; >> >> unsigned short reserved:8; >> unsigned short var_a1:2; >> unsigned short var_a2:2; >> unsigned short var_a3:2; >> unsigned short var_a4:2; >> >> unsigned int var_a5; >> }structa; >> >> typedef struct >> { >> unsigned short size; >> >> unsigned char reserved:4; >> unsigned char var_b1:1; >> unsigned char var_b2:1; >> unsigned char var_b3:1; >> unsigned char var_b4:1; >> >> structa var_structa; >> }structb; >> >> I tried to code the above in python but only got this far: >> >> class StructA(object): >> def __init__(self, size=0) >> self.size = size >> >> class StructB(object): >> def __init__(self, size=0) >> >> Any equivalent for c data structures & bit fields in python? And how do I >> define var_structa (in structb) in python? >> > > Bitfields are most commonly used for extreme space optimization - i.e. > shoving several variables and flags with carefully limited ranges into a > single work. In Python you rarely work this way (where such an optimization > is warranted, Python isn't the best tool for the job). However, as in your > use case, it is sometimes needed in Python in order to communicate with > other devices over the network or some other link. > > In my work with Python and embedded devices I've found the construct library > (http://construct.wikispaces.com/) very useful. It allows to you very easily > define complex formats for frames/messages on the bit and byte level. The > idea is to use construct to encode and decode messages being sent to an > embedded device. It works great. > > If you have further questions about this approach, feel free to ask. > > Eli That's really an excellent find. Thanks for bringing it up. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
BartC wrote: > So 'range' is just a class like any other. And that a class is something > you can blithely copy from one variable to another. And whenever you see > 'range' anywhere, you can't always be certain that someone hasn't done: > > range = 42 > > at some point. True. I read an explanation here that IMHO pretty well explains what's going on: The "range" above is a label, attached with a piece of string to an object. The object in this case is the integer 42. The same object can have multiple labels attached to it, so "foo = bar" will just create a new label "foo" and attach it to the object that the label "bar" is already attached to. Note that I said object, which includes class instances like the int 42 above, but also the classes themselves, functions (or bound functions[1]) and modules (I hope I didn't miss any). > That explains a lot about the difficulties of implementing Python > efficiently. Yes, "range" is not a keyword or something intrinsic to the interpreter. It is just a name that is looked up whenever it is encountered, and that costs time. However, you also get some nice flexibility at that cost. Cheers! Uli [1] bound function = class function where the instance parameter is bound. Example: x = [] a = x.append a(42) # calls x.append(42) -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 8 sep, 12:46, Paul Rubin wrote: > Baba writes: > > It is just unfriendly > > to tell someone to go and look it up by themselves. > > Someone seeing too many unthoughtful questions from you might tell you > to look it up yourself, in the hopes of getting you to change your > questioning style, so that your future questions will be more thoughtful > and worth answering. But according to you that would be unfriendly. > > Another thing they could do is say nothing, but quietly configure their > news-reading software to ignore your questions completely. Then none of > your future questions would have any hope of being answered. Would that > be less unfriendly, or more unfriendly? Hi Paul I would support option 1 but phrased more thoughtfully than Benjamin did (that way no feelings will be hurt): "Dear xyz, Your question can easily be researched online. We suggest you give it a try and to look it up yourself. This will be beneficial both to you and to us. We do encourage to ask questions only when they have been researched first. We care about the quality of posts but we also understand that as a beginner one can tend to look for an easy or quick way to find answers. So in the meantime here's something to get you started: link" But where do you draw the line? Can we not just let people ask questions regardless? And let those answer who want to and those who don't just ignore the question? That seems so much easier to me. -- http://mail.python.org/mailman/listinfo/python-list
Re: console-editor written in python
On 08-Sep-10 04:51 AM, Tanje Toolate wrote: hi there, greetings. i am looking for a small, console-based (opensource) texteditor, written in python (or as shellscript!), but i'm not finding something usable. very find would be emacs-keybindings ... dank u well, tanja What operating system do you use? Colin W. -- http://mail.python.org/mailman/listinfo/python-list
Re: console-editor written in python
> > What operating system do you use? > > Colin W. usually linux. sometimes bsd. tanja -- http://mail.python.org/mailman/listinfo/python-list
Re: console-editor written in python
> > What operating system do you use? > > Colin W. usually linux. sometimes bsd. tanja -- http://mail.python.org/mailman/listinfo/python-list
Why Insulation is a Good Investment
Why Insulation is a Good Investment Investing in products to make homes more energy efficient pays significant dividends over a lifetime — with none of the wild fluctuations of Wall Street. Insulation contributes to: - Greater comfort - Even temperature distribution - Improved acoustics - Better moisture control, which can reduce floor squeaks, drywall cracks, structure damage and condensation - Potential for increased resale value: Installing proper insulation levels can also make your home more attractive to potential buyers. In fact, most buyers list energy-efficiency as a prime consideration. The reason? Buyers know they can buy a more expensive home if heating and cooling bills can be kept down. - A more environmentally friendly home - Lower energy bills* Unless your home was constructed with special attention to energy efficiency, adding insulation will probably reduce your utility bills. - 60% of the existing homes in the United States are not insulated to the best level. - According toa study done by Harvard University's School of Public Health, 60% of the exising homes are likely to use more energy than newer homes, leading to very high heating and air-conditioning bills. - Even if you own a new home, adding insulation may save enough money in reduced utility bills to pay for itself within a few years and will continue to save you money for as long as you own the home.* Source: http://www.simplyinsulate.com/content/why/benefits.html -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba writes: > But where do you draw the line? Can we not just let people ask > questions regardless? And let those answer who want to and those who > don't just ignore the question? That seems so much easier to me. The first few times, it's easy to ignore the questions. After a few more times, it becomes easier to ignore the person asking the questions, and not even look at the questions. The answer to many of your questions also is not "research it online", but rather, "figure it out with your own creativity". That's what being a programmer is about--figuring out solutions that don't already exist elsewhere. -- http://mail.python.org/mailman/listinfo/python-list
One Jew RASPUTIN hires another gentile RASPUTIN - to look good in comparison - always, profligate white led by a misleader jew - roman polansky, bernard madoff, moshe katsav, Craigslist Killer Philip
One Jew RASPUTIN hires another gentile RASPUTIN - to look good in comparison - always, profligate white led by a misleader jew - roman polansky, bernard madoff, moshe katsav, Craigslist Killer Philip Markoff jew HP CEO Mark Hurd Resigns After Sexual-Harassment Probe JORDAN ROBERTSON and RACHEL METZ | 08/ 6/10 11:53 PM | AP Resignation On August 6, 2010, he resigned from all of his positions at HP, following discovery of inappropriate conduct in an investigation into a claim of sexual harassment made by former reality TV actress Jodie Fisher. THE ZIONIST JEW LARRY ELLISON WHOSE COMPANY STOLE SAUDI MONEY TO DEVELOP ARABIC SUPPORT OF ORACLE TO HELP ISRAEL MONITOR THE PALESTINIAN PRISON AND LABOR CAMP - ONE NEEDS TO DO INVESTIGATION TO FIND THE BONES - A LOT OF BONES IN HIS CLOSET . A thorough good investigation into oracle's past should prove this. There should be a wikileaks on corporate crimes as well. http://www.zpub.com/un/un-le.html "Hi there, can I buy you a car?" According to accusations from an ongoing trial, that's the approach the Oracle CEO used to convince company secretaries to, ahem, "date" the boss. The case involves a 33- year-old former employee [FALSELY AND WRONGLY] accused of forging an email message. The woman [Adelyn Lee], who was fired shortly after an affair with Ellison, obtained a $100,000 settlement from him. Larry Ellison's All-Time Top Pickup Line http://www.thesmokinggun.com/documents/crime/polanski-predator jew roman polanski raped 13 year old girl semantha geimer, orally, vaginally and anally without any mercy jew roman polanski raped 13 year old girl semantha geimer, orally, vaginally and anally without any mercy SAN FRANCISCO — Hewlett-Packard Co. ousted its CEO on Friday for allegedly falsifying documents to conceal a relationship with a former contractor and help her get paid for work she didn't do. News of Mark Hurd's abrupt departure sent HP's stock tumbling. Shares of the world's biggest maker of personal computers and printers have doubled in value during his five-year stewardship, and HP became the world's No. 1 technology company by revenue in that time. The company said it learned about the relationship several weeks ago, when the woman, who did marketing work for HP, sent a letter accusing Hurd, 53, and the company of sexual harassment. An investigation found that Hurd falsified expense reports and other financial documents to conceal the relationship. The company said it found that its sexual harassment policy wasn't violated but that its standards of business conduct were. Hurd's "systematic pattern" of submitting falsified financial reports to hide the relationship -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
On Tue, 07 Sep 2010 11:00:03 +0100, BartC wrote: >> for i in xrange(1): >>a = a + f(i) >> >> then unrolling the loop is even less useful. The overhead of the loop >> itself is likely to be trivial compared to the cost of calling f() 100 >> million times -- the added complexity to shave 3 seconds off a four >> minute calculation simply isn't worth it. > > With Python 3 and def f(x): return x+1, unrolling this loop 4x improved > speed by 15%; 4.00 minutes reduces to 3.30 minutes. I'm afraid that I can't replicate those figures. In my test, unrolling the loop causes a massive SLOWDOWN of 37%, not a speed up. Here is my test code: def f(x): return x+1 def test_loop(n): a = 0 for i in range(n): a += f(i) return a def test_unrolled(n): a = 0 for i in range(n//4): a += f(4*i) a += f(4*i+1) a += f(4*i+2) a += f(4*i+3) return a from timeit import Timer n = 1000 assert test_loop(n) == test_unrolled(n) t1 = Timer('test_loop(n)', 'from __main__ import test_loop; n=%d' % n) t2 = Timer('test_unrolled(n)', 'from __main__ import test_unrolled; n=%d' % n) And here are the results using Python 3.1. Times are the best of 5 for 10 calls each to the loop_* functions, results given in seconds: >>> min(t1.repeat(number=10, repeat=5))/10 5.9740923209 >>> min(t2.repeat(number=10, repeat=5))/10 8.25656900405883 I repeated it with a larger loop variable. Since the time per test was so large (over ten minutes per call on my machine!), I didn't see the need to run multiple trials: n *= 100 assert test_loop(n) == test_unrolled(n) t3 = Timer('test_loop(n)', 'from __main__ import test_loop; n=%d' % n) t4 = Timer('test_unrolled(n)', 'from __main__ import test_unrolled; n=%d' % n) And the results: >>> t3.timeit(number=1) 653.3572518825531 >>> t4.timeit(number=1) 864.6454808712006 That's slightly better (32% slower instead of 37% slower), but still a massive performance hit. Given these results, I'm prepared to say that loop unrolling in Python is almost certainly going to be a pessimation, not an optimization, no matter what you have inside the loop. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
Steven D'Aprano writes: >> With Python 3 and def f(x): return x+1, unrolling this loop 4x improved >> speed by 15%; 4.00 minutes reduces to 3.30 minutes. > I'm afraid that I can't replicate those figures. In my test, unrolling > the loop causes a massive SLOWDOWN of 37%, not a speed up. Here is my > test code: > > def f(x): > return x+1 > > def test_loop(n): > a = 0 > for i in range(n): > a += f(i) > return a > > def test_unrolled(n): > a = 0 > for i in range(n//4): > a += f(4*i) > a += f(4*i+1) > a += f(4*i+2) > a += f(4*i+3) > return a > > from timeit import Timer > n = 1000 > assert test_loop(n) == test_unrolled(n) > t1 = Timer('test_loop(n)', > 'from __main__ import test_loop; n=%d' % n) > t2 = Timer('test_unrolled(n)', > 'from __main__ import test_unrolled; n=%d' % n) > > And here are the results using Python 3.1. Times are the best of 5 for 10 > calls each to the loop_* functions, results given in seconds: > min(t1.repeat(number=10, repeat=5))/10 > 5.9740923209 min(t2.repeat(number=10, repeat=5))/10 > 8.25656900405883 Running this test with python 2.6 (on my laptop) led to: >>> min(t1.repeat(number=10, repeat=5))/10 2.10715539455 >>> min(t2.repeat(number=10, repeat=5))/10 2.43037149906 That's a 15% slowdown. Which is reasonable since you add four multiplies in the loop body. Changing your unrolled loop to: def test_unrolled(n): a = 0 for i in range(n//4): b = 4*i a += f(b) a += f(b+1) a += f(b+2) a += f(b+3) return a makes both versions run in approximately the same time (2.135 vs. 2.136). > That's slightly better (32% slower instead of 37% slower), but still a > massive performance hit. Given these results, I'm prepared to say that > loop unrolling in Python is almost certainly going to be a pessimation, > not an optimization, no matter what you have inside the loop. I don't really see why it should be the case. Do you have any idea? I don't think either that it should speed things up significantly. Loop unrolling in binary code is relevant mostly because it allows better instruction scheduling (i.e., scheduling has fewer constraints in longer instruction sequences). Python programs are way too far from binary code for scheduling opts to apply. -- Alain. -- http://mail.python.org/mailman/listinfo/python-list
Automatic delegation in Python 3
Delegation in old-style classes worked fine: # Python 2.6 >>> class Delegate: ... def __init__(self, x): ... self.__dict__['x'] = x ... def __getattr__(self, name): ... return getattr(self.x, name) ... def __setattr__(self, name, value): ... setattr(self.x, name, value) ... >>> obj = Delegate({}) >>> obj[1] = None >>> obj {1: None} But when I try the equivalent recipe with a new-style class, it behaves differently: >>> class Delegate2(object): ... def __init__(self, x): ... self.__dict__['x'] = x ... def __getattr__(self, name): ... return getattr(self.x, name) ... def __setattr__(self, name, value): ... setattr(self.x, name, value) ... >>> obj = Delegate2({}) >>> obj <__main__.Delegate2 object at 0x8f6130c> Okay, I get that one... because I'm inheriting from object, __getattr__ picks up object's __str__ method and uses that. But then there's this: >>> obj[1] = 0 Traceback (most recent call last): File "", line 1, in TypeError: 'Delegate2' object does not support item assignment But these work: >>> obj.__setitem__ >>> obj.__setitem__(1, None) >>> obj.x {1: None} What's going on here? I *think* this has something to do with special double-underscore methods being looked up on the class, not the instance, for new-style classes, but I'm not entirely sure. Unfortunately, I need to use delegation, not inheritance, and I need to use a new-style class, since I will be using Python 3. How can I do automatic delegation in Python 3? Is my only hope to give up on the elegance of automatic delegation, and code all the special methods as manual delegation? class Delegate2(object): def __setitem__(self, key, value): self.x[key] = value # and so on for everything else I care about... -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, 07 Sep 2010 20:35:45 -0700, Phlip wrote: > Exceptions are very dangerous by themselves, because if you don't trap > them just right they can cause side-effects. Huh? If you don't trap them just right, the cause a stack trace, which is a side-effect I suppose. But it's an *intended* side-effect, since the alternative would be a core dump (or worse, an incorrect program that *doesn't* crash). This is a good thing! -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, 07 Sep 2010 20:38:14 -0700, Phlip wrote: > On Sep 7, 5:51 pm, Terry Reedy wrote: >> On 9/7/2010 2:53 PM, Phlip wrote: >> >> > They are for situations which the caller should care not to handle. >> >> Python is simply not designed that way. Exception raising and catching >> is a common flow-control method in Python. If you cannot stand that, >> Python is not for you. > > While I'm at it, I'm going to log into comp.lang.java.misc and explain > to everyone why static typing is overkill, and implementation > inheritance is good for you. > > Everyone gets defensive about the design flaws in their own language. > But the django.db situation is not even a design flaw; just a > misinterpretation of the Samurai Principle. int('yo') shall throw an > exception, but a missing record could be the result you were looking > for, so it's not exceptional. I think you've misunderstood the meaning of "exception" if you think that. It doesn't mean "error". Nor does it mean "rare". (Although, given that exceptions in Python are expensive, one would hope they're not *too* common.) The unexceptional case of looking up a record is to find it. That, after all, is the purpose of the lookup function -- to find the given record. That's what it is designed to do, and anything else is considered exceptional. Whether the lookup function returns a special "not found" result, or raises an exception, or sets some magic global error code somewhere, it's still an exceptional case. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with simple multiprocessing script on OS X
In article , Darren Dale wrote: > >About a year ago, I contributed a patch to h5py which checks to see if >h5py is being imported into an active IPython session. If so, then a >custom tab completer is loaded to make it easier to navigate hdf5 >files. In the development version of IPython, a function that used to >return None if there was no instance of an IPython interactive shell >now creates and returns a new instance. This was the cause of the >error I was reporting. Hoist on your own petard, eh? ;-) Thanks for reporting the solution. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "...if I were on life-support, I'd rather have it run by a Gameboy than a Windows box." --Cliff Wells -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On 2010-09-08, Steven D'Aprano wrote: > On Tue, 07 Sep 2010 20:35:45 -0700, Phlip wrote: > >> Exceptions are very dangerous by themselves, because if you don't trap >> them just right they can cause side-effects. > > Huh? > > If you don't trap them just right, they cause a stack trace, Not always. That is the effect of not trapping them at all. However, you can trap them incorrectly -- which can result in hard-to-track down problems. The main example of this is a "bare except" clause that not only catches and handles the "expected" exception but also catches (and mishandles/ignores) an unexpected one. > which is a side-effect I suppose. But it's an *intended* side-effect, > since the alternative would be a core dump (or worse, an incorrect > program that *doesn't* crash). This is a good thing! -- Grant Edwards grant.b.edwardsYow! -- I have seen the at FUN -- gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: console-editor written in python
On Wednesday 08 September 2010, it occurred to Tanje Toolate to exclaim: > hi there, > > > greetings. > > i am looking for a small, console-based (opensource) texteditor, > written in python (or as shellscript!), but i'm not finding something > usable. Why? (Also, I can't imagine anyone writing a shell script that deserves, or even aspires to deserve, the name "text editor"). > > very find would be emacs-keybindings ... Why not just use a real emacs ? -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 2010-09-08, Baba wrote: > Thanks for your feedback. My question is: Who owns this forum? If we > all do then we are allowed to post questions that are simple and that > could otherwise be answered by doing research. Of course you're allowed to post such questions. And people are allowed to ignore you, to answer your question sarcastically, to attempt to teach you how to answer your own questions, or to respond in other ways. It's up to _you_ to make the effort to try to insure that you get a useful response. To that end here is how that is accomplished: http://catb.org/esr/faqs/smart-questions.html Maybe you don't _like_ the fact that you are expected to show just a tiny bit of inititiative and care in researching and posting your question, but that's pretty much the way it it works. I'm sure you'd prefer that everything was handed to you for free on a silver platter with a side order of beer and cookies. I'd prefer I was 20 years younger and 30 pounds lighter. Life's tough that way. > It is just unfriendly to tell someone to go and look it up by > themselves. 1) It isn't if give them a hint on where to look it up. 2) Posting questions like yours is considered unfriendly. > Who is licensed to judge what can and cannot be posted as a question? Nobody. Post whatever questions you want however you want in whatever language you want. If you don't care about actually _answering_ your question, feel free to continue in the same vein in which you started. If you _do_ care about getting a prompt, accurate answer, then: http://catb.org/esr/faqs/smart-questions.html > A teacher of mine used to say: "There are no stupid questions, there > are only stupid answers." Ask that teacher for help then. > It is arrogant to tell someone in a Forum to "look it up yourself, > this question is too basic". Perhaps it is. But remember you're the one asking strangers for a free service, so it's up to you to "play by the rules" in return for that free service. One of the rules is that you first try to answer the question yourself. > Can you not understand that accessing a file can seem daunting at > first? Maybe so, but reading a tutorial isn't. -- Grant Edwards grant.b.edwardsYow! I need to discuss at BUY-BACK PROVISIONS gmail.comwith at least six studio SLEAZEBALLS!! -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Wed, 08 Sep 2010 14:44:12 +, Grant Edwards wrote: > On 2010-09-08, Steven D'Aprano > wrote: >> On Tue, 07 Sep 2010 20:35:45 -0700, Phlip wrote: >> >>> Exceptions are very dangerous by themselves, because if you don't trap >>> them just right they can cause side-effects. >> >> Huh? >> >> If you don't trap them just right, they cause a stack trace, > > Not always. That is the effect of not trapping them at all. However, > you can trap them incorrectly -- which can result in hard-to-track down > problems. > > The main example of this is a "bare except" clause that not only catches > and handles the "expected" exception but also catches (and > mishandles/ignores) an unexpected one. Ah, fair enough. That would be a false positive error -- catching too much rather than too little. Still, that's no better, or worse, than misinterpreting special error codes that are returned by functions. The main error mode there is catching too little -- people neglect to check the error code, and therefore have buggy code: p = some_string.find('#') print some_string[:p] And who hasn't done this more than once? re.search(pattern, some_string).group() -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: console-editor written in python
On 8 Sep., 16:49, Thomas Jollans wrote: > On Wednesday 08 September 2010, it occurred to Tanje Toolate to exclaim: > > > hi there, > > > greetings. > > > i am looking for a small, console-based (opensource) texteditor, > > written in python (or as shellscript!), but i'm not finding something > > usable. > > Why? > (Also, I can't imagine anyone writing a shell script that deserves, or even > aspires to deserve, the name "text editor"). > > > > > very find would be emacs-keybindings ... > > Why not just use a real emacs ? why? because! ok, the part with the shellscript was a joke ... we usually use joe/jmacs but run into issues lately on customer- environments with precompiled versions of this editor. a python-based console- editor would be perfekt. tanja -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba a écrit : "Dear xyz, Your question can easily be researched online. We suggest you give it a try and to look it up yourself. This will be beneficial both to you and to us. We do encourage to ask questions only when they have been researched first." On usenet - as well as on most technical forums / mailing lists / etc -, this usually condensed in a well-known four letters acronym : "RTFM" Hopefully c.l.py is an unusually friendly and polite place, so we tend to say it a bit more elegantly and most of the time we do indeed provide a link. This being said and given your attitude - you may not realize it, but by now you would have been flamed to hell and back on quite a few other newsgroups -, I'm very tempted to switch to option 2. -- http://mail.python.org/mailman/listinfo/python-list
listening socket
Hi, I'm trying to create a listening socket connection on port 1514. I tried to follow the documentation at: http://docs.python.org/release/2.5.2/lib/socket-example.html and came up with following lines: import socket host = '' # Symbolic name meaning all available interfaces port = 1514 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.send(data) conn.close() but that is not working, i'm getting this: import: unable to open X server `' @ error/import.c/ImportImageCommand/ 362. ./sockettest.py: line 4: host: command not found ./sockettest.py: line 5: port: command not found ./sockettest.py: line 6: syntax error near unexpected token `(' ./sockettest.py: line 6: `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)' now why would it try to open an x server??? :o -- http://mail.python.org/mailman/listinfo/python-list
Re: listening socket
On Wed, Sep 8, 2010 at 12:59 PM, cerr wrote: > Hi, > > I'm trying to create a listening socket connection on port 1514. > I tried to follow the documentation at: > http://docs.python.org/release/2.5.2/lib/socket-example.html > and came up with following lines: > import socket > > host = '' # Symbolic name meaning all available > interfaces > port = 1514 # Arbitrary non-privileged port > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > s.bind((host, port)) > s.listen(1) > conn, addr = s.accept() > print 'Connected by', addr > while 1: > data = conn.recv(1024) > if not data: break > conn.send(data) > conn.close() > but that is not working, i'm getting this: > import: unable to open X server `' @ error/import.c/ImportImageCommand/ > 362. > ./sockettest.py: line 4: host: command not found > ./sockettest.py: line 5: port: command not found > ./sockettest.py: line 6: syntax error near unexpected token `(' > ./sockettest.py: line 6: `s = socket.socket(socket.AF_INET, > socket.SOCK_STREAM)' > > now why would it try to open an x server??? :o > -- Because it's not executing it as a Python program. It's trying to execute it as a shell script. If you want to run a script as a Python program, either call the interpreter directly python sockettest.py or include a Shebang line as the first line of the file that tells the computer what interpreter to use #!/usr/bin/env python The file extension itself is meaningless to a Unix shell- it's just a part of the file name. -- http://mail.python.org/mailman/listinfo/python-list
are there pros or contras, keeping a connection to a (sqlite) database ?
hello, I wrap my database in some class, and on creation of the instance, a connection to the database is created, and will stay connected until the program exists, something like this: self.conn = sqlite3.connect ( self.filename ) Now I wonder if there are pros or contras to keep the connection to the database continuously "open" ? thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: listening socket
On Sep 8, 10:06 am, Benjamin Kaplan wrote: > On Wed, Sep 8, 2010 at 12:59 PM, cerr wrote: > > Hi, > > > I'm trying to create a listening socket connection on port 1514. > > I tried to follow the documentation at: > >http://docs.python.org/release/2.5.2/lib/socket-example.html > > and came up with following lines: > > import socket > > > host = '' # Symbolic name meaning all available > > interfaces > > port = 1514 # Arbitrary non-privileged port > > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > > s.bind((host, port)) > > s.listen(1) > > conn, addr = s.accept() > > print 'Connected by', addr > > while 1: > > data = conn.recv(1024) > > if not data: break > > conn.send(data) > > conn.close() > > but that is not working, i'm getting this: > > import: unable to open X server `' @ error/import.c/ImportImageCommand/ > > 362. > > ./sockettest.py: line 4: host: command not found > > ./sockettest.py: line 5: port: command not found > > ./sockettest.py: line 6: syntax error near unexpected token `(' > > ./sockettest.py: line 6: `s = socket.socket(socket.AF_INET, > > socket.SOCK_STREAM)' > > > now why would it try to open an x server??? :o > > -- > > Because it's not executing it as a Python program. It's trying to > execute it as a shell script. If you want to run a script as a Python > program, either call the interpreter directly > python sockettest.py > > or include a Shebang line as the first line of the file that tells the > computer what interpreter to use > #!/usr/bin/env python > > The file extension itself is meaningless to a Unix shell- it's just a > part of the file name. hoops right... heh, thanks... :$ clearly doing too many things at the same time... -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
On Sep 7, 7:05 am, Baba wrote: > Hi > > I am working on an exercise which requires me to write a funtion that > will check if a given word can be found in a given dictionary (the > hand). > > def is_valid_word(word, hand, word_list): > """ > Returns True if word is in the word_list and is entirely > composed of letters in the hand. Otherwise, returns False. > Does not mutate hand or word_list.""" > > I don't understand this part: Does not mutate hand or word_list > I would re-read your exercise description. hand is *not* a dictionary, but is most likely a list of individual letters. word_list too is probably *not* a dictionary, but a list of valid words (although this does bear a resemblance to what people in everyday life call a "dictionary"). Where did you get the idea that there was a dictionary in this problem? The "Does not mutate hand or word_list." is a constraint that you are not allowed to update the hand or word_list arguments. For instance, you must not call word_list.sort() in order to search for the given word using some sort of binary search. You must not determine if all the letters in word come from hand by modifying the hand list (like dropping letters from hand as they are found in word). There are ways to copy arguments if you use a destructive process on their contents, so that the original stays unmodified - but that sounds like part of the exercise for you to learn about. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On 2010-09-08, Steven D'Aprano wrote: > On Wed, 08 Sep 2010 14:44:12 +, Grant Edwards wrote: >> >>> If you don't trap them just right, they cause a stack trace, >> >> Not always. That is the effect of not trapping them at all. However, >> you can trap them incorrectly -- which can result in hard-to-track down >> problems. >> >> The main example of this is a "bare except" clause that not only catches >> and handles the "expected" exception but also catches (and >> mishandles/ignores) an unexpected one. > > Ah, fair enough. That would be a false positive error -- catching too > much rather than too little. > > Still, that's no better, or worse, than misinterpreting special error > codes that are returned by functions. No, I didn't mean to imply that was the case. I agree with your conclusion. I find it much easier to screw things up using the "exceptional return value" method than the "exception raise" method. -- Grant Edwards grant.b.edwardsYow! Psychoanalysis?? at I thought this was a nude gmail.comrap session!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: listening socket
On 2010-09-08, Benjamin Kaplan wrote: > On Wed, Sep 8, 2010 at 12:59 PM, cerr wrote: >> but that is not working, i'm getting this: >> import: unable to open X server `' @ error/import.c/ImportImageCommand/ [...] >> now why would it try to open an x server??? :o > > Because it's not executing it as a Python program. It's trying to > execute it as a shell script. What's even more fun is if you do have an X server, and the shell is able to run the import program, and it does open the X server, and so on... [Not that things like that ever happen to _me_] -- Grant Edwards grant.b.edwardsYow! I feel partially at hydrogenated! gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: are there pros or contras, keeping a connection to a (sqlite) database ?
On 9/8/2010 10:09 AM, Stef Mientki wrote: hello, I wrap my database in some class, and on creation of the instance, a connection to the database is created, and will stay connected until the program exists, something like this: self.conn = sqlite3.connect ( self.filename ) Now I wonder if there are pros or contras to keep the connection to the database continuously "open" ? thanks, Stef Mientki Open is OK. Open is good, because the database system gets to cache some data. Open with an uncommitted transaction may leave the file locked, preventing access by other processes. So make sure you commit before you go idle. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
executing script in fork
Hi There, I want to trigger another script and having it running forked to my mother process. I googled around and came up with following: commandlist=['./GPSsim.pl',proto,'files/gps.txt'] subprocess.Popen(commandlist) print "GPS simulator started" This however doesn't seem disconnect stdout but it still throws me back to the shell. How can I launch it with disconnected stdout and still continue running further code in my mother script? Thanks, Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
"Steven D'Aprano" wrote in message news:4c878be5$0$3$c3e8...@news.astraweb.com... On Tue, 07 Sep 2010 11:00:03 +0100, BartC wrote: for i in xrange(1): a = a + f(i) With Python 3 and def f(x): return x+1, unrolling this loop 4x improved speed by 15%; 4.00 minutes reduces to 3.30 minutes. I'm afraid that I can't replicate those figures. In my test, unrolling the loop causes a massive SLOWDOWN of 37%, not a speed up. Here is my test code: You're absolutely right. I completely forgot about modulating the i index for each duplicated line. def test_unrolled(n): a = 0 for i in range(n//4): a += f(4*i) a += f(4*i+1) a += f(4*i+2) a += f(4*i+3) return a Although tidying up your calculations (as already posted) gives code that is neither faster nor slower. I'd hoped that using the following would help, but did nothing in Python 3, and gave only 8-10% speedup in Python 2: for i in xrange(0,n,4): a=a+f(i) a=a+f(i+1) a=a+f(i+2) a=a+f(i+3) (On my other example of setting list elements to 0, this did improve speed by some 10% in Python 3, and 28% in '2'.) So using manual unrolling for an indexed loop is not going to be the right approach (it's also fiddly, and there is the problem of N not being a multiple of 4 or whatever). We're trying to avoid the iteration overhead, yet we're adding it in the code anyway, and as user-level code. But, I still think that internal support for such a thing might be worthwhile, when it can make certain assumptions about the loop range and index type. -- Bartc -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
I believe I answered your question a day ago. If it has not reached you yet, try the gmane.org archives. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Slice a list of lists?
I know that I can index into a list of lists like this: a=[[1,2,3],[4,5,6],[7,8,9]] a[0][2]=3 a[2][0]=7 but when I try to use fancy indexing to select the first item in each list I get: a[0][:]=[1,2,3] a[:][0]=[1,2,3] Why is this and is there a way to select [1,4,7]? -- http://mail.python.org/mailman/listinfo/python-list
cPickle segfault with nested dicts in threaded env
Hi all, Before creating an issue @ bugs.python.org I wanted to run the following by everyone We are having a difficult time with what looks like a cPickle issue when given a data structure containing >15 nested dictionaries, but only when threading is involved Environment is FreeBSD 8, Python 2.5.5 The following diff contains an addition for /usr/ports/lang/python25/work/Python-2.5.5/Lib/test/test_cpickle.py which reproduces the issue kenn...@kenneth0 Python-2.5.5 $ cat /tmp/test_cpickle.nested_dicts_in_threaded_env.diff 1a2 > import copy 2a4 > import threading 93a96,105 > def test_nested_dicts_in_threaded_env(self): > # segfaults on python2.5.5/FreeBSD 8.0-RELEASE-p2 > def threadloop(): > x = {} > for i in range(16): > x = {i:copy.copy(x)} > cPickle.dumps(x, 2) > t = threading.Thread(target=threadloop) > t.start() > Any thoughts will be appreciated, thanks for looking, Kenneth -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, 8 Sep 2010 13:55:50 -0500 Jonno wrote: > I know that I can index into a list of lists like this: > a=[[1,2,3],[4,5,6],[7,8,9]] > a[0][2]=3 > a[2][0]=7 > > but when I try to use fancy indexing to select the first item in each > list I get: Let me write out in words what you're doing, and it should become clear: > a[0][:]=[1,2,3] Here you're making a list of all elements of the first element of a. > a[:][0]=[1,2,3] > And here you're selecting the first element of all elements of a. Huh. Not quite as clear as I hoped. But ponder on this for a few moments. It'll dawn on you eventually. > Why is this and is there a way to select [1,4,7]? zip(*a)[0] (or rather list(zip(*a)[0]), if you definitely need a list and not a tuple) /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 2:55 PM, Jonno wrote: > I know that I can index into a list of lists like this: > a=[[1,2,3],[4,5,6],[7,8,9]] > a[0][2]=3 > a[2][0]=7 > > but when I try to use fancy indexing to select the first item in each > list I get: > a[0][:]=[1,2,3] > a[:][0]=[1,2,3] > > Why is this and is there a way to select [1,4,7]? > -- It's not fancy indexing. It's called taking a slice of the existing list. Look at it this way a[0] means take the first element of a. The first element of a is [1,2,3] a[0][:] means take all the elements in that first element of a. All the elements of [1,2,3] are [1,2,3]. a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]]. a[:][0] means take the first element of all the elements of a. The first element of a[:] is [1,2,3]. There is no simple way to get [1,4,7] because it is just a list of lists and not an actual matrix. You have to extract the elements yourself. col = [] for row in a: col.append(row[0]) You can do this in one line using a list comprehension: [ row[0] for row in a ] > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
Let me rephrase what I wrote a bit. On Wed, 8 Sep 2010 15:08:11 -0400 Andreas Waldenburger wrote: > > a[0][:]=[1,2,3] > Here you're making a list of all elements of the first element of a. > That is, you're making a copy of the first element of a. > > a[:][0]=[1,2,3] > > > And here you're selecting the first element of all elements of a. > That is, you're taking the first element of a copy of a. I hope this is a little less confusing. /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, 8 Sep 2010 15:11:51 -0400 Benjamin Kaplan wrote: > There is no simple way to get [1,4,7] because it is just a list of > lists and not an actual matrix. You have to extract the elements > yourself. > > col = [] > for row in a: > col.append(row[0]) > > > You can do this in one line using a list comprehension: > [ row[0] for row in a ] I would suggest this (esp. the list comprehension version) over my suggestion of zip(). WAAA more readable. Apparently I'm getting rusty. /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] with statement syntax forces ugly line breaks?
On 08/09/2010 19:07, Georg Brandl wrote: Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. FYI, that should be "thine indenting". "My/thy" before a consonant, "mine/thine" before a vowel. Compare with "a/an", which we still do. -- http://mail.python.org/mailman/listinfo/python-list
Re: Automatic delegation in Python 3
On 9/8/2010 9:58 AM, Steven D'Aprano wrote: Delegation in old-style classes worked fine: # Python 2.6 class Delegate: ... def __init__(self, x): ... self.__dict__['x'] = x ... def __getattr__(self, name): ... return getattr(self.x, name) ... def __setattr__(self, name, value): ... setattr(self.x, name, value) ... obj = Delegate({}) obj[1] = None Since you are not setting a attribute, I was initially surprised that this worked. Then I remembered that this is executed for old-style classes as obj.__getattr__('__setitem__')(1,None) and that __setattr__ above is not involved. This is possible because old-style user classes were a world apart form builtin types. Once user-classes were integrated into the builting class/type hierarchy, they had to follow the same rules as the latter. One problem is that classes are also instances of their metaclass. This is somewhat explained in 3.3.8 'Special method lookup' (which I have read at least 3 times before understanding). As you discovered, this is not a complete win. The bottom line of 3.3.8: "Bypassing the __getattribute__() machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method must be set on the class object itself in order to be consistently invoked by the interpreter)." You are seeing the cost. Unfortunately, I need to use delegation, not inheritance, and I need to use a new-style class, since I will be using Python 3. How can I do automatic delegation in Python 3? Is my only hope to give up on the elegance of automatic delegation, and code all the special methods as manual delegation? Based on the above, it seems so. But I would search a bit more for delegation and proxy object and python 3 to see what others have done. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On 9/8/2010 12:17 PM Andreas Waldenburger said... On Wed, 8 Sep 2010 15:11:51 -0400 Benjamin Kaplan wrote: There is no simple way to get [1,4,7] because it is just a list of lists and not an actual matrix. You have to extract the elements yourself. col = [] for row in a: col.append(row[0]) You can do this in one line using a list comprehension: [ row[0] for row in a ] I would suggest this (esp. the list comprehension version) over my suggestion of zip(). WAAA more readable. Apparently I'm getting rusty. zip is very handy for inverting rows and cols (x's and y's, whatever) >>> a = [(1,2),(3,4),(5,6),(7,8),(9,10)] >>> zip(*a) [(1, 3, 5, 7, 9), (2, 4, 6, 8, 10)] So, I like zip if you're dealing with a in a matrix-ish manner, and list comps for picking selected items out of a list. Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: cPickle segfault with nested dicts in threaded env
On Wednesday 08 September 2010, it occurred to Kenneth Dombrowski to exclaim: > Environment is FreeBSD 8, Python 2.5.5 Which architecture? Also, Python 2.5 is frightfully old. There's not really any problem with still using it, but nobody's maintaining it upstream, so don't bother reporting a bug with Python 2.5, as you'll just be told to test a newer version. Check if this still occurs in Python 2.7 and/or Python 3.1, and, if it does, then please do report the bug. Chances are this was actually fixed a couple of years ago. See: http://bugs.python.org/issue3640 (and http://bugs.python.org/issue3338) -- http://mail.python.org/mailman/listinfo/python-list
Re: cPickle segfault with nested dicts in threaded env
On Sep 8, 11:58 am, Kenneth Dombrowski wrote: > Hi all, > > Before creating an issue @ bugs.python.org I wanted to run the > following by everyone > > We are having a difficult time with what looks like a cPickle issue > when given a data structure containing >15 nested dictionaries, but > only when threading is involved > > Environment is FreeBSD 8, Python 2.5.5 Since Python 2.7 is released, Python 2.5 is no longer accepting bug fixes, only security fixes. So be aware. > The following diff contains an addition for > /usr/ports/lang/python25/work/Python-2.5.5/Lib/test/test_cpickle.py > which reproduces the issue > > kenn...@kenneth0 Python-2.5.5 $ cat > /tmp/test_cpickle.nested_dicts_in_threaded_env.diff > 1a2 > > > import copy > 2a4 > > import threading > 93a96,105 > > def test_nested_dicts_in_threaded_env(self): > > # segfaults on python2.5.5/FreeBSD 8.0-RELEASE-p2 > > def threadloop(): > > x = {} > > for i in range(16): > > x = {i:copy.copy(x)} > > cPickle.dumps(x, 2) > > t = threading.Thread(target=threadloop) > > t.start() > > Any thoughts will be appreciated, thanks for looking, Bug. Python should never segfault (unless you're doing bad things with ctypes). Even if threading isn't supported in this case, it should fail with an exception or have undefined behavior, not segfault. I would check to see if the same problem exists on 2.7, and file a bug report if so. If they fix it in 2.7 maybe you could apply the same fix to your own 2.5. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan wrote: > On Wed, Sep 8, 2010 at 2:55 PM, Jonno wrote: >> I know that I can index into a list of lists like this: >> a=[[1,2,3],[4,5,6],[7,8,9]] >> a[0][2]=3 >> a[2][0]=7 >> >> but when I try to use fancy indexing to select the first item in each >> list I get: >> a[0][:]=[1,2,3] >> a[:][0]=[1,2,3] >> >> Why is this and is there a way to select [1,4,7]? >> -- > > It's not fancy indexing. It's called taking a slice of the existing > list. Look at it this way > a[0] means take the first element of a. The first element of a is [1,2,3] > a[0][:] means take all the elements in that first element of a. All > the elements of [1,2,3] are [1,2,3]. > > a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]]. > a[:][0] means take the first element of all the elements of a. The > first element of a[:] is [1,2,3]. > > There is no simple way to get [1,4,7] because it is just a list of > lists and not an actual matrix. You have to extract the elements > yourself. > > col = [] > for row in a: > col.append(row[0]) > > > You can do this in one line using a list comprehension: > [ row[0] for row in a ] > Thanks! (to Andreas too). Totally makes sense now. -- http://mail.python.org/mailman/listinfo/python-list
Re: what should __iter__ return?
On 9/7/2010 9:40 PM, Gregory Ewing wrote: Thomas Jollans wrote: Hmm. Modifying an object while iterating over it isn't a great idea, ever: I wouldn't say never. How about "Modifying a collection while iterating over it without understanding the dangers is a bad idea." " Algorithms that calculate some kind of transitive closure can be expressed rather neatly by appending items to a list being iterated over. Which is one reason list modification while iterating is not prohibited. Deleting items while forward iterating and adding items while backward iterating may miss items unexpectedly. Adding items ahead of forward iteration and deleting items behind backward iterating are ok. The most common example of the latter is successively popping item off a stack. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 3:06 PM, Jonno wrote: > On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan > wrote: >> On Wed, Sep 8, 2010 at 2:55 PM, Jonno wrote: >>> I know that I can index into a list of lists like this: >>> a=[[1,2,3],[4,5,6],[7,8,9]] >>> a[0][2]=3 >>> a[2][0]=7 >>> >>> but when I try to use fancy indexing to select the first item in each >>> list I get: >>> a[0][:]=[1,2,3] >>> a[:][0]=[1,2,3] >>> >>> Why is this and is there a way to select [1,4,7]? >>> -- >> >> It's not fancy indexing. It's called taking a slice of the existing >> list. Look at it this way >> a[0] means take the first element of a. The first element of a is [1,2,3] >> a[0][:] means take all the elements in that first element of a. All >> the elements of [1,2,3] are [1,2,3]. >> >> a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]]. >> a[:][0] means take the first element of all the elements of a. The >> first element of a[:] is [1,2,3]. >> >> There is no simple way to get [1,4,7] because it is just a list of >> lists and not an actual matrix. You have to extract the elements >> yourself. >> >> col = [] >> for row in a: >> col.append(row[0]) >> >> >> You can do this in one line using a list comprehension: >> [ row[0] for row in a ] >> > Thanks! (to Andreas too). Totally makes sense now. > Now if I want to select the first item in every 2nd item of list a (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of indices to use in a more complex for loop? -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 3:18 PM, Jonno wrote: > On Wed, Sep 8, 2010 at 3:06 PM, Jonno wrote: >> On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan >> wrote: >>> On Wed, Sep 8, 2010 at 2:55 PM, Jonno wrote: I know that I can index into a list of lists like this: a=[[1,2,3],[4,5,6],[7,8,9]] a[0][2]=3 a[2][0]=7 but when I try to use fancy indexing to select the first item in each list I get: a[0][:]=[1,2,3] a[:][0]=[1,2,3] Why is this and is there a way to select [1,4,7]? -- >>> >>> It's not fancy indexing. It's called taking a slice of the existing >>> list. Look at it this way >>> a[0] means take the first element of a. The first element of a is [1,2,3] >>> a[0][:] means take all the elements in that first element of a. All >>> the elements of [1,2,3] are [1,2,3]. >>> >>> a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]]. >>> a[:][0] means take the first element of all the elements of a. The >>> first element of a[:] is [1,2,3]. >>> >>> There is no simple way to get [1,4,7] because it is just a list of >>> lists and not an actual matrix. You have to extract the elements >>> yourself. >>> >>> col = [] >>> for row in a: >>> col.append(row[0]) >>> >>> >>> You can do this in one line using a list comprehension: >>> [ row[0] for row in a ] >>> >> Thanks! (to Andreas too). Totally makes sense now. >> > > Now if I want to select the first item in every 2nd item of list a > (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of > indices to use in a more complex for loop? > Seems like the simplest way would be: [row[0] for row in a][::2] -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On 9/8/2010 2:55 PM, Jonno wrote: I know that I can index into a list of lists like this: a=[[1,2,3],[4,5,6],[7,8,9]] a[0][2]=3 a[2][0]=7 but when I try to use fancy indexing to select the first item in each list I get: a[0][:]=[1,2,3] a[:][0]=[1,2,3] Why is this and is there a way to select [1,4,7]? You are trying to look at a list of lists as an array and have discovered where the asymmetry of the former makes the two non-equivalent. To slice multi-dimensional arrays any which way, you need an appropriate package, such as numpy. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 4:23 PM, Jonno wrote: > On Wed, Sep 8, 2010 at 3:18 PM, Jonno wrote: >> On Wed, Sep 8, 2010 at 3:06 PM, Jonno wrote: >>> On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan >>> wrote: On Wed, Sep 8, 2010 at 2:55 PM, Jonno wrote: > I know that I can index into a list of lists like this: > a=[[1,2,3],[4,5,6],[7,8,9]] > a[0][2]=3 > a[2][0]=7 > > but when I try to use fancy indexing to select the first item in each > list I get: > a[0][:]=[1,2,3] > a[:][0]=[1,2,3] > > Why is this and is there a way to select [1,4,7]? > -- It's not fancy indexing. It's called taking a slice of the existing list. Look at it this way a[0] means take the first element of a. The first element of a is [1,2,3] a[0][:] means take all the elements in that first element of a. All the elements of [1,2,3] are [1,2,3]. a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]]. a[:][0] means take the first element of all the elements of a. The first element of a[:] is [1,2,3]. There is no simple way to get [1,4,7] because it is just a list of lists and not an actual matrix. You have to extract the elements yourself. col = [] for row in a: col.append(row[0]) You can do this in one line using a list comprehension: [ row[0] for row in a ] >>> Thanks! (to Andreas too). Totally makes sense now. >>> >> >> Now if I want to select the first item in every 2nd item of list a >> (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of >> indices to use in a more complex for loop? >> > Seems like the simplest way would be: > [row[0] for row in a][::2] Yes. You could either do that, or do [row[0] for row in a[::2]]. It doesn't make that much of a difference, unless memory is really tight (doing the ::2 first means your list is smaller to begin with) > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, 8 Sep 2010 15:23:35 -0500 Jonno wrote: > On Wed, Sep 8, 2010 at 3:18 PM, Jonno wrote: > [snip] > > Now if I want to select the first item in every 2nd item of list a > > (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of > > indices to use in a more complex for loop? > > > Seems like the simplest way would be: > [row[0] for row in a][::2] What you're doing here is selecting every second item of the list of first items of the items in a, not the first items of every second item in a (head spinning yet?). If I'm not completely mindbent right now, these are logically equivalent, but not computationally. Compare [row[0] for row in a][::2] # (your Python code) with [row[0] for row in a[::2]] # (as per your description) The first one is more work for your computer, because it'll pick out the first elements of *all* of the items in a, whereas the second only picks out the first elements of every second item in a (which is only half the amount of "picks" compared to the former). I just thought I'd mention it. Because it might make a difference in one of your programs some day. And because I'm a pedant ;). /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
Re: Automatic delegation in Python 3
On Wednesday 08 September 2010, it occurred to Steven D'Aprano to exclaim: > What's going on here? I *think* this has something to do with special > double-underscore methods being looked up on the class, not the instance, > for new-style classes, but I'm not entirely sure. Yes, special methods are looked up on the type. So you have to make sure the type has the methods. > > Unfortunately, I need to use delegation, not inheritance, and I need to > use a new-style class, since I will be using Python 3. How can I do > automatic delegation in Python 3? Is my only hope to give up on the > elegance of automatic delegation, and code all the special methods as > manual delegation? Well, yes, you have to implement all the required special methods in the Delegate class. But there's no reason you have to do it manually. (I've never actually used this in the wild, but it looks like it works) >>> def makeDelegate(o): ... T = type(o) ... class Delegate: ... def __getattr__(self, name): ... return getattr(o, name) ... def __setattr__(self, name, value): ... setattr(self, name, value) ... def makewrapper(method): ... def wrapper(self, *args, **kwargs): ... return method(o, *args, **kwargs) ... return wrapper ... for methodname in dir(T): ... method = getattr(T, methodname) ... if methodname not in ('__getattr__', '__setattr__', ... '__init__', '__new__', '__class__'): ... try: ... setattr(Delegate, methodname, makewrapper(method)) ... except: pass ... return Delegate() ... >>> D = makeDelegate({'a': 1}) >>> D {'a': 1} >>> D['a'] 1 >>> D['a'] = 2 >>> D {'a': 2} >>> D.get('b') >>> D['b'] = True >>> D.get('b') True >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On Wed, Sep 8, 2010 at 3:44 PM, Andreas Waldenburger wrote: > On Wed, 8 Sep 2010 15:23:35 -0500 Jonno wrote: > >> On Wed, Sep 8, 2010 at 3:18 PM, Jonno wrote: >> [snip] >> > Now if I want to select the first item in every 2nd item of list a >> > (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of >> > indices to use in a more complex for loop? >> > >> Seems like the simplest way would be: >> [row[0] for row in a][::2] > > What you're doing here is selecting every second item of the list of > first items of the items in a, not the first items of every second item > in a (head spinning yet?). > > If I'm not completely mindbent right now, these are logically > equivalent, but not computationally. > > Compare > [row[0] for row in a][::2] # (your Python code) > with > [row[0] for row in a[::2]] # (as per your description) > > The first one is more work for your computer, because it'll pick out > the first elements of *all* of the items in a, whereas the second only > picks out the first elements of every second item in a (which is only > half the amount of "picks" compared to the former). > > I just thought I'd mention it. Because it might make a difference in > one of your programs some day. And because I'm a pedant ;). Thanks again. It is nice to know how to do things properly even though in my case it probably won't make much difference. Terry, I would have used numpy arrays (I actually use them later in the code) except the lists in my list aren't all of the same length. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 8 sep, 14:39, Paul Rubin wrote: > Baba writes: > > But where do you draw the line? Can we not just let people ask > > questions regardless? And let those answer who want to and those who > > don't just ignore the question? That seems so much easier to me. > > The first few times, it's easy to ignore the questions. After a few > more times, it becomes easier to ignore the person asking the questions, > and not even look at the questions. > > The answer to many of your questions also is not "research it online", > but rather, "figure it out with your own creativity". That's what being > a programmer is about--figuring out solutions that don't already exist > elsewhere. Hi Paul If i look where i was 4 weeks ago and the progress i made in learning Python i am quite delighted. This forum has helped me and i appreciate it. I don't think i will ever tell a beginner to "do me a favour" and to look things up by himself nor will i use the RTFM line (refering to Bruno's post), i'd just be nice and helpful as everyone here is. Didn't realise ego's were that big but so is mine i suppose... kind regards Baba -- http://mail.python.org/mailman/listinfo/python-list
Confused about nested scopes and when names get added to namespaces
I'm having trouble understanding when variables are added to namespaces. I thought I understood it, but my nested function examples below have me very confused. In each test function below I have an x variable (so "x" is in the namespace of each test function). I also have a nested function in each (innerFunc) that has different flavors of trying to access or assign a variable named "x". --- def test1(): print "running test1..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # x not present (yet) x = 2 print x innerFunc() print "x left as %s\n" % x def test2(): print "running test2..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # x not present innerFunc() print "x left as %s\n" % x def test3(): print "running test3..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # how is x in locals in this case?? print x innerFunc() print "x left as %s\n" % x test1() test2() test3() --- With the nested scope rules, I thought that *at the time of* trying to access an object with a given name, if the name was not available in the namespace of the local function python would then check the namespace of the parent function. My tests above don't seem to match this. Specifically my "at the time of" assumption. What is happening in test3? How is it that "x" ends up in the local namespace before it is ever referenced? It seems that, prior to execution time, the python compiler is "magically" determining that I'm referencing a particular name before assignment and shoving it into the local namespace so it can be used. I did not think the compiler went into function bodies, aside from basic syntax checking. Further confusing me (or confirming the compiler behavior) is adding a 4th test with only one added line... def test4(): print "running test4..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # how is x in locals in this case?? print x x = 2 #ONLY ADDED LINE TO TEST3 innerFunc() print "x left as %s\n" % x In this case I get "UnboundLocalError: local variable 'x' referenced before assignment". I think this means that the compiler (prior to runtime) inspected the code, determined I will do an assignment, decided _not_ to bring the parent's x into the local namespace, and as a result caused the unbound name problem at runtime. It seems that the parent's "x" is brought directly into the local namespace (when appropriate), rather than the namespace lookup just moving up the hierarchy when not found. This is confusing to me and is making me question my understanding of namespace lookups. Are nested scopes a special case where the lookup is handled differently? What if I want to change the value of the parent's "x"? test4 implies that I can't. Have I got this right? Can someone please clarify what is going on? -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about nested scopes and when names get added to namespaces
My tests were run in python 2.6.5. -- http://mail.python.org/mailman/listinfo/python-list
Re: Slice a list of lists?
On 9/8/10 3:27 PM, Terry Reedy wrote: On 9/8/2010 2:55 PM, Jonno wrote: I know that I can index into a list of lists like this: a=[[1,2,3],[4,5,6],[7,8,9]] a[0][2]=3 a[2][0]=7 but when I try to use fancy indexing to select the first item in each list I get: a[0][:]=[1,2,3] a[:][0]=[1,2,3] Why is this and is there a way to select [1,4,7]? You are trying to look at a list of lists as an array and have discovered where the asymmetry of the former makes the two non-equivalent. To slice multi-dimensional arrays any which way, you need an appropriate package, such as numpy. A motivating example: [~] |1> import numpy [~] |2> a = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) [~] |3> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [~] |4> a[0,2] 3 [~] |5> a[2,0] 7 [~] |6> a[0,:] array([1, 2, 3]) [~] |7> a[:,0] array([1, 4, 7]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: what should __iter__ return?
On Wed, 08 Sep 2010 16:17:02 -0400, Terry Reedy wrote: > On 9/7/2010 9:40 PM, Gregory Ewing wrote: >> Thomas Jollans wrote: >> >>> Hmm. Modifying an object while iterating over it isn't a great idea, >>> ever: >> >> I wouldn't say never. > > How about "Modifying a collection while iterating over it without > understanding the dangers is a bad idea." "... unless you're being paid by the hour for debugging your code." *wink* -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Automatic delegation in Python 3
On Wed, 08 Sep 2010 15:45:46 -0400, Terry Reedy wrote: [...] >> Unfortunately, I need to use delegation, not inheritance, and I need to >> use a new-style class, since I will be using Python 3. How can I do >> automatic delegation in Python 3? Is my only hope to give up on the >> elegance of automatic delegation, and code all the special methods as >> manual delegation? > > Based on the above, it seems so. But I would search a bit more for > delegation and proxy object and python 3 to see what others have done. That's what I was afraid of. Oh well. Thanks to those who answered. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: are there pros or contras, keeping a connection to a (sqlite) database ?
On Sep 8, 1:09 pm, Stef Mientki wrote: > hello, > > I wrap my database in some class, and on creation of the instance, a > connection to the database is > created, > and will stay connected until the program exists, something like this: > > self.conn = sqlite3.connect ( self.filename ) > > Now I wonder if there are pros or contras to keep the connection to the > database continuously "open" ? > > thanks, > Stef Mientki I do the same thing--good to hear from John that keeping it open is OK. But another question that this provokes, at least for me is: what happens when you call .connect() on the same database multiple times from within different parts of the same app? Is that bad? And is it that there now multiple connections to the database, or one connection that has multiple names in different namespaces within the app? I'm not even sure what a "connection" really is; I assumed it was nothing more than a rule that says to write to the database with the file named in the parentheses. Further elaboration from the community would be helpful. Thanks, Che -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
| Hi Paul | | If i look where i was 4 weeks ago and the progress i made in learning | Python i am quite delighted. This forum has helped me and i appreciate | it. I don't think i will ever tell a beginner to "do me a favour" and | to look things up by himself nor will i use the RTFM line (refering to | Bruno's post), i'd just be nice and helpful as everyone here is. | Didn't realise ego's were that big but so is mine i suppose... | HEREow can you be learning so much python if you're constantly expressing your personal tastes and generally posturing on the Python forum? I know how words can capture one into a realm of pretended meaning and significance. The nice thing about programming, as opposed to the humanities, is that it's about actually doing things, or rather, actually building things that do things for you. Let's get to it! Best, Bar -- -- "All men are born mad. Some remain so." --Samuel Beckett -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about nested scopes and when names get added to namespaces
On 9/8/2010 2:18 PM Russell Warren said... I'm having trouble understanding when variables are added to namespaces. I thought I understood it, but my nested function examples below have me very confused. Take a look at PEP 227 where nested scopes are introduced. http://www.python.org/dev/peps/pep-0227/ From the PEP: If a name is used within a code block, but it is not bound there and is not declared global, the use is treated as a reference to the nearest enclosing function region. In each test function below I have an x variable (so "x" is in the namespace of each test function). I also have a nested function in each (innerFunc) that has different flavors of trying to access or assign a variable named "x". --- def test1(): print "running test1..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # x not present (yet) x = 2 print x innerFunc() print "x left as %s\n" % x def test2(): print "running test2..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # x not present innerFunc() print "x left as %s\n" % x def test3(): print "running test3..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # how is x in locals in this case?? print x innerFunc() print "x left as %s\n" % x test1() test2() test3() --- With the nested scope rules, I thought that *at the time of* trying to access an object with a given name, if the name was not available in the namespace of the local function python would then check the namespace of the parent function. My tests above don't seem to match this. Specifically my "at the time of" assumption. What is happening in test3? How is it that "x" ends up in the local namespace before it is ever referenced? It seems that, prior to execution time, the python compiler is "magically" determining that I'm referencing a particular name before assignment and shoving it into the local namespace so it can be used. I did not think the compiler went into function bodies, aside from basic syntax checking. Further confusing me (or confirming the compiler behavior) is adding a 4th test with only one added line... def test4(): print "running test4..." x = 1 def innerFunc(): print "inner locals():", print "%s" % locals() # how is x in locals in this case?? print x x = 2 #ONLY ADDED LINE TO TEST3 innerFunc() print "x left as %s\n" % x In this case I get "UnboundLocalError: local variable 'x' referenced before assignment". I think this means that the compiler (prior to runtime) inspected the code, determined I will do an assignment, x is local by virtue of being assigned to within the function. decided _not_ to bring the parent's x into the local namespace, and as a result caused the unbound name problem at runtime. It seems that the parent's "x" is brought directly into the local namespace (when appropriate), rather than the namespace lookup just moving up the hierarchy when not found. This is confusing to me and is making me question my understanding of namespace lookups. Are nested scopes a special case where the lookup is handled differently? What if I want to change the value of the parent's "x"? test4 implies that I can't. You can't ever unless x is mutable. Have I got this right? Can someone please clarify what is going on? I don't have time to go into further details at the moment. HTH, Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused about nested scopes and when names get added to namespaces
In article , Russell Warren wrote: > >def test4(): >print "running test4..." >x = 1 >def innerFunc(): >print "inner locals():", >print "%s" % locals() # how is x in locals in this case?? >print x >x = 2 #ONLY ADDED LINE TO TEST3 >innerFunc() >print "x left as %s\n" % x > >In this case I get "UnboundLocalError: local variable 'x' referenced >before assignment". I think this means that the compiler (prior to >runtime) inspected the code, determined I will do an assignment, >decided _not_ to bring the parent's x into the local namespace, and as >a result caused the unbound name problem at runtime. Bingo! >It seems that the parent's "x" is brought directly into the local >namespace (when appropriate), rather than the namespace lookup just >moving up the hierarchy when not found. This is confusing to me and >is making me question my understanding of namespace lookups. Are >nested scopes a special case where the lookup is handled differently? > >What if I want to change the value of the parent's "x"? test4 implies >that I can't. You need Python 3.x and the "nonlocal" keyword. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ [on old computer technologies and programmers] "Fancy tail fins on a brand new '59 Cadillac didn't mean throwing out a whole generation of mechanics who started with model As." --Andrew Dalke -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Grant Edwards writes: > I find it much easier to screw things up using the > "exceptional return value" method than the "exception raise" method. That may be partly due to Python's not-so-good facilities for implementing the "exceptional return value" method. To be fair, plenty of other languages have similar shortcomings and it's mostly in more recent languages that the problem has really been recognized, and addressed with features like option types. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
| HEREow can you be learning so much python if you're constantly expressing typo there. I'm not sure how that happens, sometimes, but it's an untimely abbrev-expansion, in emacs VM. I meant to say, "How can you..." Cheers, Bar -- http://mail.python.org/mailman/listinfo/python-list
Re: cPickle segfault with nested dicts in threaded env
Carl Banks writes: > Since Python 2.7 is released, Python 2.5 is no longer accepting bug > fixes, only security fixes. So be aware. Segfaults should be treated as security holes unless there's convincing reasons that no exploit is possible. So the bug should be reported against 2.5 as well as later versions. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
Instead of rewriting your code you might consider wrapping it with the C-API. I prefer this approach (over ctypes) for anything "low level". http://docs.python.org/c-api/ On 09/06/2010 10:06 PM, Kwan Lai Cheng wrote: Hi, I'm trying to rewrite a c program in python & encountered several problems. I have some data structures in my c program like below: typedef struct { unsigned short size; unsigned short reserved:8; unsigned short var_a1:2; unsigned short var_a2:2; unsigned short var_a3:2; unsigned short var_a4:2; unsigned int var_a5; }structa; typedef struct { unsigned short size; unsigned char reserved:4; unsigned char var_b1:1; unsigned char var_b2:1; unsigned char var_b3:1; unsigned char var_b4:1; structa var_structa; }structb; I tried to code the above in python but only got this far: class StructA(object): def __init__(self, size=0) self.size = size class StructB(object): def __init__(self, size=0) Any equivalent for c data structures & bit fields in python? And how do I define var_structa (in structb) in python? Regards, Kwan. !SIG:4c85bc4844361237431282! -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 08/09/2010 23:56, Bar Shirtcliff wrote: | HEREow can you be learning so much python if you're constantly expressing typo there. I'm not sure how that happens, sometimes, but it's an untimely abbrev-expansion, in emacs VM. I meant to say, "How can you..." An unkind soul would say that "it's a poor workman who blames his tools", but as it's emacs... ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Class changes in circular imports when __name__ == '__main__'
All right, thank you for helping! I'd had a little voice in the back of my mind nagging me that it might not be logical to include a bunch of classes and function definitions in my startup file, but I never got around to splitting it up. The module/script distinction makes sense, and it seems more elegant, too. Also, my program works now that I've rearranged things, which is a plus. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Arguments from the command line
In message , Hugo Arts wrote: > sys.argv is a list of all arguments from the command line ... Interesting that Python didn’t bother to mimic the underlying POSIX convention of passing the command line as arguments to the mainline routine. I always felt it was more useful to have command arguments directly accessible as globals, rather than having to pass them from the mainline. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
In message <7xeid9gtuq@ruckus.brouhaha.com>, Paul Rubin wrote: > Lawrence D'Oliveiro writes: > >> That reinforces my point, about how easy it was to check the correctness >> of the code. In this case one simple fix, like this ... >> would render the code watertight. See how easy it is? > > Well, no, it's irrelevant how easy it is to fix the issue after it's > pointed out. In that case, I can similarly discount your attempts to fix up issues with garbage collectors after they’re pointed out, can I not? > Part of the problem is C itself. And yet, what are these complicated garbage collectors, that you intend relying on to work correctly with all their layers of tricks upon tricks, written in? C. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
Lawrence D'Oliveiro writes: > In that case, I can similarly discount your attempts to fix up issues with > garbage collectors after they’re pointed out, can I not? Adapting GC algorithms to improve their performance or better use the capabilities of new hardware is much different than saying they didn't work in the first place. They've been around for 5 decades, they (like Python programs) work fine if you don't mind the performance cost, and for many applications that cost is acceptable even for quite simple and naive GC's. The continued work is to get that cost down even further. (And before criticizing GC performance, Have you ever profiled CPython to see how much time it's spending messing with reference counts? I didn't think so). >> Part of the problem is C itself. > And yet, what are these complicated garbage collectors, that you intend > relying on to work correctly with all their layers of tricks upon tricks, > written in? C. What tricks on tricks? Even the fanciest GC's are orders of magnitude less complicated than any serious database, optimizing compiler, OS kernel, file system, etc. Real-world software is complicated. Get used to that fact, and look for ways to manage the complexity and keep complex programs safe. Choosing to program with unsafe methods because you wish the complexity didn't exist is just deluded. It actually seems pretty crazy to me to write a garbage collector in C today even though it a GC needs unsafe pointer operations in a few places. C made some sense in the 1980's when computers were smaller and people didn't know better. A lot of the C code around today is legacy code from that era. These days it makes more sense to use a safe language with a few isolated "jailbreaks" (like an OS kernel that has a few lines of assembly code) than to write the whole thing in a language whose unsafety is everywhere. Here's a good paper by R. Fateman (criticizing C) that I just came across: http://www.franz.com/resources/educational_resources/white_papers/fault.prevention.pdf He suggests using Lisp instead, but one can't have everything ;-). FWIW, here are a couple pages about verifying GC's: http://flint.cs.yale.edu/flint/publications/hgc.html http://www.cs.technion.ac.il/~erez/Papers/verified-gc-popl09.pdf etc. I just don't understand that weird agenda you seem to have. But whatever. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba writes: > Thanks for your feedback. My question is: Who owns this forum? If we > all do then we are allowed to post questions that are simple and that > could otherwise be answered by doing research. That's a rather subservient perspective. Why are you seeking permission to ask questions? Why do you raise the issue of whether one is *allowed* to ask questions? If you take advice and requests as commandments, then I think that's pretty close to the root of the communication failures here. When someone asks you to change your behaviour, or describes the possible consequences that your behaviour entails, they're treating you as a responsible adult, not as a slave or infant. > It is just unfriendly to tell someone to go and look it up by > themselves. Yes, to *tell* them to do it would be unfriendly. You, however, received *requests* that you do so, with reasons why. Please help us all — and you are included in “us all” — by learning the difference between a command and a request. -- \ “It is difficult to get a man to understand something when his | `\ salary depends upon his not understanding it.” —Upton Sinclair, | _o__) 1935 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Grant Edwards writes: > I'm sure you'd prefer that everything was handed to you for free on a > silver platter with a side order of beer and cookies. I'd prefer I was > 20 years younger and 30 pounds lighter. Life's tough that way. Hell no. I'd prefer to have the total of my life experience and just have my *body* be twenty years younger :-) -- \“The greatest tragedy in mankind's entire history may be the | `\ hijacking of morality by religion.” —Arthur C. Clarke, 1991 | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 2010-09-09, Ben Finney wrote: > Grant Edwards writes: > >> I'm sure you'd prefer that everything was handed to you for free on a >> silver platter with a side order of beer and cookies. I'd prefer I was >> 20 years younger and 30 pounds lighter. Life's tough that way. > > Hell no. I'd prefer to have the total of my life experience and just > have my *body* be twenty years younger :-) I'd have to think about that for a while. A little bit of innocent optimism might be nice... -- Grant -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Arguments from the command line
On 9/8/10 7:38 PM, Lawrence D'Oliveiro wrote: In message, Hugo Arts wrote: sys.argv is a list of all arguments from the command line ... Interesting that Python didn’t bother to mimic the underlying POSIX convention of passing the command line as arguments to the mainline routine. There *is* no mainline routine in Python. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: cPickle segfault with nested dicts in threaded env
In article , Thomas Jollans wrote: > >Also, Python 2.5 is frightfully old. [...] "Frightfully"??? I'm sure plenty of people are still using Python 2.3 in production environments (certainly my last job did as of 1.5 years ago, and I would be mildly surprised if they upgraded by now). -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ [on old computer technologies and programmers] "Fancy tail fins on a brand new '59 Cadillac didn't mean throwing out a whole generation of mechanics who started with model As." --Andrew Dalke -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
On Thu, 09 Sep 2010 12:41:20 +1200, Lawrence D'Oliveiro wrote: >> Part of the problem is C itself. > > And yet, what are these complicated garbage collectors, that you intend > relying on to work correctly with all their layers of tricks upon > tricks, written in? C. Not necessarily. Pascal, despite the contempt it is held in by university computer science departments, isn't quite dead, and some Pascal compilers use garbage collectors written in Pascal. FreePascal, I believe, is one of them. Likewise for other not-dead-yet low-level languages like Ada and Forth. As surprising as it seems to many, C is not the only low-level language around suitable for writing high-quality, efficient code. Just ask the Lisp community, which is thriving. For some definition of thriving. Admittedly C has far more attention to it than the others, so [insert weasel words here] the best C compilers tend to produce more efficient code than the best of the others, but Pascal, Ada and similar give you more security than C. I believe that when computer scientists of the future look back at the last few decades, they will judge that on balance C did more harm than good. Not that C is the only language that people can write buggy or insecure code, but C does tend to give the bugs so much help... :) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Arguments from the command line
On Thu, 09 Sep 2010 12:38:04 +1200, Lawrence D'Oliveiro wrote: > In message , Hugo > Arts wrote: > >> sys.argv is a list of all arguments from the command line ... > > Interesting that Python didn’t bother to mimic the underlying POSIX > convention of passing the command line as arguments to the mainline > routine. What mainline routine? > I always felt it was more useful to have command arguments directly > accessible as globals, rather than having to pass them from the > mainline. http://c2.com/cgi/wiki?GlobalVariablesAreBad That's why we have namespaces. If you need the command line arguments, you just import sys and you have access to them. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Arguments from the command line
In message , Dennis Lee Bieber wrote: > None of them have command line arguments "passed" in to some > starting point -- all had to use some runtime library function to ask > for the command line contents. It’s always a language-specific routine, since at the underlying POSIX level (exposed by C), the command line is passed as arguments to the mainline routine. -- http://mail.python.org/mailman/listinfo/python-list