python code with indention

C

Carl Banks

Michael said:
Bernhard Herzog wrote:
....
a Turing Machine in one line plus assignments - nice! Turns out that pypy is more
verbose than strictly necessary ;-)
...


BTW, I realized that it is indeed possible for a LC to maintain its own state
without being passed an external mutable. The trick is to use itertools.repeat
to return the same mutable object on each iteration.

Pay attention, chief. I suggested this days ago to remove duplicates
from a list.

from itertools import *
[ x for (x,s) in izip(iterable,repeat(set()))
if (x not in s,s.add(x))[0] ]

;)

There is a way to even avoid repeat if you're feeling EVIL.

[ x for x in iterable if x not in locals()['_[1]'].__self__ ]

Turning this into a turing machine is left as an exercise. The recipe
in effect:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297
 
M

Michael Spencer

Carl said:
Pay attention, chief. I suggested this days ago to remove duplicates
from a list.

from itertools import *
[ x for (x,s) in izip(iterable,repeat(set()))
if (x not in s,s.add(x))[0] ]

;)
Sorry, I gave up on that thread after the first 10 Million* posts. Who knows
what other pearls I may have missed?

Anyway, the good news is that you appear to have identified a new design
pattern, and will soon become very famous:

According to:
http://www.cmcrossroads.com/bradapp/docs/patterns-nutshell.html#Patterns_What

A "pattern" is ...

* An abstraction from a concrete form which keeps recurring in specific,
non-arbitrary contexts. [twice in one week]

* A recurring solution to a common problem [perl-python spam] in a given
context and system of forces.

* A named "nugget" of instructive insight, conveying the essence of a
proven solution to a recurring problem in a given context amidst competing
concerns. [who could doubt it?]

* A successfully recurring "best practice" that has proven itself in the
"trenches". [of this list anyway]

* A literary format for capturing the wisdom and experience of expert
designers, and communicating it to novices [I think we're 5 for 5]


So, I would get the book out without further delay, before some other
Johnny-come-lately lays claim.

BTW, Do you have a 1-line-LC-wiki yet?

Michael


* with due respect to Marvin
 
C

Caleb Hattingh

You can, of course, write a silly little inline script without
any control structures that will all line up at the left
margain. So what?

John Roth

I agree, John, I don't get it. The vast majority of programmers (albiet
from my limited perspective) indent their code anyway, whether required by
the language or not. This was one of the first features of Python I
learned about that made me sit up and take notice -- here was a truly
pragmatic design choice that actually benefitted the programmer (by not
having to type all those delimiters).

Caleb
 
B

Bernhard Herzog

Michael Spencer said:
So, here's factorial in one line:
# state refers to list of state history - it is initialized to [1]
# on any iteration, the previous state is in state[-1]
# the expression also uses the trick of list.append() => None
# to both update the state, and return the last state
[state.append(state[-1] * symbol) or state[-1]
... for symbol, state in it.izip(range(1,10),it.repeat([1]))
... ]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

There's no need for repeat:
[state.append(state[-1] * symbol) or state[-1]
for state in [[1]]
for symbol in range(1, 10)]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]


While we're at it, a while back I posted a list comprehension that
implements a 'recursive' flatten:

http://groups.google.de/[email protected]


Bernhard
 
X

Xah Lee

i thought it is trivial for the Python parser to spit out a version
with matching brackets. Similarly, perhaps some opensourcing student
has modified a parser to read in a matching brackets delimited version
of Python.

Xah
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html
 
M

Michael Spencer

Bernhard said:
So, here's factorial in one line:
# state refers to list of state history - it is initialized to [1]
# on any iteration, the previous state is in state[-1]
# the expression also uses the trick of list.append() => None
# to both update the state, and return the last state
[state.append(state[-1] * symbol) or state[-1]
... for symbol, state in it.izip(range(1,10),it.repeat([1]))
... ]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]


There's no need for repeat:

[state.append(state[-1] * symbol) or state[-1]

for state in [[1]]
for symbol in range(1, 10)]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]


While we're at it, a while back I posted a list comprehension that
implements a 'recursive' flatten:

http://groups.google.de/[email protected]


Bernhard
Much better - that also cleanly extends to any number of initializers. I also
like the approach you take in flatten (and as suggested by Carl Banks) of
putting the update mechanism in the if clause

So that gives:

def factorial(n):
return [state[-1]
for state in [[1]]
for count in xrange(1,n+1)
if state.append(state[-1] * count) or True
]

Probably of limited practical value, but fun to explore the language.


Thanks
Michael
 
F

Fredrik Lundh

Xah said:
i thought it is trivial for the Python parser to spit out a version
with matching brackets. Similarly, perhaps some opensourcing student
has modified a parser to read in a matching brackets delimited version
of Python.

$ python Tools\scripts\pindent.py
You must specify -c(omplete), -d(elete) or -r(eformat)

usage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ...
-c : complete a correctly indented program (add #end directives)
-d : delete #end directives
-r : reformat a completed program (use #end directives)
-s stepsize: indentation step (default 8)
-t tabsize : the worth in spaces of a tab (default 8)
-e : expand TABs into spaces (defailt OFF)
[file] ... : files are changed in place, with backups in file~
If no files are specified or a single - is given,
the program acts as a filter (reads stdin, writes stdout).
 
S

Sean Blakey

i thought it is trivial for the Python parser to spit out a version
with matching brackets. Similarly, perhaps some opensourcing student
has modified a parser to read in a matching brackets delimited version
of Python.

Xah
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html

Well, there's always the pindent.py script. Get the python source, and
look at Tools/scripts/pindent.py

This isn't an alternate parser, but it can turn a python script with
end-block comments and no indentation into a correctly indented python
script.
 
C

Carl Banks

Bernhard said:
Michael Spencer said:
So, here's factorial in one line:
# state refers to list of state history - it is initialized to [1]
# on any iteration, the previous state is in state[-1]
# the expression also uses the trick of list.append() => None
# to both update the state, and return the last state
[state.append(state[-1] * symbol) or state[-1]
... for symbol, state in it.izip(range(1,10),it.repeat([1]))
... ]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

There's no need for repeat:
[state.append(state[-1] * symbol) or state[-1]
for state in [[1]]
for symbol in range(1, 10)]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]


Nope, that's just too convenient. Now I'm going to end up doing this
all the time.
 
G

Greg Ewing

Jeremy said:
I can't figure out how to write a TM in a Python List Comprehension
without one of either "variable binding" (so we can store the last symbol
list and manipulate it in the next iteration) or "recursive function" (to
express the whole tape as a recursive function), both of which require
statements.

Lambdas can give you one-line functions, local variable
binding, if-then-else capabilities, and recursion. Everything
else should be possible from there.

As a fellow named Church once pointed out, lambdas are really
*all* you need in a language...
 
S

Sion Arrowsmith

Greg Ewing said:
As a fellow named Church once pointed out, lambdas are really
*all* you need in a language...

.... where as others argue that it is impractical not to have
some form of runtime data storage, thereby giving rise to the
separation of Church and state.
 
G

Greg Ewing

Sion said:
... where as others argue that it is impractical not to have
some form of runtime data storage, thereby giving rise to the
separation of Church and state.
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top