write a loopin one line; process file paths

Xah Lee xah at xahlee.org
Wed Oct 19 03:50:11 EDT 2005


bonono at gmail.com wrote:
> 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
 xah at xahlee.orghttp://xahlee.org/

> like :
> >>> os.paths.exists(something) and "print it is there" or "file not found"
>
> 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.
>




More information about the Python-list mailing list