Differences between revisions 8 and 9
Revision 8 as of 2004-09-19 19:05:21
Size: 7462
Editor: 218
Comment:
Revision 9 as of 2004-09-20 05:24:41
Size: 8357
Editor: dknrgwpxav02
Comment: Categorisation + yet more bad code
Deletions are marked like this. Additions are marked like this.
Line 25: Line 25:
Please Feel Free to extend with more examples. Please Feel Free to add/reorder/reallocate examples.
Line 43: Line 43:
=== Normal Printing/Concatination ===
Line 47: Line 49:

{{{
#!python
print "Just "\
      "another "\
      "Pythoneer"
}}}

{{{
#!python
j = "Just "
a = "another "
p = "Pythoneer"
print j+a+p
}}}

{{{
#!python
import sys; sys.stdout.write("Just another Pythoneer\n")
}}}

=== Substitution ===
Line 62: Line 86:
import sys; sys.stdout.write("Just another Pythoneer\n")
}}}
class JAPy(int):
    def __str__(self):
        return "%s"*5%("Just",chr(self),"another", chr(self), "Pythoneer")
print JAPy(32)
}}}

{{{
#!python
class JAPy(int): pass
def s(s): s+=1; return "Just" + chr(s) + "another" + chr(s) + "Pythoneer"
n, JAPy.__str__ = JAPy(31), s
print n
}}}

{{{
#!python
print "Just another Perl Hacker".replace("Perl Hacker", "Pythoneer")
}}}

=== Reversal ===
Line 86: Line 127:
import pickle; print pickle.loads("S'Just another Pythoneer'\012p0\012.") print "".join([x for x in list('reenohtyP rehtona tsuJ')[::-1]])
}}}

{{{
#!python
print "".join([x for x in list('reenohtyP rehtona tsuJ')][::-1])
}}}

=== Iteration ===

{{{
#!python
import sys
for c in "Just another Pythoneer\n":
    sys.stdout.write(c)
}}}

{{{
#!python
import sys; [sys.stdout.write(c) for c in "Just another Pythoneer\n"]
}}}

{{{
#!python
x = ["Just", "another", "Pythoneer"]
while x: print x[0], ; x = x[1:]
print
}}}

{{{
#!python
x = ["Pythoneer", "another", "Just",]
while x: print x[-1], ; x.pop()
print
}}}

{{{
#!python
import random, sys; x = "Just another Pythoneer\n"
while x:
  y = random.choice(x);
  if y == x[0]: sys.stdout.write(y); x = x[1:]
}}}

=== Slicing and Indexing ===
{{{
#!python
print "Just another Pythoneer and not a Perl Hacker".split(" and")[0]
}}}

{{{
#!python
print "I'm just another Pythoneer".split(" ",1)[1].capitalize()
}}}

{{{
#!python
import re
a = ["Pythoneer", "extra", "Just", "words", "another", "Hello!"]
m = re.match("(?P<three>\d+).(?P<one>\d+).(?P<two>\d+)", "1A5l2")
print a[m.start("one")], a[m.start("two")], a[m.start("three")]
}}}

{{{
#!python
#Sometimes_code_does_not_really_need_to_have_syntacally_significant_whitespace
g,u,n=range(3),"Why don't I use Java or Perl?",lambda(x),y:reduce(x.__add__,y)
print"".join([u[P:H+P]for(H,P)in(zip(n(list,n(tuple,[([d],n(list,[[1+c%2]*(c+1
)for(c)in(g)]))for(d)in(g)])[1:]),[16,12,8,19,6,5,8,1,25,23,2,8,1,5,14,25]))])
}}}

{{{
#!python
import platform as p; d=[list(x)for x in open(p.__file__[:-1])]; a=d[28]+d[12]
for f,t in zip([0,5,7,8,10,12,19,20,21,22],[15,6,9,14,19,41,25,24,23,99]):
    del a[f:t]; a[8]=a[8].lower()
print "".join(a)
}}}

=== Recursion ===

