Re: Comparisons and sorting of a numeric class....
Ian Kelly wrote: Wait, are you actually asking why bool is a doubleton? If nobody has answered that, I think probably nobody understood you were asking it, because it shouldn't need to be explained. What does perhaps need explaining is why Python goes out of its way to *enforce* the doubleton-ness of bool. I don't know all of Guido's reasoning on this, but part of it is probably to do with implementation efficiencies. There are several places in the CPython source where it's assumed that, if an object is known to be of type bool, then its truth or falseness can be determined with a pointer comparison. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: List of "python -m" tools
Thanks for all the answers! -- https://mail.python.org/mailman/listinfo/python-list
EuroPython 2015: Your chance to sign up as a launch sponsor
Companies who would like to sign up as a EuroPython 2015 launch sponsor are encouraged to contact the sponsor work group at: [email protected] Launch sponsors will get the additional benefit of being listed on the website when we launch - for free. You just need to be quick, since the launch is planned for early in February. More Booths and more Sponsor Slots -- The Euskalduna Conference Center and Concert Hall (ECC) venue in Bilbao was chosen as conference venue for EuroPython 2015: http://www.euskalduna.net/Index.asp?idioma=en It offers plenty of room for sponsor booths, so we will try to make EuroPython 2015 as effective as possible for you as sponsors by offering more booth space and sponsor slots than ever before: http://www.euskalduna.net/espacios/espacios_hall_exposiciones.asp This is your chance to reach out to more than a thousand enthusiastic and highly motivated EuroPython attendees ! Please email us at [email protected] and we’ll send you the sponsor brochure. Thanks, — EuroPython Society (EPS) http://www.europython-society.org/ PS: Please help spread the word and forward this email to companies you know, your local lists, user groups, etc. Many thanks ! -- https://mail.python.org/mailman/listinfo/python-list
Re: Comparisons and sorting of a numeric class....
Andrew Robinson wrote: I never said subclassing bool is the 'only' solution; I have indicated it's a far better solution than many. An assertion with which we very much disagree. I have spent well over twenty years on and off dealing with boolean values that are very often mixed indistinguishably with 'don't care' or 'tri-state' or 'metastable states'. I think you're overestimating how useful it will be to pass one of your "extended boolean" values to existing code expecting a plain boolean. The purpose of the bool type in Python and other languages is for making control-flow decisions. When you hit if x: do_something() else: do_something_else() and x is Undefined or TriState or "47% true", what is supposed to happen? The right thing to do will depend on the circumstances, so you're going to need custom code for dealing with those values. I'm not even sure it's right to single out two of the extended values as corresponding to True and False in all cases. It may seem obvious that the "high" state of a digital logic signal should be True and the "low" state should be False, but -- what about active-low signals? They use the opposite convention! Then I look at python development historically and look at the built in class's return values for compares; and I notice; they have over time become more and more tied to the 'type' bool. Actually, it's the opposite. Originally, all the comparison operators got funneled through a single special method __cmp__, which was required to return a negative, zero or positive integer; hard-coded logic in the interpreter then derived a boolean from that. When rich comparisons were introduced (i.e. the ability to override all the comparison operators individually), that restriction was lifted. I expect sometime in the future that python may implement an actual type check on all comparison operators so they can not be used to return anything but a bool. That's not going to happen. If nothing else, it would break NumPy, which compares arrays element-by-element and returns an array of booleans. I already noticed a type check on the return value of len() so that I can't return infinity, even when a method clearly is returning an infinitely long iterator That's acknowledged as being less than desirable, but fixing it would have performance implications, as well as breaking the C extension API. It's not really much of a limitation, anyway. Iterators don't actually have a __len__; they can optionally have a __length_hint__ method, but infinite iterators can just leave that undefined. That suggests to me that there is significant risk in python of having type checking on all __xx__ methods in the future. You need have no fear of that. The trend has actually been towards *less* restrictions on return types, and I can't imagine that changing. - use delegation to proxy True and False; That sounds like a far more likely to succeed alternative, and is one of a handful of alternatives I have been exploring on my own. A proxy is still a different type. Whether you use a proxy or a completely new type for this is entirely a matter of implementation convenience. You won't magically gain any capabilities that you couldn't have implemented otherwise. python may not be able to do it totally from the python side because there is a difference in how Python handles type() checks and isinstance() checks. There's no way I know of to make type() lie about the true type of an object, proxy or not. But that would only be a problem for code relying on type(x) is bool, which I expect to be extremely rare if it exists at all. Though I DO want to point out that Charles Bool did not invent the computer, That's correct, because he didn't exist. :-) Boolean algebra is named after George Boole. You're probably thinking of Charles Babbage, who design (although never fully implemented) what could be regarded as the first general purpose programmable computer. Name recognition is great for honoring a man -- but makes for a poor reason to choose a strict implementation of bool. This is utter nonsense. The "strict" implementation of bool you speak of isn't chosen to honour George Boole. It's chosen because it's perfectly suited for its intended application of flow-control in programming -- which is the vast majority of the use of logic *above* the hardware level. And since it happens to be a true Boolean algebra, the name is entirely appropriate. And those truth tables are not part of Boolean algebra. Oh wow I never expected to hear that -- But I guess you were never trained to do boolean algebra, formally ? A Boolean algebra is a mathematical structure with a precise definition. There exist Boolean algebras with more than two values, but the don't-care states found in data sheets don't fit into any of them. The data sheets call them "truth tables", not "Boolean algebra tables". :-) The truth tables on data sheets are VERY VERY mu
Re: lambdak: multi-line lambda implementation in native Python
Dennis Lee Bieber wrote:
On Fri, 16 Jan 2015 01:50:00 +1100, Chris Angelico
declaimed the following:
>
Problem: You have a smartphone with a 4x4 pixel screen.
BIG problem, considering that a late 70s DECWriter needed 5x7 pixels
for glyphs in an 8x10 pixel character cell {as I recall...}
If those are 24-bit RGB pixels, you could encode
3 characters in each pixel.
You'd need eyes with rather good colour perception
to read it, though.
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Comparisons and sorting of a numeric class....
On Friday, January 16, 2015 at 7:20:13 AM UTC+5:30, Andrew Robinson wrote:
Disclaimers
1. Ive not really read the above 542 lines and earlier
2. I am not a fan of OOP
Still some thoughts...
Electrical engineering (EE) and computer science (CS) may seem related
but are quite different disciplines. In fact there is some amount of
'client-supplier' in this relation - you folks make the machines we use.
Now one of the basic things that needs to be effected to make this transition
is the so-called digital abstraction
To start with we say (say) that 0V is 0-logic, 3.3V is 1-logic.
But that's hardly enough, we need margins, forbidden regions, Postel's law
etc. This mapping is hardly straightforward. And that is still the
'static-discipline'.
When time comes in we need to deal with the fact that when a gate
switches it will willy-nilly go through the forbidden region. From here
we have to go through/into clock disciplines, delay insensitie circuits etc.
Should CS-ists deal with all this??
If you say yes then what are you EE-guys doing?
If no then you are agreeing with all the others here.
In some more detail:
You seem to want a multi-valued logic. How many values?
There are quite a few answers:
- 4 -- {0,1,Z,X} - http://en.wikipedia.org/wiki/Four-valued_logic
- 9 -- above + weak drives http://en.wikipedia.org/wiki/IEEE_1164
And probably half a dozen others.
You say you REALLY NEED these in your work.
Yes, many people need many things, eg.
1. Mars orbiter was lost due to a mismatch of MKS and FPS systems
http://edition.cnn.com/TECH/space/9909/30/mars.metric.02/
Does that make a case for building in units into programming languages?
2. C.A.R Hoare said the invention of the null-pointer was a billion-dollar
mistake. Do C programmers agree with him?
3. He also considered exception handling as a terrible disaster since it
confuses flow of control. Are python (or most modern language) users likely
to agree?
All these are instances of a basic principle that Niklaus Wirth enunciated:
The most important decision of a language designer are what to leave out
of the language.
Finally in python 3.4 onwards there are enums. You can do this
>>> from enum import IntEnum
>>> class Bool4(IntEnum):
... F=0
... T=1
... Z=2
... X=3
--
https://mail.python.org/mailman/listinfo/python-list
Re: lambdak: multi-line lambda implementation in native Python
Gregory Ewing :
> Dennis Lee Bieber wrote:
>> On Fri, 16 Jan 2015 01:50:00 +1100, Chris Angelico
>> declaimed the following:
>>
>>>Problem: You have a smartphone with a 4x4 pixel screen.
>>
>> BIG problem, considering that a late 70s DECWriter needed 5x7
>> pixels for glyphs in an 8x10 pixel character cell {as I recall...}
>
> If those are 24-bit RGB pixels, you could encode
> 3 characters in each pixel.
Not since Python3. Characters are Unicode now so you'll need to dedicate
a pixel for each character.
Marko
--
https://mail.python.org/mailman/listinfo/python-list
How to "wow" someone new to Python
Scenario: You're introducing someone to Python for the first time. S/he may have some previous programming experience, or may be new to the whole idea of giving a computer instructions. You have a couple of minutes to show off how awesome Python is. What do you do? I was thinking along the lines of a simple demo in the REPL, showing off some of Python's coolest features. But then I got stuck on the specifics. What are Python's best coolnesses? What makes for a good demo? Ideally, this should be something that can be demo'd quickly and easily, and it should be impressive without going into great details of "and see, this is how it works on the inside". So, how would you brag about this language? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
If you want to show off the REPL, I'd got for iPython and show them some simple matplotlib examples (plotting sin waves, maybe dig up a CSV file on the net with some data your friend is familiar with, etc) Skip On Fri, Jan 16, 2015 at 9:03 AM, Chris Angelico wrote: > Scenario: You're introducing someone to Python for the first time. > S/he may have some previous programming experience, or may be new to > the whole idea of giving a computer instructions. You have a couple of > minutes to show off how awesome Python is. What do you do? > > I was thinking along the lines of a simple demo in the REPL, showing > off some of Python's coolest features. But then I got stuck on the > specifics. What are Python's best coolnesses? What makes for a good > demo? > > Ideally, this should be something that can be demo'd quickly and > easily, and it should be impressive without going into great details > of "and see, this is how it works on the inside". So, how would you > brag about this language? > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On 2015.01.16 09:03, Chris Angelico wrote: > Scenario: You're introducing someone to Python for the first time. > S/he may have some previous programming experience, or may be new to > the whole idea of giving a computer instructions. You have a couple of > minutes to show off how awesome Python is. What do you do? > > I was thinking along the lines of a simple demo in the REPL, showing > off some of Python's coolest features. But then I got stuck on the > specifics. What are Python's best coolnesses? What makes for a good > demo? > > Ideally, this should be something that can be demo'd quickly and > easily, and it should be impressive without going into great details > of "and see, this is how it works on the inside". So, how would you > brag about this language? If the person is already familiar with programming, you could show off how Python doesn't do a best effort guess at what to do when you make a mistake (explicit is better than implicit). Many other languages will, for example, make an undefined variable into a variable defined as an empty string or allow silly things like 5 + "cheese", whereas Python will let you know that you made a mistake somewhere instead of letting garbage propagate. This behavior can be frustrating for newbies to Python, but with someone there to explain why it works that way, they can learn to appreciate it instead of giving up in anger after Python keeps throwing exceptions at them. If the person is not familiar with programming, show them how easy it is to get something useful written quickly, even with only the stdlib. Low-level details are handled so that you can focus on what you want the program to do, and there is a ton of stuff in the stdlib so that it's likely you don't need to go searching for a bunch of different libraries so that again, you can focus what you want the program to do. For this, chances are, that person has some things in mind already that are not difficult to get started with using Python. Also, using the REPL for this makes it an even better demo. You can probably have the basic functionality of whatever cool thing they want right there in the REPL. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
Chris Angelico : > Scenario: You're introducing someone to Python for the first time. > S/he may have some previous programming experience, or may be new to > the whole idea of giving a computer instructions. You have a couple of > minutes to show off how awesome Python is. What do you do? My experience is that if you have your "customer" try their hand on programming and complete a simple challenge, they'll be extremely impressed. "Did I manage that?" On the other hand, if you want to demo your greatest achievements at the screen, they will be left unmoved. "Even my favorite Zynga game looks cooler than that." > I was thinking along the lines of a simple demo in the REPL, showing > off some of Python's coolest features. But then I got stuck on the > specifics. What are Python's best coolnesses? What makes for a good > demo? I would advise steering clear of the REPL and go directly to writing a program and executing it. Maybe the classic ASCII graphics presentation of the sine wave: #!/usr/bin/env python3 import time import math def main(): for angle in range(0, 10, 5): print(int((1 + math.sin(math.radians(angle))) * 35) * "*") time.sleep(0.1) if __name__ == "__main__": main() > Ideally, this should be something that can be demo'd quickly and > easily, and it should be impressive without going into great details > of "and see, this is how it works on the inside". So, how would you > brag about this language? I'd recommend not skipping the traditional boilerplate (see my example). Be professional from the start. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: lambdak: multi-line lambda implementation in native Python
On 01/15/2015 10:29 PM, Ian Kelly wrote: > On Thu, Jan 15, 2015 at 9:00 PM, Chris Angelico wrote: >> My first response was going to be "Well, you can always add another >> layer of indirection to try to solve your problem", but then I went >> and looked up builders on Wikipedia. Now I'm confused. What can you do >> with a builder that you can't do with a constructor? > > In Java you have to write a separate constructor for every conceivable > combination of arguments. If there are a lot of optional arguments, > that's an exponentially large number of constructors. The builder > pattern provides a solution to that problem. > > In Python you just have one initializer with defaults for the optional > arguments, so it's not an issue. Which seems to confirm my understanding that these patterns are in large part a response to limitations in the language, which certainly doesn't engender a fondness for the Java. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On 16/01/2015 16:03, Chris Angelico wrote: Scenario: You're introducing someone to Python for the first time. S/he may have some previous programming experience, or may be new to the whole idea of giving a computer instructions. You have a couple of minutes to show off how awesome Python is. What do you do? The batteries included: some useful and simple examples with only core data type objects, built-in functions and standard library -- Marco Buttu INAF-Osservatorio Astronomico di Cagliari Via della Scienza n. 5, 09047 Selargius (CA) Phone: 070 711 80 217 Email: [email protected] -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On Friday, January 16, 2015 at 8:34:20 PM UTC+5:30, Chris Angelico wrote: > Scenario: You're introducing someone to Python for the first time. > S/he may have some previous programming experience, or may be new to > the whole idea of giving a computer instructions. You have a couple of > minutes to show off how awesome Python is. What do you do? There is this story -- maybe apocryphal -- that the tendency to vote democratic or republican runs so deep it can be detected from genetic markers. Similar things apply to programming: Some people are drawn to a mathematical style; some are not Some people love cute little scripts; some are left cold Some love graphics; some dislike etc etc All corollary to: Some people can think like programmers; most cant [Who does the last quote? Steve Jobs?] So to start with, you need to 'fingerprint' (is that the word?) your subject. > > I was thinking along the lines of a simple demo in the REPL, showing > off some of Python's coolest features. But then I got stuck on the > specifics. What are Python's best coolnesses? What makes for a good > demo? The reason I find the REPL particularly cool for such demos [I am surprised that Marko doesn't] is that at least to some extent you can straddle some of the divides above. How about a little web-scrape with beautiful-soup? Followed by maybe a "throw the results into a csv-file and open in the local spreadsheet"? -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On 01/16/2015 08:33 PM, Chris Angelico wrote: > Scenario: You're introducing someone to Python for the first time. > S/he may have some previous programming experience, or may be new to > the whole idea of giving a computer instructions. You have a couple of > minutes to show off how awesome Python is. What do you do? > > I was thinking along the lines of a simple demo in the REPL, showing > off some of Python's coolest features. But then I got stuck on the > specifics. What are Python's best coolnesses? What makes for a good > demo? > > Ideally, this should be something that can be demo'd quickly and > easily, and it should be impressive without going into great details > of "and see, this is how it works on the inside". So, how would you > brag about this language? > > ChrisA hello, I am a newbie to python, I have dwelled in c,qt none in java. php a lot, though I don't make money with any of those. The best thing I find is python is very easy, the best part maybe because of my inexperience with other languages are the List and Dict data types that just solved problems I had in real life made solvable with python very easily. when I had to worry about memory and pointers to memory in those other languages, python just made me focus on the solution I want. also the way I can read python program just like they are written in plain eglish :) a personal problem I tried to solve is that how many characters exist in a sequence statistically (with 2 or more characters in len) in any given file and then their count. For a 100kb file I used practially all programming knowledge in from the other languages but failed miserably as the computation were taking more time with each bigger chunck of file. I would have to do a lot of memery management with python using those style like deleting list and dict before adding another. but when I tried to solve the problem natively with python it just took a blink of an eye for them to solve upto 50kb file. but for larger files although instant it is just memory consuming since I was limit with 2 gb I ddin't poke further. this was my exp and I find programming fun in python like ironman talking to jarvis keep computing!!! GCM -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On Fri, Jan 16, 2015 4:24 PM CET Andrew Berg wrote: >On 2015.01.16 09:03, Chris Angelico wrote: >> Scenario: You're introducing someone to Python for the first time. >> S/he may have some previous programming experience, or may be new to >> the whole idea of giving a computer instructions. You have a couple of >> minutes to show off how awesome Python is. What do you do? >> >> I was thinking along the lines of a simple demo in the REPL, showing >> off some of Python's coolest features. But then I got stuck on the >> specifics. What are Python's best coolnesses? What makes for a good >> demo? >> >> Ideally, this should be something that can be demo'd quickly and >> easily, and it should be impressive without going into great details >> of "and see, this is how it works on the inside". So, how would you >> brag about this language? >If the person is already familiar with programming, you could show off how >Python doesn't do a best effort guess at what to do when you make a mistake >(explicit is better than implicit). Many other languages will, for example, >make an undefined variable into a variable defined as an empty string or allow >silly things like 5 + "cheese", whereas Python will let you know that you made >a mistake somewhere instead of letting garbage propagate. This behavior can be >frustrating for newbies to Python, but with someone there to explain why it >works that way, they can learn to appreciate it instead of giving up in anger >after Python keeps throwing exceptions at them. > >If the person is not familiar with programming, show them how easy it is to get >something useful written quickly, even with only the stdlib Completely agree! Find out that person's itch and make a scratch.py that shows how problem-oriented the language is, with code that somewhat reads like regular English. REPL will also work, if you prepare it well enough. Ipython Notebook! >Low-level details >are handled so that you can focus on what you want the program to do, and there >is a ton of stuff in the stdlib so that it's likely you don't need to go >searching for a bunch of different libraries so that again, you can focus what >you want the program to do. For this, chances are, that person has some things >in mind already that are not difficult to get started with using Python. Also, >using the REPL for this makes it an even better demo. You can probably have the >basic functionality of whatever cool thing they want right there in the REPL. >-- >https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On Friday, January 16, 2015 at 10:51:52 PM UTC+5:30, Mirage Web Studio wrote: > On 01/16/2015 08:33 PM, Chris Angelico wrote: > > Scenario: You're introducing someone to Python for the first time. > > S/he may have some previous programming experience, or may be new to > > the whole idea of giving a computer instructions. You have a couple of > > minutes to show off how awesome Python is. What do you do? > > > > I was thinking along the lines of a simple demo in the REPL, showing > > off some of Python's coolest features. But then I got stuck on the > > specifics. What are Python's best coolnesses? What makes for a good > > demo? > > > > Ideally, this should be something that can be demo'd quickly and > > easily, and it should be impressive without going into great details > > of "and see, this is how it works on the inside". So, how would you > > brag about this language? > > > > ChrisA > > hello, > > I am a newbie to python, I have dwelled in c,qt none in java. php a lot, > though I don't make money with any of those. > > The best thing I find is python is very easy, the best part maybe > because of my inexperience with other languages are the List and Dict > data types that just solved problems I had in real life made solvable > with python very easily. when I had to worry about memory and pointers > to memory in those other languages, python just made me focus on the > solution I want. Nice point! First class concrete data structures is a blessing especially for a C programmer. Here is an old Guido workout of dicts https://www.python.org/doc/essays/graphs/ Probably can be improved to use comprehensions -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On Sat, Jan 17, 2015 at 4:31 AM, Rustom Mody wrote: > Nice point! > First class concrete data structures is a blessing especially for > a C programmer. Definitely! Worth noting. There've been some nice concepts mentioned; concrete suggestions would be good too. Some specific feature or exact line of code that would show off Python's awesomeness. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
recursive function: use a global or pass a parameter?
I have this type of situation and wonder if I should use a global variable
outside the recursive function instead of passing the updated parameter
through.
I want to get a union of all the values that any 'things' key may have, even in
a nested dictionary (and I do not know beforehand how deep the nesting might
go):
d = {'things':1, 'two':{'things':2}}
def walk(obj, res):
if not hasattr(obj, 'keys'):
return set(), set()
if 'things' in obj:
res.add(obj['things'])
for k in obj:
walk(obj[k], res)
return res
walk(d, set()) # returns {1, 2}
Is it better to use a global to keep track of the values or does it even matter?
thanks,
--Tim
--
https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote:
> I want to get a union of all the values that any 'things' key may have, even
> in a nested dictionary (and I do not know beforehand how deep the nesting
> might go):
>
> d = {'things':1, 'two':{'things':2}}
>
> def walk(obj, res):
> if not hasattr(obj, 'keys'):
> return set(), set()
>
> if 'things' in obj:
> res.add(obj['things'])
>
> for k in obj:
> walk(obj[k], res)
>
> return res
>
> walk(d, set()) # returns {1, 2}
>
> Is it better to use a global to keep track of the values or does it even
> matter?
I would use a parameter rather than a global, but I'd make the
parameter optional:
def all_keys(obj, accum=None):
if accum is None: accum=set()
if 'things' in obj:
res.add(obj['things'])
for val in obj.values():
all_keys(val, accum)
return all_keys
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On 2015-01-17 02:03, Chris Angelico wrote: > Ideally, this should be something that can be demo'd quickly and > easily, and it should be impressive without going into great details > of "and see, this is how it works on the inside". So, how would you > brag about this language? First, I agree with Andrew Berg's suggestion about the breadth of the stdlib. This always irks me when I have to return to the C/C++ world where there's no standard library for things like networking (and thus no stock libraries for IMAP, SMTP, HTTP, FTP, etc or email-message handling), CSV processing, regular expressions, zip/tar/zlib files, SHA1/MD5, command-line option processing, threading, and no available-everywhere GUI. In the Java world, it feels like much of this is available, but that the glommed-on standards have multiple ways to do them (the old way(s) and the new/improved way). In PHP, well...that's just PHP (difficult-to-grok equality testing, inconsistent naming conventions and parameter ordering, lack of namespacing, easy-to-screw-up string interpolation, hacky OOP, etc). My fast-introduction go-to items are dir() and help() within the REPL interface. Nothing speeds up my development like being able to drop to a PDB prompt and inspect an object, ask what properties it supports, dump them, get help on them, etc. There's also the bigint stuff that means I don't have to worry about over/underflow errors. I'm sure there are more great ideas, but how you market might depend on your audience's background in programming (what language did they use and what pain-points did they experience). -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: How to "wow" someone new to Python
On 1/16/2015 9:44 AM, Chris Angelico wrote: > exact line of code that would show off Python's awesomeness. a,b = b,a Emile -- https://mail.python.org/mailman/listinfo/python-list
Android Native Build Help: python build_ext
I'm building python on my Android tablet and, while the executable and library builds successfully, I run into a problem when the newly built python runs build_ext; it builds the _struct module and then immediately afterwards my build environment throws an 'undefined reference to dlopen' error. To figure out what is missing dlopen, I need to somehow figure out what build_ext is trying to do. I've tried running the command manually with the --verbose option with no results. Does build_ext build modules in a certain order? If so, what is that order? -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Friday, January 16, 2015 at 11:26:46 PM UTC+5:30, Chris Angelico wrote:
> On Sat, Jan 17, 2015 at 4:49 AM, Tim wrote:
> > I want to get a union of all the values that any 'things' key may have,
> > even in a nested dictionary (and I do not know beforehand how deep the
> > nesting might go):
> >
> > d = {'things':1, 'two':{'things':2}}
> >
> > def walk(obj, res):
> > if not hasattr(obj, 'keys'):
> > return set(), set()
> >
> > if 'things' in obj:
> > res.add(obj['things'])
> >
> > for k in obj:
> > walk(obj[k], res)
> >
> > return res
> >
> > walk(d, set()) # returns {1, 2}
> >
> > Is it better to use a global to keep track of the values or does it even
> > matter?
>
> I would use a parameter rather than a global, but I'd make the
> parameter optional:
>
> def all_keys(obj, accum=None):
> if accum is None: accum=set()
> if 'things' in obj:
> res.add(obj['things'])
> for val in obj.values():
> all_keys(val, accum)
> return all_keys
>
> ChrisA
I dont like the hardwired this. However keeping that...
def all_keys(obj):
if not isinstance(obj, type({})): return set()
return ((set([obj['things']]) if 'things' in obj else set()) |
set(v for s in obj.values() for v in all_keys(s)))
--
https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
Tim wrote:
> I have this type of situation and wonder if I should use a global variable
> outside the recursive function instead of passing the updated parameter
> through.
>
> I want to get a union of all the values that any 'things' key may have,
> even in a nested dictionary (and I do not know beforehand how deep the
> nesting might go):
>
> d = {'things':1, 'two':{'things':2}}
>
> def walk(obj, res):
> if not hasattr(obj, 'keys'):
> return set(), set()
>
> if 'things' in obj:
> res.add(obj['things'])
>
> for k in obj:
> walk(obj[k], res)
>
> return res
>
> walk(d, set()) # returns {1, 2}
>
> Is it better to use a global to keep track of the values or does it even
> matter?
Globals are generally bad as they make code non-reentrant; when two calls of
the function run simultaneously the data will be messed up.
I recommend that you use a generator:
>>> def walk(obj):
... if not hasattr(obj, "keys"):
... return
... if "things" in obj:
... yield obj["things"]
... for v in obj.values():
... yield from walk(v)
...
>>> d = {'things':1, 'two':{'things':2}}
>>> set(walk(d))
{1, 2}
In Python before 3.3 you have to replace
yield from walk(v)
with a loop:
for t in walk(v):
yield t
--
https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote:
>> Tim wrote:
>>
> Globals are generally bad as they make code non-reentrant; when two calls of
> the function run simultaneously the data will be messed up.
>
> I recommend that you use a generator:
>
> >>> def walk(obj):
> ... if not hasattr(obj, "keys"):
> ... return
> ... if "things" in obj:
> ... yield obj["things"]
> ... for v in obj.values():
> ... yield from walk(v)
> ...
> >>> d = {'things':1, 'two':{'things':2}}
> >>> set(walk(d))
> {1, 2}
>
> In Python before 3.3 you have to replace
>
> yield from walk(v)
>
> with a loop:
>
> for t in walk(v):
> yield t
Ah, a generator, I wouldn't have seen the problem in this way, but with your
example, it looks so natural.
thanks,
--Tim
--
https://mail.python.org/mailman/listinfo/python-list
[ANN] Pylint 1.4.1 / Astroid 1.3.3 released
Hello, It's my pleasure to announce the release of both Pylint 1.4.1 and Astroid 1.3.3 respectively. The following bug fixes and features made their way into Astroid 1.3.3: * Restore file_stream to a property, but deprecate it in favour of the newly added method Module.stream. * Add inference tips for 'tuple', 'list', 'dict' and 'set' builtins. * Add brain definition for most string and unicode methods * Add a new method to Class nodes, 'mro', for obtaining the the method resolution order of the class. * Add brain tips for six.moves. Closes issue #63. * .slots() can contain unicode strings on Python 2. * Add inference tips for nose.tools. The complete list of changes can be seen here: https://bitbucket.org/logilab/astroid/src/15cff6016b793bb64c92d04acc4da001a2c98683/ChangeLog?at=default#cl-4 For Pylint, we have the following fixes and features: * Add a new JSON reporter, usable through -f flag. * Add a new warning, 'redundant-unittest-assert', emitted when using unittest's methods assertTrue and assertFalse with constant value as argument. * Check the return of properties when checking for not-callable. Closes issue #406. * Warn about using the input() or round() built-ins for Python 3. Closes issue #411. * Use a mro traversal for finding abstract methods. Closes issue #415. * Fix a false positive on Python 2 for raising-bad-type, when raising tuples in the form 'raise (ZeroDivisionError, None)'. * Fix a false positive with invalid-slots-objects, where the slot entry was an unicode string on Python 2. Closes issue #421. The complete list of changes can be found here: https://bitbucket.org/logilab/pylint/src/02eabae6b72569876e7104db7724ad26a7de0af3/ChangeLog?at=default#cl-4 If you find any bugs, don't hesitate to open a new issue on our issue tracker (https://bitbucket.org/logilab/pylint/) Enjoy! -- https://mail.python.org/mailman/listinfo/python-list
Re: lambdak: multi-line lambda implementation in native Python
Marko Rauhamaa wrote: Gregory Ewing : If those are 24-bit RGB pixels, you could encode 3 characters in each pixel. Not since Python3. Characters are Unicode now so you'll need to dedicate a pixel for each character. Depends on which characters you want. With the Flexible Chromatic Representation, it could be anything from 1 to 3. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Factories and Builders [was Re: lambdak...]
Steven D'Aprano wrote: I've never really understand why "abstract factory", "factory method" and "builder" are considered different design patterns. They're variants on the same idea. I think it's because they address different problems. Factories are for hiding the details of how an object is constructed. Builders are for working around the fact that there are no keyword arguments in your language. Builders are sometimes also used for algorithmic reasons; e.g. Java's StringBuilder exists to save you from O(n**2) behaviour when creating a string from many small parts. Factories of various kinds can be useful in Python; builders, not so much. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: lambdak: multi-line lambda implementation in native Python
Chris Angelico wrote: Is this to get around style guides that reject this kind of model: x = Foo( opt1=True, opt2=True, color=Yellow, ) It's to get around the fact that you *can't* do that in Java, because it doesn't have keyword arguments. This is a source of a lot of the complexity and boilerplate found in Java code -- the need to work around deficencies in the language. But if you can pass a mapping object to the constructor, you can do the same job that way, Yes, but constructing the mapping object is just as tedious. :-( you could pass an array of item,value,item,value,item,value or something. That's certainly possible, but then you have to write tedious code in the constructor to parse the arguments, you lose compile-time type safety, incur runtime overhead, etc. We're really quite spoiled in Python-land. It's easy to forget just *how* spoiled we are until you go back and try to do something in one of the more primitive languages... -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
Tim wrote: I have this type of situation and wonder if I should use a global variable outside the recursive function instead of passing the updated parameter through. No! Globals are evil, at least for that sort of thing. The way you're doing it is fine. The only thing I would change is to wrap it all up in a top-level function that takes care of creating the result set and returning it. def walk(obj): res = set() _walk(obj, res) return res def _walk(obj, res): ... -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Sat, Jan 17, 2015 at 9:20 AM, Gregory Ewing wrote: > The only thing I would change is to wrap it all up > in a top-level function that takes care of creating > the result set and returning it. > > def walk(obj): > res = set() > _walk(obj, res) > return res > > def _walk(obj, res): > ... Point of style: I like to put these kinds of helpers _above_ the corresponding public functions, to maintain a general policy of Define-Before-Use. Tends to make code easier to read; the first reference to a function name is its definition, then all usage comes after that. It's not always possible, of course (eg mutual recursion), but in simple cases like this, it's easy enough. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote:
> [...]
> I recommend that you use a generator:
>
> >>> def walk(obj):
> ... if not hasattr(obj, "keys"):
> ... return
> ... if "things" in obj:
> ... yield obj["things"]
> ... for v in obj.values():
> ... yield from walk(v)
Cool ... but it looks like this can still potentially hit the max
recursion limit? Perhaps better to convert to an iterative style:
def walk(obj):
"""
Yield any value(s) contained within `obj` that is (are) indexed by
the key 'things'. `obj` must be dict-like.
"""
from collections import deque
vals = deque()
vals.append(obj)
while True:
try: curr_obj = vals.popleft()
except IndexError: return
if not hasattr(curr_obj, "keys"): continue
if "things" in curr_obj: yield curr_obj["things"]
vals.extend(curr_obj.values())
# Examples
d1 = list(walk({ "things": 1, "two": { "things": 2 } }))
d2 = list(walk({
"things": 1,
"two": { "things": 2 },
"three":
{ "four": 4,
"things":
{ "five": 5,
"six": 6,
"things":
{ "seven": 7,
"things": 8 } } } }))
So this effectively 'flattens' a dictionary at each level into a queue
made up of the dictionary's values, and meanwhile yields the values one
by one if they are indexed by the key 'things'.
The output for `d1` should be the same as Peter Otten's example, except
I'm using a list instead of a set because I think the yielded objects
could themselves be dictionaries or other non-hashable values.
When you're looking at the output for `d2`, remember that `walk` here
will yield _any_ object that's indexed by the key, and as I mentioned,
that could be an entire dictionary object contained within the main one.
Regards,
Yawar
--
https://mail.python.org/mailman/listinfo/python-list
pyqtdeploy on windows
hi i have a code with pyqt4 and i want run in android with this tutorial http://pyqt.sourceforge.net/Docs/pyqtdeploy/command_line.html my project include of : m.py m.pyw m.ui when run pyqtdeploy myproject.pdy in cmd get error installing py 3.4.2 pyqt5 for pyqtdeploy any one know good tutorial for this module tnx -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Friday, January 16, 2015 at 9:24:15 PM UTC-5, Yawar Amin wrote: > [...] > vals.extend(curr_obj.values()) Ah, I should mention that the above will do a breadth-first search. If we want to do a depth-first search we simply replace the above line with: vals.extendleft(curr_obj.values()) Regards, Yawar -- https://mail.python.org/mailman/listinfo/python-list
