[issue3214] Suggest change to glossary explanation: "Duck Typing"
New submission from Paddy McCarthy <[EMAIL PROTECTED]>: The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46 says: " duck-typing Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs hasattr() tests or EAFP programming. " I think it is should be changed to delete the use of hasattr and just rely on EAFP as in the second example here: http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions The text would then read: " duck-typing Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type(), hasattr() or isinstance(). Instead, it typically employs an EAFP style of programming. " The reason is that a hasattr test only tests for an attribute name. If it is present and say the method signature is wrong, then its the excecution of the code using the attribute that will find that out so it is redundant. Best to use EAFP for Duck typing as we trust you know what it is you are doing. - Paddy. -- assignee: georg.brandl components: Documentation messages: 68815 nosy: georg.brandl, paddy3118 severity: normal status: open title: Suggest change to glossary explanation: "Duck Typing" type: feature request versions: Python 2.5, Python 2.6, Python 3.0 ___ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue3214> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue3214] Suggest change to glossary explanation: "Duck Typing"
Paddy McCarthy <[EMAIL PROTECTED]> added the comment: Hi Georg, A bit of relevant background about me: I've been interested in Duck Typing _specifically_ for a couple of years when I started watching edits to it on Wikipedia. I researched the history of the use of the term and changed the attribution of first use to Alex Martelli after digging in Google, and still trawl reading code and articles on the subject. But I DONT think this makes me an expert - Duck typing is a 'mould-able' term and the things we write about it help to define it. On your comment about hasattr: I think more and more that Duck-Typing is what allows us, (as in Python), to substitute one class for another in a method previously designed with only the other in mind - you know, as in the canonical example of file-like objects such as StringIO substituting in methods defined originally for files. In this case hasattr checks don't add anything, and EAFP is all important. I tried to get wider context on this by posting it originally to comp.lang.python but no one was interested. I don't normally frequent the developers forumbut maybe we should open the issue to that audience? - Paddy. ___ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue3214> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17005] Add a topological sort algorithm
Paddy McCarthy added the comment: I've been playing with Python 3.9.0rc1 and was looking at a particular graph to see when it released tasks for processing. I ran the following code: from functools import reduce from pprint import pprint as pp from collections import defaultdict from graphlib import TopologicalSorter from heapq import heapify, heappush, heappop, heappushpop print(""" ### ### TASK DEPENDENCY ### A -> B -> C ↓↓↓ D -> E -> F """) graph3 = {"A": {"B", "D"}, "B": {"C", "E"}, "C": {"F"}, "D": {"E"}, "E": {"F"}, } tasktime = {task: 2 for task in 'ABCDEF'} def simulate(graph, tasktime): print("\n## NEW SIMULATION") t = 0 heap = [] heapify(heap) print("\n# Task runtimes:") for node, tm in tasktime.items(): print(f" {node}: {tm}") print("\n# OUTPUT. (:> for task start times, <: for stop times).\n") ts = TopologicalSorter(graph) ts.prepare() while ts.is_active(): for node in ts.get_ready(): finish = t + tasktime[node] heappush(heap, (finish, node)) print(f"{' ' * t}{node}:> @{t}") t, node = heappop(heap) print(f"{' ' * t}{node}<: @{t}") ts.done(node) simulate(graph3, tasktime) tasktime['C'] = 1 simulate(graph3, tasktime) I got the following output: ### ### TASK DEPENDENCY ### A -> B -> C ↓↓↓ D -> E -> F ## NEW SIMULATION # Task runtimes: A: 2 B: 2 C: 2 D: 2 E: 2 F: 2 # OUTPUT. (:> for task start times, <: for stop times). F:> @0 F<: @2 C:> @2 E:> @2 C<: @4 E<: @4 B:> @4 D:> @4 B<: @6 D<: @6 A:> @6 A<: @8 ## NEW SIMULATION # Task runtimes: A: 2 B: 2 C: 1 D: 2 E: 2 F: 2 # OUTPUT. (:> for task start times, <: for stop times). F:> @0 F<: @2 C:> @2 E:> @2 C<: @3 E<: @4 B:> @4 D:> @4 B<: @6 D<: @6 A:> @6 A<: @8 >>> Note that in the second simulation, C finish, but B isn't then immediately started. I have my own code that also works like this but it isn't optimal. Thanks guys. -- nosy: +Paddy McCarthy ___ Python tracker <https://bugs.python.org/issue17005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17005] Add a topological sort algorithm
Paddy McCarthy added the comment: Please ignore my earlier Message-id <1598493715.04.0.06462575371.issue17...@roundup.psfhosted.org>. I missed a dependency in cutting down a larger example. Sorry. -- ___ Python tracker <https://bugs.python.org/issue17005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33235] Better help text for dict.setdefault
New submission from Paddy McCarthy : Hi, I was answering some question and used dict.setdefault as part of the solution and posted the help() on it as part of my answer. The help was this: In [15]: help(mapper.setdefault) Help on built-in function setdefault: setdefault(...) method of builtins.dict instance D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D This seems the wrong way around. Is it not better expressed as D.setdefault(k[,d]) -> set D[k]=d if k not in D and then D.get(k,d) -- assignee: docs@python components: Documentation messages: 315015 nosy: Paddy McCarthy, docs@python priority: normal severity: normal status: open title: Better help text for dict.setdefault type: enhancement versions: Python 3.6 ___ Python tracker <https://bugs.python.org/issue33235> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16580] Add examples to int.to_bytres and int.from_bytes
New submission from Paddy McCarthy: http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes and http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes would benefit from an example showing what they do based on simpler coding. I have such an example that I wrote here: http://paddy3118.blogspot.co.uk/2012/11/some-identities-for-python-inttobytes.html that you can use. I.e. >>> n = 2491969579123783355964723219455906992268673266682165637887 >>> length = 25 >>> n2bytesbig= n.to_bytes(length, 'big') >>> n2byteslittle = n.to_bytes(length, 'little') >>> assert n2bytesbig== bytes( (n >> i*8) & 0xff for i in >>> reversed(range(length))) >>> assert n2byteslittle == bytes( (n >> i*8) & 0xff for i in range(length)) >>> assert n == sum( n2bytesbig[::-1][i] << i*8 for i in range(length) ) >>> assert n == sum( n2byteslittle[i]<< i*8 for i in range(length) ) >>> assert n == int.from_bytes(n2bytesbig,byteorder='big') >>> assert n == int.from_bytes(n2byteslittle, byteorder='little') >>> -- assignee: docs@python components: Documentation messages: 176671 nosy: docs@python, paddy3118 priority: normal severity: normal status: open title: Add examples to int.to_bytres and int.from_bytes type: enhancement versions: Python 3.3 ___ Python tracker <http://bugs.python.org/issue16580> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16580] Add examples to int.to_bytres and int.from_bytes
Paddy McCarthy added the comment: On 06/12/2012 14:31, Ezio Melotti wrote: > Ezio Melotti added the comment: > > I agree. The examples in the doc seem clear to me, whereas the ones you > proposed are not as clear. Do you think there's something that they don't > currently cover that should be added? > > -- > nosy: +ezio.melotti > > ___ > Python tracker > <http://bugs.python.org/issue16580> > ___ > First, Thanks Ezio and Andrew for your replies. My problem was that when working on bitcoin address validation I saw code that was shifting and &'ing with 0xFF to convert to multiple bytes and half remembered that there might be a Python function to do that. On finding the .to_bytes method and its parameter "big" or "little", the only way I had of working out which to use was to try each until I found out which worked. I therefore thought that what would have helped me was code that showed the equivalent "expanded Python" for the method in a similar way to what is done for some of the itertools functions etc. If we split my request into two: 1. Is such extra explanation necessary. 2. Is my specific code that extra explanation. I can work on the code a bit more. Have I persuaded you that an extra explanation is necessary? Thanks, Paddy. P.S. I guess what is currently present shows the result of the methods but nothing on how it could be generated. I am stating that the generation can aid comprehension. -- ___ Python tracker <http://bugs.python.org/issue16580> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16580] Add examples to int.to_bytres and int.from_bytes
Paddy McCarthy added the comment: On 09/12/2012 10:55, Ezio Melotti wrote: > Ezio Melotti added the comment: > > Usually we add plain Python equivalents when they are simple enough that the > code equivalent is as understandable as the prose or more (see for example > http://docs.python.org/3/library/functions.html#all, or the itertools > functions you mentioned). > For this case I think it would help if you presented an equivalent function, > e.g.: > > def to_bytes(n, length, order): > if order == 'little': > return bytes((n >> i*8) & 0xff for i in range(length)) > elif order == 'big': > return bytes((n >> i*8) & 0xff for i in reversed(range(length))) > > or even: > > def to_bytes(n, length, order): > indexes = range(length) if order == 'little' else reversed(range(length)) > return bytes((n >> i*8) & 0xff for i in indexes) > > This is also done for > http://docs.python.org/3.3/library/stdtypes.html#int.bit_length just above > to/from_bytes, so it might be a good addition. > If this is done, the equivalent function can also be added to the test suite, > so we can verify that it's indeed equivalent. > > -- > keywords: +easy > stage: -> needs patch > versions: +Python 2.7, Python 3.2, Python 3.4 > > ___ > Python tracker > <http://bugs.python.org/issue16580> > ___ > The second example looks great. I like the dual use for testing too and will try and remember both the next time I find I have ireas about the documentation. Thanks guys. It's appreciated! -- ___ Python tracker <http://bugs.python.org/issue16580> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24914] Python: Not just OO style but this is not mentioned on python.org or in FAQ
New submission from Paddy McCarthy: Just read http://www.ibmsystemsmag.com/ibmi/developer/general/different-world-python/?utm_campaign=ibm-enews&utm_medium=email&utm_source=ibmi-jul22-2015?&utm_content=exclusive1-headline It states that they could have had an officially supported version of Python on that IBM platform much earlier but for this: > "The second was that everything we read on Python, and all the examples we > encountered, led us to believe that it was a completely object oriented (OO) > language" They may have used it earlier had they known then that Python can be written in a procedural style they having no love of Java's OO, but being able to use PHP and access PHP's OO bits. Looking again on python.org, the examples are not OO, but when you delve down, say to the FAQ - it gives the mistaken impression that OO is the _only_ style of programming supported: https://docs.python.org/2/faq/general.html#what-is-python Somehow we need to explain that OO is an implementation style, but the language allows code to be written in just as much - or as little, of proceedural/OO/functional styles as the programmer is comfortable with. -- assignee: docs@python components: Documentation messages: 248987 nosy: Paddy McCarthy, docs@python priority: normal severity: normal status: open title: Python: Not just OO style but this is not mentioned on python.org or in FAQ type: enhancement ___ Python tracker <http://bugs.python.org/issue24914> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24914] Python: Not just OO style but this is not mentioned on python.org or in FAQ
Paddy McCarthy added the comment: OK, here's a suggested re-wording: "Python is an interpreted, interactive, object-oriented programming language that also supports programming in procedural and functional styles. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes; but statements are not required to be contained in class definitions and functions are first class - being able to be composed, returned from other functions and be a member of other container types such as dicts, sets, and lists." If that is too long,an alternative would be to delete my addition to the last sentence from "; but classes are not ..." Leaving it to the reader to research just how procedural and functional Python can be. (Where should a programmer, but newbie-to-Python look)? -- ___ Python tracker <http://bugs.python.org/issue24914> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23695] idiom for clustering a data series into n-length groups
New submission from Paddy McCarthy: In the zip section of the documentation, e.g. https://docs.python.org/3/library/functions.html#zip There is mention of an idiom for clustering a data series into n-length groups that I seem to only come across when people are explaining how it works on blog entries such as the three mentioned here: http://www.reddit.com/r/programming/comments/2z4rv4/a_function_for_partitioning_python_arrays/cpfvwun?context=3 It is not a straight-forward bit of code and so I think it should either be explained in more detail in the documentation or removed as an idiom, or I guess it could be encapsulated in a function and added to the stdlib. -- assignee: docs@python components: Documentation messages: 238365 nosy: Paddy McCarthy, docs@python priority: normal severity: normal status: open title: idiom for clustering a data series into n-length groups type: enhancement versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4 ___ Python tracker <http://bugs.python.org/issue23695> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23695] idiom for clustering a data series into n-length groups
Paddy McCarthy added the comment: Hmmm. It seems that the problem isn't to do with the fact that it works, or how to apply it; the problem is with *how* it works. Making it an idiom means that too many will use it without knowing why it works which could lead to later maintenance issues. I think a better description of how it works may be needed for the docs. Unfortunately my description of the how at http://paddy3118.blogspot.co.uk/2012/12/that-grouping-by-zipiter-trick-explained.html was not written with the docs in mind, but you are welcome to any part or the whole, for the Python docs. -- ___ Python tracker <http://bugs.python.org/issue23695> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23695] idiom for clustering a data series into n-length groups
Paddy McCarthy added the comment: I like R. David Murray's suggestion, but I am also aware of how it works and so cannot judge how it would look to the intermediate Python programmer who knows iterators and zip, but is new to this grouper; (who I think should be the target audience). -- ___ Python tracker <http://bugs.python.org/issue23695> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue10395] new os.path function to extract common prefix based on path components
Paddy McCarthy added the comment: Can we now: 1. Move os.path.commonprefix to str.commonprefix or string.commonprefix 2. Deprecate the use of os.path.commonprefix 3. Add os.path.commonpath 4. Update the documentation. This seems to have lingered for too long and yet people have been willing to do the work it seems (from 1999). -- nosy: +Paddy McCarthy ___ Python tracker <http://bugs.python.org/issue10395> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com