{{{
#!python
japy = lambda x: isinstance(x,dict) and x.keys()[0] + japy(x.values()[0]) or x
print japy({"Just ": {"another ": "Pythoneer"}})
Line 97: Line 222:
{{{
#!python
japy = lambda x: isinstance(x,dict) and x.keys()[0] + japy(x.values()[0]) or x
print japy({"Just ": {"another ": "Pythoneer"}})
}}}

{{{
#!python
import sys
for c in "Just another Pythoneer\n":
    sys.stdout.write(c)
}}}

{{{
#!python
import sys; [sys.stdout.write(c) for c in "Just another Pythoneer\n"]
}}}
=== Decoding ===
Line 125: Line 233:
x = ["Just", "another", "Pythoneer"]
while x: print x[0], ; x = x[1:]
print
}}}

{{{
#!python
x = ["Pythoneer", "another", "Just",]
while x: print x[-1], ; x.pop()
print
}}}

{{{
#!python
import re
a = ["Pythoneer", "extra", "Just", "words", "another", "Hello!"]
m = re.match("(?P<three>\d+).(?P<one>\d+).(?P<two>\d+)", "1A5l2")
print a[m.start("one")], a[m.start("two")], a[m.start("three")]
d,o=0x3B55094199ACFB742DB37F3F97B11175C0C8EL,[]
while d:
    d,m=divmod(d,100);o.append(chr(m+32))
print "".join(o)
}}}

{{{
#!python
def japy(d=0x3B55094199ACFB742DB37F3F97B11175C0C8EL):
    while d:
        d,m=divmod(d,100);yield m+32
print "".join([chr(x) for x in japy()])
}}}

{{{
#!python
import pickle; print pickle.loads("S'Just another Pythoneer'\012p0\012.")
}}}

{{{
#!python
print u"Whfg nabgure Clgubarre".encode('rot13')
}}}

{{{
#!python
d,n=dict(zip("0123456789ABx"," aehnosturyPJ")), 10157833755785421529629225
print "".join([d[c] for c in str(hex(n))[:-1]]).strip()
}}}

=== Exception Handling ===

{{{
#!python
try:
  vars()["Just another Pythoneer"]
except KeyError, e:
  print eval(str(e))
Line 155: Line 283:
import random, sys; x = "Just another Pythoneer\n"
while x:
  y = random.choice(x);
  if y == x[0]: sys.stdout.write(y); x = x[1:]
try: __import__("Just another Pythoneer")
except ImportError, m: print str(m)[-22:]
}}}

=== Introspection ===

{{{
#!python
"""Just another Pythoneer"""
print __doc__
}}}

{{{
#!python
class Print:
    def __call__(self): print
    def __getattr__(self, name): print name, ; return self
Print().Just.another.Pythoneer()
}}}

{{{
#!python
class JAPy: pass
def i(self,x="Just another Pythoneer"): pass
def s(self): return self.__init__.func_defaults[0]
print JAPy.__dict__.update({"__init__":i,"__str__":s}) or JAPy()
}}}

{{{
#!python
class func_defaults:
    func_defaults = "Just another Pythoneer";func_defaults=lambda \
    func_defaults = func_defaults:func_defaults.func_defaults.func_defaults[0]
print func_defaults.func_defaults(func_defaults())
}}}

{{{
class func_defaults:
    func_defaults = "Just another Pythoneer"
    def func_defaults(func_defaults=func_defaults):pass
func_defaults,=func_defaults().func_defaults.func_defaults;print func_defaults
}}}

{{{
#!python
class Just_another_Pythoneer(object):
    def __init__(self):
        print " ".join(self.__class__.__name__.split("_"))
Just_another_Pythoneer()
}}}

{{{
#!python
def Just_another_Pythoneer(fn):
    print fn.func_name.replace("_"," ")
Just_another_Pythoneer(Just_another_Pythoneer)
}}}

