write a loopin one line; process file paths

X

Xah Lee

is there a way to condense the following loop into one line?

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg',
u'/Users/t/web/Icons_dir/icon_sum.gif']

# change the image path to the full sized image, if it exists
# that is, if image ends in -s.jpg, find one without the '-s'.
temp=imgPaths[:]
imgPaths=[]
for myPath in temp:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(re.search(r'-s$',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths.append(p)

temp=[]
print imgPaths

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
X

Xah Lee

Xah said:
is there a way to condense the following loop into one line?

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg',
u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg',
u'/Users/t/web/Icons_dir/icon_sum.gif']

# change the image path to the full sized image, if it exists
# that is, if image ends in -s.jpg, find one without the '-s'.
temp=imgPaths[:]
imgPaths=[]
for myPath in temp:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(re.search(r'-s$',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths.append(p)

temp=[]
print imgPaths

this is of interest to me, because i wanted to illustrate functional
programing, and Python's short-coming of it. (if you go to wikipedia
and read the article on Python, it will proudly say that Python
supports functional programing. A huge fucking lie. (more fantastic
fucking lies in the Perl article. And it is impossible to correct them,
because there is an army of ignorant morons, usually militant too, not
shy to do justice for humanity with heartiness.) )

In functional programing proper, the following loop for example would
be a single line. Why? because in fp, one is focused on the input &
output and less on the details how it is done. So here, one simply
reads the comment there and that'd be it. The one-line code would be in
the form of impPaths = f(impPaths), where f is a function made on the
spot.

So, in fp the source code tends to take the form of paragraphs of
algorithmic units, as opposed one million lines of code filling the
page.

I would have, written the following in a one-line. But in Python this
is impossible. (if possible, probably something so unusual and
unreadable)

.... Here we may want to make a note on what does it mean to say that a
programing language supports functional programing. The FP style,
basically means that one writes pure functions and apply them to
expressions. That is, subroutines whose behavior are purely that of
input and output. (i.e. so-called without side-effects) But if one
programs in FP style, certain patterns of coding emerges. For example,
a lambda construct, sequencing of functions, assigning and moving of
functions, application of functions etc, and such languages naturally
developed many features in that direction such as types, macros, many
specialized loop constructs (recursion, nesting) and many other
features that goes by abstruse names...etc.

so here, if we judge Python (or Perl), why should we say it supports
functional programing? Simply because it is possible to define
subroutines without side-effects? What else, really, functional
language features does Python support? Can we really say, that a
functional programing coming to Python will find it reasonably supports
his FP coding style?

I would like to see the “also support functional programingâ€
removed in the wikipedia Python article.
( http://en.wikipedia.org/wiki/Python_programming_language )

(Perl the language, can arguably be said to support some functional
programing. (those Schwatchzian etc Transform for example, is a epitome
of functional programing.) However, Perl does not support
Object-Oriented programing.)

The Perl article is filled with more egregious lies. (In the past year)
I've tried to mend the article about every 3 months or so, tentatively
on obvious and technical and non-controversial items that's purely
propaganda but got denied by the Perl fuckfaces. I have written a full
page of criticism but the human animal fuckers handily censored it too.
If you think i have a point, please add this link to the Perl page's
external links.
http://xahlee.org/UnixResource_dir/perlr.html (book review and
criticism on Perl & community)

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
P

Peter Hansen

Xah said:
If you think i have a point, ...

You have neither that, nor a clue.

(Newsgroups line trimmed to reduce the effects of cross-posting trolls.)

-Peter
 
X

Xah Lee

Peter said:
You have neither that, nor a clue.

Dear Peter Hansen,

My messages speak themselfs. You and your cohorts's stamping of it does
not change its nature. And if this is done with repetitiousness, it
gives away your nature.

It is not necessary to shout against me. But if you must refute (and
that is reasonable), try to put content into your posts.
(see Philosophies of Netiquette at
http://xahlee.org/UnixResource_dir/writ/phil_netiquette.html)

If you deem fit, create a alt.fan.XahLee, and spare the rest of Python
community of your politics. I appreciate your fandom.

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
B

bonono

what do you mean by one line ? Using map/filter, I believe it is
possible.

Somthing like:

map(lambda (s,f): os.path.exists(f) and f or s,
map(lambda x: (x, re.replace(x, "-s","")), imgPaths)

My regex is a bit rusty but I hope you got the idea of what I am trying
to do. If there is a way to make the re return without destroying x,
the outer map is not needed I believe(that is run it twice, once for
getting the filename to do the testing, then again based on the testing
result).
 
P

Programmer in Chief

Xah said:
this is of interest to me,

That's why you are replying to yourself
I have written a full
page of criticism but the human animal fuckers handily censored it too.

Who would have predicted that? Maybe you should boycott them.

To those who came to this message looking for pornography, I am sorry
for the misleading keywords. Try adding "pictures" to your search
terms.
There is nothing to see here; just another keyboard wanker.

Python, Perl, Lisp, and Scheme all have one thing in common ---
the people who use them do not need any more of this drivel.

-- Programmer in Chief
 
X

Xah Lee

Thanks. Here's how the inner loop should be:

imgPaths2=map(lambda x: (x, re.sub( r"^(.+?)-s(\.[^.]+)$",r"\1\2", x)),
imgPaths)

though, now i just need something like

map( lambda x: os.path.exists(s)? x[1]:x[0],impPaths2)

but Pyhton doesn't support the
test ? trueResult : falseResult
construct.

(the semantic of this construct, of a conditional that RETURNS A
EXPRESSION, all in one line, is important in functional programing.
Perl supports it. In Mathematica, it's simply the form If[testExpr,
trueExpr, falseExpr]. In lisp, similar: (if testExpr trueExpr
falseExpr) )

is there a way to similate it?

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
B

bonono

it will be added in 2.5 I beleve. At the moment, you can:

predicate and <true expression> or <false expression>

like :
I am practicing FP in python and it is in general doable with the help
of itertools and add the needed functions as needed, like scanl/scanl1
etc.

A few things I don't like about python for FP is that it relies heavily
on exception which make these one liners not possible in many case.

Example:

a=[]
a[0] would raise exception

I usually prefer None as a special value which can be handled in normal
flow(filter out, may be "x is None and something or otherthing"). But
the liberal use of Exception force you to structure the program either
in a very odd way(test with hasattr/haskey etc. before use) or needs
lots of try/except blocks. I am using a python XHTML template Kid which
relies on one-liner and found it very difficult.

However, if you don't really need one-liner but just FP style to
shorten the program, it is fine but I just find the try/except block
quite ugly.

Then there is the side effect of iterators/generators which unless you
know can burn you badly(especially when you are chaining these objects
in these multiple map/filter calls), as mentioned in another post.

Xah said:
Thanks. Here's how the inner loop should be:

imgPaths2=map(lambda x: (x, re.sub( r"^(.+?)-s(\.[^.]+)$",r"\1\2", x)),
imgPaths)

though, now i just need something like

map( lambda x: os.path.exists(s)? x[1]:x[0],impPaths2)

but Pyhton doesn't support the
test ? trueResult : falseResult
construct.

(the semantic of this construct, of a conditional that RETURNS A
EXPRESSION, all in one line, is important in functional programing.
Perl supports it. In Mathematica, it's simply the form If[testExpr,
trueExpr, falseExpr]. In lisp, similar: (if testExpr trueExpr
falseExpr) )

is there a way to similate it?

Xah
(e-mail address removed)
∑ http://xahlee.org/

what do you mean by one line ? Using map/filter, I believe it is
possible.

Somthing like:

map(lambda (s,f): os.path.exists(f) and f or s,
map(lambda x: (x, re.replace(x, "-s","")), imgPaths)

My regex is a bit rusty but I hope you got the idea of what I am trying
to do. If there is a way to make the re return without destroying x,
the outer map is not needed I believe(that is run it twice, once for
getting the filename to do the testing, then again based on the testing
result).
 
X

Xah Lee

it will be added in 2.5 I beleve. At the moment, you can:

predicate and <true expression> or <false expression>

Ah, i see. Here's the full code again:

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u'/Users/t/t4/oh/DSCN2059m-s.jpg',
u'/Users/t/t4/oh/DSCN2062m-s.jpg', u'/Users/t/t4/oh/DSCN2097m-s.jpg',
u'/Users/t/t4/oh/DSCN2099m-s.jpg', u'/Users/t/Icons_dir/icon_sum.gif']

# change the image path to the full sized image, if it exists
# that is, if image ends in -s.jpg, find one without the '-s'.

imgPaths2=[]
for myPath in imgPaths:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(re.search(r'-s$',fileBaseName,re.U)):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths2.append(p)

imgPaths3 = map( lambda x: os.path.exists(x[1]) and x[1] or x[0],
map(lambda x: (x, re.sub( r"^(.+?)-s(\.[^.]+)$",r"\1\2", x)),
imgPaths))

print imgPaths2 == imgPaths3

------------------
A few things I don't like about python for FP is that it relies heavily
on exception which make these one liners not possible in many case.
...

yeah i know how it feels. I haven't had much experience with Python
yet... but doing it in Perl gets all twisted. (especially when one
tries to use trees as data structure (nested lists) in Perl)

besides syntactical issues, another thing with doing functional
programing in imperative languages is that they become very inefficent.
Functional languages are optimized by various means of functional
programings styles. Doing them in imperative languages usually comes
with a order of execution penalty because imperative language compilers
are usually dumb.

though, one can't totally blame for Python's lack of ability to do
functional programing. Its language's syntax of using indentations for
blocks by design, pretty much is in odds with functional programing's
sequencing of functions and other ways. (such as generic mechanism for
prefix/postfix syntax, or macros or other transformations and patterns,
or heavy reliance on the free flow of expressions/values)

I don't blame Python that it doesn't cater to functional programing BY
DESIGN. But i do hate the mother fucking fuckheads Pythoners for one
thing that they don't know what functional programing really is, and on
the other hand fucking trumpet their righteousness and lies thru their
teeth of their ignorance. (that Guido guy with his Python 3000 article
on his blog is one example, possibly forgivable in that particular
instance. (http://xahlee.org/perl-python/python_3000.html))

(excuse me for lashing out)

Xah
(e-mail address removed)
∑ http://xahlee.org/
like :
I am practicing FP in python and it is in general doable with the help
of itertools and add the needed functions as needed, like scanl/scanl1
etc.

A few things I don't like about python for FP is that it relies heavily
on exception which make these one liners not possible in many case.

Example:

a=[]
a[0] would raise exception

I usually prefer None as a special value which can be handled in normal
flow(filter out, may be "x is None and something or otherthing"). But
the liberal use of Exception force you to structure the program either
in a very odd way(test with hasattr/haskey etc. before use) or needs
lots of try/except blocks. I am using a python XHTML template Kid which
relies on one-liner and found it very difficult.

However, if you don't really need one-liner but just FP style to
shorten the program, it is fine but I just find the try/except block
quite ugly.

Then there is the side effect of iterators/generators which unless you
know can burn you badly(especially when you are chaining these objects
in these multiple map/filter calls), as mentioned in another post.
 
B

bonono

Xah said:
besides syntactical issues, another thing with doing functional
programing in imperative languages is that they become very inefficent.
Functional languages are optimized by various means of functional
programings styles. Doing them in imperative languages usually comes
with a order of execution penalty because imperative language compilers
are usually dumb.

The itertools module and the new generator feature tries to make this
better as it is lazily evaluated, in a sense. But its side effect is
nasty. Usable though need to twist my mind a bit, not like when I do it
in haskell. But the occasion imperative dip makes some of my programs
easier to code.
though, one can't totally blame for Python's lack of ability to do
functional programing. Its language's syntax of using indentations for
blocks by design, pretty much is in odds with functional programing's
sequencing of functions and other ways. (such as generic mechanism for
prefix/postfix syntax, or macros or other transformations and patterns,
or heavy reliance on the free flow of expressions/values)

That I am not sure. haskell also use this indentation but I don't see a
problem about it. One thing I find it odd though is the @decorator
construct. I just don't understand why it is viewed as simple. It
becomes very ugly IMO when there is a number of it, I am doing some web
apps which requres varies decorators(one over the other).

@format
@login_block
@validate
@something
def my_func():

I find it easier to read and under stand in the form of

format(login_block(validate(something(my_func))))) though haskell's $
and . is even better.

format.login_block.validate.somethin.my_func
I don't blame Python that it doesn't cater to functional programing BY
DESIGN. But i do hate the mother fucking fuckheads Pythoners for one
thing that they don't know what functional programing really is, and on
the other hand fucking trumpet their righteousness and lies thru their
teeth of their ignorance. (that Guido guy with his Python 3000 article
on his blog is one example, possibly forgivable in that particular
instance. (http://xahlee.org/perl-python/python_3000.html))

That seems to be the case, I mean the design. For fairness, list
comphrehension sometimes looks cleaner, though becomes ugly when you do
the pipe/chain like programming in FP(multiple for). This again is an
odd choice. It seems to be a flattening of multiple line for loop, yet
at the same time python seems to be moving towards being more
explicit(no map/filter/etc as they can be done in for loop).

But I believe Python is designed for easy to code and read and maintain
in mind. One has to admit that without some training, FP is not very
intuitive, my head spin when I see haskell code. A for loop is easier
to understand.

Well, if you want clean FP, you can always try haskell which is getting
better and better in terms of real world module support(file system,
network etc).
 
X

Xah Lee

Thanks a lot for various notes. Bonono?

I will have to look at the itertools module. Just went to the doc
http://www.python.org/doc/2.4.1/lib/module-itertools.html
looks interesting.
But I believe Python is designed for easy to code and read and maintain
in mind.
One has to admit that without some training, FP is not very
intuitive, my head spin when I see haskell code. A for loop is easier
to understand.

This i'm not sure. Of the past couple of years i increasingly developed
a theory (probably well-known among proper experts), that the
difficulty of human feats of various forms, are primarily a perception
and familiarity thing. This may be getting off topic, but i wrote an
essay expresising much of the idea using Juggling as a example:
Difficulty Perceptions in Human Feats
http://xahlee.org/Periodic_dosage_dir/t2/juggling.html

likewise, i think this applies to mental feats as well. In particular,
i think that whether imperative code or functional code is easier for
the mind is almost ENTIRELY dependent on which one the person is more
familiar with, coulped with a innate attitude one may have picked up.
Well, if you want clean FP, you can always try haskell which is getting
better and better in terms of real world module support(file system,
network etc).

oh Haskell, my love! I am really going to learn it now. (maybe i'll
start A-Java-Haskell-A-Day) This month i just learned and read about
how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
single-handedly by happenstance tried to do it, as a by-product of
learning Haskell) This Pugs (Perl6 in Haskell) really brought two
rather incompatible communities together somewhat for mutual exchange.
(the learning, on the surface, is politely said to be mutual, but i'm
pretty sure it's mostly Perlers learning from the Haskell folks)

.... there is a sentiment among the elite tech-geeking morons, early on
imbued by the concept of troll, so that they in general don't
communicate and learn from any other language except their own.
Anything cross-posted is considered as troll, and the inter-language
communication has been essentially completely cut off. Basically, the
only ones generating all the garbage posts are these troll-criers
themselves. (will have to flesh out on this particular point of
net-sociology in a essay some other day.)

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
B

bonono

Xah said:
This i'm not sure. Of the past couple of years i increasingly developed
a theory (probably well-known among proper experts), that the
difficulty of human feats of various forms, are primarily a perception
and familiarity thing. This may be getting off topic, but i wrote an
essay expresising much of the idea using Juggling as a example:
Difficulty Perceptions in Human Feats
http://xahlee.org/Periodic_dosage_dir/t2/juggling.html

likewise, i think this applies to mental feats as well. In particular,
i think that whether imperative code or functional code is easier for
the mind is almost ENTIRELY dependent on which one the person is more
familiar with, coulped with a innate attitude one may have picked up.

But most of us start learning programming with imperative language, I
started with COBOL and Pascal(are they still taught). Then we would hit
the problem of looping one way or another pretty soon. Without a doubt,
some experienced programmer would tell you to use for loop or search
for one. This is even true in two heavily used declarative tools, SQL
and Excel. If there is foldl/scanl/map in SQL, I think it would be much
easier to code. Likewise for Excel, it is very functional(like its
ancestors 1-2-3) then VBA was thrown in.

In fact, I think haskell should be taught in any CS course as it opens
up a completely different way of approaching problem and it is easier
to read than LISP.
oh Haskell, my love! I am really going to learn it now. (maybe i'll
start A-Java-Haskell-A-Day) This month i just learned and read about
how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
single-handedly by happenstance tried to do it, as a by-product of
learning Haskell) This Pugs (Perl6 in Haskell) really brought two
rather incompatible communities together somewhat for mutual exchange.
(the learning, on the surface, is politely said to be mutual, but i'm
pretty sure it's mostly Perlers learning from the Haskell folks)
After seeing Haskell, I don't think I would go back to Perl(which I
like better than python but python has the momentum as a general
purpose language). That is why I am doing the think in haskell, code in
python, whenever possible.
... there is a sentiment among the elite tech-geeking morons, early on
imbued by the concept of troll, so that they in general don't
communicate and learn from any other language except their own.
Anything cross-posted is considered as troll, and the inter-language
communication has been essentially completely cut off. Basically, the
only ones generating all the garbage posts are these troll-criers
themselves. (will have to flesh out on this particular point of
net-sociology in a essay some other day.)
May be you can tone down a bit if you want a constructive discussion. I
am too old to have feelings about opinionated posts(and insensitive to)
so I can go through the technical stuff. Confucius said "I can always
learn something whenever there is 3 people getting together", lousy
translation :)
 
J

Jeremy Jones

Xah said:
Peter Hansen wrote:



Dear Peter Hansen,

My messages speak themselfs. You and your cohorts's stamping of it does
not change its nature. And if this is done with repetitiousness, it
gives away your nature.

It is not necessary to shout against me. But if you must refute (and
that is reasonable), try to put content into your posts.
(see Philosophies of Netiquette at
http://xahlee.org/UnixResource_dir/writ/phil_netiquette.html)
Xah,

Thanks for the comic relief of this link. The first item of comedy came
from the following two sentences:

'''
Then at the other extreme is the relatively rare Victorian propensity
where each post is a gem of literature carefully crafted and researched
for an entire century of readers to appreciate and archive. Xah, Erik
Naggum, and [censored] posts are exemplary of this style, to name a few
acquaintances like myself.
'''

I really don't know which is funnier, that you stated these sentences at
all, or that you probably believe them. Several things disqualify you
from gaining my classification of "scholarly" (not that you give a fart
what I think):

- poor spelling
- poor grammar
- rambling style with lack of cohesive thought
- non-interesting, non-original ideas in your posts
- invalid or incorrect points in your discourse

The next piece of humor came from these sentences:

'''
Go to a newsgroup archive such as dejanews.com and search for your
favorite poster. If you find a huge quantity of terse posts that is
tiring, boring, has little content, and in general requires you to
carefully follow the entire thread to understand it, then you know
you've bumped into a conversationalist.
'''

By your definition, you mostly fit into the "conversationalist"
category. The only thing that may keep you out of that category is that
your ramblings are typically lengthy. So, what you provide is a large
number of lengthy, tiring, boring, content-less, non-cohesive posts.
Funny that you bash "the conversationalists" when you have so much in
common with them.

The third point of humor in this link was the paypal link at the top of
the page:

'''
If you spend more than 30 minutes on this site, please send $1 to me. Go
to http://paypal.com/ and make a payment to (e-mail address removed). Or send to:
P. O. Box 390595, Mountain View, CA 94042-0290, USA.
'''

It's humorous to think of anyone spending more than 30 minutes on your
site (apart from the obvious stunned amazement at the content, quite
like the "can't stop watching the train wreck" phenomenon). It's even
more humorous to think of anyone gaining value from it. But I wouldn't
be surprised to hear that some people have actually sent you money.
If you deem fit, create a alt.fan.XahLee, and spare the rest of Python
community of your politics. I appreciate your fandom.

Xah
(e-mail address removed)
∑ http://xahlee.org/

sorry-folks-for-feeding-the-troll-ly y'rs,

- jmj
 
J

John Thingstad

Thanks a lot for various notes. Bonono?

I will have to look at the itertools module. Just went to the doc
http://www.python.org/doc/2.4.1/lib/module-itertools.html
looks interesting.



This i'm not sure. Of the past couple of years i increasingly developed
a theory (probably well-known among proper experts), that the
difficulty of human feats of various forms, are primarily a perception
and familiarity thing. This may be getting off topic, but i wrote an
essay expresising much of the idea using Juggling as a example:
Difficulty Perceptions in Human Feats
http://xahlee.org/Periodic_dosage_dir/t2/juggling.html

likewise, i think this applies to mental feats as well. In particular,
i think that whether imperative code or functional code is easier for
the mind is almost ENTIRELY dependent on which one the person is more
familiar with, coulped with a innate attitude one may have picked up.


oh Haskell, my love! I am really going to learn it now. (maybe i'll
start A-Java-Haskell-A-Day) This month i just learned and read about
how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
single-handedly by happenstance tried to do it, as a by-product of
learning Haskell) This Pugs (Perl6 in Haskell) really brought two
rather incompatible communities together somewhat for mutual exchange.
(the learning, on the surface, is politely said to be mutual, but i'm
pretty sure it's mostly Perlers learning from the Haskell folks)

... there is a sentiment among the elite tech-geeking morons, early on
imbued by the concept of troll, so that they in general don't
communicate and learn from any other language except their own.
Anything cross-posted is considered as troll, and the inter-language
communication has been essentially completely cut off. Basically, the
only ones generating all the garbage posts are these troll-criers
themselves. (will have to flesh out on this particular point of
net-sociology in a essay some other day.)

Xah
(e-mail address removed)
∑ http://xahlee.org/

Honestly.. "your programming skills suck"

John
 
G

Graham Fawcett

Xah said:
Dear Peter Hansen,
My messages speak themselfs. You and your cohorts's stamping of it does
not change its nature. And if this is done with repetitiousness, it
gives away your nature.

Taunt not the cohorts of Peter Hansen!

Graham
 

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,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top