Stupid ways to spell simple code

C

Chris Angelico

There's a bit of a discussion on python-ideas that includes a function
that raises StopIteration. It inspired me to do something stupid, just
to see how easily I could do it...

... raise StopIteration

Here's a much more insane way to spell that:

stop = (lambda: 0 and (yield 1))().__next__

So, here's a challenge: Come up with something really simple, and
write an insanely complicated - yet perfectly valid - way to achieve
the same thing. Bonus points for horribly abusing Python's clean
syntax in the process.

Go on, do your worst!

ChrisA
 
R

Rick Johnson

So, here's a challenge: Come up with something really simple, and
write an insanely complicated - yet perfectly valid - way to achieve
the same thing. Bonus points for horribly abusing Python's clean
syntax in the process.

Chris, i'm sorry, but your challenge is decades too late. If you seek amusement you need look no further than the Python stdlib. If you REALLY want tobe amused, peruse the "idlelib" -- not only is the code obfuscated, it also breaks PEP8 and the PYTHON ZEN many times over.
 
J

Joshua Landau

Chris, i'm sorry, but your challenge is decades too late. If you seek amusement you need look no further than the Python stdlib. If you REALLY want to be amused, peruse the "idlelib" -- not only is the code obfuscated, it also breaks PEP8 and the PYTHON ZEN many times over.

To translate: Not only is the code impossible to read, inefficient and
unmaintainable whilst being only shallowly correct, but, *GASP* it
sometimes *breaks* semi-arbitrary code *style* *suggestions*! How dare it!‽
 
C

Chris Angelico

To translate: Not only is the code impossible to read, inefficient and
unmaintainable whilst being only shallowly correct, but, *GASP* it
sometimes *breaks* semi-arbitrary code *style* *suggestions*! How dare it!‽

Yeah, I cannot seriously imagine that the stdlib does anything like
the example I gave :) Pity nobody else is offering further examples, I
thought this might be a fun thread.

ChrisA
 
I

Ian Kelly

Yeah, I cannot seriously imagine that the stdlib does anything like
the example I gave :) Pity nobody else is offering further examples, I
thought this might be a fun thread.

Well, there is the "this" module. But its code is not *that*
obfuscated, and maintainability is not really an issue there since it
is short and does nothing of actual importance.
 
S

Steven D'Aprano

So, here's a challenge: Come up with something really simple, and write
an insanely complicated - yet perfectly valid - way to achieve the same
thing. Bonus points for horribly abusing Python's clean syntax in the
process.

Here's a trivially stupid way to find the length of an iterable:

sum(1 for obj in iterable)

