Re: Twisted and txJSON-RPC
This problem has come up for me as well. $ sudo easy_install pylisp-ng [sudo] password for _: install_dir /usr/local/lib/python2.6/dist-packages/ Searching for pylisp-ng Reading http://pypi.python.org/simple/pylisp-ng/ Reading https://launchpad.net/pylisp-ng Best match: pyLisp-NG 2.0.0 Downloading http://pypi.python.org/packages/source/p/pyLisp-NG/pyLisp-NG-2.0.0.tar.gz#md5=84141318cde6bf4e4f10ac4a920531be Processing pyLisp-NG-2.0.0.tar.gz Running pyLisp-NG-2.0.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-N3JX01/pyLisp-NG-2.0.0/egg-dist-tmp-CpmhdK error: docs/PRELUDE.txt: No such file or directory -- http://mail.python.org/mailman/listinfo/python-list
updating nntplib
Hello all, There are some notable deficiencies in nntlib. Here are two: 1) It says it implements NNTP as defined in RFC 977, but NNTP has a newer RFC, RFC 3977, which clarifies some vagueness and has more commands defined. However, as it currently stands you cannot issue these commands, since they aren't available to call. 2) In some cases, it will bomb out upon receiving certain greetings that it doesn't expect. As I understand it, it actually terminates the connection, not allowing for catching an exception or anything. I have not verified this myself. I'd like to remedy these deficiencies. In the case of #1, I'd like to implement some of the new commands and provide a way to send arbitrary commands to the server, because NNTP is designed to allow servers to implement optional features. I'm not sure how I'll remedy #2, but I imagine it will be clear once I examine the code. I am guessing that this is the right list for discussing this, but perhaps python-dev is better. Anyone have feedback? -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
Re: updating nntplib
On Thu, Feb 05, 2009 at 04:40:36PM -0600, Travis wrote: > 2) In some cases, it will bomb out upon receiving certain greetings > that it doesn't expect. As I understand it, it actually terminates > the connection, not allowing for catching an exception or anything. > I have not verified this myself. I just verified this; if the server responds to authentication with the string "200 Welcome feeder", then nntplib bombs out with a: nntplib.NNTPPermanentError IMHO, it shouldn't be sensitive to anything but the numeric code, and 200 indicates success. -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
zlib interface semi-broken
Hello all, The zlib interface does not indicate when you've hit the end of a compressed stream. The underlying zlib functionality provides for this. With python's zlib, you have to read past the compressed data and into the uncompressed, which gets stored in Decompress.unused_data. As a result, if you've got a network protocol which mixes compressed and non-compressed output, you may find a compressed block ending with no uncompressed data following until you send another command -- which a synchronous (non-pipelined) client will not send, because it is waiting for the [compressed] data from the previous command to be finished. As a result, you get a protocol deadlock. A simple way to fix this would be to add a finished attribute to the Decompress object. However, perhaps this would be a good time to discuss how this library works; it is somewhat awkward and perhaps there are other changes which would make it cleaner. What does the python community think? -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
Re: zlib interface semi-broken
indicate a possible resynchronization point, client code may not be capable of handling it as a non-fatal exception. Thoughts? -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
Re: zlib interface semi-broken
So I've submitted a patch to bugs.python.org to add a new member called is_finished to the zlib decompression object. Issue 5210, file 13056, msg 81780 -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
how to distribute python extensions independently of python
So, Recently I made a fix to the zlib module that I need for use at work. I would like other employees to be able to use it without recompiling python. I assume I can just rename it and distribute it as a python extension. I was wondering how I can provide a way for other employees to build it. I saw a "Universal Unix Makefile for Python extensions" that looks promising. Is this the accepted way to compile python extensions still? -- Crypto ergo sum. http://www.subspacefield.org/~travis/ Do unto other faiths as you would have them do unto yours. If you are a spammer, please email j...@subspacefield.org to get blacklisted. -- http://mail.python.org/mailman/listinfo/python-list
Re: Lisp mentality vs. Python mentality
On Apr 24, 11:06 pm, Carl Banks wrote: > In answering the recent question by Mark Tarver, I think I finally hit > on why Lisp programmers are the way they are (in particular, why they > are often so hostile to the "There should only be one obvious way to > do it" Zen). > > Say you put this task to a Lisp and a Python programmer: Come up with > a good, generic, reusable way to compare two lists. What are their > respective trains of thought? > > Lisp programmer: > > Well, there is a standard function called mismatch that does it, but I > can't recommend it. First of all, you don't know where that > function's been. Anyone and their mother could have worked on it, did > they have good, sound programming practice in mind when they wrote > it? Of course not. Let's be real here, we have to implement this by > hand. > > (defun lists-are-equal (a b) > (or (and (not a) (not b)) > (and (= (car a) (car b)) (lists-are-equal (cdr a) (cdr b > > There, much better than the standard function, and better yet, it's in > the *absolute minimal form possible*. There is no way to express list > comparison in a more reduced form. It's almost erotic how awesome it > is. I'm---whoa, ok, I'm getting a little excited now, settle down. > Well, come to think of it, that's really not that good. First of all > it's a function. I mean, it just sits there and does nothing till you > call it. How boring is that? It can't react to the current > situation. Plus it compares all lists the same way, and that is > really inefficient. Every list compare is a new problem. Different > lists need different comparative strategies. This function simply > won't do. I need a macro that can intelligently compile the right > list compare methodology in. For instance, if we want to compare two > lists that are known at compile time, we don't want to waste time > comparing them at runtime. No, the macro must detect constant > arguments and special case them. Good start. Now, we have to > consider the conditions this comparison is being done under. If the > user is passing the result of a sort to this macro, it's almost > certain that they are trying to see whether the lists have the same > elements. We can do that a lot more efficiently with a countset. So > let's have the macro check to see if the forms passed to it are all > sort calls. Better yet, let's check for my own powerful sort macro. > Hmm. Wait... I think my 4600-line sort macro already checks its > calling context to see if its results are being fed to a list > comparison. I'll have to refactor that together with this macro. Ok, > good, now I am sure other users will eventually want to customize list > comparison for their own use, after all every list comparison is > different and I can't possibly anticipate all of them. A user needs > to be able to adapt to the situation, so it's vitally important to > create a plug-in infrastructure to give them that flexibility. Now, > what about exceptions, there's a millions ways to deal with that... > > ...and so on until eyelids can no longer stay open > > Python programmer: > > a == b. Next question. > > Carl Banks, who might be exaggerating > > ...a little. I've noticed that every one of you is wrong about programming. Since I can't say it effectively, here's someone who can: http://www.youtube.com/watch?v=XHosLhPEN3k That's the answer. -- http://mail.python.org/mailman/listinfo/python-list
The Python Web Authoring and Application Pages
I've got five pages of information linked to from here: http://www.subspacefield.org/~travis/ LWMLs template systems static web page generators microframeworks web app frameworks It seems like many web app programmers and web authors know one system, or possibly two, and so you don't often get good answers to the questions like "which one should I choose?", and "what features would make me choose one over the other?" I suppose this is because most people just have to use whatever their employer is using, and few are in the enviable position of being able to choose. Man, that is a lot of work. And it just keeps growing; as I track down web app frameworks, for example, I discover new templating systems, and have to go back and update _that_ information, and then have to figure out if the other web app frameworks support it, and it also references some more templating languages as influencing it, and it just goes on and on... If I hadn't scrapped the idea of covering this for all languages, ruby and PHP would have given a combinatorial explosion beyond all measure.. For those who'd like to see such information overload, my initial attempt at this is here: http://www.subspacefield.org/~travis/static_blog_generators.html -- A Weapon of Mass Construction My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email j...@subspacefield.org to get blacklisted. pgphjoCNOQFVg.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: zlib interface semi-broken
I've come up with a good test for issue5210 and uploaded it to the bug tracker. This patch should be ready for inclusion now. -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email j...@subspacefield.org to get blacklisted. pgpXcFtrYha4u.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: proposal: add setresuid() system call to python
On Fri, Jul 17, 2009 at 04:59:53PM -0400, Jean-Paul Calderone wrote: > On Fri, 17 Jul 2009 15:01:41 -0500, travis+ml-pyt...@subspacefield.org wrote: >> I am suggesting that the setresuid function be added to python, >> perhaps in the OS module, because it has the clearest semantics for >> manipulating user ids. The reason why is best described in the >> following paper: > > Yes, it'd be good for Python to expose setresuid. The best course of > action is to file a ticket in the issue tracker. Things will be sped > along if you also attach a patch implementing the change. :) I'm now working on this as issue6758 in the tracker. Adding the calls to the os module doesn't seem like a good idea to me, because the calls are not POSIX, and it appears that os maps to posixmodule.c. Since the authors of the paper (Wagner et. al.) are proposing a new set of APIs to make all of this clearer, I'm thinking that I will create a module specifically for dropping permissions. -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email j...@subspacefield.org to get blacklisted. pgpty4aw4PVv5.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: proposal: add setresuid() system call to python
On Tue, Aug 25, 2009 at 03:03:12PM +0200, Hrvoje Niksic wrote: > You should use ctypes.get_errno() instead of os.errno; sorry about that. > > Also, when raising OSError, you should set the 'errno' attribute to the > appropriate code. How does that compare to: raise pythonapi.PyErr_SetFromErrno(py_object(OSError)) ? -- Obama Nation | My emails do not have attachments; it's a digital signature that your mail program doesn't understand. | http://www.subspacefield.org/~travis/ If you are a spammer, please email j...@subspacefield.org to get blacklisted. pgpc0M9ykZOdl.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Reusable (local) Modules
I'm relatively new to Python (coming from strong C and Smalltalk backgrounds). I've written a couple of relatively small apps (one or two .py files). I'm using PyCharm (I love it). I'm curious what the pythonic approach is to creating your own reusable modules. Any tutorials or high level explanations, or detailed, much appreciated. For example, I have a small module called valvenumbers.py. It's a family of functions that we use to do a variety of things with the serial numbers we attach to some of our products. Now I'm making a little desktop app using wxpython, and I want to use (import) that module. Using PyCharm, I have two separate projects in sibling directories. And there's another separate command line tool that wants to do the same. Currently, I just place a symlink to the valvenumbers.py, local to the directory of these apps. This seems like "the quickest thing that could possibly work", but I'm assuming there's a more pythonic way to approach this general problem. TIA! Travis Griggs "Simplicity is the ultimate sophistication." -- Leonardo Da Vinci -- http://mail.python.org/mailman/listinfo/python-list
Style help for a Smalltalk-hack
I'm writing some code that does a structured read from formatted binary file. The code I came up with looks like: # get the first four bytes, the first gap field chunk = byteStream.read(4) while chunk: # interpret the gap bytes gap, = struct.unpack('>I', chunk) # suck off the valveCount valveCount, = struct.unpack('>I', byteStream.read(4)) # collect the next valveCount signatures signatures = [struct.unpack('>I', byteStream.read(4))[0] for _ in range(valveCount)] self.script.append(ScriptSpan(gap=gap, valveSet=signatures)) # now get the next 4 bytes for the gap of the next iteration, it'll be empty if we're at end chunk = byteStream.read(4) I can't help but thinking that there's some better way (i.e. more pythonic) to do this that doesn't involve having to use another module (Construct) or exploring generators or something like that. What bugs me about it is that there is two different styles for reading/decoding values from the byte stream. valveCount and signatures are both paired invocations of unpack() and read(). But to detect the end of the stream (file), I have to split the read() and unpack() of the gap value across 3 different lines of the code, and they don't even sit adjacent to each other. I'm wandering up the Python curve with a passel of Smalltalk experience under my belt, so I expect I'm struggling with trying to map something like this across to python [byteStream atEnd] whileFalse: [ gap := (byteStream next: 4) asInteger. valveCount := (byteStream next: 4) asInteger. signatures := (1 to: valveCount) collect: [:_ | (byteStream next: 4) asInteger]. self script add: (ScriptSpan gap: gap valveSet: signatures). ] The part that doesn't seem to be there in the standard python library is the idea of an atEnd message for streams, it's inferred as a byproduct of a read(). Please be gentle/kind. I'm still learning. :) TIA -- Travis Griggs "A vital ingredient of success is not knowing that what you're attempting can't be done." -Terry Pratchett -- http://mail.python.org/mailman/listinfo/python-list
Re: Style help for a Smalltalk-hack
On Oct 22, 2012, at 6:33 PM, MRAB wrote: > Another way you could do it is: > > while True: >chunk = byteStream.read(4) >if not chunk: >break >... > > And you could fetch multiple signatures in one read: > > signatures = list(struct.unpack('>{}I'.format(valveCount), byteStream.read(4 > * valueCount))) Thanks, both great ideas. Still does the read/decode slightly different between the different sites, but at least it's localized better. Much appreciated. -- Travis Griggs "History has a habit of changing the people who think they are changing it." -Terry Pratchett -- http://mail.python.org/mailman/listinfo/python-list
Re: Style help for a Smalltalk-hack
On Oct 22, 2012, at 6:33 PM, MRAB wrote: > By the way, in Python the recommended style for variable names (well, > what you'd call a 'variable' in other languages :-)) is lowercase with > underscores, e.g. "byte_stream". We went with the "...mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility…" escape clause. :) The group of us here are working in multiple languages, and all use it (mixedCase) across all of them. Because PEP 8 makes it clear up front that "internal consistency is preferred", we felt justified in marching on. -- Travis Griggs "A vital ingredient of success is not knowing that what you're attempting can't be done." -Terry Pratchett -- http://mail.python.org/mailman/listinfo/python-list
connect windows share
I want to be able to connect to a windows share via python. My end goal is to be able to recursively search through windows shares. I want to do this in Linux as well. So given a share such as \\computer\test I would like to search through the test directory and any sub directories for any file names of interest. What's the best way of going about this? I know LDAP / AD creds may play an essential part in this as well. Thanks for your input. -- http://mail.python.org/mailman/listinfo/python-list
Mastering Python... Best Resources?
I know the Python syntax pretty well. I know a lot of the libraries and tools. When I see professional Python programmer's code, I am often blown away with the code. I realized that even though I know the language, I know nothing about using it effectively. I would like to start using Python more in my professional career. Where can I find resources that will take my skills to the next level? I would prefer to watch a streaming video series, if possible. I've read quite a few books about Python. They cover a lot of topics, but none of them covered common conventions or hacks. I mean, I got good at C++ reading books by Scott Meyers, who concentrated on common idioms, things to avoid, the proper way to do things, etc. Right now, I am at that point where I know how to do write just about anything in the language. However, I still have that hesitation I get when I'm just not sure what's the right way. -- http://mail.python.org/mailman/listinfo/python-list
Re: Mastering Python... Best Resources?
On Aug 26, 8:44 am, Chris Angelico wrote: > On Fri, Aug 26, 2011 at 10:33 PM, Travis Parks wrote: > > I know the Python syntax pretty well. I know a lot of the libraries > > and tools. When I see professional Python programmer's code, I am > > often blown away with the code. I realized that even though I know the > > language, I know nothing about using it effectively. > > I would say that there are three aspects to using Python effectively: > > 1) Understanding the syntax, which you've mastered. > 2) Understanding the philosophy > 3) Knowing algorithms. > > The second is more or less what you're asking for, but the > language-independent third may be more useful to you. This is correct > Python syntax (#1), and decently Pythonic style (#2), but a hopelessly > flawed algorithm (#3): > > def fib(x): > return fib(x-1) + fib(x-2) if x>2 else 1 > > Or: > > def fib(x): > if x<3: return 1 > return fib(x-1) + fib(x-2) > > Both versions are clean and easy to read, but neither would be what > I'd call brilliant code. > > You can get books on algorithms from all sorts of places, and with a > very few exceptions, everything you learn with apply to Python and > also to every other language you use. > > ChrisA > > Well, I think I am going more for #2. I know about things like data structures and algorithms... in your case memoization. Here is a good example of what I am talking about. Someone took the time to write quicksort in a single line of code: def qsortr(list): return [] if list==[] else qsortr([x for x in list[1:] if x < list[0]]) + [list[0]] + qsortr([x for x in list[1:] if x >= list[0]]) I would never even think to use list comprehensions and splicing like that. I would write this code the same way I'd write it in C++/C#. I'm aware that writing code like the above example is probably bad practice (and that the implementation here has some major inefficiencies), but it is the "mentality" that goes into it. I haven't gotten to the point where I can truly use the language features to my full advantage. I haven't seen enough "tricks" to be effective. I feel like there is so much of the language I am not utilizing because I'm still thinking in terms of a less powerful language. I was hoping to find a series that would familiarize me with how real Python programmers get things done. -- http://mail.python.org/mailman/listinfo/python-list
Re: Mastering Python... Best Resources?
On Aug 26, 9:28 am, Chris Angelico wrote: > On Fri, Aug 26, 2011 at 10:58 PM, Travis Parks wrote: > > I haven't gotten to the point where I can truly use the language > > features to my full advantage. I haven't seen enough "tricks" to be > > effective. I feel like there is so much of the language I am not > > utilizing because I'm still thinking in terms of a less powerful > > language. I was hoping to find a series that would familiarize me with > > how real Python programmers get things done. > > Ah! Then I recommend poking around with the standard library. No > guarantees that it's ALL good code, but it probably will be. In any > case, it sounds like you're well able to evaluate code in your own > head and recognize the good from the ugly. > > In the source distribution (I'm looking at the latest straight from > hg, but presumably it's the same everywhere), there's a whole lot of > .py files in ./Lib - there's sure to be some good examples in there > somewhere. > > ChrisA > > I've been thinking about going through the docs on the main website. Cool thing is it has links to the actual lib files. I was checking out string.py yesterday. I was searching all over youtube for good videos of some type. Google has an intro course, but it didn't really do much for me. Microsoft has these series called 'Going Deep' that occasionally runs something super in-depth. The videos on C++ and the STL are really excellent. I was hoping someone had taken the time to create a similar series for Python. I can't help but remember my one professor in college, who really made pointers, bitwise arithmetic and low level OS operations make sense. He explained to us a lot about how the STL worked and showed us tons of C++/STL hacks. I probably learned more in the 2 years I had classes with him than I have in all the time I've programmed. To get that type of insight into another language, like Python, would be the ultimate gift for someone like me. Personally, I am tired of working in languages that don't strongly support functional paradigms. -- http://mail.python.org/mailman/listinfo/python-list
Re: Mastering Python... Best Resources?
On Aug 26, 11:12 am, Roy Smith wrote: > In article > <2309ec4b-e9a3-4330-9983-1c621ac16...@ea4g2000vbb.googlegroups.com>, > Travis Parks wrote: > > > I know the Python syntax pretty well. I know a lot of the libraries > > and tools. When I see professional Python programmer's code, I am > > often blown away with the code. I realized that even though I know the > > language, I know nothing about using it effectively. > > In a sense, I'm in the same boat as you. I've been using Python since > before the 2.0 series, and I tend to think of the language in much the > same way as I did back then. Which is to say I don't use the language, > as it currently exists, as effectively as I might. > > Here's some things I suggest you look at: > > Iterators. This is such a powerful concept. When I started with the > language, iterators largely meant the difference between range() and > xrange(). Now we've got a whole ecosystem which has grown up around > them (x + " comprehension" for x in {'list', 'dictionary', 'set'}), not > to mention generators and generator expressions. And the itertools > library. > > Decorators. Another powerful concept. We use these in our web servers > for all sorts of cool things. Adding cacheing. Imposing prerequisites > on route calls. I still don't think of using these immediately, but I > do see the notational convenience they provide for many things. > > Context Managers. One of the (very few) things that I always found > lacking in Python compared to C++ was deterministic object destruction. > Context managers give you this. I'm still exploring all the neat things > you can do with them. > > The full range of containers. I started with lists, tuples, and > dictionaries. Now we've got sets, frozensets, named tuples, deques, > Counters, defaultdicts (I love those), heaps, and I'm sure a few others > I've missed. List and dicts are such well designed containers, you can > do almost anything with just those two, but all the other new ones often > make things quicker, simpler, and more obvious. > > The profiler. Most people obsess about performance early on and don't > realize that most of their guesses about what's fast and what's slow are > probably wrong. Learn to use the profiler and understand what it's > telling you. > > Unittest. Testing is, in general, a neglected practice in most software > development shops, and that's a shame. Python has some really good > capabilities to support testing which you should get familiar with. > Unittest is just one of them. There's also doctest, nose, and a bunch > of other contributed modules. Look at them all, learn at least one of > them well, and use it for everything you write. > > > I've read quite a few books about Python. They cover a lot of topics, > > but none of them covered common conventions or hacks. I mean, I got > > good at C++ reading books by Scott Meyers, who concentrated on common > > idioms, things to avoid, the proper way to do things, etc. > > Ugh. The problem with Meyers's books is that they are needed in the > first place. C++ is such a horribly complicated language, you really > can't use it without making a serious study of it. There's too many > gotchas that you MUST know to avoid disaster with even the most basic > programs. > > Python isn't that way. You can learn a small, basic subset of the > language and get a lot done. You may not be doing things the most > effective way, but you're also not going to be looking at memory > corruption because you didn't understand the details of object lifetimes > or how type promotion, function overloading, and implicit temporary > object construction all interact. > > Thanks for the input. I had been writing my Compass project (http://compass.codeplex.com) in Pythonese. I was planning on implementing a lot of the features of MS' LINQ in Python iterators, too. I am surprised that there aren't a ton of Python libraries for general purpose algorithms. "yield" is one of my favorite keywords. :-) I will take a look at decorators especially. I see them being used for properties and other coolness. I started playing with unittest the other day. unittest.main(exit=False) <-- took me a while to find I will look at the containers, too. I have been trying to push tuple syntax support in C# for years now. Named tuples are so useful. I agree that C++ is too complicated. Bjarne should have cared less about backward compatibility with C and fixed some of the issues with it. He should have also made some of the defaults more intuitive - lik
Checking Signature of Function Parameter
I am trying to write an algorithms library in Python. Most of the functions will accept functions as parameters. For instance, there is a function called any: def any(source, predicate): for item in source: if predicate(item): return true; return false; There are some things I want to make sure of. 1) I want to make sure that source is iterable. 2) More importantly, I want to make sure that predicate is callable, accepting a thing, returning a bool. This is what I have so far: if source is None: raise ValueError("") if not isinstanceof(source, collections.iterable): raise TypeError("") if not callable(predicate): raise TypeError("") The idea here is to check for issues up front. In some of the algorithms, I will be using iterators, so bad arguments might not result in a runtime error until long after the calls are made. For instance, I might implement a filter method like this: def where(source, predicate): for item in source: if predicate(item): yield item Here, an error will be delayed until the first item is pulled from the source. Of course, I realize that functions don't really have return types. Good thing is that virtually everything evaluates to a boolean. I am more concerned with the number of parameters. Finally, can I use decorators to automatically perform these checks, instead of hogging the top of all my methods? -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Aug 28, 5:31 pm, Chris Angelico wrote: > On Mon, Aug 29, 2011 at 7:20 AM, Travis Parks wrote: > > > if source is None: raise ValueError("") > > if not isinstanceof(source, collections.iterable): raise TypeError("") > > if not callable(predicate): raise TypeError("") > > Easier: Just ignore the possibilities of failure and carry on with > your code. If the source isn't iterable, you'll get an error raised by > the for loop. If the predicate's not callable, you'll get an error > raised when you try to call it. The only consideration you might need > to deal with is that the predicate's not callable, and only if you're > worried that consuming something from your source would be a problem > (which it won't be with the normal iterables - strings, lists, etc, > etc). Otherwise, just let the exceptions be raised! > > ChrisA I guess my concern is mostly with the delayed exceptions. It is hard to find the source of an error when it doesn't happen immediately. I am writing this library so all of the calls can be chained together (composed). If this nesting gets really deep, finding the source is hard to do, even with a good debugger. Maybe I should give up on it, like you said. I am still familiarizing myself with the paradigm. I want to make sure I am developing code that is consistent with the industry standards. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Aug 29, 2:30 am, Nobody wrote: > On Sun, 28 Aug 2011 14:20:11 -0700, Travis Parks wrote: > > More importantly, I want to make sure that > > predicate is callable, accepting a thing, returning a bool. > > The "callable" part is do-able, the rest isn't. > > The predicate may accept an arbitrary set of arguments via the "*args" > and/or "**kwargs" syntax, and pass these on to some other function. > Exactly *which* function may be the result of an arbitrarily complex > expression. Or it may not even call another function, but just use the > arbitrary set of arguments in an arbitrarily complex manner. > > IOW, determining in advance what will or won't work is actually impossible. > > Thanks for everyone's input. I decided that I will put some basic checks up front, like "is it None", "is it Iterable" and "is it callable". Other than that, I am letting things slide. Asking for forgiveness is always easier anyway. Just so everyone knows, I am defining these methods inside a class called IterableExtender: class IterableExtender(collections.Iterable):... I wanted to allow for calls like this: extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 == 0).first(lambda x: x % 7 == 0) It allows me to compose method calls similarly to LINQ in C#. I think this looks better than: first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0, lambda x : x % 7 == 0))) Internally to the class, there are "private" static methods taking raw inputs and performing no checks. The public instance methods are responsible for checking input arguments and wrapping results. Eventually, I will start working on algorithms that work on MutableSequences, but for now I am just doing Iterables. This is turning out to be a great learning experience. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking Signature of Function Parameter
On Aug 29, 1:42 pm, Ian Kelly wrote: > On Mon, Aug 29, 2011 at 10:45 AM, Travis Parks wrote: > > I wanted to allow for calls like this: > > > extend(range(0, 1000)).map(lambda x: x * x).where(lambda x: x % 2 == > > 0).first(lambda x: x % 7 == 0) > > > It allows me to compose method calls similarly to LINQ in C#. I think > > this looks better than: > > > first(where(map(range(0, 1000), lambda x: x * x, lambda x: x % 2 == 0, > > lambda x : x % 7 == 0))) > > FWIW, I would be inclined to write that in Python like this: > > def first(iterable): > try: > return next(iter(iterable)) > except StopIteration: > raise ValueError("iterable was empty") > > squares = (x * x for x in range(0, 1000)) > first(x for x in squares if x % 14 == 0) Python's comprehensions make the need for many of the methods I am writing unnecessary. Which probably explains why no ones really bothered to write one before. The only problem I have above is either the composition causes complex method calls first(where(map(range(..., it requires complex comprehensions or it requires breaking the code into steps. Even my approach has problems, such as the overhead of carrying an invisible wrapper around. > > It does a bit too much to comfortably be a one-liner no matter which > way you write it, so I split it into two. > > Cheers, > Ian > > Yeah. I have already seen a lot of better ways of writing my code based solely on your example. I didn't know about iter as a built-in function. I have been calling __iter__ directly. I also need to think more about whether methods like "where" and "map" are going to be beneficial. The good thing is that someone will be able to use my wrapper in any context where an Iterable can be used. It will allow someone to switch between styles on the fly. I'm still not convinced that this library is going to be very "pythony". I wrote a post a few days ago about how I know the syntax and libraries fairly well, but I don't have the "philosophy". I haven't seen a lot of tricks and I am never sure what is the "norm" in Python. I am sure if an experienced Python programmer looked at my code, they'd immediately know I was missing a few things. -- http://mail.python.org/mailman/listinfo/python-list
Handling 2.7 and 3.0 Versions of Dict
I am writing a simple algorithms library that I want to work for both Python 2.7 and 3.x. I am writing some functions like distinct, which work with dictionaries under the hood. The problem I ran into is that I am calling itervalues or values depending on which version of the language I am working in. Here is the code I wrote to overcome it: import sys def getDictValuesFoo(): if sys.version_info < (3,): return dict.itervalues else: return dict.values getValues = getDictValuesFoo() def distinct(iterable, keySelector = (lambda x: x)): lookup = {} for item in iterable: key = keySelector(item) if key not in lookup: lookup[key] = item return getValues(lookup) I was surprised to learn that getValues CANNOT be called as if it were a member of dict. I figured it was more efficient to determine what getValues was once rather than every time it was needed. First, how can I make the method getValues "private" _and_ so it only gets evaluated once? Secondly, will the body of the distinct method be evaluated immediately? How can I delay building the dict until the first value is requested? I noticed that hashing is a lot different in Python than it is in .NET languages. .NET supports custom "equality comparers" that can override a type's Equals and GetHashCode functions. This is nice when you can't change the class you are hashing. That is why I am using a key selector in my code, here. Is there a better way of overriding the default hashing of a type without actually modifying its definition? I figured a requesting a key was the easiest way. -- http://mail.python.org/mailman/listinfo/python-list
Closures and Partial Function Application
I was a little disappointed the other day when I realized that closures were read-only. I like to use closures quite a bit. Can someone explain why this limitation exists? Secondly, since I can cheat by wrapping the thing being closure-ified, how can I write a simple wrapper that has all the same members as the thing (decorator), that then applies them to the underlying thing? I also like partial function application. What is the easiest way of achieving this in Python? Would it look something like this: def foo(x, y): return x + y xFoo = lambda y: foo(10, y) -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 1:18 pm, Chris Rebert wrote: > On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks wrote: > > I was a little disappointed the other day when I realized that > > closures were read-only. I like to use closures quite a bit. > > Assuming I'm intuiting your question correctly, then you're incorrect; > they are "read/write". You just need a `nonlocal` declaration for the > variables in question. Seehttp://www.python.org/dev/peps/pep-3104/ > andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl... > for details. > > Cheers, > Chris > > Cool. So I just need to put "nonlocal" in front of the variable name. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 1:51 pm, Travis Parks wrote: > On Aug 31, 1:18 pm, Chris Rebert wrote: > > > On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks > > wrote: > > > I was a little disappointed the other day when I realized that > > > closures were read-only. I like to use closures quite a bit. > > > Assuming I'm intuiting your question correctly, then you're incorrect; > > they are "read/write". You just need a `nonlocal` declaration for the > > variables in question. Seehttp://www.python.org/dev/peps/pep-3104/ > > andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl... > > for details. > > > Cheers, > > Chris > > Cool. So I just need to put "nonlocal" in front of the variable name. Am I doing something wrong, here? nonlocal isn't registering. Which version did this get incorporated? -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 2:18 pm, Ian Kelly wrote: > On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks wrote: > > Am I doing something wrong, here? nonlocal isn't registering. Which > > version did this get incorporated? > > 3.0 Ah, okay. It would be really useful for unit testing. Unfortunately, I want to make the code I am writing compatible with 2.x and 3.x. I will just deal with it until 3.x takes over. Glad to know Guido sees the importance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures and Partial Function Application
On Aug 31, 2:03 pm, "bruno.desthuilli...@gmail.com" wrote: > On 31 août, 18:45, Travis Parks wrote: > > > I was a little disappointed the other day when I realized that > > closures were read-only. I like to use closures quite a bit. > > They are not _strictly_ read only, but Python being first and foremost > an OO language, it's usually way simpler to use OO instead of closures > when you start needing such features. I like to leave OO to large-scale architectures and leave functional paradigms for implementation details. Writing an entire class for wrapping an int seems excessive. Especially if that code is limited to a small scope. I agree, though, that there is a time and a place for everything. -- http://mail.python.org/mailman/listinfo/python-list
Re: Handling 2.7 and 3.0 Versions of Dict
On Aug 31, 7:37 pm, Gregory Ewing wrote: > Ian Kelly wrote: > > if sys.version_info < (3,): > > getDictValues = dict.itervalues > > else: > > getDictValues = dict.values > > > (which is basically what the OP was doing in the first place). > > And which he seemed to think didn't work for some > reason, but it seems fine as far as I can tell: > > Python 2.7 (r27:82500, Oct 15 2010, 21:14:33) > [GCC 4.2.1 (Apple Inc. build 5664)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> gv = dict.itervalues > >>> d = {1:'a', 2:'b'} > >>> gv(d) > > > % python3.1 > Python 3.1.2 (r312:79147, Mar 2 2011, 17:43:12) > [GCC 4.2.1 (Apple Inc. build 5664)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> gv = dict.values > >>> d = {1:'a', 2:'b'} > >>> gv(d) > dict_values(['a', 'b']) > > -- > Greg My problem was that I didn't understand the scoping rules. It is still strange to me that the getValues variable is still in scope outside the if/else branches. -- http://mail.python.org/mailman/listinfo/python-list
Algorithms Library - Asking for Pointers
Hello: I am working on an algorithms library. It provides LINQ like functionality to Python iterators. Eventually, I plan on having feaures that work against sequences and mappings. I have the code up at http://code.google.com/p/py-compass. This is my first project in Python, so I'd like some feedback. I want to know if I am following conventions (overall style and quality of code). Thanks, Travis Parks -- http://mail.python.org/mailman/listinfo/python-list
Re: Handling 2.7 and 3.0 Versions of Dict
On Sep 2, 12:36 pm, "Gabriel Genellina" wrote: > En Wed, 31 Aug 2011 22:28:09 -0300, Travis Parks > escribi : > > > On Aug 31, 7:37 pm, Gregory Ewing wrote: > >> Ian Kelly wrote: > >> > if sys.version_info < (3,): > >> > getDictValues = dict.itervalues > >> > else: > >> > getDictValues = dict.values > > >> > (which is basically what the OP was doing in the first place). > > > My problem was that I didn't understand the scoping rules. It is still > > strange to me that the getValues variable is still in scope outside > > the if/else branches. > > Those if/else are at global scope. An 'if' statement does not introduce a > new scope; so getDictValues, despite being "indented", is defined at > global scope, and may be used anywhere in the module. > > -- > Gabriel Genellina > > Does that mean the rules would be different inside a function? -- http://mail.python.org/mailman/listinfo/python-list
Re: Algorithms Library - Asking for Pointers
On Sep 2, 4:09 pm, Ian Kelly wrote: > On Fri, Sep 2, 2011 at 10:59 AM, Travis Parks wrote: > > Hello: > > > I am working on an algorithms library. It provides LINQ like > > functionality to Python iterators. Eventually, I plan on having > > feaures that work against sequences and mappings. > > > I have the code up athttp://code.google.com/p/py-compass. > > > This is my first project in Python, so I'd like some feedback. I want > > to know if I am following conventions (overall style and quality of > > code). > > Sure, here are my comments. > > In the "forever" and "__forever" functions, your use of the term > "generator" is confusing. "__forever" is a generator function, > because it has a yield statement. Its argument, called "generator", > appears to be a callable, not a generator or even necessarily a > generator function. Also, note that __forever(lambda: value) is > functionally equivalent to the more efficient itertools.repeat(value). > > The staticmethod __next(iterator) accesses the class it is defined in, > which suggests that it might be better written as a classmethod > __next(cls, iterator). > > Each of the LINQ-style methods is divided into two parts: the public > method that contains the docstring and some argument checks, and a > private staticmethod that contains the implementation. I'm not > certain what the purpose of that is. If it's to facilitate overriding > the implementation in subclasses, then you need to change the names of > the private methods to start with only one _ character so that they > won't be mangled by the compiler. > > The comments before each method that only contain the name of the > immediately following method are redundant. > > aggregate: the default aggregator is unintuitive to me. I would make > it a required field and add a separate method called sum that calls > aggregate with the operator.add aggregator. > Also, the implementation doesn't look correct. Instead of passing in > each item to the aggregator, you're passing in the number of items > seen so far? The LINQ Aggregate method is basically reduce, so rather > than reinvent the wheel I would suggest this: > > # MISSING is a unique object solely defined to represent missing arguments. > # Unlike None we can safely assume it will never be passed as actual data. > MISSING = object() > > def aggregate(self, aggregator, seed=MISSING): > if seed is self.MISSING: > return reduce(aggregator, self._iterable) > else: > return reduce(aggregator, self._iterable, seed) > > Note for compatibility that in Python 3 the reduce function has been > demoted from a builtin to a member of the functools module. > > any: the name of this method could cause some confusion with the "any" > builtin that does something a bit different. > > compare: the loop would more DRY as a for loop: > > def __compare(first, second, comparison): > for firstval, secondval in itertools.izip_longest(first, second, > fillvalue=self.MISSING): > if firstval is self.MISSING: > return -1 > elif secondval is self.MISSING: > return 1 > else: > result = comparison(firstval, secondval) > if result != 0: > return result > return 0 > > concatenate: again, no need to reinvent the wheel. This should be > more efficient: > > def concatenate(self, other): > return extend(itertools.chain(self.__iterable, other)) > > equals: could be just "return self.compare(other, comparison) == 0" > > __last: the loop could be a for loop: > > # assume we're looking at the last item and try moving to the next > item = result.Value > for item in iterator: pass > return item > > lastOrDefault: there's a lot of repeated logic here. This could just be: > > def lastOrDefault(self, default=None): > try: > return self.last() > except ValueError: > return default > > map / forEach: .NET has to separate these into separate methods due to > static typing. It seems a bit silly to have both of them in Python. > Also, map would be more efficient as "return itertools.imap(mapper, > self.__iterable)" > > max / min: it would be more efficient to use the builtin: > def max(self, key): > return max(self.__iterable, key=key) > If somebody really needs to pass a comparison function instead of a > key function, they can use functools.cmp_to_key. > > randomSamples: a more canonical way to pass the RNG would be to pass > an instance o
Re: Algorithms Library - Asking for Pointers
On Sep 2, 6:49 pm, Travis Parks wrote: > On Sep 2, 4:09 pm, Ian Kelly wrote: > > > > > > > On Fri, Sep 2, 2011 at 10:59 AM, Travis Parks > > wrote: > > > Hello: > > > > I am working on an algorithms library. It provides LINQ like > > > functionality to Python iterators. Eventually, I plan on having > > > feaures that work against sequences and mappings. > > > > I have the code up athttp://code.google.com/p/py-compass. > > > > This is my first project in Python, so I'd like some feedback. I want > > > to know if I am following conventions (overall style and quality of > > > code). > > > Sure, here are my comments. > > > In the "forever" and "__forever" functions, your use of the term > > "generator" is confusing. "__forever" is a generator function, > > because it has a yield statement. Its argument, called "generator", > > appears to be a callable, not a generator or even necessarily a > > generator function. Also, note that __forever(lambda: value) is > > functionally equivalent to the more efficient itertools.repeat(value). > > > The staticmethod __next(iterator) accesses the class it is defined in, > > which suggests that it might be better written as a classmethod > > __next(cls, iterator). > > > Each of the LINQ-style methods is divided into two parts: the public > > method that contains the docstring and some argument checks, and a > > private staticmethod that contains the implementation. I'm not > > certain what the purpose of that is. If it's to facilitate overriding > > the implementation in subclasses, then you need to change the names of > > the private methods to start with only one _ character so that they > > won't be mangled by the compiler. > > > The comments before each method that only contain the name of the > > immediately following method are redundant. > > > aggregate: the default aggregator is unintuitive to me. I would make > > it a required field and add a separate method called sum that calls > > aggregate with the operator.add aggregator. > > Also, the implementation doesn't look correct. Instead of passing in > > each item to the aggregator, you're passing in the number of items > > seen so far? The LINQ Aggregate method is basically reduce, so rather > > than reinvent the wheel I would suggest this: > > > # MISSING is a unique object solely defined to represent missing arguments. > > # Unlike None we can safely assume it will never be passed as actual data. > > MISSING = object() > > > def aggregate(self, aggregator, seed=MISSING): > > if seed is self.MISSING: > > return reduce(aggregator, self._iterable) > > else: > > return reduce(aggregator, self._iterable, seed) > > > Note for compatibility that in Python 3 the reduce function has been > > demoted from a builtin to a member of the functools module. > > > any: the name of this method could cause some confusion with the "any" > > builtin that does something a bit different. > > > compare: the loop would more DRY as a for loop: > > > def __compare(first, second, comparison): > > for firstval, secondval in itertools.izip_longest(first, second, > > fillvalue=self.MISSING): > > if firstval is self.MISSING: > > return -1 > > elif secondval is self.MISSING: > > return 1 > > else: > > result = comparison(firstval, secondval) > > if result != 0: > > return result > > return 0 > > > concatenate: again, no need to reinvent the wheel. This should be > > more efficient: > > > def concatenate(self, other): > > return extend(itertools.chain(self.__iterable, other)) > > > equals: could be just "return self.compare(other, comparison) == 0" > > > __last: the loop could be a for loop: > > > # assume we're looking at the last item and try moving to the next > > item = result.Value > > for item in iterator: pass > > return item > > > lastOrDefault: there's a lot of repeated logic here. This could just be: > > > def lastOrDefault(self, default=None): > > try: > > return self.last() > > except ValueError: > > return default > > > map / forEach: .NET has to separate these into separate methods due to > > static typing. It seems a bit silly to have both of them in Python. &
Re: Algorithms Library - Asking for Pointers
On Sep 3, 12:35 am, Chris Torek wrote: > In article <18fe4afd-569b-4580-a629-50f6c7482...@c29g2000yqd.googlegroups.com> > Travis Parks wrote: > > >[Someone] commented that the itertools algorithms will perform > >faster than the hand-written ones. Are these algorithms optimized > >internally? > > They are written in C, so avoid a lot of CPython interpreter > overhead. Mileage in Jython, etc., may vary... > -- > In-Real-Life: Chris Torek, Wind River Systems > Intel require I note that my opinions are not those of WRS or Intel > Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 > email: gmail (figure it out) http://web.torek.net/torek/index.html I thought I would point out that many of the itertools functions change between 2.x and 3.x versions. Since 2.7 is supposed to be the last 2.x language, I suppose I will wait until 3.2 becomes the norm before I incorporate some of these changes. In the mean time, I will starting working on algorithms that work against Sequences. I think a really important lesson is that Python really doesn't need an algorithms library, like many others do. A lot of the common algorithms are supported by the syntax itself. All my library did was allow for easier function composition. -- http://mail.python.org/mailman/listinfo/python-list
mapLast, mapFirst, and just general iterator questions
I want to be able to apply different transformations to the first and last elements of an arbitrary sized finite iterator in python3. It's a custom iterator so does not have _reversed_. If the first and last elements are the same (e.g. size 1), it should apply both transforms to the same element. I'm doing this because I have an iterator of time span tuples, and I want to clamp the first and last elements, but know any/all of the middle values are inherently in range. A silly example might be a process that given an iterator of strings, chops the the outer characters off of the value, and uppercases the final value. For example: def iterEmpty(): return iter([]) def iter1(): yield "howdy" def iter2(): yield "howdy" yield "byebye" def iterMany(): yield "howdy" yield "hope" yield "your" yield "day" yield "is" yield "swell" yield "byebye" def mapFirst(stream, transform): try: first = next(stream) except StopIteration: return yield transform(first) yield from stream def mapLast(stream, transform): try: previous = next(stream) except StopIteration: return for item in stream: yield previous previous = item yield transform(previous) def main(): for each in (iterEmpty, iter1, iter2, iterMany): baseIterator = each() chopFirst = mapFirst(baseIterator, lambda x: x[1:-1]) andCapLast = mapLast(chopFirst, lambda x: x.upper()) print(repr(" ".join(andCapLast))) This outputs: '' 'OWD' 'owd BYEBYE' 'owd hope your day is swell BYEBYE' Is this idiomatic? Especially my implementations of mapFirst and mapList there in the middle? Or is there some way to pull this off that is more elegant? I've been doing more with iterators and stacking them (probably because I've been playing with Elixir elsewhere), I am generally curious what the performance tradeoffs of heavy use of iterators and yield functions in python is. I know the argument for avoiding big list copies when moving between stages. Is it one of those things where there's also some overhead with them, where for small stuff, you'd just be better list-ifying the first iterator and then working with lists (where, for example, I could do the first/last clamp operation with just indexing operations). -- https://mail.python.org/mailman/listinfo/python-list
What kind of "thread safe" are deque's actually?
A while ago I chose to use a deque that is shared between two threads. I did so because the docs say: "Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.” (https://docs.python.org/3.11/library/collections.html?highlight=deque#collections.deque) Earlier today, looking through some server logs I noticed that from time to I’m getting a RuntimeError: deque mutated during iteration I guess this surprised me. When I see “thread safe”, I don’t expect to get errors. Interestingly the error also only started showing up when I switched from running a statistics.mean() on one of these, instead of what I had been using, a statistics.median(). Apparently the kind of iteration done in a mean, is more conflict prone than a median? I’ve got a couple ways I can work around this. But I was surprised. -- https://mail.python.org/mailman/listinfo/python-list
Code Formatter Questions
I've been looking into using a code formatter as a code base size has grown as well as contributing developers. I've found and played with autopep, black, and yapf. As well as whatever pycharm has (which may just be gui preferences around one of those 3). I have 2 questions: 1) Are there any major other formatters that I can/should look at? I see some "online" pretty printers, but I'm after something I can run on whole recursive directories of code. 2) I use more and type annotations (at least at the trivial level). But I like to have variable annotations tightly bound to the identifier, kind of like a subscript. So I want to see def foo_bar(baz:int) -> bool: yak:str = 'howdy mates' And NOT def foo_bar(baz: int) -> bool: yak: str = 'howdy mates' In other cases though (dictionaries for example), I'm fine with (and prefer) the spacing. Is there anyway to make any of these formatters do this? We write a lot of Swift and Kotlin as well as which uses the same general syntax (identifier:Type) for type annotation, and we'd like to have some consistency across the styles (we pack the couplets in those two). -- https://mail.python.org/mailman/listinfo/python-list
Re: Canonical conversion of dict of dicts to list of dicts
> On Mar 30, 2021, at 12:11, Stestagg wrote: > > For completeness, from 3.5 onwards, you can also do the following: > > [{'name': n, **d} for n, d in dod.items()] > Reading through these, personally I like this one best. I'm curious what about it was enabled in 3.5? Was **kwarg expansion inside a dict literal not possible before then? Anyway, I like that it uses simple elemental parts that have been around a long long time in Python. -- https://mail.python.org/mailman/listinfo/python-list
Fun Generators
Doing an "industry experience" talk to an incoming class at nearby university tomorrow. Have a couple points where I might do some "fun things" with python. Said students have been learning some python3. I'm soliciting any *fun* generators people may have seen or written? Not so much the cool or clever ones. Or the mathematical ones (e.g. fib). Something more inane and "fun". But still showcasing generators uniqueness. Short and simple is good. Thanks in advance! -- https://mail.python.org/mailman/listinfo/python-list
Re: Fun Generators
> On Apr 23, 2021, at 05:55, Frank Millman wrote: > > On 2021-04-23 7:34 AM, Travis Griggs wrote: >> Doing an "industry experience" talk to an incoming class at nearby >> university tomorrow. Have a couple points where I might do some "fun things" >> with python. Said students have been learning some python3. >> I'm soliciting any *fun* generators people may have seen or written? Not so >> much the cool or clever ones. Or the mathematical ones (e.g. fib). Something >> more inane and "fun". But still showcasing generators uniqueness. Short and >> simple is good. >> Thanks in advance! > > Have you looked at this? > > http://www.dabeaz.com/generators/ > > Frank Millman > > -- > https://mail.python.org/mailman/listinfo/python-list I hadn't. But now I have. These are really cool. But not as whimsical/simple as I would have hoped. They're actually useful :) -- https://mail.python.org/mailman/listinfo/python-list
Polymorphic imports
I guess this is kind of like mocking for testing. I have a simple module that's imported in a number of other spots in my program. There's a condition in the OS/filesystem where I'd like to import a polymorphically compatible variant of the same module. Can this be accomplished in a sort of once-and-only once spot? For example, consider something like this: client/ module_a module_a_prime lib/ paths lib_a lib_b ... model/ model_a model_b ... top_level_a top_level_b ... I have a number of imports of module_a. I have a paths module that isolates all of my file system access, and that's where the determination can be made which one to use, so I tried to do something like: def dynamic_client_module(): return client.module_a_prime if the_condition_occurs else client.module_a Hoping that I could do something like from lib import paths import paths.dynamic_client_module() But this seems to not work. Import can only take real modules? Not programatic ones? Is there a Not-Too-Evil-Way(tm) to add a level of programmatic indirection in the import declarations? Or some other trick from a different angle? -- https://mail.python.org/mailman/listinfo/python-list
Re: Why don't we call the for loop what it really is, a foreach loop?
> On Sep 13, 2016, at 13:57, rgrigo...@gmail.com wrote: > > It would help newbies and prevent confusion. for each in ['cake'] + ['eat', 'it'] * 2: print(each) -- https://mail.python.org/mailman/listinfo/python-list
Re: Why doesn't Python include non-blocking keyboard input function?
> On Oct 25, 2016, at 5:55 AM, Chris Angelico wrote: > > On Tue, Oct 25, 2016 at 11:45 PM, Marko Rauhamaa wrote: >> Chris Angelico : >> >>> On Tue, Oct 25, 2016 at 11:09 PM, Marko Rauhamaa wrote: Blocking calls are evil. >>> >>> Oh, that's why. Got it. So because blocking calls are fundamentally >>> evil, we have to... what? What's so bad about them? Remember, not >>> every program is a server handling myriad clients. >> >> Myriads or not, we are talking about interactive (or reactive) programs. >> The paradigm of choice is event-driven programming. > > Have you watched "Tron"? A program goes to the I/O tower to receive a > message from the User. It's an active operation on the part of the > program. The user cannot initiate it, only the program can. > > Tron is extremely accurate in this way. Thanks for this ChrisA. Rest of this thread has been meh for me, but this one post, definitely won my MostValueablePost for the thread. :) Still chuckling. -- https://mail.python.org/mailman/listinfo/python-list
Most pythonic way to implement byte stuffing algorithm
I posted this on SO, but… yeah… I'm doing some serial protocol stuff and want to implement a basic byte stuffing algorithm in python. Though really what this really generalizes to is “what is the most pythonic way to transform one sequence of bytes where some bytes are passed through 1:1, but others are transformed to longer subsequences of bytes?” I’m pretty sure this rules out the use of transform() which expects a 1:1 mapping. So far, I've come with 5 different approaches, and each of them has something I don't like about it: 1 Via Generator def stuff1(bits): for byte in bits: if byte in _EscapeCodes: yield PacketCode.Escape yield byte ^ 0xFF else: yield byte This may be my favorite, but maybe just because I'm kind of fascinated by yield based generators. I worried that the generator would make it slow, but it's actually the second fastest of the bunch. 2 Simply bytes() def stuff2(bits): result = bytes() for byte in bits: if byte in _EscapeCodes: result += bytes([PacketCode.Escape, byte ^ 0xFF]) else: result += bytes([byte]) return result Constantly has to create single element arrays just to throw them out because I'm not aware of any "copy with one additional element" operation. It ties for the slowest of the bunch. 3 Use bytearray() def stuff3(bits): result = bytearray() for byte in bits: if byte in _EscapeCodes: result.append(PacketCode.Escape) result.append(byte ^ 0xFF) else: result.append(byte) return result Seems better than the direct bytes() approach. Actually slower than the yield generator and can do one byte at a time (instead of needing intermediate 1 element collections). But it feels brutish. It's middle of the pack performance. 4 BytesIO() def stuff4(bits): bio = BytesIO() for byte in bits: if byte in _EscapeCodes: bio.write(bytes([PacketCode.Escape, byte ^ 0xFF])) else: bio.write(bytes([byte])) return bio.getbuffer() I like the stream based approach here. But it is annoying that there doesn't seem to be something like a write1() API that could just add 1 byte, so I have to make those intermediate bytes again. If there was a "write single byte", I'd like this one. It ties for slowest. 5 Use replace() def stuff5(bits): escapeStuffed = bytes(bits).replace(bytes([PacketCode.Escape]), bytes([PacketCode.Escape, PacketCode.Escape ^ 0xFF])) stopStuffed= escapeStuffed.replace(bytes([PacketCode.Stop]), bytes([PacketCode.Escape, PacketCode.Stop ^ 0xFF])) return stopStuffed.replace(bytes([PacketCode.Start]), bytes([PacketCode.Escape, PacketCode.Start ^ 0xFF])) This is the fastest. But I don't like the way the code reads and the intermediate sweeps. -- https://mail.python.org/mailman/listinfo/python-list
Re: Most pythonic way to implement byte stuffing algorithm
> On Apr 17, 2018, at 11:15 AM, MRAB wrote: > > On 2018-04-17 17:02, Travis Griggs wrote: >> I posted this on SO, but… yeah… >> I'm doing some serial protocol stuff and want to implement a basic byte >> stuffing algorithm in python. Though really what this really generalizes to >> is “what is the most pythonic way to transform one sequence of bytes where >> some bytes are passed through 1:1, but others are transformed to longer >> subsequences of bytes?” I’m pretty sure this rules out the use of >> transform() which expects a 1:1 mapping. > [snip] > There are only 256 possible input bytes, so just put them into a dict and > look them up. > -- > https://mail.python.org/mailman/listinfo/python-list So something like this? LUT = list(bytes([x]) for x in range(256)) LUT[PacketCode.Escape] = bytes([PacketCode.Escape, PacketCode.Escape ^ 0xFF]) LUT[PacketCode.Start] = bytes([PacketCode.Escape, PacketCode.Start ^ 0xFF]) LUT[PacketCode.Stop] = bytes([PacketCode.Escape, PacketCode.Stop ^ 0xFF]) def stuff6(bits): return b''.join(LUT[x] for x in bits) -- https://mail.python.org/mailman/listinfo/python-list
Simplest way to clobber/replace one populated directory with another?
I have a directory structure that might look something like: Data Current A B C Previous A X In as simple/quick a step as possible, I want to rename Current as Previous including the contents and wiping out the original such that it is now: Data Previous A B C I've tried something like: from pathlib import Path src = Path('Data/Current’) dest = Path('Data/Previous’) src.replace(dest) The docs led me to hope this would work: "If target points to an existing file or directory, it will be unconditionally replaced.” But it *does* appear to be conditional. I get a "Directory not empty" exception. I guess I could recursively delete the ‘Previous' directory first. Is that basically the only solution? Or is there a better way to achieve this? (I prefer `pathlib`, but if `os` or `shutil` is the better hammer here, I'm not opposed to them) (I am running on Linux) -- https://mail.python.org/mailman/listinfo/python-list
Google weirdness
I somehow managed to trigger the dialog below by typing in a certain Python phrase to Google. Anyone know what it's about? It shows up in what appears to be terminal screen. Viz: Google has a code challenge ready for you. Been here before? This invitation will expire if you close this page. Success! You've managed to infiltrate Commander Lambda's evil organization, and finally earned yourself an entry-level position as a Minion on her space station. From here, you just might be able to subvert her plans to use the LAMBCHOP doomsday device to destroy Bunny Planet. Problem is, Minions are the lowest of the low in the Lambda hierarchy. Better buck up and get working, or you'll never make it to the top... For a list of commands type help. To get started with your first challenge type request. foobar:~/ guest$ -- https://mail.python.org/mailman/listinfo/python-list
Re: Learning Python (or Haskell) makes you a worse programmer
> On Mar 30, 2016, at 2:36 PM, Gregory Ewing > wrote: > > Tim Golden wrote: > >> (I don't know how other English-speaking groups say the word, but in >> England the first syllable is stressed and the second is the >> conventional short "uh" sound). > > I can attest that New Zealand follows the UK on this. I was > surprised when I first heard an American pronounce it too. > > The curious can hear the difference on these pages: > > British: http://www.oxforddictionaries.com/definition/english/python > American: http://www.dictionary.com/browse/python?s=t That does it. If I ever make some sort of open source module for pythun/pythawn I’ll be sure to call it either tuhmayto/tomawto. Or maybe I’ll call it puhtayto/potawto. -- https://mail.python.org/mailman/listinfo/python-list
More elegant way to avoid this hacky implementation of single line reduce for grouping a collection?
Yesterday, I was pondering how to implement groupby, more in the vein of how Kotlin, Swift, Objc, Smalltalk do it, where order doesn’t matter. For example: def groupby(iterable, groupfunc): result = defaultdict(list) for each in iterable: result[groupfunc(each)].append(each) return result original = [1, 2, 3, 4, 5, 1, 2, 4, 2] groupby(original, lambda x: str(x)) ==> {‘1’: [1, 1], ‘2’: [2, 2, 2], ‘3’: [3], ‘4’: [4, 4], ‘5’: [5]} Easy enough, but I found myself obsessing about doing it with a reduce. At one point, I lost sight of whether that was even a better idea or not (the above is pretty simple); I just wanted to know if I could do it. My naive attempt didn’t work so well: grouped = reduce( lambda grouper, each: grouper[str(each)].append(each), allValues, defaultdict(list)) Since the result of the append() function is None, the second reduction fails, because the accumulator ceases to be a dictionary. I persisted and came up with the following piece of evil, using a tuple to move the dict reference from reduction to reduction, but also force the (ignored) side effect of updating the same dict: grouped = reduce( lambda accum, each: (accum[0], accum[0][str(each)].append(each)), allValues, (defaultdict(list), None))[0] My question, only for the sake of learning python3 fu/enlightenment, is there a simpler way to do this with a reduce? I get there’s lots of way to do a groupby. The pursuit here is what’s the simplest/cleverest/sneakiest way to do it with reduce, especially if the quality that gorupfunc (str() in this example) is only called once per item is persevered. -- https://mail.python.org/mailman/listinfo/python-list
How do/can I generate a PKCS#12 file the cryptography module?
I’m using the cryptography module (https://cryptography.io/en/latest/) to try and generate some cert/key/identities. It's pretty easy using said module to generate the contents of .pem file for a private key: keyPEMBytes = privateKey.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption()) It’s also easy to generate the contents of a .cer/.pem file for an associated cert: certBytes = certificate.public_bytes(encoding=serialization.Encoding.PEM) But I need them (and their chain) balled up on a single .p12 (PKCS12) file. Said module documents how to parse/consume PKCS12 formats, but nothing (that I can find) about how one can generate them. My understanding of PKI stuff is hit and miss though, so maybe I'm just not searching the right keyword in the documentation? I can create the .p12 file at the command line on Linux using openssl pkcs12 -export -out myIdentity.p12 -inkey myPrivKey.pem -in myCert.crt -certfile myCertChain.crt So I could just wrap calls like this with subprocess/cmd and mess with tempfiles/pipes. I was hoping to keep it all in memory/python though. Is there a different python TLS library that I should be considering, that can do this? (stack overflow version if you’re into the points and all that: https://stackoverflow.com/questions/54677841/how-do-can-i-generate-a-pkcs12-file-using-python-and-the-cryptography-module) -- https://mail.python.org/mailman/listinfo/python-list
ANN: SciPy Core (Numeric Python Replacement) Version 0.4.X (beta) released
Background: Numeric is an add-on Python module that has seen widespread adoption. It enables Python to be used as a Scientific Computing Environment similar to MATLAB or IDL. Numeric was originally written nearly 10 years ago, and while still performing admirably needed much updating to take advantage of the new features in Python and to remove old warts. SciPy Core 0.4.1 (beta) SciPy Core is a new system which builds on top of Numeric, but implements features (such as advanced index-selection, and user-settable error modes). There are over 20 major new features over Numeric. The LICENSE is still a BSD style License---the same as old Numeric. More information can be found at the web-site: http://numeric.scipy.org The primary developer of scipy core (besides the original creators of Numeric upon which it is based) is Travis Oliphant ([EMAIL PROTECTED]), but his work received ideas and support from a wide cast of community members including: Pearu Peterson, Robert Kern, Perry Greenfield, Eric Jones, John Hunter, Fernando Perez, Konrad Hinsen, and Paul Dubois. These individuals should not be held responsible for any bugs remaining in the code. -- http://mail.python.org/mailman/listinfo/python-list
segfault when calling Python from C thread
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I call the callback from the parent process, it works fine. The PyObject is static, and holds the same value in both Parent and thread, so I'm at a loss as to what would be different in the pthread from the parent that would cause a segfault on the callback. The machine specifics are an x86 intel processor with RedHat linux. Here is some clips of the C callback code showing how I'm storing the callback, then what the actual callback function is like. Any ideas? The function being called is a simply to display the string of text, and execution never seems to reach back to the Python code at all. Thanks, Travis B. /* callback function to the Python code */ static PyObject * my_callback = NULL; /* setting callback function */ static PyObject * my_set_callback(PyObject *dummy, PyObject *args) { PyObject *result = NULL; PyObject *temp; PyObject *arglist; if (PyArg_ParseTuple(args, "O:set_callback", &temp)) { if (!PyCallable_Check(temp)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } Py_XINCREF(temp); /* Add a reference to new callback */ Py_XDECREF(my_callback); /* Dispose of previous callback */ my_callback = temp; /* Remember new callback */ /* return "None" */ Py_INCREF(Py_None); result = Py_None; } return result; } /* calling callback */ void callback(char * str) { PyObject *arglist; PyObject *result; if(str == NULL) return; if(my_callback == NULL) { printf("no callback function provided, returning...\n"); return; } /* Time to call the callback */ arglist = Py_BuildValue("(s)", str); result = PyEval_CallObject(my_callback, arglist); Py_DECREF(arglist); if(result == NULL) return; Py_DECREF(result); } -- http://mail.python.org/mailman/listinfo/python-list
Nevow examples
There was a request for nevow examples. Nevow is a fantastic web-development framework for Python. I used nevow to create http://www.scipy.org/livedocs/ This site uses nevow and self introspection to produce (live) documentation for scipy based on the internal docstrings. It would be nice to add the capability for users to update the documentation through the web-site. But, that functionality is not complete. The code itself is available in the util directory of scipy which can be checked out of CVS (or browsed). Go to http://www.scipy.org for mor details. -Travis Oliphant -- http://mail.python.org/mailman/listinfo/python-list
Distutils spawn on unix acting strange
I have a normal looking setup.py file with a single extension module. When distutils runs (python setup.py build), the module compiles fine, but an error is issued that seems to indicate that gcc is being called with a "blank" input file (and gives an error). It appears that the spawn process inside of distutils is generating two calls: one that succeeds in compiling the module (it takes a while to compile) and another that is giving an error. Here is a typical output: unning install running build running build_py creating build creating build/lib.linux-i686-2.3 creating build/lib.linux-i686-2.3/ndarray copying Lib/numeric.py -> build/lib.linux-i686-2.3/ndarray copying Lib/numeric_version.py -> build/lib.linux-i686-2.3/ndarray copying Lib/numerictypes.py -> build/lib.linux-i686-2.3/ndarray copying Lib/array_printer.py -> build/lib.linux-i686-2.3/ndarray copying Lib/__init__.py -> build/lib.linux-i686-2.3/ndarray running build_ext building 'ndarray/multiarray' extension creating build/temp.linux-i686-2.3 creating build/temp.linux-i686-2.3/Src cc -fno-strict-aliasing -DNDEBUG -O2 -fomit-frame-pointer -pipe -march=i586 -mtune=pentiumpro -g -fPIC -DSIZEOF_LONG_DOUBLE=12 -IInclude -I/usr/include/python2.3 -c Src/multiarraymodule.c -o build/temp.linux-i686-2.3/Src/multiarraymodule.o cc: : No such file or directory [snip] error: command 'cc' failed with exit status 1 The error is apparently coming from cc (gcc) which states "no such file or directory", but there is no file given so apparently cc is being called with a "blank" file (not just no file, but a "blank" file). The trouble is, the module is actually compiling fine (I can run python setup.py install again and it finds the recent build and goes forward). I also don't get the mysterious errror when I just cut-and-paste the compile line. I am very confused. Has anyone seen this or anything like this before? Any help appreciated. -Travis Oliphant -- http://mail.python.org/mailman/listinfo/python-list
How to install pip for python3 on OS X?
OSX (Mavericks) has python2.7 stock installed. But I do all my own personal python stuff with 3.3. I just flushed my 3.3.2 install and installed the new 3.3.3. So I need to install pyserial again. I can do it the way I've done it before, which is: Download pyserial from pypi untar pyserial.tgz cd pyserial python3 setup.py install But I'd like to do like the cool kids do, and just do something like pip3 install pyserial. But it's not clear how I get to that point. And just that point. Not interested (unless I have to be) in virtualenv yet.-- https://mail.python.org/mailman/listinfo/python-list
Re: How to install pip for python3 on OS X?
On Nov 20, 2013, at 6:01 AM, Mark Lawrence wrote: > On 20/11/2013 06:55, Travis Griggs wrote: >> OSX (Mavericks) has python2.7 stock installed. But I do all my own >> personal python stuff with 3.3. I just flushed my 3.3.2 install and >> installed the new 3.3.3. So I need to install pyserial again. > > Just idle curiosity but why do you have to do this? On Windows I just whack > 3.3.3 over the top of 3.3.2, job done. I think in this case, it was a chance to clean house, and maybe up the “tools” game (e.g. use pip) instead of what I had been doing. So you’re correct. The flushing of 3.3.2 was more that I *wanted* to, instead of *needing* to. (aside. I do not use GoogleGroups, but have been accused of somehow sending email that looks like I do. Does this email look like that?) -- https://mail.python.org/mailman/listinfo/python-list
Re: How to install pip for python3 on OS X?
On Nov 19, 2013, at 11:27 PM, Ned Deily wrote: > In article <6856a21c-57e8-4cdd-a9e8-5dd738c36...@gmail.com>, > Travis Griggs wrote: > >> OSX (Mavericks) has python2.7 stock installed. But I do all my own personal >> python stuff with 3.3. I just flushed my 3.3.2 install and installed the new >> 3.3.3. So I need to install pyserial again. I can do it the way I've done it >> before, which is: >> >> Download pyserial from pypi >> untar pyserial.tgz >> cd pyserial >> python3 setup.py install >> But I'd like to do like the cool kids do, and just do something like pip3 >> install pyserial. But it's not clear how I get to that point. And just that >> point. Not interested (unless I have to be) in virtualenv >> yet.- > > http://www.pip-installer.org/en/latest/installing.html > > # download and install setuptools > curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py > python3 ez_setup.py > # download and install pip > curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py > python3 get-pip.py > # use pip to install > python3 -m pip install pyserial > # Don't want it? > python3 -m pip uninstall pyserial > > -- > Ned Deily, > n...@acm.org Ned, Thank you! Belatedly. I’ve had some fires to put out at work. And have gotten back to this, and this is exactly what I was looking for. I added the additional step of: cd /usr/local/bin ln -s ../../../Library/Frameworks/Python.framework/Versions/3.3/bin/pip pip Works, like a charm. (aside. I do not use GoogleGroups, but have been accused of somehow sending email that looks like I do. Does this email look like that?) -- https://mail.python.org/mailman/listinfo/python-list
Re: tkinter bug on mac maverick python 3.3.3
On Nov 27, 2013, at 3:32 AM, Dan Wissme wrote: > Hi ! > Am I the only one to get a bug in GUIs using tkinter on my Mac under maverick > and Python 3.3.3 ? > When will they get rid of Tcl/Tk which causes recurrent problems at almost > each new Python version ! > Please, for the rest of us... I’m curious, if they get rid of Tcl/Tk as you wished, what do you propose replacing it with? It’s not like there are other “light weight cross platform ui frameworks” that are obvious replacements. Most of the others are far heavier. And all have issues with their purported cross platformness. Or are you proposing that Tcl/Tk be moved out of the python core distro, and instead delivered as a separate package? If *this* is your proposal, I wholeheartedly agree. Just the other day, I was working on putting python3 on a beaglebone black (similar to a raspberry pi). It built OK, but I had to ignore lots of warnings about Tcl/Tk not working, which of course was a “duh”. I was surprised, that among some, there’s a sentiment that python core MUST include it. Which was interesting. One of the core principles of this language is all about modules and modularity. Why can’t the Tcl/Tk module be the same as numpy and scipy and many of the other widely installed-after-the-fact-as-appropriate packages? -- https://mail.python.org/mailman/listinfo/python-list
Re: Managing Google Groups headaches
Sent from my iPhone > On Nov 28, 2013, at 7:40, Michael Torrie wrote: > >> On 11/28/2013 08:08 AM, Chris Angelico wrote: >> Which is easier, fiddling around with your setup so you can post >> reasonably on Google Groups, or just getting a better client? With >> your setup, you have to drop out to another editor and press F9 for it >> to work. With pretty much any other newsreader on the planet, this >> works straight off, no setup necessary. >> >> I'm still going to advise people to stop using buggy rubbish. > > My opinion is that the Python list should dump the Usenet tie-in and > just go straight e-mail. Python is the only list I'm on that has a > usenet gateway. > > I used to love usenet back in the day, but in the present internet > climate makes it unworkable, though I concede that e-mail is reaching > the end of its usefulness as well. > > I wouldn't oppose a dual e-mail list and web-based forum system, > provided the forum system supported threaded conversations in a clean > and useful way (maybe like google wave used to). > -- > https://mail.python.org/mailman/listinfo/python-list Here! Here! Well said and amen. My thoughts exactly. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python for microcontrollers
On Dec 3, 2013, at 6:18 AM, Colin J. Williams wrote: > On 03/12/2013 7:58 AM, Mark Lawrence wrote: >> I thought this might be of interest >> Http://www.kickstarter.com/projects/214379695/micro-python-python-for-microcontrollers >> >> > Is this intended to be better than the Raspberry PI? RPi handles Python 2 or > 3. > > How would it differ? IMO, a whole different class of computer. From that page, the board they’re targeting “... clocked at 168MHz and has 1MiB flash and 192KiB RAM.” They’re running OS-less. The Pi, on the other hand actually runs a full OS (Linux) and has specs like 700 MHz, 512MB Ram, and an sd card for storage which means you’re going to have to work hard to find something as small as 2G, the sweet price point is going to actually give you 8G. Whether or not you go for their board, they’re targeting a compute environment that is 5x-ish slower, has at least 2000x less storage space, and works with about a thousandth of the ram of a Pi. Having forayed into the world of small small micro controllers myself this last year and a half, I’m kind of torn on whether this is a good idea or not. But I think it’s cool they’re trying. And I’d definitely try it to see how it worked out. -- https://mail.python.org/mailman/listinfo/python-list
Re: Managing Google Groups headaches
On Dec 4, 2013, at 6:52 AM, Rich Kulawiec wrote: > Yes, I'm > aware of web forums: I've used hundreds of them. They suck. They ALL > suck, they just all suck differently. I could spend the next several > thousand lines explaining why, but instead I'll just abbreviate: they > don't handle threading, they don't let me use my editor of choice, > they don't let me build my own archive that I can search MY way including > when I'm offline, they are brittle and highly vulnerable to abuse > and security breaches, they encourage worst practices in writing > style (including top-posting and full-quoting), they translate poorly > to other formats, they are difficult to archive, they're even more > difficult to migrate (whereas Unix mbox format files from 30 years ago > are still perfectly usable today), they aren't standardized, they > aren't easily scalable, they're overly complex, they don't support > proper quoting, they don't support proper attribution, they can't > be easily forwarded, they...oh, it just goes on. My point being that > there's a reason that the IETF and the W3C and NANOG and lots of other > groups that could use anything they want use mailing lists: they work. One of the best rants I’ve ever read. Full mental harmonic resonance while I read this. Hope you don’t mind, but I think I’ll be plagiarizing your comments in the future. Maybe I’ll post it on a couple of the web forums I currently have the luxury of regularly hating. -- https://mail.python.org/mailman/listinfo/python-list
Re: Packaging a proprietary Python library for multiple OSs
On Dec 5, 2013, at 2:56 AM, rusi wrote: > 3. https://groups.google.com/forum/#!forum/python-virtualenv may be a better > place to ask Am I the only one that sees the irony in this suggestion? Given the long running tirades^H^H^H^H^H^H thread about “Managing Google Groups headaches”? “Pleassse don’t use Google Groupesss. It’sss nasssty. It hurtssess our eyessse with itsss long lineieesss. Unless it ha a ssspecial ned. Then the groupssesss are OK, Ye?" -- https://mail.python.org/mailman/listinfo/python-list
Meta Fight About Posting (was: python programming help)
On Dec 9, 2013, at 1:34 AM, Mark Lawrence wrote: > On 09/12/2013 05:07, ru...@yahoo.com wrote: >> On 12/08/2013 05:27 PM, Mark Lawrence wrote: >>> On 09/12/2013 00:08, ru...@yahoo.com wrote: On 12/08/2013 12:17 PM, Chris Angelico wrote: > On Mon, Dec 9, 2013 at 6:06 AM, wrote:>[...] >>> [...] >>> To the OP, please ignore the above, it's sheer, unadulterated rubbish. >>> Nobody has ever been bullied into doing anything. People have however >>> been asked repeatedly to either A) use the link referenced above to >>> avoid sending double spaced crap here from the inferior google groups >>> product or B) use an alternative technology that doesn't send double >>> spaced crap. >> >> Mark, I appreciate your calm and reasonable requests for people >> to checkout the page you gave a link to, that's why I repeated >> your advice. It is also why I responded to Chris and not to you. >> >> However it does not change the fact that people here have responded >> in rather extreme way to GG posts including calling GG users "twits" >> and claiming GG posts damage their eyesight, as well as repeatedly >> denying the obvious fact that GG is much easier to use for many than >> to subscribe to a usenet provider or to a mailing list. One frequently >> sees words like "crap", "slimy", "rubbish" etc to describe GG posts >> which is pretty intimating to people who just want some help with a >> python question using a tool they already know how to use and have >> had no complaints about in other places. >> > > Well you can ask iMath, amongst others, not to send double spaced google > nonsense. They've been asked repeatedly, politely, but apparently have no > consideration at all for people who have no interest in seeing this ill > formed dross spread throughout web land. As long as we’re in full scale rant drift, I’d like to remind others of the time honored tradition of changing the post subject, when, er, uh, the subject changes. Because this obviously is not "programming help" anymore. The python mailing list is the only one I know of that is cross posted between 3 different technologies. Maybe it’s an outgrowth of the “multi paradigm” philosophy of python or something. It would be an interesting experiment, to shut down the cross forum replication engines for a month. Personally, I think they should each thrive, or die, on their own. If there’s enough mass on the groups to answer the occasional one off question, it’ll go on, indifferent of the existence of the mailing list. Comp.lang.python can truly become a troll haven. :) And the mailing list can be for the more thorough threads, or something. If you’re worried about “fragmentation”… these weekly rants seem to indicate it’s happened anyway, and the impedance mismatch between styles/technologies/formats is generating more heat from friction than it is contributing light to the cross-sharing. Besides, there’s nothing stopping periodic posts being sent to any of the sites saying “by the way, did you know there’s also a mailing list…” The nice thing about doing it for a month (or so), is that it’s not a “huge disturbance in the force.” If it stinks, you turn them back on in a month (or so). If you’re still not sold, and find yourself solidly in the “keep it all together” group, I propose, we embrace that idea, and set up a bi-directional engine between the IRC channel (which I’ve found very helpful often) and the mailing list. -- https://mail.python.org/mailman/listinfo/python-list
Re: grab dict keys/values without iterating ?!
On Dec 11, 2013, at 5:31 AM, rusi wrote: > > The classic data structure for this is the trie: > General idea: http://en.wikipedia.org/wiki/Trie > In python: > http://stackoverflow.com/questions/11015320/how-to-create-a-trie-in-python/ My thoughts exactly! If you wade through the comments there, someone has done a more-than-naive implementation here: https://github.com/kmike/marisa-trie The write up makes it look pretty favorable as well for performance (scroll 2/3s down to the Benchmarks section). -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie question. Are those different objects ?
On Dec 20, 2013, at 8:00 AM, Mark Lawrence wrote: > A good point. Shall I write a PEP asking for a language change which > requires that that stupid = sign is replaced by a keyword reading something > like thenameonthelefthandsideisassignedtheobjectontherighthandside ? Or a symbol like :=. As a former Smalltalker, I still miss this as the assignment operator, and the “gets” verbiage that went along with it. One said: x := 4 as in “x gets 4” I always got a kick out of the following paragraph from http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html. "1970 - Niklaus Wirth creates Pascal, a procedural language. Critics immediately denounce Pascal because it uses "x := x + y" syntax instead of the more familiar C-like "x = x + y". This criticism happens in spite of the fact that C has not yet been invented." -- https://mail.python.org/mailman/listinfo/python-list
Re: cascading python executions only if return code is 0
On Dec 22, 2013, at 10:37 AM, Frank Cui wrote: > hey guys, > > I have a requirement where I need to sequentially execute a bunch of > executions, each execution has a return code. the followed executions should > only be executed if the return code is 0. is there a cleaner or more pythonic > way to do this other than the following ? > > if a() == 0: > if b() == 0: > c() I know I’m a little late to the game on this one. Good answers and interesting discussions (and sometimes not). In some situations, I might do something like the following. It depends on just how many functions your cascading, and how arbitrary or pluggable those are. If your model is that you have a sort of todo list of functions to execute (more than 3 or so), you might want to separate the definition of that run list from the logic that executes them. Given something like: def a(): print(‘Aye’) return 0 def b(): print(‘Bee’) return 0 def c(): print(’See’) return 0 def d(): print(‘Dee’) return 1 def e(): print(‘Eee’) return 1 You do the nested if as you original proposed or the chained or as also proposed. Or you could put the commands in a list: script = [ a, b, c, d, e] This gives you a nice succinct list of stuff that needs to be done, not to different if you’d just coded it with no or logic, e.g. a b c d e Refactoring/evolving them then feels the same. To run the script we could do it the good ol’ C'ish way: for step in script: if step() != 0: break But we have more functional style stuff via builtins which can capture that pattern: ranToEnd = all(step() == 0 for step in script) So IF you’re use case lends itself to separation of the “list of stuff to do” and the “logic to execute said list”, then… this approach might be appealing. Travis Griggs -- https://mail.python.org/mailman/listinfo/python-list
So, what's the real story on Python 2 vs Python 3?
The Python.org site says that the future is Python 3, yet whenever I try something new in Python, such as Tkinter which I am learning now, everything seems to default to Python 2. By this I mean that, whenever I find that I need to install another package, it shows up as Python 2 unless I explicitly specify Python 3. What's the deal? If I want to make a distributable software package, should it be 2 or 3? Enquiring minds want to know. -- https://mail.python.org/mailman/listinfo/python-list
Python in the news
From Twitter: RT @cjbrummitt Python kills security guard at Sanur Hyatt, Bali (Ind). bit.ly/1fLCWvn < bad coding has CONSEQUENCES, ppl! -- https://mail.python.org/mailman/listinfo/python-list
PySerial for Python 2 vs. Python 3
I've been working with a simple serial device that attaches to a USB port. It takes as commands short strings. I wanted to use PySerial under Python 3, and, of course had the Devil's own time getting it installed and working since everything is geared towards Python 2. Anyway, I finally got it installed, but when I try to use a statement of the sort ser.write("string") I get an exception which seems to imply that the argument needs to be an integer, rather than a string. With some minor adjustments it works just fine under Python 2, so, in a sense, this is a non-issue. However, I'd be interested to hear from anyone who can comment on what the problem is. Thanks, Travis -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3.x adoption
Here we go again… On Jan 14, 2014, at 11:33 AM, Staszek wrote: > Hi > > What's the problem with Python 3.x? It was first released in 2008, but > web hosting companies still seem to offer Python 2.x rather. > > For example, Google App Engine only offers Python 2.7. > > What's wrong?... Maybe what it means is that Python3 is just fine, but Google App Engine isn’t seeing a lot of development/improvement lately, that it’s just in maintenance mode. Imagine that, Google not finishing/maintaining something. I wish amongst the periodic maelstroms of Python2 vs Python3 handwringing, people would look at the new project starts. When I work with someone’s old library that they’ve moved on from, I use python2 if I have to, but anytime I can, I use python3. Personally, I wish they’d start python4, sure would take the heat out of the 3 vs 2 debates. And maybe there’d be a program called twentyfour as a result. -- https://mail.python.org/mailman/listinfo/python-list
Re: 'Straße' ('Strasse') and Python 2
On Jan 15, 2014, at 4:50 AM, Robin Becker wrote: > On 15/01/2014 12:13, Ned Batchelder wrote: > >>> On my utf8 based system >>> >>> robin@everest ~: $ cat ooo.py if __name__=='__main__': import sys s='A̅B' print('version_info=%s\nlen(%s)=%d' % (sys.version_info,s,len(s))) robin@everest ~: $ python ooo.py version_info=sys.version_info(major=3, minor=3, micro=3, releaselevel='final', serial=0) len(A̅B)=3 robin@everest ~: $ >>> >>> > >> You are right that more than one codepoint makes up a grapheme, and that >> you'll >> need code to deal with the correspondence between them. But let's not muddy >> these already confusing waters by referring to that mapping as an encoding. >> >> In Unicode terms, an encoding is a mapping between codepoints and bytes. >> Python >> 3's str is a sequence of codepoints. >> > Semantics is everything. For me graphemes are the endpoint (or should be); to > get a proper rendering of a sequence of graphemes I can use either a sequence > of bytes or a sequence of codepoints. They are both encodings of the > graphemes; what unicode says is an encoding doesn't define what encodings are > ie mappings from some source alphabet to a target alphabet. But you’re talking about two levels of encoding. One runs on top of the other. So insisting that you be able to call them all encodings, makes the term pointless, because now it’s ambiguous as to what you’re referring to. Are you referring to encoding in the sense of representing code points with bytes? Or are you referring to what the unicode guys call “forms”? For example, the NFC form of ‘ñ’ is ’\u00F1’. ‘nThe NFD form represents the exact same grapheme, but is ‘\u006e\u0303’. You can call them encodings if you want, but I echo Ned’s sentiment that you keep that to yourself. Conventionally, they’re different forms, not different encodings. You can encode either form with an encoding, e.g. '\u00F1'.encode('utf8’) '\u00F1'.encode('utf16’) '\u006e\u0303'.encode('utf8’) '\u006e\u0303'.encode('utf16') -- https://mail.python.org/mailman/listinfo/python-list
Re: 'StraÃe' ('Strasse') and Python 2
On Jan 16, 2014, at 2:51 AM, Robin Becker wrote: > I assure you that I fully understand my ignorance of ... Robin, donât take this personally, I totally got what you meant. At the same time, I got a real chuckle out of this line. That beats âarmy intelligenceâ any day. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3.x adoption
Looks like the 2/3 topic has lain fallow for a couple of days, gotta keep it burning… I’m a relatively recent python convert, but been coding and talking to others about coding for many moons on this big blue orb. I think the industrial side of this debate has been talked up quite a bit. We have tools, we have the wall of shame/superpowers for libraries and projects. I think the desires of the core of people moving python forward are pretty clear to those of us that plug in. Move to 3. Period. We can debate, hate it, go on all day, but they’ve been pretty steady. I’ve had a bunch of interns around me lately though, wanting to get into python, and this is where I find the momentum really breaks down. If newcomers go to take an online course in python, they might try MIT’s Open Courseware (who doesn’t want to learn from the illustrious MIT after all?). They’ll be taught Python 2, not 3. Or they might try Code Academy. Again, they’ll be taught 2, not 3. If the newbie googles “python reference”… top link will be python 2. So in my mind, the wall of superpowers/shame is no longer well aligned with where the real battlefront of adoption is at. The legacy of the internet caches and education sites are. Personally, I have no idea why an education site would favor a version that sooner or later they’re going to have to try and explain how super() works. The other area, I think, that puts a dent in perceived adoption is in alternate interpreters. Back in the day, everyone was making some branch of python (e.g. IronPython, Jython, Cython, PyPy, Stackless, etc). All of them did python 2. Very few are doing python 3. Some have been abandoned (as is the nature of research endeavors like these were), but there doesn’t seem to be the broad swath of people still building alternate python expressions, especially in python 3. Being a fan of JIT, I have big hopes for PyPy, I can’t figure out why they aren’t pitching their “cutting edge” interpreter, for the “cutting edge” version of python. There should be a wall of superpowers/shame for interpreters. -- https://mail.python.org/mailman/listinfo/python-list
Re: PyWart: More surpises via "implict conversion to boolean" (and other steaming piles!)
On Feb 10, 2014, at 10:30 PM, Steven D'Aprano wrote: >> >>1. Parenthesis should not be required for parameter- less functions. > > Of course they should. Firstly, parameter-less functions are a code- > smell, and ought to be discouraged. Secondly, even if you have a good > reason for using one -- for example, random.random -- then the difference > between referring to the object and calling the object should be clear. Interesting. Can you clarify or provide some links to the "parameter-less functions are a code-smell” bit? I agree with your points about consistency. I disagree with the original poster that niladic functions should have a different syntax than the others. I empathize with him, I’ve made the same mistake before (being an ardent Smalltalker in the past, it’s an easy habit to have bite you). But the consistency is more important. And in python, things “happen” when parentheses appear. I just accept that. OTOH, I’m not sure I’ve heard the parameters-less functions are a code one? Is it just loose functions that you’re referring to? As opposed to methods (which are just bound functions)? I could maybe accept that. But methods with fewer arguments, and even none, are a desirable thing. There are code smells that are the opposite in fact, methods with long parameter lists are generally seen as code smell (“passing a paragraph”). Anyway, I’d love to understand better what you see as the code smell and why. -- https://mail.python.org/mailman/listinfo/python-list
Re: PyWart: More surpises via "implict conversion to boolean" (and other steaming piles!)
On Feb 11, 2014, at 7:52 AM, Chris Angelico wrote: > On Wed, Feb 12, 2014 at 2:36 AM, Travis Griggs wrote: >> OTOH, I’m not sure I’ve heard the parameters-less functions are a code one? >> Is it just loose functions that you’re referring to? As opposed to methods >> (which are just bound functions)? I could maybe accept that. But methods >> with fewer arguments, and even none, are a desirable thing. There are code >> smells that are the opposite in fact, methods with long parameter lists are >> generally seen as code smell (“passing a paragraph”). >> > > 'self' is, imo, a parameter. When you call a parameter-less method on > an object, it's usually an imperative with a direct object (or > sometimes a subject): > > some_file.close() # "Close some_file" > some_list.shuffle() # "Shuffle some_list" > some_file.readline() # "Some_file, read in a line" > > There are times when, for convenience, the object is implicit. > > print("some text", file=some_file) # Print that text > print(file=some_file) # Print a blank line > print("some text") # Print that text to sys.stdout > print() # Print a blank line to sys.stdout > > So in that situation, the no-args call does make sense. Of course, > this is a call to a function that does take args, but it's accepting > all the defaults and providing no additional content. It's quite > different to actually define a function that mandates exactly zero > arguments, and isn't making use of some form of implicit state (eg a > closure, or maybe a module-level function that manipulates > module-level state - random.random() would be an example of the > latter). Syntactically, Python can't tell the difference between > "print()" and "foo()" where foo can never take args. So at this point, what I’m reading is that actually making a “no arg function” is difficult, if we widen the semantics. The “arguments” of a function may be bound to its implicit self parameter, or tied to module state. > > I'd say that a function taking no args is code smell, unless it's > obviously taking its state from somewhere else (callbacks, for > instance - maybe you pass a bound method, or maybe a closure, but in > either case it has implicit state that's not described by function > args); but _calling_ with no args isn't as smelly. It's certainly less > common than using args, but there are plenty of times when a type is > called without args, for instance[1]. Which leaves me wondering, how would I get my code to smell this way then? What IS an example of a no arg function that doesn’t have an implicit object, that smells? It seems I can escape the smell clause, as long as I find some data that I reason is attached to my function. This all aside, I don’t think these are what the OP had in mind. A code inspection algorithm is not going to be able to discern when an explicitly parameterless function has implicit parameters. It’s just going to see something like print vs print() or aPoint.transpose vs aPoint.transpose() -- https://mail.python.org/mailman/listinfo/python-list
Metaprogramming question
The discussion about niladic functions, made me want to follow a segue and do some reflection/introspective programming in Python. I’ve not done a lot of that yet, and it seemed like an educational (well, at least entertaining) goose chase. If I run the following code: import datetime datetime.datetime.now(13, 42) I will get an error (expected). The error I will get is: Traceback (most recent call last): File "", line 1, in TypeError: now() takes at most 1 argument (2 given) So, at some point, there’s some metadata in the system attached to the builtin datetime.datetime.now method, that indicates 1 argument, tops. So here’s my basic question. Is there anyway to programatically query that information in python code? inspect.signature(datetime.datetime.now) just gives a ValueError. inspect must only be good for the python code that I write, in python. Is there another way to dig out what the interpreter knows there? -- https://mail.python.org/mailman/listinfo/python-list
Fun with function argument counts
After the recent discussion about the classic error: if self.isFooBar: return 42 Among many thing, the OPs contention was that the ability to have this kind of error was a Bad Thing (tm). Which led to me asking about code smells and parameterless functions/methods. So I got curious. Semantics of implicit objects aside, how often is it possible to write code like that. In short, how frequent are methods/functions that have zero explicit args (not implicit, because while fun, that’s not how we code them). It was a fun experiment, I’ve been doing python for a little over a year now, and I thought it would be enjoyable/educational to do a bit of metaprogramming. First the results though. Below is a histogram of function argument count: argCount x occurrences (% of total) -1 x 10426 ( 3.2%) # these are where I stuffed all of the routines I couldn’t extract the argspecs for 0 x 160763 (48.7%) 1 x 109028 (33.0%) 2 x 40473 (12.3%) 3 x 7059 ( 2.1%) 4 x 2383 ( 0.7%) 5 x 141 ( 0.0%) 6 x46 ( 0.0%) 7 x12 ( 0.0%) 10 x 1 ( 0.0%) 16 x 1 ( 0.0%) 19 x 2 ( 0.0%) Nearly half of the functions/methods I scanned were zero args (48.7). Which was more than I expected. I haven’t dug enough to see if there’s a flock of canaries in there or not. The code to produce that table is here: https://gist.github.com/anonymous/8947229. Yes, it’s hacky. Wasn’t trying to win any style/idiom awards with this one. To get this table, I used PyPy 3-2.1 beta for OSX. I basically attempted to parse all of the modules found in it’s lib-python directory. A subset of the modules wouldn’t load, I’m not sure whether to write that off as the work-in-progress nature of pypy or what. And I black listed some of them (such as idlelib.idle, ctypes.test.*, etc). But from the counts, I was able to get a pretty large corpus of them. I learned a number of fun things as part of the exercise: 1) I did not want to try load all modules at once into my environment. I suspect that would create problems. Using os.fork() to isolate the load/analysis of each module was a handy way to deal with that. The trick of using if pid: branch to split of the flow of execution was cool. Maybe it’s the wrong tool for the job and I missed an obvious one, but I thought it was kinda clever. 2) Using cpython, a lot of the core library can’t be reflected on. I tried to use the inspect module, and found that things like inspect.getmembers(datetime.datetime, inspect.ismethod) resulted in a big surprise. Especially if you leave off the predicate, and see that there are a lot of things in there that claim they ARE methods). I finally stumbled into inspect.isroutine, but found that most of the results couldn’t be reified using inspect.signature anyway. The “it’s all python, all the way down, well mostly” ideology of pypy ensured that a higher percentage opt the base libraries could be analyzed. 3) It’s easy to take 3.3.x for granted. I found right away that signature was introduced in 3.3, so I had to use inspect.getfullargspec() instead. 4) optional arguments were an interesting dillema. I chose to reduce the argument count of a signature by the number of default arguments. Since they are essentially optional. So the stats there have a bias to the “minimal” call signature. 5) my method of scanning loadable modules is probably very naive/brute force/stupid. I would love it if there was a better way to do that. 6) Who writes a function with 19 mandatory arguments anyway subprocess._execute_child() and distutils.cygwincompiler._execute_child() 7) I’m not entirely sure, given #6, that I’m not seeing inherited methods for all classes, so getting an overinflated count from them. Can anyone answer that? -- https://mail.python.org/mailman/listinfo/python-list
Confused by python-dbus weird behavior
This may not be a great list for this question (which would be?); it’s a big group, and I’m hoping there’s some people here that cross into these same areas. I’m new to dbus, it seems it’s a sort of CORBA for the Linux world. :) Python seems to be a popular way to interact with it. I’m trying to interact with the BlueZ services for Bluetooth LE stuff, and the scant hints I can find seem to indicate dbus is the preferred way going forward. The BlueZ distribution even provides example code. That said, my question should be independent of whether this was BLE or a dbus interface for a Calculator program. There is a class defined as such: class Characteristic(dbus.service.Object): def __init__(self, bus, index, uuid, flags, service): # … set a bunch of attributes dbus.service.Object.__init__(self, bus, self.path) @dbus.service.method(GATT_CHRC_IFACE, in_signature='ay') def WriteValue(self, value): print('Default WriteValue called, returning error’) raise NotSupportedException() Then I have a subclass of my own: class MyCharacteristic(Characteristic): def __init__(self, bus, index, uuid, flags, service): Characteristic.__init__(self, bus, index, uuid, flags, service) # … set some of my own attributes def WriteValue(self, value): print(‘Got write value:’, value) self.anotherMethod(value) print(‘back from anotherMethod’) def anotherMethod(self, value): print(‘pondering this value:’, value) My program does not call WriteValue directly. It seems to be called by the bluetooth machinery. The mainloop, or something like that. I don’t honestly know. I just know I use some other boilerplate code involving registration and the mainloop, to get it all running. And so the MyCharacteristic.WriteValue() method DOES get fired. I see the output. But when it gets to the anotherMethod call, it just seems to... not. More callbacks may fire later. But that’s the end of that one. I’ve tried this under python2 AND python3. So my basic python-dbus question is: Is this some nuance where a callback method, inheriting from what looks like a proxy of some sort (dbus.service.Object) should/can not send messages to itself? Help me python-obis, help me. You’re my only hope. -- https://mail.python.org/mailman/listinfo/python-list
Re: When I need classes?
> On Jan 10, 2016, at 9:48 AM, Bernardo Sulzbach > wrote: > > Essentially, classes (as modules) are used mainly for organizational purposes. > > Although you can solve any problem you would solve using classes > without classes, solutions to some big problems may be cheaper and > more feasible using classes. As a long term OO purist practitioner, I would add to this. Obviously, you can organize your code any way you want, with or without classes. You could put all your functions with an odd number of letters in one class, and all of the even numbered ones in another class. Having listened to the guy (Alan Kay) who coined the term (Object Oriented Programming) quite a bit over the years, I believe that the focus of OO (of which classes are a particular implementation approach) is to bind behavior to data. In “traditional” programming approaches, one focused on the algorithm (behavior) first, and then figured out what data needed to flow where to get the job done. Classes provided a mechanism to turn that equation, generally speaking, around. One thinks about the data first, and then figures out what behavior binds best to that data. And how that data will interact (inter-object behavior, often called messages) to get your job done. For some (many) problems, this can be a real win. And for some, not so much. I think, this is often why, for a simple script, OO just kind of gets in the way. You have a straightforward procedure that you just want to do. The state (data) is not rich enough to make making it the focal point of your program. -- https://mail.python.org/mailman/listinfo/python-list
How to fix my imports/file structure
I wrote a simple set of python3 files for emulating a small set of mongodb features on a 32 bit platform. I fired up PyCharm and put together a directory that looked like: minu/ client.py database.py collection.py test_client.py test_database.py test_client.py My imports are simple. For example, client.py has the following at the top: from collection import Collection Basically, client has a Client class, collection has a Collection class, and database has a Database class. Not too tough. As long as I cd into the minu directory, I can fire up a python3 interpreter and do things like: >>> from client import Client >>> c = Client(pathstring='something’) And everything just works. I can run the test_files as well, which use the same sorts of imports. I'd like to modularize this, so I can use it another project by just dropping the minu directory alongside my application's .py files and just have everything work. E.g. SomeDirectory/ application.py minu/ … and application.py does something like: from minu.client import Client When I try this though, and am running python3 from another directory, the local imports don't work. I placed an empty init.py in the minu directory. That made it so I could import minu. But the others broke. I tried using things like from .collection import Collection #added the dot but then I can't run things in the original directory anymore, like I could before. What is the simple/right way to do this? I have looked around a bit with Dr. Google, but none of the examples really clarify this well (at least, for me), feel free to point out the one I missed. -- https://mail.python.org/mailman/listinfo/python-list
Re: FYI: Micro Python running on kickstarter pyBoard project, now shipping
> On Oct 23, 2014, at 2:11 PM, sohcahto...@gmail.com wrote: > > On Thursday, October 23, 2014 10:07:26 AM UTC-7, jkn wrote: >> Hi all >>I haven't heard in mentioned here, but since I saw one of the boards >> today thought I'd pass on the news: >> >> The Kickstarter 'MicroPython' project, which has a tiny 'pyboard' (only a >> couple of sq.inches in size) with a processor running 'a complete re-write >> of the Python (version 3.4) programming language so that it fits and runs on >> a microcontroller' is now shipping. >> >>https://micropython.org/ >> >> Looks nice; I have no connection but will be getting one myself to play >> with... >> >>Cheers >>J^n > > > Is there any particular reason to get one of these when I can get a Raspberry > Pi which is faster, has more memory, and a bundle of other features? > > I mean, the idea seems cool and all, but I'm trying to understand why I would > want to spend the ~$45 on something like that when a ~$35 Raspberry Pi will > do everything and more, and do it faster. Power Consumption. I don’t know (looked quick, but didn’t find anything fast enough) the exact numbers, but the Pi is meant to be plugged in to something, or chew through batteries quickly. If your IoT device fits in that space and you need all that periphery, that’s great. The Pyboard is running a STM32F405RG (low power contex M4). So I’m betting various children of mine, that it can go a whole lot longer on the same bit of power. Coin cell operation for long periods is probable. I think you look at the $45 as a development board. The site says you can get access to just about everything, so there’s nothing to keep you from prototyping your killer pythonic IoT gadget with these, then doing your own tiny board, populating them with the same chip that you can get from DigiKey for $7 a piece in quantity. Can’t really do that with the Pi. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a cairo like surface for the screen without the window hassle
> On Feb 2, 2015, at 5:20 AM, Antoon Pardon > wrote: > > I need to have a program construct a number of designs. Of course I can > directly > use a pfd surface and later use a pdf viewer to check. But that becomes rather > cumbersome fast. But if I use a cairo-surface for on the screen I suddenly > have > to cope with expose events and all such things I am not really interested in. > > So does someone know of a package that provides a cairo like surface but that > would take care of the events in a rather straight forward matter, so that my > program could make it's design in a window on the screen just as if it is > designing it in a pdf file. > For the most part, you cannot draw directly to the screen with Cairo. Some OSes kind of allow out, but they won’t repaint it for you. Any viewing software that will auto detect file updates and reload would do the trick. For example, I know that preview on OS X will automatically reload a png file that I write. Cairo can generate png output. Just open preview on the file, and then have the program rewrite the same file. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cairo module
> On Feb 3, 2015, at 1:00 PM, Poul Riis wrote: > > I just tried the Cairo Python module. > I ran the test file below. > It works perfectly but instead of saving the resulting image as a file I want > to see it displayed directly on the screen. > How can I do that? > I have quiet a bit of experience with Cairo (I wrote language bindings for it in Smalltalk and had the time of my life with it there); I have no experience with the pycairo bindings. > > import math > import cairo > > WIDTH, HEIGHT = 256, 256 > > surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT) This is your basic problem right here. And ImageSurface is for creating an Image (sometimes called a raster graphic or bitmap). If you want to display directly to your screen, you need to create a surface that binds to your screen’s display functionality. There is one for each main operating system: Win32Surface XLibSurface QuartzSurface (I see that this is missing from the pycairo documentation, but it is in the cairo documentation, and the pycairo.c file at least has some reference to it) Allocating one of these involves getting handles (and other information) for a given window on screen of your host OS and creating the surface from it. > ctx = cairo.Context (surface) > > ctx.scale (WIDTH, HEIGHT) # Normalizing the canvas > > pat = cairo.LinearGradient (0.0, 0.0, 0.0, 1.0) > pat.add_color_stop_rgba (1, 0.7, 0, 0, 0.5) # First stop, 50% opacity > pat.add_color_stop_rgba (0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity > > ctx.rectangle (0, 0, 1, 1) # Rectangle(x0, y0, x1, y1) > ctx.set_source (pat) > ctx.fill () > > ctx.translate (0.1, 0.1) # Changing the current transformation matrix > > ctx.move_to (0, 0) > ctx.arc (0.2, 0.1, 0.1, -math.pi/2, 0) # Arc(cx, cy, radius, start_angle, > stop_angle) > ctx.line_to (0.5, 0.1) # Line to (x,y) > ctx.curve_to (0.5, 0.2, 0.5, 0.4, 0.2, 0.8) # Curve(x1, y1, x2, y2, x3, y3) > ctx.close_path () > > ctx.set_source_rgb (0.3, 0.2, 0.5) # Solid color > ctx.set_line_width (0.02) > ctx.stroke () > > surface.write_to_png ("example.png") # Output to PNG > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
pymongo and attribute dictionaries
I really like pymongo. And I really like Python. But one thing my fingers really get tired of typing is someDoc[‘_’id’] This just does not roll of the fingers well. Too many “reach for modifier keys” in a row. I would rather use someDoc._id Googling shows that I’m not the first to want to do this in the general sense (e.g. http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python). Arguments aside of whether this should or shouldn’t be done, I want to know how I might solve this with Python. Consider it an academic pursuit. The problem I have is not how to do the AttributeDictionary subclass, there are plenty of those examples. The problem is that the pymongo APIs already return dictionaries. In a language (Smalltalk, Objective-C, Ruby) that supports class extensions, that would be my first tool of choice to solve this problem. I’d just extend Dictionary to behave the way I want and be done with it. I can’t do that in Python though. I guess I could make my own module that subclasses the relevant pymongo classes, and do super() calling implementations of all of the relevant methods, coercing the return type. That is a maintenance headache though. What are my options, if any? -- https://mail.python.org/mailman/listinfo/python-list
Re: pymongo and attribute dictionaries
> On Feb 4, 2015, at 9:22 AM, Ian Kelly wrote: > > On Wed, Feb 4, 2015 at 9:50 AM, Travis Griggs wrote: >> I really like pymongo. And I really like Python. But one thing my fingers >> really get tired of typing is >> >> someDoc[‘_’id’] >> >> This just does not roll of the fingers well. Too many “reach for modifier >> keys” in a row. I would rather use >> >> someDoc._id >> >> Googling shows that I’m not the first to want to do this in the general >> sense (e.g. >> http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python). >> >> Arguments aside of whether this should or shouldn’t be done, I want to know >> how I might solve this with Python. Consider it an academic pursuit. >> >> The problem I have is not how to do the AttributeDictionary subclass, there >> are plenty of those examples. The problem is that the pymongo APIs already >> return dictionaries. In a language (Smalltalk, Objective-C, Ruby) that >> supports class extensions, that would be my first tool of choice to solve >> this problem. I’d just extend Dictionary to behave the way I want and be >> done with it. I can’t do that in Python though. I guess I could make my own >> module that subclasses the relevant pymongo classes, and do super() calling >> implementations of all of the relevant methods, coercing the return type. >> That is a maintenance headache though. >> >> What are my options, if any? > > You could construct the AttributeDictionary by copying the dict > returned from pymongo. The question then is whether the copying would > be too expensive or not. > > Alternately, you could just wrap the dictionaries returned by pymongo > in an object. Something like this should be all you need: > > class AttrDictWrapper(object): >def __init__(self, the_dict): >self.__dict__ = the_dict > >>>> d = AttrDictWrapper({'spam': 42, 'ham': False}) >>>> d.spam > 42 >>>> d.ham > False > Yes, that is clever. So if you wanted to minimize the amount of typing you had to do at all of your pymongo API call sites, what strategy would you use to keep that relatively terse? Is the following the right approach to take? class Doc(object): def __init__(self, target): self.__dict__ = target and then something like for doc in client.db.radios.find({’_id': {’$regex’: ‘^[ABC]'}}): pprint(doc) changes to for doc in ((Doc(d) for d in client.db.radios.find({’_id': {’$regex’: ‘^[ABC]'}})): pprint(doc) Are there other approaches? Feel free to impress me with evil abuses in the interest of academic enrichment... -- https://mail.python.org/mailman/listinfo/python-list
Python & Peewee Query Example Needed
I'm new to python and peewee and was looking for an example on how to query a mysql table with a datetime column only returning rows that are 30 days old. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python & Peewee Query Example Needed
On Monday, February 16, 2015 at 12:35:00 PM UTC-6, Travis VanDame wrote: > I'm new to python and peewee and was looking for an example on how to query a > mysql table with a datetime column only returning rows that are 30 days old. Well this is what I've come up with @classmethod def get_archive_xml(cls, day_count): return cls.select().where(cls.created + datetime.timedelta(days=int(day_count)) >= datetime.date.today()) -- https://mail.python.org/mailman/listinfo/python-list
Re: The sum of numbers in a line from a file
On Feb 20, 2014, at 8:54 AM, Dave Angel wrote: > kxjakkk Wrote in message: >> Let's say I have a sample file like this: >> >> Name1 2 34 5 6 78 >> >> name1099-66-7871 A-FY10067815998 >> name2999-88-7766 A-FN99 100969190 >> name3000-00-0110AUD5100281976 >> name4398-72-P/FY7684496978 >> name5909-37-3689A-FY97941006179 >> >> For name1, I want to add together columns 4, 5, 6, and get an average from >> that, then do the same for the last two columns. I want to do this for every >> name. >> >> All I've got is >> sum([int(s.strip()) for s in open('file').readlines()]) >> > > Don'ttrytodoitallinoneline.thatwayyouactuallymighthaveaplacetoinse > rtsomeextralogic. > Yes. Clearly the preferred way to do it is with lots of lines with room for expandability. Sorry Dave, couldn’t resist. Clearly a balance between extremes is desirable. (Mark, I intentionally put the blank lines in this time ) Travis Griggs "“Every institution tends to perish by an excess of its own basic principle.” — Lord Acton -- https://mail.python.org/mailman/listinfo/python-list
Re: Remove comma from tuples in python.
On Feb 21, 2014, at 6:32 AM, Roy Smith wrote: > In article , > Peter Otten <__pete...@web.de> wrote: > > >> [x*x for (x,) in lst] >> >> [paraphrasing...] can be better written as: >> >> [x*x for [x] in items] > > I'm torn between, "Yes, the second form is distinctly easier to read" > and, "If you think the second form is easier to read, you're admitting > you're not really fluent in Python”. I’ve used the comma form with struct.unpack() frequently: count, = struct.unpack(‘!I’, self.packet) That’s after I don’t use it and end up scratching my head for a while and finally remember that unpack returns a tuple regardless of how many things I unpack from it. It’s just natural if you’re doing lots of single unpacks to think it returns a single value. Either way, I much prefer it to: count = struct.unpack(‘!I’, self.packet)[0] -- https://mail.python.org/mailman/listinfo/python-list
Re: Can global variable be passed into Python function?
On Feb 21, 2014, at 4:13 AM, Ned Batchelder wrote: > Man, do I hate this idea that Python has no variables. It has variables > (names associated with values, and the values can change over the course of > the program), they just don't work the same as C or Fortran variables. In > fact, they work exactly the same as Javascript or Ruby variables. Thank you! +11 I get tired of the “Python doesn’t have variables” line. What makes Python variables/bindings/references/aliases/namedvalues/slots/bucketsofstuff surprising to new arrivals from other language kingdoms, is that accessing is pragmatically implicit (walks the scope tree for you) and assignment may require explicitness. IOW, for some “variables”, you have to do something explicit to make the variable you want to refer to, vary. Some might say there is a lack of symmetry. Pros and cons. Personally, I don’t care. It’s one of those lessons you just learn as you go. -- https://mail.python.org/mailman/listinfo/python-list
Re: Functions help
> On Feb 23, 2014, at 17:09, Mark Lawrence wrote: > > For the benefit of newbies, besides the obvious indentation error above, the > underscore basically acts as a dummy variable. I'll let the language lawyers > give a very detailed, precise description :) You mean a dummy name binding, right? If we say "variable" we might confuse those newly arrived pilgrims from other language kingdom. (If you squint hard, I think there's some tags in there :) ) -- https://mail.python.org/mailman/listinfo/python-list
Re: test
> On Mar 15, 2014, at 14:24, Mark H Harris wrote: > > test Pass -- https://mail.python.org/mailman/listinfo/python-list
Why does isoformat() optionally not print the fractional seconds?
Python(3) let me down today. Better to be explicit, and all that, didn’t pan out for me. I have time series data being recorded in a mongo database (I love pymongo). I have an iOS app that consumes the data. Since JSON doesn’t have a time format, I have to stringify the times when transmitting between the two. To parse it on the obj-c side, I use NSDateFormatter *parser = [NSDateFormatter new]; parser = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [parser setDateFormat:@"-MM-dd'T'HH:mm:ss.S”]; NSDate *date = [parser dateFromString: thatJsonString]; Which was working swimmingly, until I started getting occasional and infrequent nil dates at times. I thought I had a storage issue or something with my REST api, or the server, or something. But it was simply now and then again, why 1000’s of data points, I managed to get 0 milliseconds from time to time, which resulted in the isoformat() I was using to suddenly leave off the .S part of the string. And since the parse then failed, the iOS side decided it wasn’t valid and returned a nil. Haven’t decided where/how I’ll work around it yet, but the isoformat() seemed unpythonic to me today. Thanks for hearing me whine. -- https://mail.python.org/mailman/listinfo/python-list
Re: How keep Python 3 moving forward
Sent from my iPhone > On May 24, 2014, at 7:35, blindanagram wrote: > >> On 24/05/2014 08:13, wxjmfa...@gmail.com wrote: >> Le vendredi 23 mai 2014 22:16:10 UTC+2, Mark Lawrence a écrit : >>> An article by Brett Cannon that I thought might be of interest >>> >>> http://nothingbutsnark.svbtle.com/my-view-on-the-current-state-of-python-3 >>> >>> >>> >>> -- >>> >>> My fellow Pythonistas, ask not what our language can do for you, ask >>> >>> what you can do for our language. >>> >>> >>> >>> Mark Lawrence >>> >>> >>> >>> --- >>> >>> This email is free from viruses and malware because avast! Antivirus >>> protection is active. >>> >>> http://www.avast.com >> >> = >> = >> >> Quote: >> """ And with Python 3.4 I really have not heard anyone complain that they >> wouldn't like to use Python 3 instead of Python 2. """ >> >> Or the devs do not wish to listen. >> >> Python 3 will never work. > > It works for me. Works for me too. I do python3 exclusively. If the library/tool I need is python 2 only, I figure it's obvious it's in maintenance mode only and find something else. -- https://mail.python.org/mailman/listinfo/python-list
Re: IDE for python
> On May 28, 2014, at 3:43, Sameer Rathoud wrote: > > Hello everyone, > > I am new to python. > > I am currently using python 3.3 > > With python I got IDLE, but I am not very comfortable with this. > > Please suggest, if we have any free ide for python development. > -- > https://mail.python.org/mailman/listinfo/python-list I use either vim or textwrangler for simple one file scripts. For larger things with multiple files and/or classes, I like pycharm best ( free community edition ). I tried both pydev and wing before that. -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I calculate a mean with python?
On Sep 16, 2013, at 4:33 PM, William Bryant wrote: > Hey I am new to python so go easy, but I wanted to know how to make a program > that calculates the maen. > > List = [15, 6, 6, 7, 8, 9, 40] > def mean(): >global themean, thesum >for i in List: >thecount = List.count(i) >thesum = sum(List) >themean = thesum / thecount > > Why doesn't this work? > -- > https://mail.python.org/mailman/listinfo/python-list You've had a number of interesting and good responses, some holding your hand quite a bit, and others differently. I think there's a different way to learn what's going wrong, that ISN'T mentioned here, and for some people it's a quite effective method of learning. I'm a relatively recent import from the Smalltalk community, where this approach is more prevalent, I wish it were more so in the Python community. The way I suggest is to use a debugger. The nice thing about a debugger, is that you add a call to mean() at the end, put a breakpoint right there, run, and then you can visually walk through what it's doing. This can help find your bug, but probably also clarify how Python works in the first place. I use pycharm (anyone can use it for free). And there are probably others for free as well. -- https://mail.python.org/mailman/listinfo/python-list
Simple security between prototype iPhone app and SimpleHTTPServer REST service?
I'm prototyping a simple data collection service. I've implemented a simple REST API implemented with python 3x stock HTTPServer. And a simple iPhone app that submits data via a json/POST. And it all works just great when my iPhone is on the same network as the server. But now I want to go the next step. I don't need to move beyond prototype/PoC yet, I just want to be able to do it outside of our internal network. Issues aside of getting access, name resolution, a port and that kind of stuff... what kind of security should I add to it? I might as well be a complete neophyte in this area. I've read a number of posts and such, and I get some of the pieces, at some level, but any confidence how to put that part of a web stack together elude me. I found a example of how to add SSL to my python service (https://gist.github.com/ubershmekel/6194556). If I can figure out how to get the right keys embedded into my iPhone app (it's just on my phone, not anyone else's), is that enough? Or should I include some sort of auth? If so, what kind? And any pointers to how to start that would be much appreciated. Some have blithely replied that I should be using Flask or Tornado. I get that I'm going to hit a wall with HTTPServer and that it's more of a "toy" implementation. But I don't want to get buried in learning a big framework either. If it was relatively easy to convert my simple REST service to one running on Tornado or Flask, without loading a bunch of other frameworks, and I got easy access to security services and good examples how to do them, that'd be fine with me. So far, my searches haven't turned up the simple recipe of "so, you've made a simple REST API with HttpServer, here's how to take it to the semi secure public level using a real web framework." Travis Griggs -- I multiple all estimates by pi to account from running around in circles. -- https://mail.python.org/mailman/listinfo/python-list