{{{
#!python
def _(Just, another, Pythoneer):_
print " ".join(_.func_code.co_varnames)
Line 169: Line 355:
{{{
#!python
class Print:
    def __call__(self): print
    def __getattr__(self, name): print name, ; return self
Print().Just.another.Pythoneer()
}}}

{{{
#!python
#Sometimes_code_does_not_really_need_to_have_syntacally_significant_whitespace
g,u,n=range(3),"Why don't I use Java or Perl?",lambda(x),y:reduce(x.__add__,y)
print"".join([u[P:H+P]for(H,P)in(zip(n(list,n(tuple,[([d],n(list,[[1+c%2]*(c+1
)for(c)in(g)]))for(d)in(g)])[1:]),[16,12,8,19,6,5,8,1,25,23,2,8,1,5,14,25]))])
}}}

{{{
#!python
class Just_another_Pythoneer(object):
    def __init__(self):
        print " ".join(self.__class__.__name__.split("_"))
Just_another_Pythoneer()
}}}

{{{
#!python
def Just_another_Pythoneer(fn):
    print fn.func_name.replace("_"," ")
Just_another_Pythoneer(Just_another_Pythoneer)
}}}

{{{
#!python
def _(Just, another, Pythoneer):_
print " ".join(_.func_code.co_varnames)
}}}

{{{
#!python
d,o=0x3B55094199ACFB742DB37F3F97B11175C0C8EL,[]
while d:
    d,m=divmod(d,100);o.append(chr(m+32))
print "".join(o)
}}}

{{{
#!python
def japy(d=0x3B55094199ACFB742DB37F3F97B11175C0C8EL):
    while d:
        d,m=divmod(d,100);yield m+32
print "".join([chr(x) for x in japy()])
}}}

{{{
#!python
print "".join([x for x in list('reenohtyP rehtona tsuJ')[::-1]])
}}}

{{{
#!python
print "".join([x for x in list('reenohtyP rehtona tsuJ')][::-1])
}}}

{{{
#!python
print "Just another Pythoneer and not a Perl Hacker".split(" and")[0]
}}}

{{{
#!python
print "I'm just another Pythoneer".split(" ",1)[1].capitalize()
}}}

{{{
#!python
print u"Whfg nabgure Clgubarre".encode('rot13')
}}}

{{{
#!python
import platform as p; d=[list(x)for x in open(p.__file__[:-1])]; a=d[28]+d[12]
for f,t in zip([0,5,7,8,10,12,19,20,21,22],[15,6,9,14,19,41,25,24,23,99]):
    del a[f:t]; a[8]=a[8].lower()
print "".join(a)
}}}

{{{
#!python
d,n=dict(zip("0123456789ABx"," aehnosturyPJ")), 10157833755785421529629225
print "".join([d[c] for c in str(hex(n))[:-1]]).strip()
}}}

{{{
#!python
try: __import__("Just another Pythoneer")
except ImportError, m: print str(m)[-22:]
}}}

{{{
#!python
class JAPy(int):
    def __str__(self):
        return "%s"*5%("Just",chr(self),"another", chr(self), "Pythoneer")
print JAPy(32)
}}}

{{{
#!python
class JAPy(int): pass
def s(s): s+=1; return "Just" + chr(s) + "another" + chr(s) + "Pythoneer"
n, JAPy.__str__ = JAPy(31), s
print n
}}}

{{{
#!python
class JAPy: pass
def i(self,x="Just another Pythoneer"): pass
def s(self): return self.__init__.func_defaults[0]
print JAPy.__dict__.update({"__init__":i,"__str__":s}) or JAPy()
}}}

{{{
#!python
class func_defaults:
    func_defaults = "Just another Pythoneer";func_defaults=lambda \
    func_defaults = func_defaults:func_defaults.func_defaults.func_defaults[0]
print func_defaults().func_defaults()
}}}

{{{
#!python
"""Just another Pythoneer"""
print __doc__
}}}

{{{
#!python
try:
  vars()["Just another Pythoneer"]
except KeyError, e:
  print eval(str(e))
=== Runtime Code ===
{{{
#!python
exec "print 'Just another Pythoneer'"
}}}

{{{
#!python
x = compile("print 'Just another Pythoneer'\n", "japy", "single")
eval(x)

Discussion

Perl coders are proud that Perl's motto is "There's more than one way to do it."BR In comparison, Python is sometimes stereotyped as "There's only one way to do it", but this is of course not true. The Zen of Python, accessed via

import this

includes the maxims:BR Beautiful is better than ugly.BR Explicit is better than implicit.BR Simple is better than complex.BR Complex is better than complicated.BR Flat is better than nested.BR Sparse is better than dense.BR Readability counts.BR If the implementation is hard to explain, it's a bad idea.BR There should be one-- and preferably only one --obvious way to do it.BR

The net effect is that there is a limited set of good solutions for a given problem, and users are strongly encouraged to use them.

While Python attempts to discourage bad practices, and its indentation-based block structure and limited anonymous blocks (lambdas) make certain techniques difficult, it is possible to produce many more bad solutions than good for a given problem.

Please Feel Free to add/reorder/reallocate examples.

The Problem

Your signature block (up to four lines of 78 characters each) needs code that prints "Just another Pythoneer".

The Limited Set of Good Solutions

   1 print "Just another Pythoneer"

(Though someone may point out that as a Python user following correct Pythonic practice, you are more correctly a Pythonista... and give you a brief 6000 word essay on the historical debate over the naming of Python Users and the etymology of the root Python and the stem -ista... and advise you against using the combination in an impromptu Spanish examination)

The Larger Set of Not-So-Good Solutions

Normal Printing/Concatination

   1 print "Just", "another", "Pythoneer"

   1 print "Just "\
   2       "another "\
   3       "Pythoneer"

   1 j = "Just "
   2 a = "another "
   3 p = "Pythoneer"
   4 print j+a+p

   1 import sys; sys.stdout.write("Just another Pythoneer\n")

Substitution

   1 print '''%s %s %s''' % ("Just", "another", "Pythoneer")

   1 print '%(j)s %(a)s %(py)s' % { "j": "Just", "a": "another", \
   2   "pl": "Perl Hacker", "py": "Pythoneer", "c": "C Coder", \
   3   "f": "Fortran Freak", "turtle" : "Logo Lover" }

   1 class JAPy(int):
   2     def __str__(self):
   3         return "%s"*5%("Just",chr(self),"another", chr(self), "Pythoneer")
   4 print JAPy(32)

   1 class JAPy(int): pass
   2 def s(s): s+=1; return "Just" + chr(s) + "another" + chr(s) + "Pythoneer"
   3 n, JAPy.__str__ = JAPy(31), s
   4 print n

   1 print "Just another Perl Hacker".replace("Perl Hacker", "Pythoneer")

Reversal

   1 x = ['r','e','e','n','o','h','t','y','P',' ','r','e','h','t','o','n','a',' ',
   2      't','s','u','J']
   3 x.reverse()
   4 print "".join(x)

   1 print "".join(['r','e','e','n','o','h','t','y','P',' ','r','e','h','t','o','n',
   2                'a',' ','t','s','u','J'][::-1])

   1 print "reenohtyP rehtona tsuJ"[::-1]

   1 print "".join([x for x in list('reenohtyP rehtona tsuJ')[::-1]])

   1 print "".join([x for x in list('reenohtyP rehtona tsuJ')][::-1])

Iteration

   1 import sys
   2 for c in "Just another Pythoneer\n":
   3     sys.stdout.write(c)

   1 import sys; [sys.stdout.write(c) for c in "Just another Pythoneer\n"]

   1 x = ["Just", "another", "Pythoneer"]
   2 while x: print x[0], ; x = x[1:]
   3 print

   1 x = ["Pythoneer", "another", "Just",]
   2 while x: print x[-1], ; x.pop()
   3 print

   1 import random, sys; x = "Just another Pythoneer\n"
   2 while x:
   3   y = random.choice(x);
   4   if y == x[0]: sys.stdout.write(y); x = x[1:]

Slicing and Indexing

   1 print "Just another Pythoneer and not a Perl Hacker".split(" and")[0]

   1 print "I'm just another Pythoneer".split(" ",1)[1].capitalize()

   1 import re
   2 a = ["Pythoneer", "extra", "Just", "words", "another", "Hello!"]
   3 m = re.match("(?P<three>\d+).(?P<one>\d+).(?P<two>\d+)", "1A5l2")
   4 print a[m.start("one")], a[m.start("two")], a[m.start("three")]

   1 #Sometimes_code_does_not_really_need_to_have_syntacally_significant_whitespace
   2 g,u,n=range(3),"Why don't I use Java or Perl?",lambda(x),y:reduce(x.__add__,y)
   3 print"".join([u[P:H+P]for(H,P)in(zip(n(list,n(tuple,[([d],n(list,[[1+c%2]*(c+1
   4 )for(c)in(g)]))for(d)in(g)])[1:]),[16,12,8,19,6,5,8,1,25,23,2,8,1,5,14,25]))])

   1 import platform as p; d=[list(x)for x in open(p.__file__[:-1])]; a=d[28]+d[12]
   2 for f,t in zip([0,5,7,8,10,12,19,20,21,22],[15,6,9,14,19,41,25,24,23,99]):
   3     del a[f:t]; a[8]=a[8].lower()
   4 print "".join(a)

Recursion

   1 japy = lambda x: isinstance(x,dict) and x.keys()[0] + japy(x.values()[0]) or x
   2 print japy({"Just ": {"another ": "Pythoneer"}})

   1 def k(x):
   2     if type(x) == type({}): return x.keys()[0] + k(x.values()[0])
   3     return x
   4 print k({"Just ": {"another ": "Pythoneer"}})

Decoding

   1 import uu, StringIO
   2 s = StringIO.StringIO(); uu.decode(StringIO.StringIO('begin 666 '+\
   3   '-\01262G5S="!A;F]T:&5R(%!Y=&AO;F5E<@  \012 \012end\012'), s)
   4 print s.getvalue()

   1 d,o=0x3B55094199ACFB742DB37F3F97B11175C0C8EL,[]
   2 while d:
   3     d,m=divmod(d,100);o.append(chr(m+32))
   4 print "".join(o)

   1 def japy(d=0x3B55094199ACFB742DB37F3F97B11175C0C8EL):
   2     while d:
   3         d,m=divmod(d,100);yield m+32
   4 print "".join([chr(x) for x in japy()])

   1 import pickle; print pickle.loads("S'Just another Pythoneer'\012p0\012.")

   1 print u"Whfg nabgure Clgubarre".encode('rot13')

   1 d,n=dict(zip("0123456789ABx"," aehnosturyPJ")), 10157833755785421529629225
   2 print "".join([d[c] for c in str(hex(n))[:-1]]).strip()

Exception Handling

   1 try:
   2   vars()["Just another Pythoneer"]
   3 except KeyError, e:
   4   print eval(str(e))

   1 try:
   2   raise "Just another Pythoneer"
   3 except:
   4   print sys.exc_info()[0]

   1 try: __import__("Just another Pythoneer")
   2 except ImportError, m: print str(m)[-22:]

Introspection

   1 """Just another Pythoneer"""
   2 print __doc__

   1 class Print:
   2     def __call__(self): print
   3     def __getattr__(self, name): print name, ; return self
   4 Print().Just.another.Pythoneer()

   1 class JAPy: pass
   2 def i(self,x="Just another Pythoneer"): pass
   3 def s(self): return self.__init__.func_defaults[0]
   4 print JAPy.__dict__.update({"__init__":i,"__str__":s}) or JAPy()

   1 class func_defaults:
   2     func_defaults = "Just another Pythoneer";func_defaults=lambda \
   3     func_defaults = func_defaults:func_defaults.func_defaults.func_defaults[0]
   4 print func_defaults.func_defaults(func_defaults())

class func_defaults:
    func_defaults = "Just another Pythoneer"
    def func_defaults(func_defaults=func_defaults):pass
func_defaults,=func_defaults().func_defaults.func_defaults;print func_defaults

   1 class Just_another_Pythoneer(object):
   2     def __init__(self):
   3         print " ".join(self.__class__.__name__.split("_"))
   4 Just_another_Pythoneer()

   1 def Just_another_Pythoneer(fn):
   2     print fn.func_name.replace("_"," ")
   3 Just_another_Pythoneer(Just_another_Pythoneer)

   1 def _(Just, another, Pythoneer):_
   2 print " ".join(_.func_code.co_varnames)

   1 def f(z):
   2   '''Just another Pythoneer (and remember to document your code!)'''
   3   pass
   4 print f.__doc__[:22]

Runtime Code

   1 exec "print 'Just another Pythoneer'"

   1 x = compile("print 'Just another Pythoneer'\n", "japy", "single")
   2 eval(x)

Credits

AndrewDalkeBR HansNowakBR "Snippet 181"BR TaroOgawaBR KazuoMoriwakaBR "just another Pythoneer"

JustAnotherPythoneer (last edited 2008-11-15 14:01:16 by localhost)

Unable to edit the page? See the FrontPage for instructions.