(except it isn't actually stupid, if the iterable is an iterator, and you
don't mind consuming it to find out home many items it had). This leads
to an obvious obfuscation:

sum(('.' for obj in iterable), '').count('.')


but sadly Python, in a rare case of protecting you from shooting your own
foot off, doesn't allow this. Never mind, we can defeat the safety catch
on sum() and complicate the code at the same time:

sum(('.' for obj in iterable), type('S', (),
{'__add__': lambda self, o: o})()).count('.')


Obfuscated *and* quadratic performance. Do I win?

Wait, I can beat that. Sorting is too trivial in Python:

alist.sort()

Pfft! Where's the challenge in that? Let's use an O(n!) algorithm for
sorting -- yes, n factorial -- AND abuse a generator expression for its
side effect. As a bonus, we use itertools, and just for the lulz, I
obfuscate as many of the names as I can:


from random import shuffle as OOO00O
from itertools import takewhile as OO0O0O, count as O0OO0O

OO0O00 = range(5)

list(OO0O0O(lambda O: any(O[O0] < O[O0-1] for O0 in
range(1, sum(('O' for O in O), type('O', (),
{'__add__': lambda O0O, OO0: OO0})()).count('O'))),
(OOO00O(OO0O00) or OO0O00 for O in O0OO0O())))[0]
 
R

Roy Smith

Steven D'Aprano said:
Here's a trivially stupid way to find the length of an iterable:

sum(1 for obj in iterable)

That's how you would do it in a map-reduce environment :)
 
J

Joshua Landau

Pfft! Where's the challenge in that? Let's use an O(n!) algorithm for
sorting -- yes, n factorial -- AND abuse a generator expression for its
side effect. As a bonus, we use itertools, and just for the lulz, I
obfuscate as many of the names as I can:


from random import shuffle as OOO00O
from itertools import takewhile as OO0O0O, count as O0OO0O

OO0O00 = range(5)

list(OO0O0O(lambda O: any(O[O0] < O[O0-1] for O0 in
range(1, sum(('O' for O in O), type('O', (),
{'__add__': lambda O0O, OO0: OO0})()).count('O'))),
(OOO00O(OO0O00) or OO0O00 for O in O0OO0O())))[0]

I've got to say -- that last line got me for quite a while, especially
as the list of lists was returning a list of sorted lists!

For obfuscation, though, you want unicode. It's much harder to reason
about code that you can't read.

from itertools import count as æ, permutations as Æ, starmap as ð
[globals().setdefault("%c"%sum(ø.encode()),ß)for ø,ß in
vars(__builtins__).items()if ø!="vars"]
sorted = lambda ħ:ƿ(ƞ for ı in
φ(ƴ(lambda:ħ,æ),ń(0,ń(ħ)**(len(ħ)*2-1)*len(ħ)))for ƞ in Æ(ħ)if
ǂ(()).__eq__((ŕ(ð(ǂ(Ƽ(ƞ).pop()).__rpow__,φ(ŕ(œ(ƞ,ƴ(lambda:0,ħ)),()),1))),ħ),ı))[::-1]

print(sorted([4, -3, 4, 2, -1]))

This only works for integers (including negatives) and some things
that seem to do nothing but slow it down are needed to cover special
cases. This one takes about 3½ minutes on my computer -- it's got a
runtime efficiency of something along the lines of O( (L!) * ((L)**i)
) for i being related to the elements in the list and L being the
length of the list.

That said, it's rather OK at sorting [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].


I know this is more oriented towards obfuscation of code than
obfuscation of technique, but --as I said-- I'm writing a library for
that. I'll be back when it's ready.
 
N

Neil Cerutti

So, here's a challenge: Come up with something really simple,
and write an insanely complicated - yet perfectly valid - way
to achieve the same thing. Bonus points for horribly abusing
Python's clean syntax in the process.

Go on, do your worst!

I've often thought it was redundant for Python to support 'if'
when it has dictionaries, cf the rationale for having no
'switch'.

valid_name = None
while not valid_name:
name = input("Enter your name: ")
valid_name = {
True: lambda: print("No name longer than 20 letters."),
False: lambda: True,
}[len(name) > 20]()

Much better.
 
C

Chris Angelico

So, here's a challenge: Come up with something really simple,
and write an insanely complicated - yet perfectly valid - way
to achieve the same thing. Bonus points for horribly abusing
Python's clean syntax in the process.

Go on, do your worst!

I've often thought it was redundant for Python to support 'if'
when it has dictionaries, cf the rationale for having no
'switch'.

valid_name = None
while not valid_name:
name = input("Enter your name: ")
valid_name = {
True: lambda: print("No name longer than 20 letters."),
False: lambda: True,
}[len(name) > 20]()

Much better.

Good! Good! But, waaaah. Waaaah.

def get_name():
while True:
name = input("Enter your name: ")
yield {
True: lambda: print("No name longer than 20 letters."),
False: lambda: name,
}[len(name) > 20]()
name = next(filter(None,get_name()))

ChrisA
 
J

Joshua Landau

So, here's a challenge: Come up with something really simple,
and write an insanely complicated - yet perfectly valid - way
to achieve the same thing. Bonus points for horribly abusing
Python's clean syntax in the process.

Go on, do your worst!

I've often thought it was redundant for Python to support 'if'
when it has dictionaries, cf the rationale for having no
'switch'.

valid_name = None
while not valid_name:
name = input("Enter your name: ")
valid_name = {
True: lambda: print("No name longer than 20 letters."),
False: lambda: True,
}[len(name) > 20]()

Much better.

Good! Good! But, waaaah. Waaaah.

def get_name():
while True:
name = input("Enter your name: ")
yield {
True: lambda: print("No name longer than 20 letters."),
False: lambda: name,
}[len(name) > 20]()
name = next(filter(None,get_name()))

Oh, cruel. But you can do worse. Who needs "while" when you have
filter(iter(FUNCTION, object()))?

def get_name():
name = input("Enter your name: ")
return [
lambda: name,
lambda: print("No name longer than 20 letters."),
][len(name) > 20]()

name = next(filter(None, iter(get_name, object())))


But who needs *any* of this! Defining functions is so old-hat. It's
all already in the standard library (using only assignments and function-calls):

from functools import partial
from operator import getitem, ge, methodcaller
from itertools import compress, tee
apply = methodcaller("__call__")
ret_true = partial(getitem, [True], 0)
print_invalid = partial(print, "No name longer than 20 letters.")
inputs = iter(partial(input, "Enter your name: "), ...)
inputs, valid = tee(inputs)
valid = map(len, valid)
valid = map(partial(ge, 20), valid)
side_effect_valid = map(partial(getitem, [print_invalid, ret_true]), valid)
side_effect_valid = map(apply, side_effect_valid)
valid_inputs = compress(inputs, side_effect_valid)
name = next(valid_inputs)


Which can be "neatly" expressed as two statements (I'm struggling to
got it to one without those evil lambdas):

from functools import partial
from operator import getitem, ge, methodcaller
from itertools import compress, tee

inputs, valid = tee(iter(partial(input, "Enter your name: "), ...))

name = next(
compress(
inputs,
map(
methodcaller("__call__"),
map(
partial(
getitem,
[
partial(print, "No name longer than 20 letters."),
partial(getitem, [True], 0)
]
),
map(
partial(ge, 20),
map(len, valid)
)
)
)
)
)


Beautiful, see?


Of course, the most powerful function deals with this much more quickly:

exec("""
while True:
name = input("Enter your name: ")

if len(name) <= 20:
break

else:
print("No name longer than 20 letters.")
""")
 
M

Marcin Szamotulski

So, here's a challenge: Come up with something really simple,
and write an insanely complicated - yet perfectly valid - way
to achieve the same thing. Bonus points for horribly abusing
Python's clean syntax in the process.

Go on, do your worst!

I've often thought it was redundant for Python to support 'if'
when it has dictionaries, cf the rationale for having no
'switch'.

valid_name = None
while not valid_name:
name = input("Enter your name: ")
valid_name = {
True: lambda: print("No name longer than 20 letters."),
False: lambda: True,
}[len(name) > 20]()

Much better.

Good! Good! But, waaaah. Waaaah.

def get_name():
while True:
name = input("Enter your name: ")
yield {
True: lambda: print("No name longer than 20 letters."),
False: lambda: name,
}[len(name) > 20]()
name = next(filter(None,get_name()))

Oh, cruel. But you can do worse. Who needs "while" when you have
filter(iter(FUNCTION, object()))?

def get_name():
name = input("Enter your name: ")
return [
lambda: name,
lambda: print("No name longer than 20 letters."),
][len(name) > 20]()

name = next(filter(None, iter(get_name, object())))


But who needs *any* of this! Defining functions is so old-hat. It's
all already in the standard library (using only assignments and function-calls):

from functools import partial
from operator import getitem, ge, methodcaller
from itertools import compress, tee
apply = methodcaller("__call__")
ret_true = partial(getitem, [True], 0)
print_invalid = partial(print, "No name longer than 20 letters.")
inputs = iter(partial(input, "Enter your name: "), ...)
inputs, valid = tee(inputs)
valid = map(len, valid)
valid = map(partial(ge, 20), valid)
side_effect_valid = map(partial(getitem, [print_invalid, ret_true]), valid)
side_effect_valid = map(apply, side_effect_valid)
valid_inputs = compress(inputs, side_effect_valid)
name = next(valid_inputs)


Which can be "neatly" expressed as two statements (I'm struggling to
got it to one without those evil lambdas):

from functools import partial
from operator import getitem, ge, methodcaller
from itertools import compress, tee

inputs, valid = tee(iter(partial(input, "Enter your name: "), ...))

name = next(
compress(
inputs,
map(
methodcaller("__call__"),
map(
partial(
getitem,
[
partial(print, "No name longer than 20 letters."),
partial(getitem, [True], 0)
]
),
map(
partial(ge, 20),
map(len, valid)
)
)
)
)
)


Beautiful, see?


Of course, the most powerful function deals with this much more quickly:

exec("""
while True:
name = input("Enter your name: ")

if len(name) <= 20:
break

else:
print("No name longer than 20 letters.")
""")


Here is another example which I came across when playing with
generators, the first function is actually quite useful, the second
generator is the whole fun:

from functools import wraps
def init(func):
"""decorator which initialises the generator
"""
@wraps(func)
def inner(*args, **kwargs):
g = func(*args, **kwargs)
g.send(None)
return g
return inner

@init
def gen(func):
x = (yield)
while True:
x = (yield func(x))


now if you have function f
def f(arg):
return arg**2

then calling f(5) is the same as

g = gen(f)
g.send(5)


I wrote a blog post where I did include this as a `useless` example:
http://pycorner.herokuapp.com/blog/5

Best regards,
Marcin
 
S

Steven D'Aprano

Here is another example which I came across when playing with
generators, the first function is actually quite useful, the second
generator is the whole fun:

from functools import wraps
def init(func):
"""decorator which initialises the generator """
@wraps(func)
def inner(*args, **kwargs):
g = func(*args, **kwargs)
g.send(None)
return g
return inner

@init
def gen(func):
x = (yield)
while True:
x = (yield func(x))


now if you have function f
def f(arg):
return arg**2

then calling f(5) is the same as

g = gen(f)
g.send(5)



I think you must be missing an important part of the trick, because
calling f(5) returns 25. It's not:

@gen
def f(arg):
return arg**2


because that raises TypeError.
 
8

88888 Dihedral

Steven D'Apranoæ–¼ 2013å¹´7月2日星期二UTC+8上åˆ6時09分18秒寫é“:
I think you must be missing an important part of the trick, because

calling f(5) returns 25. It's not:



@gen

def f(arg):

return arg**2





because that raises TypeError.

Lets be serious about generators and iterators.

A generator can be used only once in a program
is different from a a generator method of a class that
can produce several instances with generators of the same kind
but operated in each instance of the class.
 
M

Marcin Szamotulski

I think you must be missing an important part of the trick, because
calling f(5) returns 25. It's not:

@gen
def f(arg):
return arg**2


because that raises TypeError.

Sure it does, you're now supposed to use .send method instead of
calling it but this is just different syntax. If you want to call it
use this :

def identity(func):
@init
def gen(func):
x = (yield)
while True:
x = (yield func(x))
return gen(func).send

Now you will get:
1

Best regards,
Marcin
 
R

Russel Walker

There's a bit of a discussion on python-ideas that includes a function

that raises StopIteration. It inspired me to do something stupid, just

to see how easily I could do it...




Re: [Python-ideas] "Iteration stopping" syntax
... raise StopIteration



Here's a much more insane way to spell that:



stop = (lambda: 0 and (yield 1))().__next__



So, here's a challenge: Come up with something really simple, and

write an insanely complicated - yet perfectly valid - way to achieve

the same thing. Bonus points for horribly abusing Python's clean

syntax in the process.



Go on, do your worst!



ChrisA

Here's a way to count items in a string.

def count(string, x):
return len(''.join(string)) - len(''.join(string).replace(x, '')) / len(x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top