Sphinx: Co-Maintainer(s) wanted
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Dear Sphinx users, dear Python community, unfortunately, it has turned out that at the moment I don't have enough spare time to fulfill my duties as Python core developer and release manager, as well as fully maintain my other open-source projects. Therefore, I'm looking for co-maintainers for Sphinx (http://sphinx.pocoo.org; http://bitbucket.org/birkenfeld/sphinx/). So if you want to be involved in the maintenance and evolution of *the* Python documentation tool, please let me know; either in the sphinx-dev group (sphinx-...@googlegroups.com) or personally by email (ge...@python.org). Ideally, we can get a group of several people with push privileges going; of course I will try to be as much help as possible. Thanks, Georg -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.19 (GNU/Linux) iEYEARECAAYFAlCNeD0ACgkQN9GcIYhpnLC9QgCgsJnzpZl/+yX3iXuOD7ofjuia pSoAn0S/Zzi81vljPhGYYIBSn7fqL61e =AsLj -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: Numpy module
On Sun, Oct 28, 2012 at 10:40 PM, farrellpolym...@gmail.com wrote: I've learned a lot about Ubuntu just trying to install numpy for Python 3.2.3. I've finally managed to put it in the Python3.2 directory but when I try to import it, I still get there's no module named numpy. There are other modules in the same directory, like 'email' and it imports fine. A. It properly belongs under site-packages B. You ought to just install it using pip (http://www.pip-installer.org ) or apt-get, rather than manually. Does Numpy 1.6.2 not run with Python 3.2.3? They are compatible. http://scipy.github.com/faq.html#do-numpy-and-scipy-support-python-3-x : The first release of NumPy to support Python 3 was NumPy 1.5.0. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: [Gimp-user] export to non xcf
On 10/28/2012 09:09 PM, Michael Schumacher wrote: Von: Donald Miller damill...@gmail.com Can't directly save to jpg, so exported. Export to jpg made png. Same for psd. Shouldn't name track chosen format, so no manual override needed? Maybe you had set the file-type chooser to this format? The default By extension should do what you want, any other value is for special cases like you've discovered, like ambiguous file name extensions. Regards, Michael ___ gimp-user-list mailing list gimp-user-l...@gnome.org https://mail.gnome.org/mailman/listinfo/gimp-user-list I am contending with a similar malfunction: I can export by extension, but when done Gimp closes. The exported file exists, but I need to restart Gimp after every export. No loss of capability, but annoying. Judging by the flood of Gimp-related posts of late and the great variety of the issues raised, there seem to be major stability or environmental compatibility problems. I am running Gimp 2.6.12 on Ubunty 12.04. I found a Gimp user list (https://mail.gnome.org/mailman/listinfo/gimp-user-list) and intend to check it out the moment I get around to it. Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Sunday, October 28, 2012 9:26:01 PM UTC-7, Ian wrote: On Sun, Oct 28, 2012 at 10:00 PM, Andrew wrote: Hi Ian, Well, no it really isn't equivalent. Consider a programmer who writes: xrange(-4,3) *wants* [-4,-3,-2,-1,0,1,2] That is the idea of a range; for what reason would anyone *EVER* want -4 to +3 to be 6:3??? That is what ranges do, but your question was about slices, not ranges. Actually, I said in the OP: I also don't understand why slice() is not equivalent to an iterator, but can replace an integer in __getitem__() whereas xrange() can't. = Thank you for the code snippet; I don't think it likely that existing programs depend on nor use a negative index and a positive index expecting to take a small chunk in the center... hence, I would return the whole array; Or if someone said [-len(listX) : len(listX)+1 ] I would return the whole array twice. That's the maximum that is possible. If someone could show me a normal/reasonable script which *would* expect the other behavior, I'd like to know; compatibility is important. = My intended inferences about the iterator vs. slice question was perhaps not obvious to you; Notice: an iterator is not *allowed* in __getitem__(). The slice class when passed to __getitem__() was created to merely pass two numbers and a stride to __getitem__; As far as I know slice() itself does *nothing* in the actual processing of the elements. So, it's *redundant* functionality, and far worse, it's restrictive. The philosophy of Python is to have exactly one way to do something when possible; so, why create a stand alone class that does nothing an existing class could already do, and do it better ? A simple list of three values would be just as efficient as slice()! xrange is more flexible, and can be just as efficient. So, Have I misunderstood the operation of slice()? I think I might have... but I don't know. In 'C', where Python is written, circularly linked lists -- and arrays are both very efficient ways of accessing data. Arrays can, in fact, have negative indexes -- perhaps contrary to what you thought. One merely defines a variable to act as the base pointer to the array and initialize it to the *end* of the array. Nor is the size of the data elements an issue, since in Python all classes are accessed by pointers which are of uniform size. I routinely do this in C. Consider, also, that xrange() does not actually create a list -- but merely an iterator generating integers which is exactly what __getitem__ works on. So, xrange() does not need to incur a memory or noticeable time penalty. From micro-python, it's clear that their implementation of xrange() is at the 'C' level; which is extremely fast. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote: Andrew writes: So: Why does python choose to convert them to positive indexes, and have slice operate differently than xrange There was a thread a few years back, I think started by Bryan Olson, that made the case that slice indexing is a Python wart for further reasons than the above, and suggesting a notation like x[$-5] to denote what we now call x[-5] (i.e. $ is the length of the string). So your example x[$-4:3] would clearly be the same thing as x[6:3] and not give any suggestion that it might wrap around. I'm getting very frustrated with the editor provided for this group... It keeps posting prematurely, and putting my email in even when I tell it not to each time; and there is no way to edit a post... but deleting is ok... I think Olson makes a good point. The len() operator is so ubiquitous that it would be very useful to have a shorthand like that. I'll have to look for his thread. I'm thinking that I might just patch my version of Python 3.x, in C, to allow iterators to be passed to __getitem__; I haven't ever seen someone wanting to use mixed sign indexes to extract a small chunk of an array in the middle; so I don't think my patch will break existing code. The snippets of code given by other posters in the thread might also be used to make a compatibility wrapper; I'll have to study it closer; so that distributed code would still work on unpatched python, albeit much slower. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 12:54 AM, Andrew andrewr3m...@gmail.com wrote: On Sunday, October 28, 2012 9:26:01 PM UTC-7, Ian wrote: On Sun, Oct 28, 2012 at 10:00 PM, Andrew wrote: snip The slice class when passed to __getitem__() was created to merely pass two numbers and a stride to __getitem__; As far as I know slice() itself does *nothing* in the actual processing of the elements. So, it's *redundant* functionality, and far worse, it's restrictive. The philosophy of Python is to have exactly one way to do something when possible; so, why create a stand alone class that does nothing an existing class could already do, and do it better ? A simple list of three values would be just as efficient as slice()! xrange is more flexible, and can be just as efficient. So, Have I misunderstood the operation of slice()? I think I might have... but I don't know. `slice` is intentionally lenient about the types of the start, stop, and step: class Foo: ... def __getitem__(self, slice_): ... print(slice_) ... return 42 ... Foo()[a:b:c] slice('a', 'b', 'c') 42 Thus, the thing being sliced is free to interpret the parts of the slice however it wishes; hence, slice() is unable to contain the processing you speak of. By contrast, xrange() limits itself to integers. To support the more general case, the slice syntax thus produces a `slice` rather than an `xrange`. Doubtlessly, there are also historical issues involved. As implied by the ugliness of its name, `xrange` was added to the language relatively later. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote: On Oct 29, 2:09 pm, Andrew andrewr3m...@gmail.com wrote: I use this arbitrary range code *often* so I need a general purpose solution. I looked up slice() but the help is of no use, I don't even know how I might overload it to embed some logic to concatenate ranges of data; nor even if it is possible. Slices are passed in if provided to __getitem__/__setitem__/ __delitem__, so you'd need to override it at the list level: class RangedSlicer(list): def __getitem__(self, item): # map item.start, .stop and .step to your own semantics Then wrap your lists with your RangedSlicer class as needed. Hmmm... I began a test in an interactive shell: class RangedSlicer(list): ... def __getitem__(self,item): ... print item ... a=[1,2,3,4,5] a.__getitem__( slice(1,5) ) [2, 3, 4, 5] Very odd... I would have expected [1,2,3,4] a.__getitem__( slice(1,8) ) [2, 3, 4, 5] So, slice() somehow was truncated although it ought to have been executed first, and passed to __getitem__() before __getitem__ could affect it. That requires some tricky programming! Not only that, but, a.__getitem__( xrange[1,8] ) Causes an exception before the __getitem__ shadowing received it. I don't see how I can over-ride it with your suggestion, but that's very inconsistent for your idea seems to be normal python that would work for user defined classes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 1:08 AM, andrewr3m...@gmail.com wrote: On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote: Andrew writes: snip I'm getting very frustrated with the editor provided for this group... It keeps posting prematurely, and putting my email in even when I tell it not to each time; and there is no way to edit a post... but deleting is ok... This is a Usenet newsgroup[1], not a web forum. There are noteworthy differences between the two. FWICT, you happen to be accessing us via Google Groups, which is widely acknowledged to suck. We are not hosted *by* Google Groups; they just happen to carry our posts. Personally, I'd suggest using our mailing list mirror instead: http://mail.python.org/mailman/listinfo/python-list Or use some other, better newsgroup provider that also carries us. [1]: http://en.wikipedia.org/wiki/Usenet Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 1:24 AM, andrewr3m...@gmail.com wrote: On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote: On Oct 29, 2:09 pm, Andrew andrewr3m...@gmail.com wrote: snip class RangedSlicer(list): snip Then wrap your lists with your RangedSlicer class as needed. Hmmm... I began a test in an interactive shell: class RangedSlicer(list): ... def __getitem__(self,item): ... print item … This just defines a class; it doesn't modify in-place the normal behavior of plain lists. You have to actually *use* the class. a=[1,2,3,4,5] You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer! You wanted: a = RangedSlicer([1,2,3,4,5]) a.__getitem__( slice(1,5) ) [2, 3, 4, 5] Very odd... I would have expected [1,2,3,4] [2, 3, 4, 5] is the return value from `a.__getitem__( slice(1,5) )` (or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of print item; that line of code is never executed since you never used the RangedSlicer class at all. Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Monday, October 29, 2012 1:38:04 AM UTC-7, Chris Rebert wrote: On Mon, Oct 29, 2012 at 1:24 AM, On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote: On Oct 29, 2:09 pm, Andrew wrote: You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer! You wanted: a = RangedSlicer([1,2,3,4,5]) a.__getitem__( slice(1,5) ) [2, 3, 4, 5] Very odd... I would have expected [1,2,3,4] [2, 3, 4, 5] is the return value from `a.__getitem__( slice(1,5) )` (or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of print item; that line of code is never executed since you never used the RangedSlicer class at all. Regards, Chris My apology --- I deleted that post; yet it didn't delete... I saw my mistake seconds after posting. * gmail. Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread... A brief suggestion, or link to a howto would be *much* appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 29/10/2012 08:59, andrewr3m...@gmail.com wrote: Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread... A brief suggestion, or link to a howto would be *much* appreciated. Get yourself a decent email client. I read all the Python lists that I'm interested in using Thunderbird on Windows via gmane. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
Ok, hopefully this is better. I love my own e-mail editor... I can see that the slice() function can pass in arbitrary arguments. I'm not sure for lists, which is what the range is applied to, why an argument like a would be part of a slice. I *really* don't see what the advantage of a slice class is over a mere list in the order of start, stop, step eg: [ 1,4,9 ] In a dictionary, where a could be a key -- I wasn't aware that there was a defined order that the idea of slice could apply to. When I look at the documentation, http://www.python.org/doc//current/c-api/slice The only thing that slice has which is special, is that the the length of the sequence can be given -- and the start and stop index are either trimmed or an error (exception???) is thrown. Where is the information on the more general case of slice()? :-\ I am thinking, can one use the 'super' type of access, to override -- within the list object itself -- the __getitem__ method, and after pre-processing -- call the shadowed method with the modified parameters? That would allow me to use the normal a[-4:6] notation, without having to write a wrapper class that must be explicitly called. I'm thinking something like, PyListObject.__getitem__= lambda self, slice: --Andrew. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 29/10/2012 02:31, Andrew Robinson wrote: Ok, hopefully this is better. I love my own e-mail editor... I can see that the slice() function can pass in arbitrary arguments. I'm not sure for lists, which is what the range is applied to, why an argument like a would be part of a slice. I *really* don't see what the advantage of a slice class is over a mere list in the order of start, stop, step eg: [ 1,4,9 ] In a dictionary, where a could be a key -- I wasn't aware that there was a defined order that the idea of slice could apply to. When I look at the documentation, http://www.python.org/doc//current/c-api/slice The only thing that slice has which is special, is that the the length of the sequence can be given -- and the start and stop index are either trimmed or an error (exception???) is thrown. Where is the information on the more general case of slice()? :-\ I am thinking, can one use the 'super' type of access, to override -- within the list object itself -- the __getitem__ method, and after pre-processing -- call the shadowed method with the modified parameters? That would allow me to use the normal a[-4:6] notation, without having to write a wrapper class that must be explicitly called. I'm thinking something like, PyListObject.__getitem__= lambda self, slice: --Andrew. I suggest that you go back and read the tutorial about slicing. I say this because we've started with negative array indicies and slice() (but Python arrays haven't been mentioned :), then moved onto (x)range and now lists, dictionaries and the C API for slices. An alternative is to tell us precisely what you're trying to achieve. The odds are that there's a simple answer waiting in the wings for a simple question. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, 29 Oct 2012 01:59:06 -0700, andrewr3mail wrote: Note: I subscribed to the python-list, and am able to recieve e-mails, but I don't see how to write a post for this particular thread nor subscribe to this particular thread... The beauty of email is that you don't have to subscribe to a thread. Once you subscribe to the mailing list, email is delivered into your inbox. To reply to it, just reply to it. To ignore it, throw it in the trash. Gmail should have a button or three that say Reply to email or similar. You want the button that says Reply to All or Reply to List. Make sure that the reply includes python-list@python.org as a recipient. Delete bits of the quoted email (the lines that start with characters) that are no longer relevant to the conversation. Type your reply. Double check that the reply is going to python-list. Then hit Send. (P.S. when you signed up for python-list@python.org, if you selected the option to receive a single daily digest instead of individual emails, you're going to have a bad time. Do yourself a favour -- and the rest of us -- and change back to individual emails.) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding an Object Oriented Program example
Am 29.10.2012 00:30, schrieb goldtech: class Contact: all_contacts = [] def __init__(self, name, email): self.name = name self.email = email Contact.all_contacts.append(self) Okay, a class that automatically registers all instances in a central list. OK, no I do this: c = Contact('aaa','bbb') c = Contact('ccc','ddd') c = Contact('eee','fff') for i in Contact.all_contacts: print i.name + ' ' + i.email aaa bbb ccc ddd eee fff c.name 'eee' So wouldn't be good to add a check that the var (in this case c) does not point to any object before creating an object to keep the list correct? Since you don't use c, there is no use storing it at all! Note that you don't have to store a reference to an object that you created, just calling Contact('fou', 'barre') without assigning to anything is fine. Note that I don't find this example good, in reality I would prefer a factory method (e.g. called register(name, email)) that makes clear that you are not simply creating an instance. Also, concerning OOP, classes in Python are objects, too. Therefore, this could be decorated with @classmethod to allow the use with derived classes. However, I think that's going a bit too far at the moment. Just wanted to mention that there are more features waiting for you to discover. Also all_contacts is a class variable. I think the author is hinting that this would be a good idea for a contact list, But I don't fully see the usage of it. How would each object use a class variable like this? What would be the dot notation? How would an object use a method defined in the class? The point is that when calling fou.barre(42), the expression fou.barre is evaluated first and then used in a call expression with the parameter 42. Note that you can even evaluate that expression without calling the resulting function, but instead assign its result to a variable. In order to evaluate that expression, Python first looks for an attribute barre in the instance and returns that if found. If the instance doesn't have it, it looks in the class via the instances __class__ attribute. At that point, a little case-specific magic happens. If it finds a normal function without @classmethod or @staticmethod decorator, it returns this function with the first parameter (customary called self) bound to the instance. If it finds a non-function, that object is returned as-is instead. To sum up, you can use Contact.all_contacts or e.g. c.all_contacts to refer to the list of contacts. The second syntax also includes self.all_contacts when inside a memberfunction, after all the self is nothing magic or special. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, 29 Oct 2012 00:54:29 -0700, Andrew wrote: Actually, I said in the OP: I also don't understand why slice() is not equivalent to an iterator, but can replace an integer in __getitem__() whereas xrange() can't. Slices and iterators have different purposes and therefore have not been made interchangeable. Yes, there are certain similarities between a slice and xrange, but there are also significant differences. Thank you for the code snippet; I don't think it likely that existing programs depend on nor use a negative index and a positive index expecting to take a small chunk in the center... On the contrary. That is the most straightforward and useful idea of slicing, to grab a contiguous slice of items. Why would you want to grab a slice from the end of the list, and a slice from the start of the list, and swap them around? Apart from simulating card shuffles and cuts, who does that? hence, I would return the whole array; Or if someone said [-len(listX) : len(listX)+1 ] I would return the whole array twice. Well, that's one possible interpretation, but it is not the one that Python uses. When you create your own language, you can choose whatever interpretation seems most useful to you too. That's the maximum that is possible. If someone could show me a normal/reasonable script which *would* expect the other behavior, I'd like to know; compatibility is important. I'm not entirely sure I understand what you are asking here. My intended inferences about the iterator vs. slice question was perhaps not obvious to you; Notice: an iterator is not *allowed* in __getitem__(). Actually, you can write __getitem__ for your own classes to accept anything you like. py class Test: ... def __getitem__(self, index): ... return index ... py t = Test() py t[Hello world] 'Hello world' py t[{'x': None}] {'x': None} The slice class when passed to __getitem__() was created to merely pass two numbers and a stride to __getitem__; As far as I know slice() itself does *nothing* in the actual processing of the elements. So, it's *redundant* functionality, and far worse, it's restrictive. You say that as if it were a bad thing. The philosophy of Python is to have exactly one way to do something when possible; so, why create a stand alone class that does nothing an existing class could already do, and do it better ? What existing class is that? It certainly isn't xrange. Because xrange represents a concrete sequence of numbers, all three of start, end and stride must be concrete, known, integers: py xrange(4, None, 2) Traceback (most recent call last): File stdin, line 1, in module TypeError: an integer is required Whereas slices can trivially include blanks that get filled in only when actually used: py hello world[aslice] 'owrd' py NOBODY expects the Spanish Inquisition![aslice] 'D xet h pns nusto!' So, no, xrange is no substitute for slices. Not even close. A simple list of three values would be just as efficient as slice()! On the contrary, a simple list of three values not only could not do everything a slice does, but it's over twice the size! py sys.getsizeof([1, 2, 3]) 44 py sys.getsizeof(slice(1, 2, 3)) 20 xrange is more flexible, and can be just as efficient. Less flexible, less efficient. [snip] In 'C', where Python is written, That's a popular misapprehension. Python is written in Java, or Lisp, or Haskell, or CLR (dot Net), or RPython, or Ocaml, or Parrot. Each of those languages have, or had, at least one Python implementation. Oh, there's also a version written in C, or so I have heard. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 10:19 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: In 'C', where Python is written, That's a popular misapprehension. Python is written in Java, or Lisp, or Haskell, or CLR (dot Net), or RPython, or Ocaml, or Parrot. Each of those languages have, or had, at least one Python implementation. Oh, there's also a version written in C, or so I have heard. And that's not including the human-brain implementation, perhaps the most important of all. Although the current port of Python to my brain isn't quite a complete implementation, lacking a few bits that I should probably get to at some point, but even so, it's as useful to me as firing up IDLE. I wonder if what the OP is looking for is not slicing, but something more akin to map. Start with a large object and an iterator that produces keys, and create an iterator/list of their corresponding values. Something like: a=[1,2,3,4,5,6,7,8,9,10] b=[a[i] for i in xrange(-4,3)] It's not strictly a slice operation, but it's a similar sort of thing, and it can do the wraparound quite happily. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Applying a paid third party ssl certificate
Hi all, I haven't quite figured out how to apply a paid ssl cert, say RapidSSL free SSL test from Python's recent sponsor sslmatrix.com and what to do with that to make Python happy. This good fellow suggests using the PEM format. I tried and failed. http://www.minnmyatsoe.com/category/python-2/ The self signed cert recepies found all work swell, but some browsers (webkit) gets very upset indeed. I want to use ajax requests from clients (e.g autocompletion, stats collection etc) and put that in a python program without hogging down the main apache stack, but without a proper ssl cert this doesn't work. Does anyone have any ideas what to do? Thanks, Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 10/29/2012 04:32 AM, Chris Angelico wrote: I wonder if what the OP is looking for is not slicing, but something more akin to map. Start with a large object and an iterator that produces keys, and create an iterator/list of their corresponding values. Something like: a=[1,2,3,4,5,6,7,8,9,10] b=[a[i] for i in xrange(-4,3)] It's not strictly a slice operation, but it's a similar sort of thing, and it can do the wraparound quite happily. ChrisA A list comprehension ? That does do what I am interested in, *very* much so. Quite a gem, Chris! :-\ I am curious as to how quickly it constructs the result compared to a slice operation. Eg: a[1:5] vs. [ a[i] for i in xrange[1:5] ] But, unless it were grossly slower -- so that if/then logic and slices were generally faster -- I will use it. Thanks. --Andrew. -- http://mail.python.org/mailman/listinfo/python-list
Re: attaching names to subexpressions
On 2012-10-28, Devin Jeanpierre jeanpierr...@gmail.com wrote: The 'canonical way' while True: line = complex_expression if not line: break do_something_with(line) avoids this problem, but I was never really convinced about the beauty / readbility of this construct. In my opinion I shouldn't be obliged to read any of the indented lines of the while statement on a first 'visual' pass through somebody elses code and still be able to see what the loop iterates through. Fine. Then write your code as: line = function(x, y, z) while line: do something with(line) line = function(x, y, z) We have a problem, and two solutions. Solution 1 has downside A, and solution 2 has downside B. If he complains about downside A, you say, well, use solution 2. If he complains about downside B, you say, well, use solution 1. What if he wants to avoid both downsides A and B? What solution does he use then? You abandon the while loop and compose a generator. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
I need help installing pypng in Python 3.3
Hello all, I am very new to python. I am currently porting a little project of mine from java to python and I need to be able to construct and write png images. I naturally turned myself toward pypng to accomplish this. I learned from the net that pypng 0.0.13 is supposed to work in Python 3.x when run through '2to3'. However, in my case, it apparently does not. I ran into various problems when trying to install it, some of them I could fix myself. First I attempted to install pip but I couldn't get Windows to recognize pip as an internal command as the prompt informs me. Then I tried to directly run setup.py install from the pypng folder. It didn't work as I got a syntax error. I noticed that in the beginning of the installation process, the following warning was displayed: conf[use_2to3 = true] unrecognized configuration option At that point I was already pretty pissed but I managed to make the warning disappear by updating (installing?) distribute. After that there are no more errors during the installation process and everything seems to work fine. (This seems still strange to me as 2to3 was present in Tools/Script out of the box) I use pydev in Eclipse and pydev reports several error in png.py anytime I want to import or use it. When trying to import from IDLE I get: except ValueError, e: ^ Syntax error, line 1863 that paricular error I was able to fix by changing it to except ValueError as e: but I don't understand why 2to3 didn't do it automatically. There are several more errors reported by pydev, here are a few: line 1368 : raise ValueError('Chunk %s too short for checksum.', tag) Undefined variable: tag line 2736 : rows = [map(numpy.uint16, range(0,0x1,0x))] Undefined variable from import: uint16 The second one made me suspect I needed to install numpy too. As there are no installers for numpy aimed toward Python3.3. I decided to install Python2.7 and retry the whole process. Unfortunately I didn't get much more success. I get the exact same errors in pydev when using the 2.7 version of the interpreter and I cannot install numpy either because when I run the .exe file aimed toward Python 2.7 i get the error message: Python version 2.7 required, which was not found in the registry I then have the option to manually enter the path to Python 2.7 but the textform is greyed out and I can't type in anything. At that point I decided to go look for help and Here I am... I am truly grateful to anyone who takes the time to help me in this matter. Thank you! -- http://mail.python.org/mailman/listinfo/python-list
OrderedDict / DIctComprehension
Hi, is there a way building an OrderedDict faster? Thanks in advance Christian @timeit def ordered(n=10): d = OrderedDict() for i in xrange(n): d['key'+str(i)] = i return d @timeit def comprehension(n=10): d = { 'key'+str(i):i for i in xrange(n) } return d ordered() comprehension() 'ordered' ((), {}) 0.724609 sec 'comprehension' ((), {}) 0.098318 sec -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 3:52 PM, Andrew Robinson andr...@r3dsolutions.com wrote: I am curious as to how quickly it constructs the result compared to a slice operation. Eg: a[1:5] vs. [ a[i] for i in xrange[1:5] ] For the most part, don't concern yourself with performance. Go with functionality and readability. In the trivial case shown here, the slice is WAY clearer, so it should definitely be the one used; in other cases, the slice might simply be insufficient, so you go with whatever achieves your goal. Performance will usually be good enough, even if there's a marginally faster way. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
thank you guys for the huge list of answers, In my setting I have to access some routers and firewall from a linux-client. I think I'll try Fabric. On 26.10.2012 06:20, Rodrick Brown wrote: On Oct 25, 2012, at 6:34 AM, Schneider j...@globe.de wrote: Hi Folkz, how can i create a SSH-Connection with python? I have to send some commands to the remote host and parse their answers. greatz Johannes Fabric is the way to go! -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
Am Donnerstag, 25. Oktober 2012 12:31:46 UTC+2 schrieb Schneider: Hi Folkz, how can i create a SSH-Connection with python? I have to send some commands to the remote host and parse their answers. greatz Johannes There is a module in chilkat. http://www.example-code.com/python/ssh_exec.asp Don't know if it is worth the money, never used it. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 10/29/2012 04:19 AM, Steven D'Aprano wrote: On Mon, 29 Oct 2012 00:54:29 -0700, Andrew wrote: Slices and iterators have different purposes and therefore have not been made interchangeable. Yes, there are certain similarities between a slice and xrange, but there are also significant differences. Aha, now were getting to the actual subject. [snip] In 'C', where Python is written, That's a popular misapprehension. Python is written in Java, or Lisp, or Haskell, or CLR (dot Net), or RPython, or Ocaml, or Parrot. Each of those languages have, or had, at least one Python implementation. Oh, there's also a version written in C, or so I have heard. :-P I didn't say it was only written in C, but in C where it is implemented. I will be porting Python 3.xx to a super low power embedded processor (MSP430), both space and speed are at a premium. Running Python on top of Java would be a *SERIOUS* mistake. .NET won't even run on this system. etc. Thank you for the code snippet; I don't think it likely that existing programs depend on nor use a negative index and a positive index expecting to take a small chunk in the center... On the contrary. That is the most straightforward and useful idea of slicing, to grab a contiguous slice of items. Show me an example where someone would write a slice with a negative and a positive index (both in the same slice); and have that slice grab a contiguous slice in the *middle* of the list with orientation of lower index to greater index. I have asked before; It's not that I don't think it possible -- it's that I can't imagine a common situation. Why would you want to grab a slice from the end of the list, and a slice from the start of the list, and swap them around? Apart from simulating card shuffles and cuts, who does that? Advanced statistics programmers using lookup tables that are symmetrical. Try Physicists too -- but they're notably weird. My intended inferences about the iterator vs. slice question was perhaps not obvious to you; Notice: an iterator is not *allowed* in __getitem__(). Actually, you can write __getitem__ for your own classes to accept anything you like. Yes, I realize that. But, why can't I just overload the existing __getitem__ for lists and not bother writing an entire class? Everything in Python is supposed to be an object and one of the big supposed selling points is the ability to overload any object's methods. The lists aren't special -- they're just a bunch of constant decimal numbers, typically given as a large tuple. py class Test: ... def __getitem__(self, index): ... return index ... Better: class Test: ... def __getitem__( self, *index ): ... return index No extra curlies required... You say that as if it were a bad thing. hmmm... and you as if sarcastic? :-) It is a bad thing to have any strictly un-necessary and non-code saving objects where memory is restricted. What existing class is that? It certainly isn't xrange. Because xrange represents a concrete sequence of numbers, all three of start, end and stride must be concrete, known, integers: Let's up the ante. I'll admit xrange() won't do later fill in the blank -- BUT -- xrange() is a subclass of an *existing* class called iterator. Iterators are very general. They can even be made random. The philosophy of Python is to have exactly one way to do something when possible; so, why create a stand alone class that does nothing an existing class could already do, and do it better ? py xrange(4, None, 2) Traceback (most recent call last): File stdin, line 1, inmodule TypeError: an integer is required Hmmm.. Let's try your example exactly as shown... hello world[aslice] Traceback (most recent call last): File stdin, line 1, in module NameError: name 'aslice' is not defined WOW. Cool. Where did the blanks *actually* get filled in? Or HOW WILL they in your next post? On the contrary, a simple list of three values not only could not do everything a slice does, but it's over twice the size! Yeah, There is a definite issue there. But the case isn't decided by that number alone. A slice is storing three integers -- and an integer is size is 12. So, slices don't use integers. If the type that *IS* used happens to be a real Python type, we may merely typecast integers to that type -- insert them in a tuple and by definition, they must be the same size. Looking at some of the online programming notes -- a slice apparently doesn't use an integer storage variable that is capable of arbitrary expansion. =-O -- and hence, won't work for very large sized lists. That actually explains some crashes I have noted in the past when working with 20 million element lists that I wanted a slice of. I had *plenty* of ram on that system. Besides: The program code to implement slice() is undoubtedly larger than 12 bytes of savings! How many slices() are typically found in memory simultaneously?
Re: SSH Connection with Python
In article mailman.2977.1351455364.27098.python-l...@python.org, Gelonida N gelon...@gmail.com wrote: The sh module looks intersting, but it's not supported for Windows platforms. The X module looks interesting but it's not supported for Windows is true for many values of X. It's all part of the TCO of using a brain-dead operating system. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
On 10/29/2012 05:23 AM, icgwh wrote: Hello all, I am very new to python. I am currently porting a little project of mine from java to python and I need to be able to construct and write png images. I naturally turned myself toward pypng to accomplish this. I don't know if this will help, but: There is a package called image magic; which can convert any image to any other type of image. If that would not be a burden to install (most OS's have pre-compiled working binaries) -- you could easily write a portable bitmap file to disk using python (even without a library) -- and then convert it to png. I have a little script I wrote in python to create a canvas, and allow you to draw on it using very simple line drawing primitives, and then save it to PBM. It's simple enough (only draws lines with different pens) that you could examine it and write your own script to do the same or better. If this interests you, I will see if I can post the py script; or email it to you. --Andrew. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
I probably should have mentioned that I'm under W7 ultimate x64, I'm using eclipse Juno (latest) and pydev 2.7.1 -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
That's very kind of you but I don't think it would be particularly fitted to my needs. The program I'm trying to code creates an image as an 2D array of pixels which is defined by RGBA value. My program needs to access and modifies every component of every pixels in the image following a set of rules, kind of like the game of life, only more complex. In fact I only need a library to push this array of pixels in a displayable format for the GUI and in PNG format to write the image to disk. I don't need to do any fancy stuff with the image, just being able to display and write it. -- http://mail.python.org/mailman/listinfo/python-list
Re: OT Questions
On Sun, Oct 28, 2012 at 10:15 AM, Joshua Landau joshua.landau...@gmail.com wrote: I feel necessity to argue against this point. It is a common thing to stereotype teens in this way - but, being teen myself, I feel one should try to avoid it. It's painful to watch every time someone claims he can't be a teenager - his spelling/grammar is too good or any derivation of it, as with the inverse concept that the uneducated are always teenagers. i have techers who type lke this, and I have teachers who type very professionally. I have peers of my age group who meticulously craft their online conversations, n i no BFFs who dun b like that. How someone speaks on the internet seems to me proportional almost entirely to their intelligence*, not to their wisdom. Sure, there is an age where one, no matter how bright, will speak like trash, but this normally coincides with an inability to speak well, too. Most older teenagers have as good a grasp on language as they will ever learn, so it hardly applies to them. The aggressive defensiveness had me thinking intelligent *younger* teenager, actually, possibly a tween. Mostly though the idea was based on what I had seen of his website, which reminded me of some of the ambitious efforts of myself and my peers when I was around that age myself. In any case, he's claimed to be older, so with no real evidence to the contrary, I will take his word for it. * Note that I speak not of IQ or problem-solving ability, but more of a general social intelligence that can be seen in almost any poster on this list. My social intelligence is pretty darn low in real life, but I like to think that I comport myself reasonably well on the net. -- http://mail.python.org/mailman/listinfo/python-list
Re: Where are documentation for Gnome
On Sunday 28 October 2012 16:45:12 GangGreene did opine: On Sun, 28 Oct 2012 16:29:07 +, Mark Lawrence wrote: On 13/10/2012 18:49, Santosh Kumar wrote: Try your local garden centre. I inquired at the local garden centre, Just got strange looks Are you sure that is the correct place? ;) I think I'll stick around and see where this goes... Cheers, Gene -- There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order. -Ed Howdershelt (Author) My web page: http://coyoteden.dyndns-free.com:85/gene is up! Be careful what you set your heart on -- for it will surely be yours. -- James Baldwin, Nobody Knows My Name -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 5:01 PM, Andrew Robinson andr...@r3dsolutions.com wrote: Looking at some of the online programming notes -- a slice apparently doesn't use an integer storage variable that is capable of arbitrary expansion. =-O -- and hence, won't work for very large sized lists. That actually explains some crashes I have noted in the past when working with 20 million element lists that I wanted a slice of. I had *plenty* of ram on that system. Can you provide links to these notes? I'm looking at cpython/Include/sliceobject.h that has this comment: /* A slice object containing start, stop, and step data members (the names are from range). After much talk with Guido, it was decided to let these be any arbitrary python type. Py_None stands for omitted values. */ Also, the code for slice objects in CPython works with Py_ssize_t (a signed quantity of the same length as size_t), which will allow at least 2**31 for an index. I would guess that your crashes were nothing to do with 20 million elements and slices. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
In article mailman.3009.1351516065.27098.python-l...@python.org, Andrew Robinson andr...@r3dsolutions.com wrote: Show me an example where someone would write a slice with a negative and a positive index (both in the same slice); and have that slice grab a contiguous slice in the *middle* of the list with orientation of lower index to greater index. It's possible in bioinformatics. Many organisms have circular chromosomes. It's a single DNA molecule spliced into a loop. There's an origin, but it's more a convenience thing for people to assign some particular base-pair to be location 0. From the organism's point of view, the origin isn't anything special (but there *is* a fixed orientation). It's entirely plausible for somebody to want to extract the sub-sequence from 100 bp (base-pairs) before the origin to 100 bp after the origin. If you were storing the sequence in Python string (or list), the most convenient way to express this would be seq[-100:100]. Likewise, if you wanted the *other* fragment, you would write seq[100:-100]. There is a minor confounding factor here in that biologists number sequences starting with 1, not 0. At least that was the way when I was doing this stuff mumble years ago. I don't know what the current convention is. -- http://mail.python.org/mailman/listinfo/python-list
[ANN] 2 new articles in The Python Papers
Hi all I'm pleased to announce 2 newly published articles in The Python Papers (ojs.pythonpapers.org). FUSE’ing Python for Development of Storage Efficient Filesystem (http://ojs.pythonpapers.org/index.php/tpp/article/view/244) Abstract: Filesystem is a core component of a functional operating system. Traditional Filesystem development has been confined to the kernel space. A customized, purpose-built, and user-driven Filesystem development involves extensive knowledge of kernel internals, tools and processes. Alternatively, user-space Filesystems are preferred over the kernel space Filesystem, for ease of development, portability and developing prototypes Filesystems, particularly for intuitive abstraction of “non-file” objects. This paper proposes usage of FUSE kernel module to develop a functional Filesystem in user-space, titled “seFS”. Apart from offering convenience of user-space development, FUSE allows on-par features and functionality of a kernel space Filesystem. We demonstrate development of a Filesystem in Python on Ubuntu 11.04 system with Python-Fuse bindings. seFS Filesystem abstracts a SQLite database to store files data and metadata. By developing a Filesystem with Python-FUSE, we quickly solved the problem of efficient data management with online de-duplication and data compression. We discuss the internals of FUSE, its operation and implementation in this paper. An Artificial Life Simulation Library Based on Genetic Algorithm, 3-Character Genetic Code and Biological Hierarchy (http://ojs.pythonpapers.org/index.php/tpp/article/view/245) Abstract: Genetic algorithm (GA) is inspired by biological evolution of genetic organisms by optimizing the genotypic combinations encoded within each individual with the help of evolutionary operators, suggesting that GA may be a suitable model for studying real-life evolutionary processes. This paper describes the design of a Python library for artificial life simulation, Digital Organism Simulation Environment (DOSE), based on GA and biological hierarchy starting from genetic sequence to population. A 3-character instruction set that does not take any operand is introduced as genetic code for digital organism. This mimics the 3-nucleotide codon structure in naturally occurring DNA. In addition, the context of a 3-dimensional world composing of ecological cells is introduced to simulate a physical ecosystem. Using DOSE, an experiment to examine the changes in genetic sequences with respect to mutation rates is presented. Maurice Ling Co-EIC, The Python Papers -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
On Mon, Oct 29, 2012 at 8:23 AM, ic...@tagyourself.com wrote: Hello all, I am very new to python. I am currently porting a little project of mine from java to python and I need to be able to construct and write png images. I naturally turned myself toward pypng to accomplish this. I learned from the net that pypng 0.0.13 is supposed to work in Python 3.x when run through '2to3'. However, in my case, it apparently does not. You haven't shown that. I ran into various problems when trying to install it, some of them I could fix myself. First I attempted to install pip but I couldn't get Windows to recognize pip as an internal command as the prompt informs me. No, it's not an internal command, but you don't need that. I'm assuming your python is installed under C:\Python33. If not, adjust as needed. Try: C:\Python33\Scripts\pip install pypng [You didn't say what you did to install pip so I don't know if pip.exe actually exists in C:\Python33\Scripts] Then I tried to directly run setup.py install from the pypng folder. It didn't work as I got a syntax error. I noticed that in the beginning of the installation process, the following warning was displayed: conf[use_2to3 = true] unrecognized configuration option The following works for me: C:\Python33\python setup.py install At that point I was already pretty pissed but I managed to make the warning disappear by updating (installing?) distribute. After that there are no more errors during the installation process and everything seems to work fine. (This seems still strange to me as 2to3 was present in Tools/Script out of the box) It's not clear what you did here. setup.py should run 2to3 for you. What command did you use to successfully? install it? I use pydev in Eclipse and pydev reports several error in png.py anytime I want to import or use it. When trying to import from IDLE I get: except ValueError, e: ^ Syntax error, line 1863 It's clear that the install process didn't run 2to3. that paricular error I was able to fix by changing it to except ValueError as e: but I don't understand why 2to3 didn't do it automatically. 2to3 does do it automatically. There are several more errors reported by pydev, here are a few: line 1368 : raise ValueError('Chunk %s too short for checksum.', tag) Undefined variable: tag Looking at the code, this appears to be a bug. line 2736 : rows = [map(numpy.uint16, range(0,0x1,0x))] Undefined variable from import: uint16 The second one made me suspect I needed to install numpy too. As there are no installers for numpy aimed toward Python3.3. A few lines above there's a try/except for importing numpy. It should skip this test if numpy is not available. I'm not sure how you were able to execute that line [unless you've got a stray numpy.py file somewhere somewhere] I decided to install Python2.7 and retry the whole process. Unfortunately I didn't get much more success. I get the exact same errors in pydev when using the 2.7 version of the interpreter and I cannot install numpy either because when I run the .exe file aimed toward Python 2.7 i get the error message: Python version 2.7 required, which was not found in the registry I then have the option to manually enter the path to Python 2.7 but the textform is greyed out and I can't type in anything. Do you have the file c:\Python33\Lib\site-packages\pypng-0.0.13-py3.3.egg ? If not, you have not successfully installed pypng. Please try one of the methods I gave above. -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
On Sun, Oct 28, 2012 at 4:09 PM, Gelonida N gelon...@gmail.com wrote: The only thing I'm concerned about paramiko is, that I don't see any activity on the paramiko site and that one library it depends on is not available is windows binary package for newer versions of python. I don't understand why this is a problem. \python27\python setup.py install #for pycrypto paramiko \python27\python test.py # for paramiko Works for me. Of course, you need Visual C++ 2008, but the free express edition is sufficient, and you should have that anyway if you're doing Windows development. If that's too hard for you, try http://www.serenethinking.com/bitlift/download.html [not my site, no guarantees] -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
- Original Message - I have a philosofical doubt about immutability, that arised while doing the SCALA functional programming course. Now suppose I have a simple NumWrapper class, that very stupidly does: class NumWrapper(object): def __init__(self, number): self.number = number and we want to change its state incrementing the number, normally I would do this def increment(self): self.number += 1 But the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Now on one hand I would love to use only immutable data in my code, but on the other hand I wonder if it makes so much sense in Python. My impression is that things get more clumsy in the immutable form, for example in the mutable form I would do simply this: number = NumWrapper(1) number.increment() while with immutability I have to do this instead: new_number = number.increment() But more importantly normally classes are way more complicated than my stupid example, so recreating a new object with the modified state might be quite complex. Any comments about this? What do you prefer and why? -- http://mail.python.org/mailman/listinfo/python-list return NumWrapper(self.number + 1) still returns a(nother) mutable object. So what's the point of all this ? JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
2012/10/29 Jean-Michel Pichavant jeanmic...@sequans.com: return NumWrapper(self.number + 1) still returns a(nother) mutable object. So what's the point of all this ? JM Well sure but it doesn't modify the first object, just creates a new one. There are in general good reasons to do that, for example I can then compose things nicely: num.increment().increment() or I can parallelize operations safely not caring about the order of operations. But while I do this all the time with more functional languages, I don't tend to do exactly the same in Python, because I have the impression that is not worth, but maybe I'm wrong.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
2012/10/29 andrea crotti andrea.crott...@gmail.com: Well sure but it doesn't modify the first object, just creates a new one. There are in general good reasons to do that, for example I can then compose things nicely: num.increment().increment() or I can parallelize operations safely not caring about the order of operations. But while I do this all the time with more functional languages, I don't tend to do exactly the same in Python, because I have the impression that is not worth, but maybe I'm wrong.. By the way on this topic there is a great talk by the creator of Clojure: http://www.infoq.com/presentations/Value-Values -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On 29/10/2012 15:20, andrea crotti wrote: I have a philosofical doubt about immutability, that arised while doing the SCALA functional programming course. Now suppose I have a simple NumWrapper class, that very stupidly does: class NumWrapper(object): def __init__(self, number): self.number = number and we want to change its state incrementing the number, normally I would do this def increment(self): self.number += 1 But the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Now on one hand I would love to use only immutable data in my code, but on the other hand I wonder if it makes so much sense in Python. My impression is that things get more clumsy in the immutable form, for example in the mutable form I would do simply this: number = NumWrapper(1) number.increment() while with immutability I have to do this instead: new_number = number.increment() But more importantly normally classes are way more complicated than my stupid example, so recreating a new object with the modified state might be quite complex. Any comments about this? What do you prefer and why? I prefer practicality beats purity. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
andrea crotti andrea.crott...@gmail.com writes: and we want to change its state incrementing the number ... the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Immutability purists would say that numbers don't have state and if you're trying to change a number's state by incrementing it, that's not immutability. You end up with a rather different programming style than imperative programming, for example using tail recursion (maybe wrapped in an itertools-like higher-order function) instead of indexed loops to iterate over a structure. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
- Original Message - 2012/10/29 Jean-Michel Pichavant jeanmic...@sequans.com: return NumWrapper(self.number + 1) still returns a(nother) mutable object. So what's the point of all this ? JM Well sure but it doesn't modify the first object, just creates a new one. There are in general good reasons to do that, for example I can then compose things nicely: num.increment().increment() or I can parallelize operations safely not caring about the order of operations. But while I do this all the time with more functional languages, I don't tend to do exactly the same in Python, because I have the impression that is not worth, but maybe I'm wrong.. In an OOP language num.increment() is expected to modify the object in place. So I think you're right when you say that functional languages technics do not necessarily apply to Python, because they don't. I would add that what you're trying to suggest in the first post was not really about immutability, immutable objects in python are ... well immutable, they can be used as a dict key for instance, your NumWrapper object cannot. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Tue, Oct 30, 2012 at 2:55 AM, Paul Rubin no.email@nospam.invalid wrote: andrea crotti andrea.crott...@gmail.com writes: and we want to change its state incrementing the number ... the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Immutability purists would say that numbers don't have state and if you're trying to change a number's state by incrementing it, that's not immutability. You end up with a rather different programming style than imperative programming, for example using tail recursion (maybe wrapped in an itertools-like higher-order function) instead of indexed loops to iterate over a structure. In that case, rename increment to next_integer and TYAOOYDAO. [1] You're not changing the state of this number, you're locating the number which has a particular relationship to this one (in the same way that GUI systems generally let you locate the next and previous siblings of any given object). ChrisA [1] there you are, out of your difficulty at once - cf WS Gilbert's Iolanthe -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
2012/10/29 Jean-Michel Pichavant jeanmic...@sequans.com: In an OOP language num.increment() is expected to modify the object in place. So I think you're right when you say that functional languages technics do not necessarily apply to Python, because they don't. I would add that what you're trying to suggest in the first post was not really about immutability, immutable objects in python are ... well immutable, they can be used as a dict key for instance, your NumWrapper object cannot. JM Yes right immutable was not the right word, I meant that as a contract with myself I'm never going to modify its state. Also because how doi I make an immutable object in pure Python? But the example with the dictionary is not correct though, because this: In [145]: class C(object): .: def __hash__(self): .: return 42 .: In [146]: d = {C(): 1} works perfectly, but an object of class C can mutate as much as it wants, as my NumWrapper instance.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
2012/10/29 Chris Angelico ros...@gmail.com: On Tue, Oct 30, 2012 at 2:55 AM, Paul Rubin no.email@nospam.invalid wrote: andrea crotti andrea.crott...@gmail.com writes: and we want to change its state incrementing the number ... the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Immutability purists would say that numbers don't have state and if you're trying to change a number's state by incrementing it, that's not immutability. You end up with a rather different programming style than imperative programming, for example using tail recursion (maybe wrapped in an itertools-like higher-order function) instead of indexed loops to iterate over a structure. In that case, rename increment to next_integer and TYAOOYDAO. [1] You're not changing the state of this number, you're locating the number which has a particular relationship to this one (in the same way that GUI systems generally let you locate the next and previous siblings of any given object). ChrisA [1] there you are, out of your difficulty at once - cf WS Gilbert's Iolanthe -- http://mail.python.org/mailman/listinfo/python-list Yes the name should be changed, but the point is that they are both ways to implement the same thing. For example suppose I want to have 10 objects (for some silly reason) that represent the next number, in the first case I would do: numbers = [NumWrapper(orig.number)] * 10 for num in numbers: num.increment() while in the second is as simple as: numbers = [orig.next_number()] * 10 composing things become much easier, but as a downside it's not always so easy and convienient to write code in this way, it probably depends on the use case.. -- http://mail.python.org/mailman/listinfo/python-list
Nice solution wanted: Hide internal interfaces
Hi there, I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). I'm pretty damn sure there's a nicer (prettier) solution out there, but I can't currently think of it. Do you have any hints? Best regards, Joe -- Wo hattest Du das Beben nochmal GENAU vorhergesagt? Zumindest nicht öffentlich! Ah, der neueste und bis heute genialste Streich unsere großen Kosmologen: Die Geheim-Vorhersage. - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
2012/10/29 Johannes Bauer dfnsonfsdu...@gmx.de: Hi there, I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). I'm pretty damn sure there's a nicer (prettier) solution out there, but I can't currently think of it. Do you have any hints? Best regards, Joe And how are you declaring methods private? Because there is no real private attribute in Python, if you declare them with a starting _ they are still perfectly accessible.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On Mon, Oct 29, 2012 at 9:33 AM, Johannes Bauer dfnsonfsdu...@gmx.de wrote: Hi there, I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). I'm pretty damn sure there's a nicer (prettier) solution out there, but I can't currently think of it. Do you have any hints? Best regards, Joe What do you mean declare the internal methods private? Python doesn't have this notion of restricted access. By convention, names starting with a leading underscore are private, but it's not enforced by the language. -- http://mail.python.org/mailman/listinfo/python-list
Re: Numpy module
On Sun, Oct 28, 2012 at 10:40 PM, farrellpolym...@gmail.com wrote: Hello to the group! I've learned a lot about Ubuntu just trying to install numpy for Python 3.2.3. I've finally managed to put it in the Python3.2 directory but when I try to import it, I still get there's no module named numpy. There are other modules in the same directory, like 'email' and it imports fine. Does Numpy 1.6.2 not run with Python 3.2.3? Can anybody help? Thank you in advance. Peter Farrell -- Numpy is written in C. You can't just drop the source code into a folder. It has to be compiled against that version of Python. You could do that yourself, or you could just run sudo apt-get install python3-numpy -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On Tue, Oct 30, 2012 at 3:33 AM, Johannes Bauer dfnsonfsdu...@gmx.de wrote: Hi there, I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. The usual convention for private methods is a leading underscore on the name: class A: def foo(self): print(Fooing!) def _bar(self): print(Only my friends may bar me.) It's only a convention, though; it doesn't make it hard to call them, it just sends the message this is private, I don't promise that it'll be stable across versions. Incidentally, you may want to use a nested class, if the definition of B is entirely dependent on A. Something like this: class A: class B: def _asdf(self,newval=None): if newval is not None: self._val=newval return self._val def _qwer(self,parent): parent._bar(My value is: +self._val) def foo(self): self.b=self.B() self.b._asdf(Hello, world!) self.b._qwer(self) def _bar(self,msg): print(Message from internal: +msg) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
andrea crotti andrea.crott...@gmail.com writes: Also because how doi I make an immutable object in pure Python? Numbers in Python are already immutable. What you're really looking for is a programming style where you don't bind any variable more than once. This gives rise to a programming style that Python can support to a certain extent, but for which some other languages are designed from the beginning. You might look at http://learnyouahaskell.com (online book) if you want to try the above approach. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On 2012-10-29, Johannes Bauer dfnsonfsdu...@gmx.de wrote: I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. The Python way of telling a user not to call certain methods is to prefix their names with an underscore. The double-underscore thing that munges the names is just telling that a bit louder -- but they're still free to ignore that advice. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). By decleare them privide do you mean using __ASDF__ name-munging? It sounds to me like you're just making life hard on yourself. I'm pretty damn sure there's a nicer (prettier) solution out there, but I can't currently think of it. Do you have any hints? IMO, the right thing to do in Python is to use single underscore names for methods that you intend to be called by friend modules (is that correct C++ lingo?) but don't intend to be called by the end-user. -- Grant Edwards grant.b.edwardsYow! I just had a NOSE at JOB!! gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On 29.10.2012 17:47, Chris Angelico wrote: The usual convention for private methods is a leading underscore on the name: Yup, that's what I'm using. It's only a convention, though; it doesn't make it hard to call them, it just sends the message this is private, I don't promise that it'll be stable across versions. Yes, I know. But it's good enough. I don't want to restrict the use under all circumstances, just make it clear to the user what she is supposed to use and what not. Incidentally, you may want to use a nested class, if the definition of B is entirely dependent on A. Something like this: Ah, that's nice. I didn't know that nested classes could access their private members naturally (i.e. without using any magic, just with plain old attribute access). This makes the source files largish however (they're currently split up in different files). Can I use the nested class advantage and somehow include the inner class from another file? Best regards, Joe -- Wo hattest Du das Beben nochmal GENAU vorhergesagt? Zumindest nicht öffentlich! Ah, der neueste und bis heute genialste Streich unsere großen Kosmologen: Die Geheim-Vorhersage. - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On 29.10.2012 17:52, Grant Edwards wrote: By decleare them privide do you mean using __ASDF__ name-munging? It sounds to me like you're just making life hard on yourself. Gaah, you are right. I just noticed that using the single underscore (as I do) does not restrict usage in any non-natural way. I was actually mixing this up in my head with the __xyz__ name-munging. Thank you very much for hitting my head with a brick. Much clearer now :-) Best regards, Johannes -- Wo hattest Du das Beben nochmal GENAU vorhergesagt? Zumindest nicht öffentlich! Ah, der neueste und bis heute genialste Streich unsere großen Kosmologen: Die Geheim-Vorhersage. - Karl Kaos über Rüdiger Thomas in dsa hidbv3$om2$1...@speranza.aioe.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
Johannes Bauer wrote: Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). I'm pretty damn sure there's a nicer (prettier) solution out there, but I can't currently think of it. Do you have any hints? Maybe you can wrap A into a class that delegates to A: class A(object): ... def __private(self): print private A ... class Friend(object): ... def __init__(self, obj): ... self.__obj = obj ... self.__prefix = _%s_ % obj.__class__.__name__ ... def __getattr__(self, name): ... return getattr(self.__obj, self.__prefix + name) ... a = A() a._A__private() # hard private A f = Friend(a) f._private() # easy private A The B instance would refer to A via the Friend instance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
Johannes Bauer dfnsonfsdu...@gmx.de writes: This makes the source files largish however (they're currently split up in different files). Can I use the nested class advantage and somehow include the inner class from another file? You could possibly duck-punch class A: import B class A: ... A.B = B.B Disclaimer: I haven't tested the above and I'd consider it pretty ugly. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
Johannes Bauer wrote: On 29.10.2012 17:52, Grant Edwards wrote: By decleare them privide do you mean using __ASDF__ name-munging? It sounds to me like you're just making life hard on yourself. Gaah, you are right. I just noticed that using the single underscore (as I do) does not restrict usage in any non-natural way. I was actually mixing this up in my head with the __xyz__ name-munging. Name-munging occurs only if the method name starts but doesn't end with __: class A(object): pass ... class B(A): ... __before = __between__ = __ = 42 ... set(dir(B)) - set(dir(A)) set(['__', '_B__before', '__between__']) -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Oct 29, 2012 7:10 AM, Andrew Robinson andr...@r3dsolutions.com wrote: I will be porting Python 3.xx to a super low power embedded processor (MSP430), both space and speed are at a premium. Running Python on top of Java would be a *SERIOUS* mistake. .NET won't even run on this system. etc. If that's the case, then running Python at all is probably a mistake. You know the interpreter alone has an overhead of nearly 6 MB? Yes, I realize that. But, why can't I just overload the existing __getitem__ for lists and not bother writing an entire class? You can just overload that one method in a subclass of list. Being able to monkey-patch __getitem__ for the list class itself would not be advisable, as it would affect all list slicing anywhere in your program and possibly lead to some unexpected behaviors. Hmmm.. Let's try your example exactly as shown... hello world[aslice] Traceback (most recent call last): File stdin, line 1, in module NameError: name 'aslice' is not defined WOW. Cool. Where did the blanks *actually* get filled in? Or HOW WILL they in your next post? It appears that Steven omitted the definition of aslice by mistake. It looks like it should have been: aslice = slice(4, None, 2) Looking at some of the online programming notes -- a slice apparently doesn't use an integer storage variable that is capable of arbitrary expansion. =-O -- and hence, won't work for very large sized lists. That actually explains some crashes I have noted in the past when working with 20 million element lists that I wanted a slice of. I had *plenty* of ram on that system. 20 million is nothing. On a 32-bit system, sys.maxsize == 2 ** 31 - 1. If the error you were seeing was MemoryError, then more likely you were running into dynamic allocation issues due to fragmentation of virtual memory. -- http://mail.python.org/mailman/listinfo/python-list
Re: OrderedDict / DIctComprehension
On 10/29/2012 8:36 AM, Christian wrote: Hi, is there a way building an OrderedDict faster? Thanks in advance Christian @timeit def ordered(n=10): d = OrderedDict() for i in xrange(n): d['key'+str(i)] = i return d try d = OrderedDict(['key'+str(i),i for i in xrange(n)]) In Py3, just range(n). Also d = OrderedDict(('key'+str(i)) for i in range(n)) may be faster or slower. @timeit def comprehension(n=10): d = { 'key'+str(i):i for i in xrange(n) } return d ordered() comprehension() 'ordered' ((), {}) 0.724609 sec 'comprehension' ((), {}) 0.098318 sec -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 1:54 AM, Andrew andrewr3m...@gmail.com wrote: My intended inferences about the iterator vs. slice question was perhaps not obvious to you; Notice: an iterator is not *allowed* in __getitem__(). Yes, I misconstrued your question. I thought you wanted to change the behavior of slicing to wrap around the end when start stop instead of returning an empty sequence. What you actually want is a new sequence built from indexes supplied by an iterable. Chris has already given you a list comprehension solution to solve that. You could also use map for this: new_seq = list(map(old_seq.__getitem__, iterable)) Since you seem to be concerned about performance, I'm not sure in this case whether the map or the list comprehension will be faster. I'll leave you to test that on your intended hardware. In 'C', where Python is written, circularly linked lists -- and arrays are both very efficient ways of accessing data. Arrays can, in fact, have negative indexes -- perhaps contrary to what you thought. One merely defines a variable to act as the base pointer to the array and initialize it to the *end* of the array. Nor is the size of the data elements an issue, since in Python all classes are accessed by pointers which are of uniform size. I routinely do this in C. I'm aware of what is possible in C with pointer arithmetic. This is Python, though, and Python by design has neither pointers nor pointer arithmetic. In any case, initializing the pointer to the end of the array would still not do what you want, since the positive indices would then extend past the end of the array. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On 10/29/2012 11:20 AM, andrea crotti wrote: I have a philosofical doubt about immutability, that arised while doing the SCALA functional programming course. In real life, the physical world, things have mutable state, at least down to the atomic level. Do you only want to model mathematical worlds, or also physical worlds. Even mathematically, I do not think there necessarily a problem with mutable collections. The fact that sets are defined by content does not mean that everything has to be. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Immutability and Python
On 10/29/2012 12:05 PM, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. The short answer is: you don't, not really, except by using NamedTuple if that gives you what you want. The longer answer: You can kinda get it somewhat if you define your own __getattribute__/__setattribute__ functions. __setattribute__ of course should never do anything except raise an error (one way or another you'll need to make an exception for your __init__ function of course). __getattribute__ should make sure no mutable references are returned: e.g. you'll probably want to make it so someone can't side-step your setter by saying someobject.__dict__[foo] = bar. (I return a copy of the dict.) It will still be possible to bypass these protections though. To really get true immutability in pure Python, you'll have to inherit from tuple or NamedTuple (which inherits from tuple, I think). You can see some discussion on Stack Overflow and some other places about this; having played around with this a bit, I think it's not worth the hassle and have done the __getattribute__/__setattribute__ thing the couple of times I wanted immutability. Evan signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On 10/29/2012 1:05 PM, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. If you mean class with immutable instances, mutate new instances in __new__ instead of __init__ and write a custom .__setattr__ that prevents changes thereafter. If you want the class itself to be immutable (after creation), write a custom metaclass. You may also need to think about .__getattribute__, but I never studied the detail completely and have forgotten what I learned. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On 2012-10-29, Johannes Bauer dfnsonfsdu...@gmx.de wrote: On 29.10.2012 17:47, Chris Angelico wrote: The usual convention for private methods is a leading underscore on the name: Yup, that's what I'm using. It's only a convention, though; it doesn't make it hard to call them, it just sends the message this is private, I don't promise that it'll be stable across versions. Yes, I know. But it's good enough. I don't want to restrict the use under all circumstances, just make it clear to the user what she is supposed to use and what not. The single underscore indicates that the user is not to use the method. -- Grant Edwards grant.b.edwardsYow! I am covered with at pure vegetable oil and I am gmail.comwriting a best seller! -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
Do you have the file c:\Python33\Lib\site-packages\pypng-0.0.13-py3.3.egg ? If not, you have not successfully installed pypng. Please try one of the methods I gave above. Yes I do have the egg. I'm gonna try to summarize: I don't have installations problems anymore but it seems that png.py is not run through 2to3 although it should since the setup.py is properly configured: if sys.version_info = (3,): conf['use_2to3'] = True I even tried to skip the if to ensure the second line is executed but nothing changed. I don't know what I'm doing wrong but it seems that png.py is not properly translated to Python 3. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, Oct 29, 2012 at 12:46 PM, Paul Rubin no.email@nospam.invalid wrote: andrea crotti andrea.crott...@gmail.com writes: Also because how doi I make an immutable object in pure Python? Numbers in Python are already immutable. What you're really looking for is a programming style where you don't bind any variable more than once. No, they were looking for a way to create classes whose instances are immutable. Also, immutability has nothing to do with the presence or lack of for loops. -- Devin -- http://mail.python.org/mailman/listinfo/python-list
Re: OrderedDict / DIctComprehension
Too bad that's not (using python2.7) 'ordered_dict_generator' ((), {}) 1.089588 sec Anyway thanks for your hint! Hi, is there a way building an OrderedDict faster? Thanks in advance Christian @timeit def ordered(n=10): d = OrderedDict() for i in xrange(n): d['key'+str(i)] = i return d @timeit def comprehension(n=10): d = { 'key'+str(i):i for i in xrange(n) } return d ordered() comprehension() 'ordered' ((), {}) 0.724609 sec 'comprehension' ((), {}) 0.098318 sec -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On Mon, Oct 29, 2012 at 10:58 AM, Johannes Bauer dfnsonfsdu...@gmx.de wrote: Ah, that's nice. I didn't know that nested classes could access their private members naturally (i.e. without using any magic, just with plain old attribute access). There is nothing at all special about nested classes that is different from non-nested classes. All it affects is organization; instead of classes A and B, you have classes A and A.B. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, Oct 29, 2012 at 10:12 AM, andrea crotti andrea.crott...@gmail.com wrote: Also because how doi I make an immutable object in pure Python? I sometimes use namedtuples for this. from collections import namedtuple MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') If you want default arguments then use a factory function. Or if you want the class to have methods, then subclass it: _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') class MyImmutableClass(_MyImmutableClass): def __new__(cls, field1, field2, field3=None, field4=42): return super().__new__(cls, field1, field2, field3, field4) def get_sum(self): return self.field1 + self.field2 Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly ian.g.ke...@gmail.com wrote: _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') class MyImmutableClass(_MyImmutableClass): Question: Is it clearer to take advantage of the fact that the base class can be an arbitrary expression? class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2 field3 field4')): You lose the unnecessary temporary and triplication of name, but gain instead a rather long line. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, Oct 29, 2012 at 1:36 PM, Chris Angelico ros...@gmail.com wrote: Question: Is it clearer to take advantage of the fact that the base class can be an arbitrary expression? class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2 field3 field4')): You lose the unnecessary temporary and triplication of name, but gain instead a rather long line. I think it's more readable if separated, but YMMV. -- http://mail.python.org/mailman/listinfo/python-list
Python Bug Day this Saturday in Montreal and on IRC
This Saturday, you have the opportunity of participating in the Python Bug Day. How would you like to be one of the contributors of Python? If you have ideas for improving parts of the official documentation, the standard library, the language itself, or if you have a patch waiting for a review that you would like to see committed, or if you just want to come and fix an existing bug, it's your day! Join us for an effort at closing some Python bugs and feature requests. Get quick feedback on your patches and bugfixes, learn how to submit and examine patches, and have fun chatting with the Python developers and other contributors. You don’t need to know the CPython codebase or process to join, just Python programming knowledge. If you live in Montreal, come at Caravan to meet fellow hackers and take part in a physical sprint! The address is 5334 de Gaspé; we'll be there from 10:00 to 18:00. Please register at http://mpbugday.eventbrite.ca/ to let us know how many people to expect. People from around the world are welcome to join the #python-dev IRC channel to participate in the bug day. This page contains all the information you need to get set up, see the list of bugs or learn about IRC: http://wiki.python.org/moin/PythonBugDay If you need any help beforehand, feel free to ask on http://mail.python.org/mailman/listinfo/core-mentorship Thanks to Caravan for sponsoring us! http://caravaravan.coop/ See you on Saturday! -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
On 10/29/2012 04:18 PM, David Robinow wrote: On Sun, Oct 28, 2012 at 4:09 PM, Gelonida N gelon...@gmail.com wrote: The only thing I'm concerned about paramiko is, that I don't see any activity on the paramiko site and that one library it depends on is not available is windows binary package for newer versions of python. I don't understand why this is a problem. \python27\python setup.py install #for pycrypto paramiko \python27\python test.py # for paramiko Works for me. Of course, you need Visual C++ 2008, but the free express edition is sufficient, and you should have that anyway if you're doing Windows development. If that's too hard for you, try http://www.serenethinking.com/bitlift/download.html [not my site, no guarantees] It's not a problem. It's an inconvenience. We're having multiple PCs. Many of the PC owners don't want to write any C-code and don't want to be bothered with registering at Microsoft just to install a module and using a library. Normally my preferred approach is, that somebody wanting to use any library, that I wrote) can install all dependencies by: - Installing Python - installing easy_install (and pip) and be able to install all the rest with easy_install Using a library, that forces users to have to install MS-VC or mingw or to install binaries from non-pypy sites is something I try to avoid. My next preferred approach would be to bundle such dependencies by myself, but this would involve to check all the legal stuff of each library to see whether this is possible or not, etc . . . Apart from that I consider the existence of Windows binary packages as kind of an indicator of the health/popularity of a package and whether it has been used sufficiently under Windows to be considered working well under Windows. -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
On 10/29/2012 02:10 PM, Roy Smith wrote: In article mailman.2977.1351455364.27098.python-l...@python.org, Gelonida N gelon...@gmail.com wrote: The sh module looks intersting, but it's not supported for Windows platforms. The X module looks interesting but it's not supported for Windows is true for many values of X. It's all part of the TCO of using a brain-dead operating system. If I write server side code, then I choose my server and my OS, so I won't encounter Windows Unfortunately most the customers won't let me choose their client hardware / client OS. Thus I decide to write my applications cross platform whenever possible and try to choose libraries accordingly. -- http://mail.python.org/mailman/listinfo/python-list
Re: I need help installing pypng in Python 3.3
On 10/29/2012 06:39 AM, ic...@tagyourself.com wrote: That's very kind of you but I don't think it would be particularly fitted to my needs. The program I'm trying to code creates an image as an 2D array of pixels which is defined by RGBA value. My program needs to access and modifies every component of every pixels in the image following a set of rules, kind of like the game of life, only more complex. In fact I only need a library to push this array of pixels in a displayable format for the GUI and in PNG format to write the image to disk. I don't need to do any fancy stuff with the image, just being able to display and write it. Then, actually, what I am suggesting was *almost* perfect. To do transparency, you need to write the portable any map (PAM) formation. Simply print a text header to a file which says: P7 WIDTH 10 HEIGHT 10 DEPTH 4 MAXVAL 255 TUPLTYPE RGB_ALPHA ENDHDR And then dump your 2D array to that same file. A very quick example in 17 lines of code: io = open( anyname.pam,w) x,y = 10,10 gray=(128,128,128,255) # R,G,B,A value picture = [ [ gray ] * x ] * y # Make a blank gray canvas 2D array # Do whatever you want to the 2D picture array here! io.write( P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n % (x,y) ) for yi in xrange( y ): for xi in xrange( x ): pixel = picture[yi][xi] io.write( chr(pixel[0]) ) # R value io.write( chr(pixel[1]) ) # G value io.write( chr(pixel[2]) ) # B value io.write( chr(pixel[3]) ) # A value io.flush() io.close() And that's it. You may of course make this more efficient -- I'm just showing it this way for clarity. Many programs can read PAM directly; but for those that can't you can use nettools, or imagemagick, to convert it to PNG. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, 29 Oct 2012 23:40:53 +1100, Chris Angelico wrote: On Mon, Oct 29, 2012 at 3:52 PM, Andrew Robinson andr...@r3dsolutions.com wrote: I am curious as to how quickly it constructs the result compared to a slice operation. Eg: a[1:5] vs. [ a[i] for i in xrange[1:5] ] For the most part, don't concern yourself with performance. Go with functionality and readability. In the trivial case shown here, the slice is WAY clearer, so it should definitely be the one used; in other cases, the slice might simply be insufficient, so you go with whatever achieves your goal. Performance will usually be good enough, even if there's a marginally faster way. Slicing is about an order of magnitude faster: [steve@ando ~]$ python2.7 -m timeit -s x = range(100, 1000, 2) x [20:40] 100 loops, best of 3: 0.342 usec per loop [steve@ando ~]$ python2.7 -m timeit -s x = range(100, 1000, 2) [x[i] for i in xrange(20, 40)] 10 loops, best of 3: 3.43 usec per loop -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: SSH Connection with Python
In article mailman.3048.1351547492.27098.python-l...@python.org, Gelonida N gelon...@gmail.com wrote: On 10/29/2012 02:10 PM, Roy Smith wrote: In article mailman.2977.1351455364.27098.python-l...@python.org, Gelonida N gelon...@gmail.com wrote: The sh module looks intersting, but it's not supported for Windows platforms. The X module looks interesting but it's not supported for Windows is true for many values of X. It's all part of the TCO of using a brain-dead operating system. If I write server side code, then I choose my server and my OS, so I won't encounter Windows Unfortunately most the customers won't let me choose their client hardware / client OS. Thus I decide to write my applications cross platform whenever possible and try to choose libraries accordingly. The other alternative is to chose your customers better :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, 29 Oct 2012 11:19:38 +, Steven D'Aprano wrote: Because xrange represents a concrete sequence of numbers, all three of start, end and stride must be concrete, known, integers: py xrange(4, None, 2) Traceback (most recent call last): File stdin, line 1, in module TypeError: an integer is required Whereas slices can trivially include blanks that get filled in only when actually used: py hello world[aslice] 'owrd' py NOBODY expects the Spanish Inquisition![aslice] 'D xet h pns nusto!' /me facepalms/ Argggh, I forgot to copy-and-paste the critical line defining aslice: aslice = slice(4, None, 2) Sorry about that. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 10/29/2012 06:52 AM, Roy Smith wrote: Show me an example where someone would write a slice with a negative and a positive index (both in the same slice); and have that slice grab a contiguous slice in the *middle* of the list with orientation of lower index to greater index. It's possible in bioinformatics. ... eq[100:-100]. I decided to go to bed... I was starting to write very badly worded responses. :) Thanks, Roy, what you have just shown is another example that agrees with what I am trying to do. FYI: I was asking for a reason why Python's present implementation is desirable... I wonder, for example: Given an arbitrary list: a=[1,2,3,4,5,6,7,8,9,10,11,12] Why would someone *want* to do: a[-7,10] Instead of saying a[5:10] or a[-7:-2] ? eg: What algorithm would naturally *desire* the default behavior of slicing when using *mixed* negative and positive indexes? In the case of a bacterial circular DNA/RNA ring, asking for codons[ -10: 10 ] would logically desire codon[-10:] + codon[:10] not an empty list, right? I think your example is a very reasonable thing the scientific community would want to do with Python. :) -- http://mail.python.org/mailman/listinfo/python-list
RE: better way for ' '.join(args) + '\n'?
Thomas Rachel wrote: Am 26.10.2012 09:49 schrieb Ulrich Eckhardt: Hi! General advise when assembling strings is to not concatenate them repeatedly but instead use string's join() function, because it avoids repeated reallocations and is at least as expressive as any alternative. What I have now is a case where I'm assembling lines of text for driving a program with a commandline interface. Stop. In this case, you think too complicated. Just do subprocess.Popen(['prog', 'foo', 'bar', 'baz']) - is the most safest thing for this use case. If it should not be possible for any reason, you should be aware of any traps you could catch - e.g., if you want to feed your string to a Bourne shell, you should escape the strings properly. In such cases, I use def shellquote(*strs): rInput: file names, output: ''-enclosed strings where every ' is replaced with '\''. Intended for usage with the shell. # just take over everything except '; # replace ' with '\'' # The shell sees ''' as ''\'''\'''\'''. Ugly, but works. return .join([ '+st.replace(','\\'')+' for st in strs ]) so I can use shellquote('program name', 'argument 1', '$arg 2', even args containing a ' are ok) For Windows, you'll have to modify this somehow. The subprocess module suggests using pipes.quote for escaping. a ('program name', 'argument 1', '$arg 2', even args containing a ' are ok) import pipes map(pipes.quote, a) ['program name', '\'argument 1\'', '$arg 2', '\'even args containing a \'\'\' are ok\''] ' '.join(a) '\'program name\' \'argument 1\' \'$arg 2\' \'even args containing a \'\\\'\' are ok\'' Ramit Prasad This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. Well, you can't *quite* make a truly immutable class in pure-Python, because if *your* Python code can manipulate the class during construction then so can the caller's Python code after construction. The trivial way to make an immutable class in Python is to inherit from an already immutable class and add behaviour but no state: class MyInt(int): def inc(self): return self.__class__(self + 1) Otherwise, you can add private state and rely on the caller not shooting themselves in the foot by accessing single-underscore names, use properties to protect private state, etc. See the source code for collections.namedtuple and decimal.Decimal for some examples. Warning: namedtuple is special, because it uses some runtime exec magic; most immutable classes do not need that. And Decimal is seriously large and complicated. But you can get some ideas from them both. Also, see this: http://northernplanets.blogspot.com.au/2007/01/immutable-instances-in-python.html -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Tue, 30 Oct 2012 06:36:52 +1100, Chris Angelico wrote: On Tue, Oct 30, 2012 at 6:23 AM, Ian Kelly ian.g.ke...@gmail.com wrote: _MyImmutableClass = namedtuple('MyImmutableClass', 'field1 field2 field3 field4') class MyImmutableClass(_MyImmutableClass): Question: Is it clearer to take advantage of the fact that the base class can be an arbitrary expression? class MyImmutableClass(namedtuple('MyImmutableClass', 'field1 field2 field3 field4')): I'm too lazy to google for it, but if you read the examples provided by namedtuple's creator, Raymond Hettinger, that is precisely one of the styles he uses. No need to explicitly declare the base class before using it. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Nice solution wanted: Hide internal interfaces
On Mon, 29 Oct 2012 17:33:24 +0100, Johannes Bauer wrote: Hi there, I'm currently looking for a good solution to the following problem: I have two classes A and B, which interact with each other and which interact with the user. Instances of B are always created by A. Now I want A to call some private methods of B and vice versa (i.e. what C++ friends are), but I want to make it hard for the user to call these private methods. In B, name these private methods with a leading underscore, exactly as you would for any other private name, e.g. B._method. Do not document the existence of B._method in external documentation aimed at the user, except to note that all methods with leading underscores are private, and the use of them is unsupported and subject to change without notice. In A, use B._method normally. After all, it's *your* code, you can do whatever you see fit. Currently my ugly approach is this: I delare the internal methods private (hide from user). Then I have a function which gives me a dictionary of callbacks to the private functions of the other objects. This is in my opinion pretty ugly (but it works and does what I want). Seems to me that this dictionary of callbacks does nothing but add unnecessary complexity to your code. What's the point of it? Besides, if your users are foolish enough to use flagged private _methods, they're foolish enough to access the functions in the callback dictionary. So you gain nothing but extra work. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. Well, you can't *quite* make a truly immutable class in pure-Python, because if *your* Python code can manipulate the class during construction then so can the caller's Python code after construction. The trivial way to make an immutable class in Python is to inherit from an already immutable class and add behaviour but no state: class MyInt(int): def inc(self): return self.__class__(self + 1) Otherwise, you can add private state and rely on the caller not shooting themselves in the foot by accessing single-underscore names, use properties to protect private state, etc. You'd also need to add __slots__ = () to the class definition to make it immutable. Otherwise they still can shoot themselves in the foot by adding new attributes. class MyInt(int): ...def inc(self): ...return self.__class__(self+1) ... a = MyInt() a.b = 1 # Oops. Mutated a. a.b 1 class MyInt(int): ... __slots__ = () ... def inc(self): ... return self.__class__(self + 1) ... a = MyInt() a.b = 1 AttributeError: 'MyInt' object has no attribute 'b' Traceback (most recent call last): File stdin-inspect, line 1, in module AttributeError: 'MyInt' object has no attribute 'b' -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 10/29/2012 10:09 AM, Ian Kelly wrote: On Oct 29, 2012 7:10 AM, Andrew Robinsonandr...@r3dsolutions.com wrote: I will be porting Python 3.xx to a super low power embedded processor (MSP430), both space and speed are at a premium. Running Python on top of Java would be a *SERIOUS* mistake. .NET won't even run on this system. etc. If that's the case, then running Python at all is probably a mistake. You know the interpreter alone has an overhead of nearly 6 MB? There's already a version of the python interpreter which fits in under 100K: http://code.google.com/p/python-on-a-chip/ It's not the 3.x series, though; and I don't want to redo this once 2.7 really does become obsolete. Yes, I realize that. But, why can't I just overload the existing __getitem__ for lists and not bother writing an entire class? You can just overload that one method in a subclass of list. Being able to monkey-patch __getitem__ for the list class itself would not be advisable, as it would affect all list slicing anywhere in your program and possibly lead to some unexpected behaviors. That's what I am curious about. What unexpected behaviors would a monkey patch typically cause? If no one really uses negative and positive indexes in the same slice operation, because there is no reason to do so... It will only break the occasional esoteric application. 20 million is nothing. On a 32-bit system, sys.maxsize == 2 ** 31 - 1. If the error you were seeing was MemoryError, then more likely you were running into dynamic allocation issues due to fragmentation of virtual memory. No, there was no error at all. Pthon just crashed exited; not even an exception that I can recall. It was if it exited normally! The list was generated in a single pass by many .append() 's, and then copied once -- the original was left in place; and then I attempted to slice it. I am able to routinely to 5 million length lists, copy, slice, cut, append, and delete from them without this ever happening. If fragmentation were the issue, I'd think the shorter lists would cause the problem after many manipulations... It may not be a bug in python itself, though, of course. There are libraries it uses which might have a bug. -- http://mail.python.org/mailman/listinfo/python-list
Re: problems with xml parsing (python 3.3)
Am Sonntag, 28. Oktober 2012 03:27:14 UTC+1 schrieb jann...@gmail.com: Hello all, I am new to Python and have a problem with the behaviour of the xml parser. Assume we have this xml document: ?xml version=1.0 encoding=UTF-8? bibliography entry Title of the first book. /entry entry coauthored/ Title of the second book. /entry /bibliography If I now check for the text of all 'entry' nodes, the text for the node with the empty element isn't shown import xml.etree.ElementTree as ET tree = ET.ElementTree(file='test.xml') root = tree.getroot() resultSet = root.findall(.//entry) for r in resultSet: print (r.text) thanks a lot for your answer. as I am looking for a tool to teach using xml in programming it is a pity that this modul implements a very idiosyncratic view on xml data, but dom and sax are out there too, so I will look at them. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Tue, Oct 30, 2012 at 2:42 AM, Andrew Robinson andr...@r3dsolutions.com wrote: No, there was no error at all. Pthon just crashed exited; not even an exception that I can recall. It was if it exited normally! Can you create a reproducible test case? There's usually a cause to these sorts of things. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 9:20 AM, Andrew Robinson andr...@r3dsolutions.com wrote: FYI: I was asking for a reason why Python's present implementation is desirable... I wonder, for example: Given an arbitrary list: a=[1,2,3,4,5,6,7,8,9,10,11,12] Why would someone *want* to do: a[-7,10] Instead of saying a[5:10] or a[-7:-2] ? A quick search of local code turns up examples like this: if name.startswith('{') and name.endswith('}'): name = name[1:-1] If slices worked like ranges, then the result of that would be empty, which is obviously not desirable. I don't know of a reason why one might need to use a negative start with a positive stop, though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, 29 Oct 2012 15:20:02 +, andrea crotti wrote: I have a philosofical doubt about immutability, that arised while doing the SCALA functional programming course. Philosophical. Like most words derived from the ancient Greeks, the F sound uses ph rather than f. Now suppose I have a simple NumWrapper class, that very stupidly does: class NumWrapper(object): def __init__(self, number): self.number = number and we want to change its state incrementing the number, normally I would do this def increment(self): self.number += 1 That's a perfectly fine (although incomplete) design for a mutable numeric class. But as the basis of an immutable class, it's lousy. But the immutability purists would instead suggest to do this: def increment(self): return NumWrapper(self.number + 1) Only if they don't know Python very well :-) In this example, the right way to get an immutable class is: class NumWrapper(int): # not exactly a *wrapper* def increment(self): return self.__class__(self + 1) and you're done. Immutability for free, because you don't store state anywhere that pure-Python code can get to it. (Technically, using ctypes you could mutate it, so don't do that.) Here's a sketch of another technique: class MyNum(object): __slots__ = '_num' def __new__(cls, arg): instance = object.__new__(cls) instance._num = int(arg) return instance @property def value(self): return self._num def increment(self): return self.__class__(self.value + 1) Now on one hand I would love to use only immutable data in my code, but on the other hand I wonder if it makes so much sense in Python. You can go a long, long way using only immutable primitives and functional style in Python, and I recommend it. On the other hand, a *purely* functional approach doesn't make a lot of sense for some tasks. Python is not a pure functional language, and doesn't force you to hammer round pegs into the square hole of the functional style. Some problems are best modelled by an object that holds state and can change over time, e.g. a database or a dict. Other problems are best modelled by constants which do not change, but can be replaced by other constants, e.g. numbers. Some problems fall into a grey area, e.g. lists, arrays, sets, sequences, strings. My advice is to always be alert for square pegs in your code, and write them in functional style using immutable instances, but don't be a purist. If you have a round peg, write that part of your code using a mutable instance with in-place mutator methods, and be happy. The beauty of Python is that you can use whichever style suits the problem best. My impression is that things get more clumsy in the immutable form, for example in the mutable form I would do simply this: number = NumWrapper(1) number.increment() while with immutability I have to do this instead: new_number = number.increment() Why is this clumsy? Do you have problems with this? x = 1 y = x+1 -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 9:42 AM, Andrew Robinson andr...@r3dsolutions.com wrote: The list was generated in a single pass by many .append() 's, and then copied once -- the original was left in place; and then I attempted to slice it. Note that if the list was generated by .appends, then it was copied more than once. Python reserves a specific amount of space for the list. When it grows past that, the list must be reallocated and copied. It grows the list exponentially in order to keep the amortized time complexity of append at O(1), but the point is that a list of 20 million items is going to be resized and copied several times before it is complete. -- http://mail.python.org/mailman/listinfo/python-list
Re: Immutability and Python
On Mon, 29 Oct 2012 15:45:59 -0700, Chris Kaynor wrote: On Mon, Oct 29, 2012 at 3:30 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Mon, 29 Oct 2012 17:05:07 +, andrea crotti wrote: I meant how do I create new immutables classes myself, I guess that's possible writing C extensions but I don't see in pure Python.. Well, you can't *quite* make a truly immutable class in pure-Python, because if *your* Python code can manipulate the class during construction then so can the caller's Python code after construction. The trivial way to make an immutable class in Python is to inherit from an already immutable class and add behaviour but no state: class MyInt(int): def inc(self): return self.__class__(self + 1) Otherwise, you can add private state and rely on the caller not shooting themselves in the foot by accessing single-underscore names, use properties to protect private state, etc. You'd also need to add __slots__ = () to the class definition to make it immutable. Otherwise they still can shoot themselves in the foot by adding new attributes. Doctor, it hurts when I do this. Then don't do that. I'm not a big fan of preventatively using __slots__ merely to prevent the caller from tagging an object with extra data. Why do you care if the caller sticks a postit note on the object? It doesn't hurt the object, and if the caller loses track of which object has a postit note, that's their responsibility, not yours. I often wish I could sick an attribute on built-ins, e.g. after calculating some numeric result as a float, stick an error estimate on it. Callers who care about the error estimate can inspect it; those who don't, will never even notice it. If you have a good reason for using __slots__, then go right ahead. Otherwise, don't be paternalistic. This is Python, we have the right to shoot ourselves in the foot if we like. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
date and time comparison how to
All, I need help with a date and time comparison. Say a user enters a date-n-time and a file on disk. I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process. How best to do? I have looked at the datetime module, tried a few things, no luck. Is os.stat a part of it? Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time. ?? Any help would be appreciated! -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
In article mailman.3056.1351552107.27098.python-l...@python.org, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 29, 2012 at 9:42 AM, Andrew Robinson andr...@r3dsolutions.com wrote: The list was generated in a single pass by many .append() 's, and then copied once -- the original was left in place; and then I attempted to slice it. Note that if the list was generated by .appends, then it was copied more than once. Python reserves a specific amount of space for the list. When it grows past that, the list must be reallocated and copied. It grows the list exponentially in order to keep the amortized time complexity of append at O(1), but the point is that a list of 20 million items is going to be resized and copied several times before it is complete. I think you're missing the point of amortized constant time. Yes, the first item appended to the list will be copied lg(20,000,000) ~= 25 times, because the list will be resized that many times(*). But, on average (I'm not sure if average is strictly the correct word here), each item will be copied only once. Still, it always stuck me as odd that there's no preallocate() method. There are times when you really do know how many items you're going to add to the list, and doing a single allocation would be a win. And it doesn't cost anything to provide it. I suppose, however, if you're adding enough items that preallocating would really matter, then maybe you want an array instead. (*) I don't know the exact implementation; I'm assuming each resize is a factor of 2. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 5:24 PM, Roy Smith r...@panix.com wrote: I think you're missing the point of amortized constant time. Yes, the first item appended to the list will be copied lg(20,000,000) ~= 25 times, because the list will be resized that many times(*). But, on average (I'm not sure if average is strictly the correct word here), each item will be copied only once. Still, it always stuck me as odd that there's no preallocate() method. There are times when you really do know how many items you're going to add to the list, and doing a single allocation would be a win. And it doesn't cost anything to provide it. I suppose, however, if you're adding enough items that preallocating would really matter, then maybe you want an array instead. (*) I don't know the exact implementation; I'm assuming each resize is a factor of 2. The growth factor is approximately 1.125. Approximately because there is also a small constant term. The average number of copies per item converges on 8. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On 29 October 2012 23:01, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 29, 2012 at 9:20 AM, Andrew Robinson andr...@r3dsolutions.com wrote: FYI: I was asking for a reason why Python's present implementation is desirable... I wonder, for example: Given an arbitrary list: a=[1,2,3,4,5,6,7,8,9,10,11,12] Why would someone *want* to do: a[-7,10] Instead of saying a[5:10] or a[-7:-2] ? A quick search of local code turns up examples like this: if name.startswith('{') and name.endswith('}'): name = name[1:-1] If slices worked like ranges, then the result of that would be empty, which is obviously not desirable. I don't know of a reason why one might need to use a negative start with a positive stop, though. It's useful when getting a reversed slice: a = [1,2,3,4,5,6,7,8,9,10] a[-3:3:-1] [8, 7, 6, 5] Oscar -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, Oct 29, 2012 at 5:43 PM, Ian Kelly ian.g.ke...@gmail.com wrote: The growth factor is approximately 1.125. Approximately because there is also a small constant term. The average number of copies per item converges on 8. Of course, that is the *maximum* number of copies. The actual number could be much less if realloc() performs well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
On Mon, 29 Oct 2012 08:42:39 -0700, Andrew Robinson wrote: But, why can't I just overload the existing __getitem__ for lists and not bother writing an entire class? You say that as if writing an entire class was a big complicated effort. It isn't. It is trivially simple, a single line: class MyList(list): ... plus the __getitem__ definition, which you would have to write anyway. It is a trivial amount of extra effort. You can just overload that one method in a subclass of list. Being able to monkey-patch __getitem__ for the list class itself would not be advisable, as it would affect all list slicing anywhere in your program and possibly lead to some unexpected behaviors. That's what I am curious about. What unexpected behaviors would a monkey patch typically cause? What part of unexpected is unclear? Monkey-patching is poor programming technique because it leads to *unexpected* and *impossible to predict* interactions between *distant* parts of the code. It leads to impossible to predict differences between the source code on disk and the actual running code. It leads to impossible to predict differences between documented behaviour and actual behaviour. Let me see if I can illustrate a flavour of the sort of things that can happen if monkey-patching built-ins were allowed. You create a list and print it: # simulated output py x = [5, 2, 4, 1] py print(x) [1, 2, 4, 5] What? How did that happen? That's not the list you provided. The order has been lost. So you dig deep into your code, and you don't find anything. And you read the Python documentation for lists, and don't find anything. And you google the Internet, and don't find anything. And you ask for help, and everybody says you're crazy because when they duplicate your code they get the expected behaviour. And you report a bug in Python, and it gets closed as cannot replicate. Finally you search deep into the libraries used in your code, and *five days later* discover that your code uses library A which uses library B which uses library C which uses library D which installs a harmless monkey-patch to print, but only if library E is installed, and you just happen to have E installed even though your code never uses it, AND that monkey-patch clashes with a harmless monkey-patch to list.__getitem__ installed by library F. And even though each monkey-patch alone is harmless, the combination breaks your code's output. Python allows, but does not encourage, monkey-patching of code written in pure Python, because it sometimes can be useful. It flat out prohibits monkey-patching of builtins, because it is just too dangerous. Ruby allows monkey-patching of everything. And the result was predictable: http://devblog.avdi.org/2008/02/23/why-monkeypatching-is-destroying-ruby/ -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: date and time comparison how to
On 10/29/2012 04:13 PM, noydb wrote: All, I need help with a date and time comparison. Say a user enters a date-n-time and a file on disk. I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process. How best to do? I have looked at the datetime module, tried a few things, no luck. Is os.stat a part of it? Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time. ?? Any help would be appreciated! Use the datetime module (distributed with Python) to compare date/times. You can turn a filesystem time into a datetime with something like the following: import datetime, os, stat mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time dt = datetime.datetime.fromtimestamp(mtime) -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -- http://mail.python.org/mailman/listinfo/python-list
Re: Negative array indicies and slice()
In article mailman.3057.1351554215.27098.python-l...@python.org, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 29, 2012 at 5:24 PM, Roy Smith r...@panix.com wrote: I think you're missing the point of amortized constant time. Yes, the first item appended to the list will be copied lg(20,000,000) ~= 25 times, because the list will be resized that many times(*). But, on average (I'm not sure if average is strictly the correct word here), each item will be copied only once. Still, it always stuck me as odd that there's no preallocate() method. There are times when you really do know how many items you're going to add to the list, and doing a single allocation would be a win. And it doesn't cost anything to provide it. I suppose, however, if you're adding enough items that preallocating would really matter, then maybe you want an array instead. (*) I don't know the exact implementation; I'm assuming each resize is a factor of 2. The growth factor is approximately 1.125. Approximately because there is also a small constant term. The average number of copies per item converges on 8. Wow, that's surprising. It also makes it that much more surprising that there's no way to pre-allocate. -- http://mail.python.org/mailman/listinfo/python-list