Re: Fun python 3.2 one-liner
In article , Daniel Fetchinson wrote: >>> what is the character limit on a one liner :P. >> >> For PEP 8 compliance, 80 characters. :-) > >Yeah, but we don't live in the 80's or 90's anymore and our screens >can support xterms (or let alone IDE widows) much wider than 80 >characters. I'm using 140 for python these days. Seriously, who would >want to limit him/herself to 80 characters in 2011? I want to limit myself to 72 char's for readability. 80 char's is over the top. > >Cheers, >Daniel > Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On 04/06/11 01:07, Steven D'Aprano wrote: > On Tue, 05 Apr 2011 15:38:28 +0200, Daniel Fetchinson wrote: > Personally, I find that the discipline of keeping to 80 characters is > good for me. It reduces the temptation of writing obfuscated Python one- > liners when two lines would be better. The *only* time it is a burden is > when I write doc strings, and even then, only a small one. Unless the editor I'm using has an 80-char autowrapping or a 80-char guiding lines, I tend to wrap docstring in ~40-60 char. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On the right hand side of my gmail window, Google posited that I might be interested in "One-liner jokes". And I have to confess, the first thing I thought of was "So I was writing a one-liner in assembly and..." ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
Raymond Hettinger writes: > On Apr 5, 6:38 am, Daniel Fetchinson > wrote: > > Yeah, but we don't live in the 80's or 90's anymore and our screens > > can support xterms (or let alone IDE widows) much wider than 80 > > characters. I'm using 140 for python these days. Seriously, who > > would want to limit him/herself to 80 characters in 2011? A broadsheet newspaper allows a whole lot of characters on a single line, yet they still limit the width of each passage of text. The limit on line length is not only for technical reasons, but for readability. > I wonder how many people will shorten otherwise clear > variable names just to get their lines to fit in 80 characters? Not me. When the line is over 78 characters or so, I just break the line at an appropriate point. Line breaks (within bracketing syntax) are far less expensive than uncommunicative variable names. -- \ “I must have a prodigious quantity of mind; it takes me as much | `\ as a week sometimes to make it up.” —Mark Twain, _The Innocents | _o__) Abroad_ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Apr 5, 6:38 am, Daniel Fetchinson wrote: > >> what is the character limit on a one liner :P. > > > For PEP 8 compliance, 80 characters. :-) > > Yeah, but we don't live in the 80's or 90's anymore and our screens > can support xterms (or let alone IDE widows) much wider than 80 > characters. I'm using 140 for python these days. Seriously, who would > want to limit him/herself to 80 characters in 2011? I wonder how many people will shorten otherwise clear variable names just to get their lines to fit in 80 characters? Raymond -- http://mail.python.org/mailman/listinfo/python-list
Indentation levels and line lengths (was Re: Fun python 3.2 one-liner)
On Wed, Apr 6, 2011 at 3:48 AM, Steven D'Aprano wrote: >> and has on occasion gone as far as 12-16. > > I would consider anything more than four indents a code smell. That is, > four is unexceptional; five would make me look over the code to see if it > could be refactored; six would make me look at it *carefully*; eight > would have me questioning my own sanity. I wouldn't give a hard limit > beyond which I would "never" go beyond, but I find it difficult to > imagine what drugs I'd need to be on to go beyond eight. > > *wink* Chocolate. Chocolate and heavy C code work. The alternative is to break everything out into separate functions, which inevitably results in heaps of unnecessary parameter-passing; granted, a C compiler will often optimize most of it away, but it's still annoying to have to pass a whole lot of reference parameters to a function that's only ever called from one place. As an example, think of a typical GUI message loop (in OS/2 or Windows): int WndProc(HWND hwnd,int msg,int param1,int param2) { switch (msg) { case WM_PAINT: { if (have_message) { display_message(); y+=msg_height; } } //etc, etc, etc default: return 0; } } That's four levels of indentation right there. Now imagine that the whole kaboodle is inside a class definition and that's a fifth, and then it only takes a small amount of extra code complexity to hit eight. I agree that it's a code smell, though. For something to get to the 10 or 12 that I mentioned above, it really has to be something with a whole lot of relatively insignificant indentations early on, so the entire "useful logic" is contained in levels 6-10 or something; and the 16 was probably a little artificial, in that I *could* have refactored that function but just never got around to it (which means it's pushing the limits of "legitimately"). But I know I've had C code get past 10 without there being any logical reason to break some of the code out. The other time I have miles of indentation is when I'm working in an unfamiliar language, and I'm not comfortable with its variable scoping rules (hello there, PHP). That's not what I'd call "legitimate" indentation, but I sure am glad there's no hard limit on what's accepted. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
what is the character limit on a one liner :P. >>> >>> For PEP 8 compliance, 80 characters. :-) >> >> Yeah, but we don't live in the 80's or 90's anymore and our screens can >> support xterms (or let alone IDE widows) much wider than 80 characters. >> I'm using 140 for python these days. Seriously, who would want to limit >> him/herself to 80 characters in 2011? > > Seriously, or is that a rhetorical question? Rhetorical :) What it intended to demonstrate is that it is possible to rationalize ancient habits which have their origins in particular circumstances of those ancient times but actually nothing necessitates them today other than the momentum built around them during their lifetime (which is a pretty strong argument for them by the way). Cheers, Daniel > People who like to have two source files side-by-side on a standard > sized monitor, or three on a wide-screen monitor. > > People who need to read or edit source code on a virtual terminal rather > than in a GUI console app. > > People who print out source code to read later. > > People with poor vision who need to use a significantly larger sized > characters, and therefore can't fit as many on a line. > > People who might want to email code snippets without having them > inconveniently wrapped by the mail client. > > People who don't like reading really long, overly complex, lines, and > prefer to keep lines short for readability. > > And most importantly... people who want to have their code accepted into > the Python standard library. > > > Personally, I find that the discipline of keeping to 80 characters is > good for me. It reduces the temptation of writing obfuscated Python one- > liners when two lines would be better. The *only* time it is a burden is > when I write doc strings, and even then, only a small one. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Wed, 06 Apr 2011 01:19:06 +1000, Chris Angelico wrote: > On Wed, Apr 6, 2011 at 1:07 AM, Steven D'Aprano > wrote: >> On Tue, 05 Apr 2011 15:38:28 +0200, Daniel Fetchinson wrote: >> >>> Seriously, who would want to limit >>> him/herself to 80 characters in 2011? >> >> Seriously, or is that a rhetorical question? >> >> People who like to have two source files side-by-side on a standard >> sized monitor, or three on a wide-screen monitor. >> >> And most importantly... people who want to have their code accepted >> into the Python standard library. > > Is that 80 including indentation, or excluding? And if including, does > that put a hard limit of 20 indentation levels for standard library > code? Including. As for the hard limit, pretty much. > Only partly tongue-in-cheek. I have code that quite legitimately has > gone to ten tabs in, and stayed there, "Legitimately"? I very much doubt it. (Only half joking.) > and has on occasion gone as far as 12-16. I would consider anything more than four indents a code smell. That is, four is unexceptional; five would make me look over the code to see if it could be refactored; six would make me look at it *carefully*; eight would have me questioning my own sanity. I wouldn't give a hard limit beyond which I would "never" go beyond, but I find it difficult to imagine what drugs I'd need to be on to go beyond eight. *wink* -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Tue, 2011-04-05 at 15:38 +0200, Daniel Fetchinson wrote: > Yeah, but we don't live in the 80's or 90's anymore and our screens > can support xterms (or let alone IDE widows) much wider than 80 > characters. I'm using 140 for python these days. Seriously, who would > want to limit him/herself to 80 characters in 2011? I'd rather have two files open with 80 columns in them than a single file with 160 columns and have to switch between files. Tim Wintle -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Wed, Apr 6, 2011 at 1:07 AM, Steven D'Aprano wrote: > On Tue, 05 Apr 2011 15:38:28 +0200, Daniel Fetchinson wrote: > >> Seriously, who would want to limit >> him/herself to 80 characters in 2011? > > Seriously, or is that a rhetorical question? > > People who like to have two source files side-by-side on a standard > sized monitor, or three on a wide-screen monitor. > > And most importantly... people who want to have their code accepted into > the Python standard library. Is that 80 including indentation, or excluding? And if including, does that put a hard limit of 20 indentation levels for standard library code? Only partly tongue-in-cheek. I have code that quite legitimately has gone to ten tabs in, and stayed there, and has on occasion gone as far as 12-16. Generally I just scroll my display horizontally and ignore the preceding tab levels. And sometimes I cheat a bit, in C or Pike, and don't indent at some level; if it's a loop (or more likely a switch) that surrounds the entire function, I might do: void functionname(parameters) {while (cond) { //code at one indent, not two }} Can't do that in Python - for better or for worse. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Tue, 05 Apr 2011 15:38:28 +0200, Daniel Fetchinson wrote: >>> what is the character limit on a one liner :P. >> >> For PEP 8 compliance, 80 characters. :-) > > Yeah, but we don't live in the 80's or 90's anymore and our screens can > support xterms (or let alone IDE widows) much wider than 80 characters. > I'm using 140 for python these days. Seriously, who would want to limit > him/herself to 80 characters in 2011? Seriously, or is that a rhetorical question? People who like to have two source files side-by-side on a standard sized monitor, or three on a wide-screen monitor. People who need to read or edit source code on a virtual terminal rather than in a GUI console app. People who print out source code to read later. People with poor vision who need to use a significantly larger sized characters, and therefore can't fit as many on a line. People who might want to email code snippets without having them inconveniently wrapped by the mail client. People who don't like reading really long, overly complex, lines, and prefer to keep lines short for readability. And most importantly... people who want to have their code accepted into the Python standard library. Personally, I find that the discipline of keeping to 80 characters is good for me. It reduces the temptation of writing obfuscated Python one- liners when two lines would be better. The *only* time it is a burden is when I write doc strings, and even then, only a small one. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
>> what is the character limit on a one liner :P. > > For PEP 8 compliance, 80 characters. :-) Yeah, but we don't live in the 80's or 90's anymore and our screens can support xterms (or let alone IDE widows) much wider than 80 characters. I'm using 140 for python these days. Seriously, who would want to limit him/herself to 80 characters in 2011? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
Chris Angelico writes: >> def f(x,n,w): return x if n==1 else\ >> (lambda x0=f(x[::2],n/2,w[::2]),\ >> x1=f(x[1::2],n/2,w[::2]): reduce(lambda a,b: a+b ,\ >> zip(*[(x0[k]+w[k]*x1[k],\ >> x0[k]-w[k]*x1[k])\ >> for k in range(n/2)])))() > What sort of parameters does this take? So far all I can figure out > is that n is an integer and x and w are sliceables, but I'm not sure > whether x and w should be strings or arrays. def direct_fft(x,n): return f(x,n,[exp(-2*pi*1j*k/n) for k in range(n/2)]) def inverse_fft(x,n): return [x/n for x in f(x,n,[exp(+2*pi*1j*k/n) for k in range(n/2)])] -- le mie sacrosante questioni di principio VS gli sciocchi puntigli di quel cretino del mio vicino -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On 4/4/2011 3:16 PM Gregory Ewing said... Chris Angelico wrote: (Remind me how it is that Python code is more readable than line noise or Perl code?) Crazy thought: I wonder if Perl programmers have "multi line Perl" competitions where they laugh their heads off at how readable the code is, and how nobody in their right mind would ever write Perl code that way?-) I don't know if I should be laughing or nodding in agreement... :) Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Tue, Apr 5, 2011 at 8:16 AM, Gregory Ewing wrote: > Chris Angelico wrote: > > Crazy thought: I wonder if Perl programmers have "multi > line Perl" competitions where they laugh their heads off > at how readable the code is, and how nobody in their > right mind would ever write Perl code that way?-) Ha!! Okay, now I have to explain to my fellow bus passengers what it is that I just cracked up laughing at... You know what? I don't think I can. I do like readable code, but quite a few of my favorite language features are the ones commonly (ab)used to make unreadable code. C's ?: operator, Pike's interpretation of || for defaults, Python's lambda functions... all great ways to shorten your code, but so easily used for evil. I think I like things that can be used for evil. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
Chris Angelico wrote: (Remind me how it is that Python code is more readable than line noise or Perl code?) Crazy thought: I wonder if Perl programmers have "multi line Perl" competitions where they laugh their heads off at how readable the code is, and how nobody in their right mind would ever write Perl code that way?-) -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Tue, Apr 5, 2011 at 6:09 AM, gb wrote: > harrismh777 writes: > >> Seriously, these little one liners teach me more about the python >> language in less time than [...] > > def f(x,n,w): return x if n==1 else\ > (lambda x0=f(x[::2],n/2,w[::2]),\ > x1=f(x[1::2],n/2,w[::2]): reduce(lambda a,b: a+b ,\ > zip(*[(x0[k]+w[k]*x1[k],\ > x0[k]-w[k]*x1[k])\ > for k in range(n/2)])))() > > it was a joke of sort played on it.comp.lang.python (Remind me how it is that Python code is more readable than line noise or Perl code?) What sort of parameters does this take? So far all I can figure out is that n is an integer and x and w are sliceables, but I'm not sure whether x and w should be strings or arrays. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
harrismh777 writes: > Seriously, these little one liners teach me more about the python > language in less time than [...] def f(x,n,w): return x if n==1 else\ (lambda x0=f(x[::2],n/2,w[::2]),\ x1=f(x[1::2],n/2,w[::2]): reduce(lambda a,b: a+b ,\ zip(*[(x0[k]+w[k]*x1[k],\ x0[k]-w[k]*x1[k])\ for k in range(n/2)])))() it was a joke of sort played on it.comp.lang.python [thanks to marco (zip(*...)) and antonio (lambda with default arguments)] -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
Martin De Kauwe wrote: what is the character limit on a one liner :P. For PEP 8 compliance, 80 characters. :-) -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Mar 30, 2:19 am, Martin De Kauwe wrote: > what is the character limit on a one liner :P. Very interesting > jesting apart, any more? Sure, here are three one-liners using itertools.groupby() to emulate some Unix pipelines: sort letters | uniq # list unique values sort letters | uniq -c# count unique values sort letters | uniq -d# find duplicates >>> from itertools import groupby >>> [k for k, g in groupby(sorted('abracadabra'))] ['a', 'b', 'c', 'd', 'r'] >>> [(k, len(list(g))) for k, g in groupby(sorted('abracadabra'))] [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] >>> [k for k, g in groupby(sorted('abracadabra')) if len(list(g)) > 1] ['a', 'b', 'r'] Raymond P.S. Of course, there are many ways to do this. >>> sorted(set('abracadabra')) ['a', 'b', 'c', 'd', 'r'] >>> sorted(Counter('abracadabra').items()) [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] >>> sorted(k for k,c in Counter('abracadabra').items() if c > 1) ['a', 'b', 'r'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Wed, Mar 30, 2011 at 8:19 PM, Martin De Kauwe wrote: > what is the character limit on a one liner :P. Very interesting > jesting apart, any more? Not sure if this can be redone as a one-liner; currently it's two. for i in range(3): print '\n\t"'+("minor","medium","major")[i]+' ":({\n\t\t'+"\n\t\t".join([q[0]+',"'+" ".join(q[1:])+'",' for q in [q.split("\x96")[-1].split(" ") for q in "\n".join([q.split(" ",3)[i]+" "+q.split(" ",3)[3] for q in a.split("\n")]).replace("\x92","'").split("\n")] if q[0]!="\x97"])+'\n\t}),' That's the code I used in IDLE to translate this: Minor Medium Major Ring Market Price 01–18 — — Protection +1 2,000 gp 19–28 — — Feather falling 2,200 gp 61–70 01–05 — Counterspells 4,000 gp 71–75 06–08 — Mind shielding 8,000 gp 86–90 24–28 — Ram 8,600 gp — 29–34 — Climbing, improved 10,000 gp — 35–40 — Jumping, improved 10,000 gp — 41–46 — Swimming, improved 10,000 gp 91–93 47–51 — Animal friendship 10,800 gp 94–96 50–56 01–02 Energy resistance, minor 12,000 gp 99–100 62–66 — Water walking 15,000 gp — 94–97 29–32 Blinking 27,000 gp — 98–100 33–39 Energy resistance, major 28,000 gp — — 40–49 Protection +4 32,000 gp — — 98 Elemental command (fire) 200,000 gp — — 99 Elemental command (water) 200,000 gp — — 100 Spell storing, major 200,000 gp into something that my dice-roller can use. (In the interests of brevity I've chopped a whole lot of the table out, but each column contains every possible value from 01 to 100.) Not as "cool" as the previous one, but it sure was handy! In case you're wondering: Yes, that is Dungeons and Dragons. I run an online D&D server. Nerd? Only slightly. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
what is the character limit on a one liner :P. Very interesting jesting apart, any more? -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On 3/29/2011 5:50 AM, Raymond Hettinger wrote: from collections import Counter from itertools import product print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum, product(range(6), repeat=8))).items( The line break makes that hard to read; the axis is not labeled (and labels help understand the code). So my suggested revision is: from collections import Counter from itertools import product print('\n'.join('{:2d}: {}'.format(i, '*'*(c//2000)) for i,c in sorted( Counter(map(sum,product(range(6), repeat=8))).items() ))) 0: 1: 2: 3: 4: 5: 6: 7: * 8: *** 9: * 10: 11: 12: ** 13: * 14: 15: * 16: * 17: 18: ** 19: ** 20: *** 21: ** 22: ** 23: 24: * 25: * 26: 27: * 28: ** 29: 30: 31: * 32: *** 33: * 34: 35: 36: 37: 38: 39: 40: -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
Raymond Hettinger wrote: almost-normally-yours, Raymond thanks ... interesting Seriously, these little one liners teach me more about the python language in less time than *all* of the books I'm trying to digest right now. The toughest part of learning python is learning about what's available so as not to reinvent the proverbial wheel... thanks again... fun. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
On Tue, Mar 29, 2011 at 11:24 AM, Raymond Hettinger wrote: print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum, product(range(6), repeat=8))).items( > > > > > > > > > * > *** > * > > > ** > * > > * > * > > ** > ** > *** > ** > ** > > * > * > > * > ** > > > * > *** > * > > > > Re-posting (forgot to attach the output). > Besides the interesting output, other > interesting virtues include very low > memory usage and that the inner-loop > pipeline runs entirely in C. Another (related) 3.2 one liner: from math import erf, sqrt def normal_cdf(x, mu=0, sigma=1): return (1/2) * (1 + erf((x-mu)/(sigma*sqrt(2 approximates the cumulative distribution function of the normal distribution. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
>>> print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum, >>> product(range(6), repeat=8))).items( * *** * ** * * * ** ** *** ** ** * * * ** * *** * Re-posting (forgot to attach the output). Besides the interesting output, other interesting virtues include very low memory usage and that the inner-loop pipeline runs entirely in C. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: Fun python 3.2 one-liner
http://www.ideone.com/infch ^ Result of the below code On 29 March 2011 19:50, Raymond Hettinger wrote: > from collections import Counter > from itertools import product > > print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum, > product(range(6), repeat=8))).items( > > > almost-normally-yours, > > Raymond > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Fun python 3.2 one-liner
from collections import Counter from itertools import product print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum, product(range(6), repeat=8))).items( almost-normally-yours, Raymond -- http://mail.python.org/mailman/listinfo/python-list