Re: tree functions daily exercise: Table

2005-06-24 Thread xah
Dear Dr Jon D Harrop,

“List Comprehension” is a special concise syntax form that
generates lists, as a way to save typing such as for loops. It is
primarily a jargon bandied about by programers which have contributed
huge misunderstandings and miscommunications. The extensiveness of this
particular jargon's harm has also encroached into functional programing
languages as to cause irregular syntax and less powerful semantics. The
utility of “list comprehension” is just a (normal) function
designed to generate list.

References:
• A exposition of list comprehension in Python.
http://xahlee.org/perl-python/list_comprehension.html

• Jargons of Info Tech Industry
http://xahlee.org/UnixResource_dir/writ/jargons.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

Jon Harrop wrote:
> David Hopwood wrote:
> > That's consistent with the behaviour of list comprehensions in other
> > languages -- including "set/tuple formers" in SETL
> > <http://www.cs.nyu.edu/~bacon/setl-doc.html>, which I believe was the
> > first language to support them. I don't know of any language where
> > a list comprehension with multiple loops creates nested lists.
>
> I'm not entirely sure what a list comprehension is, but Mathematica might be
> an example of a language in which multiple loops create nested lists.
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy
> http://www.ffconsultancy.com

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: tree functions daily exercise

2005-05-16 Thread xah
K!
http://en.wikipedia.org/wiki/K_programming_language

Interesting.
Looking at your program, they are so short. I don't know if they are
full implementation or what...

Btw, the functions listed at
 http://xahlee.org/PerlMathematica_dir/Matica.html
are motly from Mathematica, except the Tree Index Set Utilities:
   RandomIndexSet, LeavesIndexSet, NonleavesIndexSet, MinimumIndexSet,
   CompleteIndexSet, IndexSetSort, TreeToIndexSet, IndexSetToTree

which isn't trivial to code. (nor is it trivial for some of the
Mathematica functions)
the complete documentation can be seen online
http://documents.wolfram.com/mathematica/

 Xah
 [EMAIL PROTECTED]
â http://xahlee.org/


sa wrote:
> xah:
>
> i've provided k implementations here:
>
> http://www.nsl.com/k/xah.k
>
> of a dozen or so of the functions in your toolkit.  many are trivial,
> since they're simply k primitives.  e.g. transpose is just +:
>
> here are some of the definitions (the script contains test-examples
> as well):
>
> range1:!:
> range2:{x+!1+y-x}
> range3:{x+z*!1+_(y-x)%z}
> depth:{:[(@x)|~#x;0;1+|/_f'x]}
> leaves:,//
> leafcount:#leaves@
> leafpaths:{:[EMAIL PROTECTED];,();,/(!#x),/:'_f'x]}
> subpaths:{(1+!#x)#\:x}
> nonleafpaths:?,/subpaths'-1_'leafpaths@
> nonleafcount:#nonleafpaths@
> nodecount:+/(leafcount;nonleafcount)@\:
> dimensions:^:
> part:.
> transpose:+:
> apply:{y{x'}/x}
> flattenat:{apply[,/;y]x}
> rotateleft:1!
>
> my immediate reaction is that a well-designed set of primitives
> obviates the need for such a toolkit.
> 
> nb: i've eliminated the newsgroup crossposting.

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python's doc problems: sort

2008-05-21 Thread Xah
alex23 wrote:
«No, what was generally rejected was the idea that *you* could bring
more clarity to the documentation, based on the complete absence of it
in your posts & "essays". Basically, noone wanted docs that would
randomly degenerate into ad hominem accusations of elitism aimed at
the module authors.»

Dear Alex moron number 23,

The art of writing takes many forms.

Let's take, the hallowed name Shakespeare.

Imagine, we injest his writing style into modern journalism. What is
the resulting quality? It would be condemned as the worst journalism
possible. Imprecise, logorrhea, obscure, and in general, much ado
about nothing.

On the other hand, if we apply the spirit of scientific report into
poetry, what is the resulting quality? Dry, inert, dead. A man without
a cock, a woman without tits.

You see, i had to craft my style to fit occasions. In dealing with
newsgroup morons like yourself, the proper style is to infuse insult
with fanfare.

Further readings:

The Tragedy Of Titus Andronicus
 http://xahlee.org/p/titus/titus.html

Politics and the English Language
 http://xahlee.org/p/george_orwell_english.html

English Lawers (or, how to master writing)
 http://xahlee.org/UnixResource_dir/writ/english_lawers.html

  Xah
  [EMAIL PROTECTED]
∑ http://xahlee.org/

☄
--
http://mail.python.org/mailman/listinfo/python-list

perl + python tutorial available for download

2008-06-30 Thread Xah
my perl and python tutorial

http://xahlee.org/perl-python/index.html

is now available for download for offline reading.
Download link at the bottom.

   Xah
∑ http://xahlee.org/

☄
--
http://mail.python.org/mailman/listinfo/python-list

New Science Discovery: Perl Idiots Remain Idiots After A Decade!New Science Discovery: Perl Idiots Remain Idiots After A Decade!

2012-02-29 Thread Xah Lee
New Science Discovery: Perl Idiots Remain Idiots After A Decade!

A excerpt from the new book 〈Modern Perl〉, just published, chapter 4
on “Operators”. Quote:

«The associativity of an operator governs whether it evaluates from
left to right or right to left. Addition is left associative, such
that 2 + 3 + 4 evaluates 2 + 3 first, then adds 4 to the result.
Exponentiation is right associative, such that 2 ** 3 ** 4 evaluates 3
** 4 first, then raises 2 to the 81st power. »

LOL. Looks like the perl folks haven't changed. Fundamentals of
serious math got botched so badly.

Let me explain the idiocy.

It says “The associativity of an operator governs whether it evaluates
from left to right or right to left.”. Ok, so let's say we have 2
operators: a white triangle △ and a black triangle ▲. Now, by the
perl's teaching above, let's suppose the white triangle is “right
associative” and the black triangle is “left associative”. Now, look
at this:

3 △ 6 ▲ 5

seems like the white and black triangles are going to draw a pistol
and fight for the chick 6 there. LOL.

Now, let me tell you what operator precedence is. First of all, let's
limit ourselfs to discuss operators that are so-called binary
operators, which, in our context, basically means single symbol
operator that takes it's left and right side as operands. Now, each
symbol have a “precedence”, or in other words, the set of operators
has a order. (one easy way to think of this is that, suppose you have
n symbols, then you give each a number, from 1 to n, as their order)
So, when 2 symbols are placed side by side such as 「3 △ 6 ▲ 5」, the
symbol with higher precedence wins. Another easy way to think of this
is that each operator has a stickiness level. The higher its level, it
more sticky it is.

the problem with the perl explanations is that it's one misleading
confusion ball. It isn't about “left/right associativity”. It isn't
about “evaluates from left to right or right to left”. Worse, the word
“associativity” is a math term that describe a property of algebra
that has nothing to do with operator precedence, yet is easily
confused with because it is a property about order of evaluation. (for
example, the addition function is associative, meaning: 「(3+6)+5 =
3+(6+5)」.)

compare it with this:

〈Perl & Python: Complex Numbers〉
http://xahlee.org/perl-python/complex_numbers.html

and for a good understanding of functions and operators, see:

〈What's Function, What's Operator?〉
http://xahlee.org/math/function_and_operators.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New Science Discovery: Perl Idiots Remain Idiots After A Decade!New Science Discovery: Perl Idiots Remain Idiots After A Decade!

2012-02-29 Thread Xah Lee
i missed a point in my original post. That is, when the same operator
are adjacent. e.g. 「3 ▲ 6 ▲ 5」.

This is pointed out by Kiuhnm 〔kiuhnm03.4t.yahoo.it〕 and Tim Bradshaw.
Thanks.

though, i disagree the way they expressed it, or any sense this is
different from math.

to clarify, amend my original post, here's what's needed for binary
operator precedence:

① the symbols are ordered. (e.g. given a unique integer)

② each symbol is has either one of left-side stickness or right-side
stickness spec. (needed when adjacent symbols are the same.)

About the lisp case mentioned by Tim, e.g.  in「(f a b c)」, whether it
means 「(f (f a b) c)」 or 「(f a (f b c))」 . It is not directly relevant
to the context of my original post, because it isn't about to
operators. It's about function argument eval order. Good point,
nevertheless.

the perl doc, is still misleading, terribly bad written. Becha ass!

 Xah

On Feb 29, 4:08 am, Kiuhnm  wrote:
> On 2/29/2012 9:09, Xah Lee wrote:
>
>
> > New Science Discovery: Perl Idiots Remain Idiots After A Decade!
>
> > A excerpt from the new book 〈Modern Perl〉, just published, chapter 4
> > on “Operators”. Quote:
>
> > «The associativity of an operator governs whether it evaluates from
> > left to right or right to left. Addition is left associative, such
> > that 2 + 3 + 4 evaluates 2 + 3 first, then adds 4 to the result.
> > Exponentiation is right associative, such that 2 ** 3 ** 4 evaluates 3
> > ** 4 first, then raises 2 to the 81st power. »
>
> > LOL. Looks like the perl folks haven't changed. Fundamentals of
> > serious math got botched so badly.
>
> > Let me explain the idiocy.
>
> > It says “The associativity of an operator governs whether it evaluates
> > from left to right or right to left.”. Ok, so let's say we have 2
> > operators: a white triangle △ and a black triangle ▲. Now, by the
> > perl's teaching above, let's suppose the white triangle is “right
> > associative” and the black triangle is “left associative”. Now, look
> > at this:
>
> > 3 △ 6 ▲ 5
>
> > seems like the white and black triangles are going to draw a pistol
> > and fight for the chick 6 there. LOL.
>
> Sorry, but you're wrong and they're right.
> Associativity governs the order of evaluation of a group of operators
> *OF THE SAME PRECEDENCE*.
> If you write
>    2**3**4
> only the fact the '**' is right associative will tell you that the order is
>    2**(3**4)
> and not
>    (2**3)**4
> I remind you that 2^(3^4) != (2^3)^4.
>
> Kiuhnm
-- 
http://mail.python.org/mailman/listinfo/python-list


lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp

2012-02-29 Thread Xah Lee
fun example.

in-place algorithm for reversing a list in Perl, Python, Lisp
http://xahlee.org/comp/in-place_algorithm.html

plain text follows


What's “In-place Algorithm”?

Xah Lee, 2012-02-29

This page tells you what's “In-place algorithm”, using {python, perl,
emacs lisp} code to illustrate.

Here's Wikipedia In-place algorithm excerpt:

In computer science, an in-place algorithm (or in Latin in situ) is an
algorithm which transforms input using a data structure with a small,
constant amount of extra storage space. The input is usually
overwritten by the output as the algorithm executes. An algorithm
which is not in-place is sometimes called not-in-place or out-of-
place.

Python

Here's a python code for reversing a list. Done by creating a new
list, NOT using in-place:

# python
# reverse a list

list_a = ["a", "b", "c", "d", "e", "f", "g"]

list_length = len(list_a)
list_b = [0] * list_length

for i in range(list_length):
list_b[i] = list_a[list_length -1 - i]

print list_b
Here's in-place algorithm for reversing a list:

# python
# in-place algorithm for reversing a list

list_a = ["a", "b", "c", "d", "e", "f", "g"]

list_length = len(list_a)

for i in range(list_length/2):
x = list_a[i]
list_a[i] = list_a[ list_length -1 - i]
list_a[ list_length -1 - i] = x

print list_a
Perl

Here's a perl code for reversing a list. Done by creating a new list,
NOT using in-place:

# perl

use strict;
use Data::Dumper;

my @listA = qw(a b c d e f g);

my $listLength = scalar @listA;
my @listB = ();

for ( my $i = 0; $i < $listLength; $i++ ) {
 $listB[$i] = $listA[ $listLength - 1 - $i];
}

print Dumper(\@listB);

# perl
# in-place algorithm for reversing a list.

use strict;
use Data::Dumper;
use POSIX; # for “floor”

my @listA = qw(a b c d e f g);

my $listLength = scalar @listA;

for ( my $i = 0; $i < floor($listLength/2); $i++ ) {
  my $x = $listA[$i];
  $listA[$i] = $listA[ $listLength - 1 - $i];
  $listA[ $listLength - 1 - $i] = $x;
}

print Dumper(\@listA);
__END__

emacs lisp

;; emacs lisp
;; reverse a array

(setq arrayA ["a" "b" "c" "d" "e" "f" "g"])

(setq arrayLength (length arrayA))

(setq arrayB (make-vector arrayLength 0))

(dotimes (i arrayLength )
  (aset arrayB i (aref arrayA (- (1- arrayLength) i)) )
  )

(print (format "%S" arrayB))
;; emacs lisp
;; in-place algorithm for reversing a array

(setq arrayA ["a" "b" "c" "d" "e" "f" "g"])

(setq arrayLength (length arrayA))

(dotimes (i (floor (/ arrayLength 2)))
  (let (x)
(setq x (aref arrayA i))
(aset arrayA i (aref arrayA (- (1- arrayLength) i)))
(aset arrayA (- (1- arrayLength) i) x) ) )

(print (format "%S" arrayA))

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lang comparison: in-place algorithm for reversing a list in Perl, Python, Lisp

2012-02-29 Thread Xah Lee
On Feb 29, 9:01 pm, Steven D'Aprano  wrote:
> You don't need a temporary variable to swap two values in
> Python. A better way to reverse a list using more Pythonic idioms is:
>
> for i in range(len(list_a)//2):
>     list_a[i], list_a[-i-1] = list_a[-i-1], list_a[i]

forgive me sir, but i haven't been at python for a while. :)
i was, actually, refreshing myself of what little polyglot skills i
have.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp

2012-03-01 Thread Xah Lee
On Mar 1, 7:04 am, Kaz Kylheku  wrote:
 lisp:
 (floor (/ x y)) --[rewrite]--> (floor x y)

Thanks for this interesting point.

I don't think it's a good lang design, more of a lang quirk.

similarly, in Python 2.x,
x/y
will work when both x and y are integers. Also,
x//y
works too, but that // is just perlish unreadable syntax quirk.

similarly, in perl, either one
require POSIX; floor(x/y);
the require POSIX instead of Math is a quirk. But even, floor should
really be builtin.
or
using a perl hack
int(x/y)

all of the above are quirks. They rely on computer engineering by-
products (such as int), or rely on the lang's idiosyncrasy. One easy
way to measure it is whether a programer can read and understand a
program without having to delve into its idiosyncrasies. Problem with
these lang idioms is that it's harder to understand, and whatever
advantage/optimization they provide is microscopic and temporary.

best is really floor(x/y).

idiomatic programing, is a bad thing. It was spread by perl, of
course, in the 1990s. Idiomatic lang, i.e. lang with huge number of
bizarre idioms, such as perl, is the worst.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lang comparison: in-place algorithm for reversing a list in Perl,Python, Lisp

2012-03-02 Thread Xah Lee

Xah Lee wrote:
«… One easy way to measure it is whether a programer can read and
understand a program without having to delve into its idiosyncrasies.
…»

Chris Angelico wrote:
«Neither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". …»

they are computer engineering by-products. Are quirks and
idiosyncracies. Check out a advanced lang such as Mathematica. There,
one can learn how the mathematical concept of integer or real number
are implemented in a computer language, without lots by-products of
comp engineering as in vast majority of langs (all those that chalks
up to some IEEE. (which, sadly, includes C, C++, java, perl,
python, lisp, and almost all. (lisp idiots speak of the jargon “number
tower” instead I.) (part of the reason almost all langs stick to
some I stuff is because it's kinda standard, and everyone
understand it, in the sense that unix RFC (aka really fucking common)
is wide-spread because its free yet technically worst. (in a sense,
when everybody's stupid, there arise a cost to not be stupid..

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New Science Discovery: Perl Idiots Remain Idiots After A Decade!New Science Discovery: Perl Idiots Remain Idiots After A Decade!

2012-03-02 Thread Xah Lee
On Mar 1, 3:00 am, Kiuhnm  wrote:
> They did not make up the terminology, if that is what you are saying.
> The concepts of left and right associativity are well-known and accepted
> in TCS (Theoretical CS).


> Aho, Sethi and Ullman explain it this way in "Compilers: Principles,
> Techniques and Tools":
> "We say that the operator + associates to the left because an operand
> with plus signs on both sides of it is taken by the operator to its
> left. [...]"
> And they also show parse trees similar to the ones I wrote above.

how do they explain when 2 operators are adjacent e.g. 「3 △ 6 ▲ 5 」?

do you happen to know some site that shows the relevant page i can
have a look?

thanks.

 Xah

On Mar 1, 3:00 am, Kiuhnm  wrote:
> On 3/1/2012 1:02, Xah Lee wrote:
>
> > i missed a point in my original post. That is, when the same operator
> > are adjacent. e.g. 「3 ▲ 6 ▲ 5」.
>
> > This is pointed out by Kiuhnm 〔kiuhnm03.4t.yahoo.it〕 and Tim Bradshaw.
> > Thanks.
>
> > though, i disagree the way they expressed it, or any sense this is
> > different from math.
>
> They did not make up the terminology, if that is what you are saying.
> The concepts of left and right associativity are well-known and accepted
> in TCS (Theoretical CS).
>
> If you change the terminology, no one will understand you unless you
> provide your definitions every time (and then they may not accept them).
>
> Another way of saying that an operator is left-associative is that its
> parse tree is a left-tree, i.e. a complete tree where each right child
> is a leaf.
> For instance, (use a monospaced font)
>    1 + 2 + 3 + 4
> gives you this left-tree:
>        +
>      +   4
>    +   3
>   1 2
> while 1**2**3**4
> gives you this right-tree:
>    **
> 1    **
>     2    **
>         3  4
>
> Aho, Sethi and Ullman explain it this way in "Compilers: Principles,
> Techniques and Tools":
> "We say that the operator + associates to the left because an operand
> with plus signs on both sides of it is taken by the operator to its
> left. [...]"
> And they also show parse trees similar to the ones I wrote above.
>
> Kiuhnm
-- 
http://mail.python.org/mailman/listinfo/python-list


are int, float, long, double, side-effects of computer engineering?

2012-03-05 Thread Xah Lee
some additional info i thought is relevant.

are int, float, long, double, side-effects of computer engineering?

Xah Lee wrote:
«… One easy way to measure it is whether a programer can read and
understand a program without having to delve into its idiosyncrasies.
…»

Chris Angelico wrote:
«Neither the behavior of ints nor the behavior of IEEE floating point
is a "quirk" or an "idiosyncracy". …»

they are computer engineering by-products. Are quirks and
idiosyncracies. Check out a advanced lang such as Mathematica. There,
one can learn how the mathematical concept of integer or real number
are implemented in a computer language, without lots by-products of
comp engineering as in vast majority of langs (all those that chalks
up to some IEEE. (which, sadly, includes C, C++, perl, python,
lisp, and almost all. (Common/Scheme lisp idiots speak of the jargon
“number tower” instead I.) (part of the reason almost all langs
stick to some I stuff is because it's kinda standard, and
everyone understand it, in the sense that unix RFC (aka really fucking
common) is wide-spread because its free yet technically worst. (in a
sense, when everybody's stupid, there arise a cost to not be
stupid..


A friend asked: «Can you enlighten us as to Mathematica's way of
handling numbers, either by a post or a link to suitable
documentation? …»

what i meant to point out is that Mathematica deals with numbers at a
high-level human way. That is, one doesn't think in terms of float,
long, int, double. These words are never mentioned. Instead, you have
concepts of machine precision, accuracy. The lang automatically handle
the translation to hardware, and invoking exact value or infinite
precision as required or requested.

in most lang's doc, words like int, long, double, float are part of
the lang, and it's quick to mention IEEE. Then you have the wide-
spread overflow issue in your lang. In M, the programer only need to
think in terms of math, i.e. Real number, Integer, complex number,
precision, accuracy, etc.

this is what i meat that most lang deals with computer engineering by-
products, and i wished them to be higher level like M.

http://reference.wolfram.com/mathematica/guide/PrecisionAndAccuracyControl.html

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: are int, float, long, double, side-effects of computer engineering?

2012-03-05 Thread Xah Lee

On Mar 5, 9:26 pm, Tim Roberts  wrote:
> Xah Lee  wrote:
>
> >some additional info i thought is relevant.
>
> >are int, float, long, double, side-effects of computer engineering?
>
> Of course they are.  Such concepts violate the purity of a computer
> language's abstraction of the underlying hardware.  We accept that
> violation because of performance reasons.  There are, as you point out,
> languages that do maintain the purity of the abstraction, but that purity
> is ALWAYS at the expense of performance.
>
> I would also point out pre-emptively that there is nothing inherently wrong
> with asking us to accept an impure abstraction in exchange for performance.
> It is a performance choice that we choose to make.

while what you said is true, but the problem is that 99.99% of
programers do NOT know this. They do not know Mathematica. They've
never seen a language with such feature. The concept is alien. This is
what i'd like to point out and spread awareness.

also, argument about raw speed and fine control vs automatic
management, rots with time. Happened with auto memory management,
managed code, compilers, auto type conversion, auto extension of
array, auto type system, dynamic/scripting languages, etc.

i'd share this these talks:

〈Programing Language: Steve Yegge on Dynamic Languages〉
http://xahlee.org/comp/Steve_Yegge_on_dynamic_languages.html

〈Guy Steele on Parallel Programing: Get rid of cons!〉
http://xahlee.org/comp/Guy_Steele_parallel_computing.html

〈Ocaml Use in Industry (Janestreet Talk by Yaron Minsky)〉
http://xahlee.org/comp/Yaron_Minsky_Janestreet_talk.html

〈Stephen Wolfram: The Background and Vision of Mathematica 〉
http://xahlee.blogspot.com/2011/10/stephen-wolfram-background-and-vision.html

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


a interesting Parallel Programing Problem: asciify-string

2012-03-06 Thread Xah Lee
here's a interesting problem that we are discussing at comp.lang.lisp.

〈Parallel Programing Problem: asciify-string〉
http://xahlee.org/comp/parallel_programing_exercise_asciify-string.html

here's the plain text. Code example is emacs lisp, but the problem is
general.

for a bit python relevancy… is there any python compiler that's
parallel-algorithm aware?

---
Parallel Programing Problem: asciify-string

Here's a interesting parallel programing problem.

Problem Description

The task is to change this function so it's parallelable. (code
example in emacs lisp)

(defun asciify-string (inputStr)
  "Make Unicode string into equivalent ASCII ones."
  (setq inputStr (replace-regexp-in-string "á\\|à\\|â\\|ä" "a"
inputStr))
  (setq inputStr (replace-regexp-in-string "é\\|è\\|ê\\|ë" "e"
inputStr))
  (setq inputStr (replace-regexp-in-string "í\\|ì\\|î\\|ï" "i"
inputStr))
  (setq inputStr (replace-regexp-in-string "ó\\|ò\\|ô\\|ö" "o"
inputStr))
  (setq inputStr (replace-regexp-in-string "ú\\|ù\\|û\\|ü" "u"
inputStr))
  inputStr
  )

Here's a more general description of the problem.

You are given a Unicode text file that's a few peta bytes. For certain
characters in the file, they need to be changed to different char.
(For example of practical application, see: IDN homograph attack ◇
Duplicate characters in Unicode.)

One easy solution is to simply use regex, as the above sample code, to
search thru the file sequentially, and perform the transfrom of a
particular set of chars, then repeat for each char chat needs to be
changed.

But your task is to use a algorithm parallelizable. That is, in a
parallel-algorithm aware language (e.g. Fortress), the compiler will
automatically span the computation to multiple processors.

Refer to Guy Steele's video talk if you haven't seen already. See: Guy
Steele on Parallel Programing.
Solution Suggestions

A better way to write it for parallel programing, is to map a char-
transform function to each char in the string. Here's a pseudo-code in
lisp by Helmut Eller:

(defun asciify-char (c)
  (case c
((? ? ? ?) ?a)
((? ? ? ?) ?e)
((? ? ? ?) ?i)
((? ? ? ?) ?o)
((? ? ? ?) ?u)
(t c)))

(defun asciify-string (string) (map 'string #'asciify-string string))

One problem with this is that the function “asciify-char” itself is
sequential, and not 100% parallelizable. (we might assume here that
there are billions of chars in Unicode that needs to be transformed)

It would be a interesting small project, if someone actually use a
parallel-algorithm-aware language to work on this problem, and report
on the break-point of file-size of parallel-algorithm vs sequential-
algorithm.

Anyone would try it? Perhaps in Fortress, Erlang, Ease, Alice, X10, or
other? Is the Clojure parallel aware?

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


perldoc: the key to perl

2012-03-26 Thread Xah Lee
〈Perl Documentation: The Key to Perl〉
http://xahlee.org/perl-python/key_to_perl.html

plain text follows
-

So, i wanted to know what the option perl -C does. So, here's perldoc
perlrun. Excerpt:

-C [*number/list*]
 The -C flag controls some of the Perl Unicode features.

 As of 5.8.1, the -C can be followed either by a number or
a list of
 option letters. The letters, their numeric values, and
effects are
 as follows; listing the letters is equal to summing the
numbers.

 I 1   STDIN is assumed to be in UTF-8
 O 2   STDOUT will be in UTF-8
 E 4   STDERR will be in UTF-8
 S 7   I + O + E
 i 8   UTF-8 is the default PerlIO layer for input
streams
 o16   UTF-8 is the default PerlIO layer for
output streams
 D24   i + o
 A32   the @ARGV elements are expected to be
strings encoded
   in UTF-8
 L64   normally the "IOEioA" are unconditional,
   the L makes them conditional on the locale
environment
   variables (the LC_ALL, LC_TYPE, and LANG,
in the order
   of decreasing precedence) -- if the
variables indicate
   UTF-8, then the selected "IOEioA" are in
effect
 a   256   Set ${^UTF8CACHE} to -1, to run the UTF-8
caching code in
   debugging mode.

 For example, -COE and -C6 will both turn on UTF-8-ness on
both
 STDOUT and STDERR. Repeating letters is just redundant,
not
 cumulative nor toggling.

 The "io" options mean that any subsequent open() (or
similar I/O
 operations) in the current file scope will have the
":utf8" PerlIO
 layer implicitly applied to them, in other words, UTF-8
is expected
 from any input stream, and UTF-8 is produced to any
output stream.
 This is just the default, with explicit layers in open()
and with
 binmode() one can manipulate streams as usual.

 -C on its own (not followed by any number or option
list), or the
 empty string "" for the "PERL_UNICODE" environment
variable, has
 the same effect as -CSDL. In other words, the standard I/
O handles
 and the default "open()" layer are UTF-8-fied *but* only
if the
 locale environment variables indicate a UTF-8 locale.
This
 behaviour follows the *implicit* (and problematic) UTF-8
behaviour
 of Perl 5.8.0.

 You can use -C0 (or "0" for "PERL_UNICODE") to explicitly
disable
 all the above Unicode features.

 The read-only magic variable "${^UNICODE}" reflects the
numeric
 value of this setting. This variable is set during Perl
startup and
 is thereafter read-only. If you want runtime effects, use
the
 three-arg open() (see "open" in perlfunc), the two-arg
binmode()
 (see "binmode" in perlfunc), and the "open" pragma (see
open).

 (In Perls earlier than 5.8.1 the -C switch was a Win32-
only switch
 that enabled the use of Unicode-aware "wide system call"
Win32
 APIs. This feature was practically unused, however, and
the command
 line switch was therefore "recycled".)

 Note: Since perl 5.10.1, if the -C option is used on the
"#!" line,
 it must be specified on the command line as well, since
the
 standard streams are already set up at this point in the
execution
 of the perl interpreter. You can also use binmode() to
set the
 encoding of an I/O stream.

reading that is like a adventure. It's like this:

The -C is a key to unlock many secrets. Just get it, and you'll be
all
good to go, except in cases you may need the inner key. You'll
find a
hinge in the key, open it, then there's a subkey. On the subkey,
there's a number. Take that number to the lock, it will open with
keyX. When you use keyX, it must be matched with the previous
inner
key with 8th bit. keyX doesn't have a ID, but you can make one by
finding the number at the place you found the key C. Key C is
actually
optional, but when inner key and keyX's number matches, it changes
the
nature of the lock. This is when you need to turn on keyMode …

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Is Programing Art or Science?

2012-04-02 Thread Xah Lee
the refreshen of the blood, from Xah's Entertainment Enterprise, i
bring you:

〈Is Programing Art or Science〉
http://xahlee.org/UnixResource_dir/writ/art_or_science.html

penned in the year of our lord two thousand and two, plain text
version follows.


Is Programing Art or Science?

Dear friends,

You mentioned the title of Donald Knuth's magnum opus Art of
Programming in the context of discussion that fringes on whether
programing is science or art. I'm quite pissed off at work at the
moment, so let me take the time to give some guide on this matter to
the daily programers.

At the bottom rung of programers, there's no question about whether
programing is science or art. Because monkey coders could not care
less. These folks ain't be reading this post, for they hardly will
have heard of lisp.

This leaves us with elite programers who have a smattering of
interests on cogitation and philosophical conundrums. So, is
programing a science or art?

For the programing morons, this question is associated with erudition.
It certainly is a hip subject among hackers such as those hardcore
Perl advocates and unix proponents, who would casually hint on such
realization, impressing a sophistication among peers.

Such a question is not uncommon among those curious. For example, “Is
mathematics science or art?”, is the same type of question that has
been broached by dabblers now and then. We can also detect such
dilemma in the titles conferred to blathering computer jockeys: which
one are thee: baccalaureate of science or baccalaureate of arts? It
really makes no fucking difference.

Ultimately, fantastically stupid questions like these are not
discussed by mathematicians nor philosophers. These are natural
language side-effects, trapping dummies to fuzz about nothing; not
unlike quotations.

For these computing jockeys, there remains the question of why Knuth
named his books the “Art” of Computer Programing, or why some
computing luminaries litter the caution that programing is as much a
art as science. What elite dimwits need to realize is that these
authors are not defining or correcting, but breaking precepts among
the automatons in programing industry.

To the readers of hip literature, words like science and art are
spellbinding, and the need to pigeonhole is imminent. Of these
ruminating class of people, the problem lies in their wanting of
originality. What fills their banal brain are the stale food of
thought that has been chewed and spewed. These above-average eggheads
mop up the scholastic tidbits of its day to mull and muse with fellow
eggheads. They could not see new perspectives. Could not understand
gists. Could not detect non-questions. They are the holder and passer
of knowledge, a bucket of pre-digested purees. Their train of thought
forever loops around established tracks — going nowhere, anytime!

So, is programing a art or science? is it art or science? I really
need to know.

 • Theory vs Practice
 • Jargons of IT Industry
 • The Lambda Logo Tour
 • English Lawers

PS don't forget to checkout: 〈From Why Not Ruby to Fuck Python, Hello
Ruby〉 @ http://xahlee.org/UnixResource_dir/writ/why_not_Ruby.html

yours humbly,

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Google Tech Talk: lisp at JPL

2012-04-02 Thread Xah Lee
Dearly beloved lisperati,

I present you, Ron Garret (aka Erann Gat — aka Naggum hater and enemy
of Kenny Tilton), at Google Tech Talk

〈The Remote Agent Experiment: Debugging Code from 60 Million Miles
Away〉
Google Tech Talk, (2012-02-14) Presented by Ron Garret. @
http://www.youtube.com/watch?v=_gZK0tW8EhQ

i just started watching, havn't done yet.

(thx jcs's blog for the news)

PS posted to python and perl forums too, because i think might be
interesting for lang aficionados . Reply to just your community
please.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is Programing Art or Science?

2012-04-03 Thread Xah Lee
On Apr 3, 8:22 am, Rainer Weikusat  wrote:
> Xah Lee  writes:
>
> [...]
>
> > For example, “Is mathematics science or art?”, is the same type of
> > question that has been broached by dabblers now and then.
>

 http://en.wikipedia.org/wiki/Liberal_arts

this is the best reply in this thread!

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


how i loved lisp cons and UML and Agile and Design Patterns and Pythonic and KISS and YMMV and stopped worrying

2012-04-07 Thread Xah Lee
OMG, how i loved lisp cons and macros and UML and Agile eXtreme
Programing and Design Patterns and Anti-Patterns and Pythonic and KISS
and YMMV and stopped worrying.

〈World Multiconference on Systemics, Cybernetics and Informatics???〉
http://xahlee.org/comp/WMSCI.html

highly advanced plain text format follows, as a amenity for tech
geekers.

---
World Multiconference on Systemics, Cybernetics and Informatics ???

Xah Lee, 2010-04-04

Starting in 2004, i regularly receive email asking me to participate a
conference, called “World Multiconference on Systemics, Cybernetics
and Informatics” (WMSCI). Here's one of such email i got today:


Dear Xah Lee:

As you know the Nobel Laureate Herbert Simon affirmed that design is
an essential ingredient of the Artificial Sciences Ranulph Glanville,
president of the American Society for Cybernetics and expert in design
theory, affirms that “Research is a variety of design. So do research
as design. Design is key to research. Research has to be designed.” An
increasing number of authors are stressing the relationships between
Design and Research. Design is a mean for Research, and Research is a
mean for Design. Design and research are related via cybernetic loops
in the context of means-ends logic. Consequently, we invite you to
submit a paper/abstract and/ot to organize an invited session in the
International Symposium on Design and Research in the Artificial and
the Natural Sciences: DRANS 2010 (http://www.sysconfer.org/drans)
which is being organized in the context of The 14th World Multi-
Conference on Systemics, Cybernetics and Informatics: WMSCI 2010
(http://www.sysconfer.org/wmsci), 2010 in Orlando, Florida, USA.

…

Here's the first email i got from them from my mail archive:

 From: sci2...@iiis.org
Subject: Inviting you to participate in SCI 2005
Date: October 20, 2004 1:39:48 PM PDT
To: x...@xahlee.org

 Dear Dr. Xah Lee:

On behalf of the SCI 2005 Organizing Committee, I would like to invite
you to participate in the 9th World Multi-Conference on Systemics,
Cybernetics and Informatics (http://www.iiisci.org/sci2005), which
will take place in Orlando, Florida, USA, from July 10-13, 2005.

Full text wmsci.txt.

I do not know this organization. I don't know how they got my email or
how they know that i'm involved in the computer science community.
(surely from trawling email addresses in science forums) Though, after
getting a few of their emails, one clearly gets a sense that it is a
scam, soliciting innocent idiotic academicians (many PhDs are
idiots.).

Here's what Wikipedia has to say about them: World Multiconference on
Systemics, Cybernetics and Informatics. Here's a juicy quote:


WMSCI attracted publicity of a less favorable sort in 2005 when three
graduate students at MIT succeeded in getting a paper accepted as a
“non-reviewed paper” to the conference that had been randomly
generated by a computer program called SCIgen.[8] Documents generated
by this software have been used to submit papers to other similar
conferences. Compare to the Sokal affair.

WMSCI has been accused of using spam to advertise its conferences.[8]

Now and then, whenever i got their email, the curiosity in me do
lookup the several terms they used in the email, partly to check the
validity. For example, in this one, it mentions Herbert Simon. Another
one i recall i got recently mentioned Science 2.0. Both of the terms i
haven't heard of before.

One'd think that it is easy to tell scam from real science, but with
today's science proliferation, it's actually not that easy. Even if
you are a academic, it's rather common that many new science terms you
never heard of, because there are tremendous growth of new disciplines
or cross disciplines, along with new jargons. Cross-discipline is
rather common and natural, unlike in the past where science is more or
less clearly delineated hierarchy like Physics, Math, Chemistry,
Biology, etc and their sub-branches. However, many of today's new
areas is a bit questionable, sometimes a deliberate money-making
scheme, which i suppose is the case for WMSCI. Many of these, use
terms like “post-modern”, “science 2.0” to excuse themselves from the
rather strict judgment of classic science. Many of these terms such as
“systemics”, “cybernetics”, “infomatics” are vague. Depending on the
context, it could be a valid emerging science discipline, but it could
also be pure new-age hogwash. And sometimes, nobody really knows
today. Fledgling scientific fields may started off as pseudo-science
but later became well accepted with more solid theories. (e.g.
evolutionary psychology)

In the past 2 decades, there are quite a few cases where peer reviewed
papers published in respected journals are exposed as highly
questionable or deliberate hoax, arose massive debate on the peer
review system. The peer-review system itself can't hold all the blame,
bu

f python?

2012-04-08 Thread Xah Lee
hi guys,

sorry am feeling a bit prolifit lately.

today's show, is: 〈Fuck Python〉
http://xahlee.org/comp/fuck_python.html


Fuck Python
 By Xah Lee, 2012-04-08

fuck Python.

just fucking spend 2 hours and still going.

here's the short story.

so recently i switched to a Windows version of python. Now, Windows
version takes path using win backslash, instead of cygwin slash. This
fucking broke my find/replace scripts that takes a dir level as input.
Because i was counting slashes.

Ok no problem. My sloppiness. After all, my implementation wasn't
portable. So, let's fix it. After a while, discovered there's the
「os.sep」. Ok, replace 「"/"」 to 「os.sep」, done. Then, bang, all hell
went lose. Because, the backslash is used as escape in string, so any
regex that manipulate path got fucked majorly. So, now you need to
find a quoting mechanism. Then, fuck python doc incomprehensible
scattered comp-sci-r-us BNF shit. Then, fuck python for “os.path” and
“os” modules then string object and string functions inconsistent
ball. And FUCK Guido who wants to fuck change python for his idiotic
OOP concept of “elegance” so that some of these are deprecated.

So after several exploration of “repr()”, “format()”, “‹str›.count()”,
“os.path.normpath()”, “re.split()”, “len(re.search().group())” etc,
after a long time, let's use “re.escape()”. 2 hours has passed. Also,
discovered that “os.path.walk” is now deprecated, and one is supposed
to use the sparkling “os.walk”. In the process of refreshing my
python, the “os.path.walk” semantics is really one fucked up fuck.
Meanwhile, the “os.walk” went into incomprehensible OOP object and
iterators fuck.

now, it's close to 3 hours. This fix is supposed to be done in 10 min.
I'd have done it in elisp in just 10 minutes if not for my
waywardness.

This is Before

def process_file(dummy, current_dir, file_list):
   current_dir_level = len(re.split("/", current_dir)) -
len(re.split("/", input_dir))
   cur_file_level = current_dir_level+1
   if min_level <= cur_file_level <= max_level:
  for a_file in file_list:
 if re.search(r"\.html$", a_file, re.U) and
os.path.isfile(current_dir + "/" + a_file):
replace_string_in_file(current_dir + "/" + a_file)

 This is After

def process_file(dummy, current_dir, file_list):
   current_dir = os.path.normpath(current_dir)
   cur_dir_level = re.sub( "^" + re.escape(input_dir), "",
current_dir).count( os.sep)
   cur_file_level = cur_dir_level + 1
   if min_level <= cur_file_level <= max_level:
  for a_file in file_list:
 if re.search(r"\.html$", a_file, re.U) and
os.path.isfile(current_dir + re.escape(os.sep) + a_file):
replace_string_in_file(current_dir + os.sep + a_file)
# print "%d %s" % (cur_file_level, (current_dir + os.sep +
a_file))

 Complete File

# -*- coding: utf-8 -*-
# Python

# find & replace strings in a dir

import os, sys, shutil, re

# if this this is not empty, then only these files will be processed
my_files  = []

input_dir = "c:/Users/h3/web/xahlee_org/lojban/hrefgram2/"
input_dir = "/cygdrive/c/Users/h3/web/zz"
input_dir = "c:/Users/h3/web/xahlee_org/"

min_level = 2; # files and dirs inside input_dir are level 1.
max_level = 2; # inclusive

print_no_change = False

find_replace_list = [

(
u"""http://xahlee.org/
footer.html">""",
u"""""",
),

]

def replace_string_in_file(file_path):
   "Replaces all findStr by repStr in file file_path"
   temp_fname = file_path + "~lc~"
   backup_fname = file_path + "~bk~"

   # print "reading:", file_path
   input_file = open(file_path, "rb")
   file_content = unicode(input_file.read(), "utf-8")
   input_file.close()

   num_replaced = 0
   for a_pair in find_replace_list:
  num_replaced += file_content.count(a_pair[0])
  output_text = file_content.replace(a_pair[0], a_pair[1])
  file_content = output_text

   if num_replaced > 0:
  print "◆ ", num_replaced, " ", file_path.replace("\\", "/")
  shutil.copy2(file_path, backup_fname)
  output_file = open(file_path, "r+b")
  output_file.read() # we do this way instead of “os.rename” to
preserve file creation date
  output_file.seek(0)
  output_file.write(output_text.encode("utf-8"))
  output_file.truncate()
  output_file.close()
   else:
  if print_no_change == True:
 print "no change:", file_path

#  os.remove(file_path)
#  os.rename(temp_fname, file_path)

def process_file(dummy, current_dir, file_list):
   current_dir = os.path.normpath(current_dir)
   cur_dir_level = re.sub( "^" + re.escape

Re: f python?

2012-04-08 Thread Xah Lee
On Apr 8, 4:34 am, Steven D'Aprano  wrote:
> On Sun, 08 Apr 2012 04:11:20 -0700, Xah Lee wrote:
>
> [...]
>
> I have read Xah Lee's post so that you don't have to.
>
> Shorter Xah Lee:
>
>     "I don't know Python very well, and rather than admit I made
>      some pretty lousy design choices in my code, I blame Python.
>      And then I cross-post about it, because I'm the most important
>      person in the Universe."
>
> When the only tool you know how to use is a hammer, everything looks like
> a nail. Instead of using regexes ("now you have two problems"), use the
> right tool: to count path components, split the path, then count the
> number of path components directly.
>
> import os
> components = os.path.split(some_path)
> print len(components)
>
> No matter what separator the OS users, os.path.split will do the right
> thing. There's no need to mess about escaping separators so you can
> hammer it with the regex module, when Python comes with the perfectly
> functional socket-wrench you actually need.

Lol. i think you tried to make fun of me too fast.

check your code.

O, was it you who made fun of my python tutorial before? i was busy,
i'll have to get back on that down the road.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: f python?

2012-04-08 Thread Xah Lee
Xah Lee wrote:

« http://xahlee.org/comp/fuck_python.html »

David Canzi wrote

«When Microsoft created MS-DOS, they decided to use '\' as the
separator in file names.  This was at a time when several previously
existing interactive operating systems were using '/' as the file name
separator and at least one was using '\' as an escape character.  As a
result of Microsoft's decision to use '\' as the separator, people
have had to do extra work to adapt programs written for Windows to run
in non-Windows environments, and vice versa.  People have had to do
extra work to write software that is portable between these
environments.  People have done extra work while creating tools to
make writing portable software easier.  And people have to do extra
work when they use these tools, because using them is still harder
than writing portable code for operating systems that all used '/' as
their separator would have been.»

namekuseijin wrote:

> yes, absolutely.  But you got 2 inaccuracies there:  1) Microsoft didn't 
> create DOS; 2) fucking DOS was written in C, and guess what, it uses \ as 
> escape character.  Fucking microsoft.
>
> > So, when you say fuck Python, are you sure you're shooting at the
> > right target?
>
> I agree.  Fuck winDOS and fucking microsoft.

No. The choice to use backslash than slash is actually a good one.

because, slash is one of the useful char, far more so than backslash.
Users should be able to use that for file names.

i don't know the detailed history of path separator, but if i were to
blame, it's fuck unix. The entirety of unix, unix geek, unixers, unix
fuckheads. Fuck unix.

〈On Unix Filename Characters Problem〉
http://xahlee.org/UnixResource_dir/writ/unix_filename_chars.html

〈On Unix File System's Case Sensitivity〉
http://xahlee.org/UnixResource_dir/_/fileCaseSens.html

〈UNIX Tar Problem: File Length Truncation, Unicode Name Support〉
http://xahlee.org/comp/unix_tar_problem.html

〈What Characters Are Not Allowed in File Names?〉
http://xahlee.org/mswin/allowed_chars_in_file_names.html

〈Unicode Support in File Names: Windows, Mac, Emacs, Unison, Rsync,
USB, Zip〉
http://xahlee.org/mswin/unicode_support_file_names.html

〈The Nature of the Unix Philosophy〉
http://xahlee.org/UnixResource_dir/writ/unix_phil.html

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Emacs Lisp vs Perl: Validate Local File Links

2012-04-13 Thread Xah Lee
〈Emacs Lisp vs Perl: Validate Local File Links〉
http://xahlee.org/emacs/elisp_vs_perl_validate_links.html

a comparison of 2 scripts.

lots code, so i won't paste plain text version here.

i have some comments at the bottom. Excerpt:

--

«One thing interesting is to compare the approaches in perl and emacs
lisp.»

«For our case, regex is not powerful enough to deal with the problem
by itself, due to the nested nature of html. This is why, in my perl
code, i split the file by < into segments first, then, use regex to
deal with now the non-nested segment. This will break if you have math. This cannot be worked
around unless you really start to write a real parser.»

«The elisp here is more powerful, not because of any lisp features,
but because emacs's buffer datatype. You can think of it as a
glorified string datatype, that you can move a cursor back and forth,
or use regex to search forward or backward, or save cursor positions
(index) and grab parts of text for further analysis.»

--

If you are a perl coder, and disagree, let me know your opinion.
(showing working code is very welcome) My comment about perl there
applies to python too. (python code welcome too.)

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


A Design Pattern Question for Functional Programers

2012-04-18 Thread Xah Lee
Functional programing is getting the presses in mainstream. I ran
across this dialogue where a imperative coder was trying to get into
functional programing:

  A: What are the design patterns that help structure functional
systems?

  B: Design patterns? Hey everyone, look at the muggle try to get the
wand to work!

from:
〈Code Watch: Functional programming's smugness problem〉 (2012-04-16)
By By Larry O'brien. @ 
http://www.sdtimes.com/content/article.aspx?ArticleID=36534

hi, my dearly beloved C++ java perl python hackers, design pattern
your mom!

further readings:

〈Why Software Suck〉
http://xahlee.org/UnixResource_dir/writ/why_software_suck.html

〈What is a Tech Geeker?〉
http://xahlee.org/UnixResource_dir/writ/tech_geeker.html

〈Book Review: Patterns of Software〉
http://xahlee.org/PageTwo_dir/Personal_dir/bookReviewRichardGabriel.html

〈Are You Intelligent Enough to Understand HTML5?〉
http://xahlee.org/UnixResource_dir/writ/html5_vs_intelligence.html

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


John Carmack glorifying functional programing in 3k words

2012-04-26 Thread Xah Lee
John Carmack glorifying functional programing in 3k words

http://www.altdevblogaday.com/2012/04/26/functional-programming-in-c/

where was he ten years ago?

O, and btw, i heard that Common Lispers don't do functional
programing, is that right?

Fuck Common Lispers. Yeah, fuck them. One bunch of Fuckfaces. (and
Fuck Pythoners. Python fucking idiots.)

O, don't forget,

〈Programing: What are OOP's Jargons and Complexities (Object Oriented
Program as Functional Program)〉
http://xahlee.org/Periodic_dosage_dir/t2/oop.html

please you peruse of it.

your servant, humbly

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Learn Technical Writing from Unix Man in 10 Days

2012-04-28 Thread Xah Lee
Learn Technical Writing from Unix Man in 10 Days

Quote from man apt-get:

remove
remove is identical to install except that packages are
removed
instead of installed.

Translation:

kicking
kicking is identical to kissing except that receiver is kicked
instead of kissed.

further readings:

• 〈The Idiocy of Computer Language Docs〉
http://xahlee.org/comp/idiocy_of_comp_lang.html

• 〈Why Open Source Documentation is of Low Quality〉
http://xahlee.org/UnixResource_dir/writ/gubni_papri.html

• 〈Python Documentation Problems〉
http://xahlee.org/perl-python/python_doc_index.html



DISAPPEARING URL IN DOC

so, i was reading man git. Quote:

Formatted and hyperlinked version of the latest git documentation
can
be viewed at http://www.kernel.org/pub/software/scm/git/docs/.

but if you go to that url, it shows a list of over one hundred fourty
empty dirs.

I guess unix/linux idiots can't be bothered to have correct
documentation. Inability to write is one thing, but they are unable to
maintain a link or update doc?

does this ever happens to Apple's docs? If it did, i don't ever recall
seeing it from 18 years of using Mac.

more records of careless dead link:

• 〈Hackers: Dead Links and Human Compassion?〉
http://xahlee.org/comp/hacker_dead_links_and_compassion.html

• 〈Why Qi Lisp Fails and Clojure Succeeds〉
http://xahlee.org/UnixResource_dir/writ/qi_lang_marketing.html

• 〈unix: Hunspell Path Pain〉
http://xahlee.org/comp/hunspell_spell_path_pain.html

• 〈Python Doc URL disappearance〉
http://xahlee.org/perl-python/python_doc_url_disappearance.html

• 〈A Record of Frustration in IT Industry; Disappearing FSF URLs〉
http://xahlee.org/emacs/gnu_doc.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Learn Technical Writing from Unix Man in 10 Days

2012-04-29 Thread Xah Lee
On Apr 29, 7:43 pm, Jason Earl  wrote:
> On Sat, Apr 28 2012, Steven D'Aprano wrote:
> > On Sat, 28 Apr 2012 14:55:42 -0700, Xah Lee wrote:
>
> >> Learn Technical Writing from Unix Man in 10 Days
>
> >> Quote from man apt-get:
>
> >>     remove
> >>         remove is identical to install except that packages are
> >> removed
> >>         instead of installed.
>
> > Do you also expect the documentation to define "except", "instead", "is",
> > "to" and "the"?
>
> > If you don't know what "install" and "remove" means, then you need an
> > English dictionary, not a technical manual.
>
> It is considerably worse than that.  If you look at what the
> documentation for apt-get actually says, instead of just the badly
> mangled version that Xah shares you would realize that the post was
> basically a bald-face troll.
>
> The rest of Xah's links in this particular article was even worse.  For
> the most part he was criticizing documentation flaws that have
> disappeared years ago.
>
> Heck, his criticism of Emacs' missing documentation has been fixed since
> Emacs 21 (the Emacs developers are currently getting ready to release
> Emacs 24).  His criticism of git's documentation is also grossly
> misleading.  kernel.org still has the empty directories, but git-scm.org
> has been the official home for git's documentation for years.
>
> I am sure that the rest of the examples are just as ridiculous.  I tend
> to like Xah's writing.  Heck, I even sent a few bucks his way as thanks
> for his Emacs Lisp tutorials.  However, that particular post was simply
> ridiculous.
>
> Jason

jason, are you trolling me, or me you?

☺

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


uhmm... your chance to spit on me

2011-06-10 Thread Xah Lee
Dear lisp comrades, it's Friday!

Dear Xah, your writing is:

• Full of bad grammar. River of Hiccups.

• Stilted. Chocked under useless structure and logic.

• WRONG — Filled with uncouth advices.

• Needlessly insulting. You have problems.

• Simply stinks. Worthless.

• Mediocre. Just like everybody, admit it.

• I love it.

• Your writing is pro!

• you are genius! one of the great expositor, eassyist.

• Dude, you are full of shit. I've not seen a crank quite like
you.

Vote at: http://xahlee.blogspot.com/2011/06/xahs-writing-is.html.

 Xah
 (i code python small time too)
-- 
http://mail.python.org/mailman/listinfo/python-list


Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-11 Thread Xah Lee
(a lil weekend distraction from comp lang!)

in recent years, there came this Colemak layout. The guy who created
it, Colemak, has a site, and aggressively market his layout. It's in
linuxes distro by default, and has become somewhat popular.

I remember first discovering it perhaps in 2007. Me, being a Dvorak
typist since 1994, am curious on what he has to say about comparison.
I recall, i was offended seeing how he paints a bias in peddling his
creation.

So, here, let me repaint his bias. Here it is, and judge for yourself.

〈Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the
Dvorak Layout?〉
http://xahlee.org/kbd/dvorak_vs_colemak.html

here's a interesting excerpt:


Just How Much Do You Type?

Many programers all claim to type 8 or 10 hours a day. They may be
sitting in front of the computer all day, but the time their fingers
actually dance on keyboard is probably less than 1 hour per day.

Contrast data-entry clerks. They are the real typists. Their fingers
actually type, continuously, for perhaps 6 hours per day.

It is important get a sense of how much you actually type. This you
can do by logging you keystrokes using a software.

Let's assume a pro typist sustain at 60 wpm. 60 wpm is 300 strokes per
min, or 18k per hour. Suppose she works 8 hours a day, and assume just
3 hours actually typing. 18k × 3 = 54k chars per day. With this
figure, you can get a sense of how many “hours” you actually type per
day.

I sit in front of computer on average 13 hours per day for the past
several years. I program and write several blogs. My actual typing is
probably double or triple of average day-job programers. From my emacs
command frequency log for 6 months in 2008, it seems i only type 17k
strokes per day. That's 31% of the data-entry clerk scenario above.
Or, i only type ONE hour a day!

I was quite surprised how low my own figure is. But thinking about it…
it make sense. Even we sit in front of computer all day, but the
actual typing is probably some miniscule percentage of that. Most of
the time, you have to chat, lunch, run errands, browse web, read docs,
run to the bathroom. Perhaps only half of your work time is active
coding or writing (emails; docs). Of that duration, perhaps majority
of time you are digesting the info on screen. Your whole day's typing
probably can be done in less than 20 minutes if you just type
continuously.

If your typing doesn't come anywhere close to a data-entry clerk, then
any layout “more efficient” than Dvorak is practically meaningless.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-13 Thread Xah Lee

On Jun 13, 6:45 pm, Gregory Ewing  wrote:
> Chris Angelico wrote:
> > And did any of the studies take into account the fact that a lot of
> > computer users - in all but the purest data entry tasks - will use a
> > mouse as well as a keyboard?
>
> What I think's really stupid is designing keyboards with two
> big blocks of keys between the alphabetic keys and the mouse.
> Back when standard-grade keyboards didn't usually have a
> built-in numeric keypad, it was much easier to move one's
> right hand back and forth between the keyboard and mouse.
>
> Nowadays I find myself perpetually prone to off-by-one errors
> when moving back to the keyboard. :-(

numerical keypad is useful to many. Most people can't touch type. Even
for touch typist, many doesn't do the number keys. So, when they need
to type credit, phone number, etc, they go for the number pad. Also, i
think the number pad esentially have become a calculator for vast
majority of computer users. These days, almost all keyboard from
Microsoft or Logitech has a Calculator button near the number pad to
launch it.

i myself, am a qwerty typist since ~1987, also worked as data entry
clerk for a couple of years. Am a dvorak touch typist since 1994. (and
emacs since 1997) However, i never learned touch type the numbers on
the main section till i think ~2005. Since about 2008, the numerical
keypad is used as extra function keys.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-14 Thread Xah Lee
On Jun 13, 6:19 am, Steven D'Aprano 〔steve
+comp.lang.pyt...@pearwood.info〕 wrote:

│ I don't know if there are any studies that indicate how much of a
│ programmer's work is actual mechanical typing but I'd be surprised
if it
│ were as much as 20% of the work day. The rest of the time being
thinking,
│ planning, debugging, communicating with customers or managers,
reading
│ documentation, testing, committing code, sketching data schemas on
the
│ whiteboard ... to say nothing of the dreaded strategy meetings.

you can find the study on my site. URL in the first post of this
thread.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs. Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-14 Thread Xah Lee


Ba Wha 13, 7:23 nz, Ehfgbz Zbql 〔ehfgbzcz...@tznvy.pbz〕 jebgr:
│ Qibenx -- yvxr djregl naq nal bgure xrlobneq ynlbhg -- nffhzrf gur
│ pbzchgre vf n glcrjevgre.
│ Guvf zrnaf va rssrpg ng yrnfg gjb pbafgenvagf, arprffnel sbe gur
│ glcrjevgre ohg abg sbe gur pbzchgre:
│
│ n. Gur glcvfg pna glcr bayl 1 xrl ng n gvzr
│ o. Bar (xrl)fgebxr trarengrf rknpgyl 1 yrggre
│
│ Rkprcgvbaf gb [n] ner Fuvsg (Pgey) rgp ohg pyrneyl va ehaavat hfr
gurl
│ ner gur rkprcgvba abg gur ehyr.
│
│ │ Jurer fcrrq ernyyl vf ivgny, fhpu nf sbe pbheg fgrabtencuref,
fcrpvny zrpunavpny
│ │ fubegunaq znpuvarf fhpu nf fgrabglcrf ner hfrq, pbfgvat gubhfnaqf
bs qbyynef ohg nyybjvat
│ │ gur glcvfg gb ernpu fcrrqf bs bire 300 jcz.
│
│ Lrf, vafgehzragf yvxr fgrabglcrf fcrrq hc glcvat ol hanffhzvat [n]
│ Yvxrjvfr cvnavfgf pna or fnvq (naq frra) gb qb zber ng gur cvnab
guna
│ glcvfgf ng n pbzchgre orpnhfr pubeqf ner cneg bs gur 'nyybjrq
│ ynathntr'.
│
│ Nffhzcgvba [o] yvxrjvfr vf haarprffnevyl erfgevpgvir ba n pbzchgre.
│ Guvax bs nyy gur 'nooeri/favccrg/fubegsbez/grzcyngr' flfgrzf yvxr
│ lnfavccrg, grkgzngr-favccrgf, rznpf/iv nooerif rgp.
│
│ Sbe beqvanel Ratyvfu gurer ner guvatf yvxr 
xrlfpevcguggc://jjj.serrjrof.pbz/pnfflwnarx
│
│ Sbe rknzcyr gur zbfg pbzzba jbeqf (rfgvzngrq gb or nebhaq 40% bs
│ Ratyvfu) ner fubegsbezrq nf:
│ o = ohg
│ p = jvgu
│ q = unq
│ r = guvf
│ s = bs
│ t = gung
│ u = gur
│ w = juvpu
│ a = naq
│ ...rgp rgp hcgb
│ m = jnf
│
│ gura pbzzba cuenfrf
│ noyr gb  = po
│ unq orra = qa
│ qb abg   = qk
│ qvq abg  = rk
│ qbrf abg = qfk
│
│ rgp
│
│ Pyrneyl, sbe cebtenzzref guvf vf hayvxryl gb or zhpu hfr --
│ cebtenzzvat ynathntrf ner abg Ratyvfu.
│
│ Ohg ohg vg vf pregnvayl na bcra dhrfgvba jurgure vs gur ercrngvat
│ cnggreaf va cebtenzzvat ynathntrf ner pncgherq vagb fbzr flfgrz, gur
│ erfhygvat orarsvg jbhyq or n zrer zvpeb-bcgvzvmngvba be fbzrguvat
zber
│ fvtavsvpnag.  V unir frra fbzr tbbq cebtenzzref fjrne ol
│ rznpf-lnfavccrgf, grkgzngr-favccrgf rgp.

gurer'f fcrpvny vachg qrivprf qrfvtarq sbe pubeqvat, pnyyrq pubeqvat
xrlobneq. Gurer'f qngnunaq. Ybbx hc Jvxvcrqvn sbe n yvfg.

gurer'f nyfb xvarfvf naq bguref gung jbexf jvgu sbbg crqnyf. Fb, vg'f
yvxr pubeqvat jvgu lbhe srrg gbb. Rire frra gubfr penml betnavfg jvgu
srrg ohfl ba 30 crqnyf?

unir lbh gevrq ibvpr vachg? Jvaqbjf pbzrf jvgu vg. Cerggl tbbq.
Gubhtu, qbrfa'g jbex fb jryy jvgu nccf vzcyrzragrq bhgfvqr bs ZF'f
senzrjbex, fhpu nf rznpf.

fbzr cebtenzre'f fbyhgvbaf:

〈Pryroevgl Cebtenzref jvgu EFV (Ercrgvgvir Fgenva Vawhel)〉
 uggc://knuyrr.bet/rznpf/rznpf_unaq_cnva_pryroevgl.ugzy

 Knu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs. Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-14 Thread Xah Lee
for some reason, was unable to post the previous message. (but can
post others) So, the message is rot13'd and it works. Not sure what's
up with Google groups. (this happened a few years back once.
Apparantly, the message content  might have something to do with it
because rot13 clearly works. Yet, the problem doesnt seem to be my
name or embedded url, since it only happens with the previous message)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-17 Thread Xah Lee
On Jun 14, 7:50 am, Dotan Cohen  wrote:
> On Mon, Jun 13, 2011 at 10:21, Elena  wrote:
> > On 13 Giu, 06:30, Tim Roberts  wrote:
> >> Studies have shown that even a
> >> strictly alphabetical layout works perfectly well, once the typist is
> >> acclimated.
>
> > Once the user is acclimated to move her hands much  more (about 40%
> > more for Qwerty versus Dvorak), that is.
>
> And disproportionate usage of fingers. On QWERTY the weakest fingers
> (pinkies) do almost 1/4 of the keypresses when modifier keys, enter,
> tab, and backspace are taken into account.
>
> I'm developing a QWERTY-based layout that moves the load off the
> pinkies and onto the index 
> fingers:http://dotancohen.com/eng/noah_ergonomic_keyboard_layout.html
>
> There is a Colemak version in the works as well.

u r aware that there are already tens of layouts, each created by
programer, thinking that they can create the best layout?

if not, check
〈Computer Keyboards, Layouts, Hotkeys, Macros, RSI ⌨〉
xahlee.org/Periodic_dosage_dir/keyboarding.html

on layout section. Lots people all creating layouts.

also, you want to put {Enter, Tab}, etc keys in the middle, but I
don't understand from ur website how u gonna do that since it requires
keyboard hardware modification. e.g. r u creating key layout on PC
keyboard or are you creating hardware keyboard Key layout? The former
is a dime a million, the latter is rare but also there are several
sites all trying to do it. Talk is cheap, the hardest part is actually
to get money to finance and manufacture it. The latest one, which i
deem good, is Truely Ergonomic keyboard. It sells for $200 and is in
pre-order only now.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-17 Thread Xah Lee
On Jun 15, 5:43 am, rusi  wrote:
> On Jun 15, 5:32 pm, Dotan Cohen  wrote:
>
> > Thanks. From testing small movements with my fingers I see that the
> > fourth finger is in fact a bit weaker than the last finger, but more
> > importantly, it is much less dexterous. Good to know!
>
> Most of the piano technique-icians emphasis, especially those of the
> last century like Hanon, was to cultivate 'independence' of the
> fingers.  The main target of these attacks being the 4th finger.
>
> The number of potential-pianists who ruined their hands and lives
> chasing this holy grail is unknown

Hi rusi, am afaid going to contradict what u say here.

i pretty much mastered Hanon 60. All of it, but it was now 8 years
ago. The idea that pinky is stronger than 4th is silly. I can't fathom
any logic or science to support that. Perhaps what u meant is that in
many situations the use of pinky can be worked around because it in at
the edge of your hand so you can apply chopping motion or similar.
(which, is BAD if you want to develope piano finger skill) However,
that's entirely different than saying pinky being stronger than 4th.

there's many ways we can cookup tests right away to see. e.g. try to
squeeze a rubber ball with 4th and thumb. Repeat with pink + thumb.
Or, reverse exercise by stretching a rubber band wrapped on the 2
fingers of interest. You can easy see that pinky isn't stronger.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-17 Thread Xah Lee

On Jun 17, 2:26 pm, Dotan Cohen  wrote:
> On Fri, Jun 17, 2011 at 20:43, Xah Lee  wrote:
> > u r aware that there are already tens of layouts, each created by
> > programer, thinking that they can create the best layout?
>
> Yes. Mine is better :)
> Had Stallman not heard of VI when he set out to write Emacs?
>
>
>
>
>
> > if not, check
> > 〈Computer Keyboards, Layouts, Hotkeys, Macros, RSI ⌨〉
> > xahlee.org/Periodic_dosage_dir/keyboarding.html
>
> > on layout section. Lots people all creating layouts.
>
> > also, you want to put {Enter, Tab}, etc keys in the middle, but I
> > don't understand from ur website how u gonna do that since it requires
> > keyboard hardware modification. e.g. r u creating key layout on PC
> > keyboard or are you creating hardware keyboard Key layout? The former
> > is a dime a million, the latter is rare but also there are several
> > sites all trying to do it. Talk is cheap, the hardest part is actually
> > to get money to finance and manufacture it. The latest one, which i
> > deem good, is Truely Ergonomic keyboard. It sells for $200 and is in
> > pre-order only now.
>
> I ordered the Truley Ergonomic keyboard, I waited for half a year
> after delivery was supposed to happen to request my money back. Too
> many delays, so in the end I bought a Ducky mechanical (Cherry Browns)
> instead.
>
> I am writing a software keyboard layout. I'm actually having a hard
> time moving the modifier keys (Alt, Ctrl) to a new location. If you
> know how to do that I would much appreciate some advice, I'll post the
> problem here or in private mail.
>
> Thanks, Lee. (or should that be Thanks, Xah?)

thanks. didn't know about Ducky keyboard. Looks good. Also nice to
hear your experience about Truly Ergonomic keyboard.

no actually i don't know how to make normal letter keys as (ctrl, alt)
modifiers. You'll need a usb hid remapper. (there's a couple for mac
os x i linked on my site but i couldn't verify cuz am now on a 6 years
old powerpc with outdated mac os x) For Windows, Microsoft made a
layout maker. I haven't used it so i don't know if it allows mapping
letter keys as modifier. Have you tried it?

i don't know much about the subject but from what i read am guessing
it's possible, because each key just send up/down signals. (whether
you are using usb or ps/2 makes a difference too.)

(am assumbing above that you want to put modifiers in normal letter
key positions. But if all you want to do is swap modifier among
themselves, that's pretty easy. Lots of tools to do that for mac and
windows.)

But even if you succeded in putting modifiers to letter key positions,
you may run into problems with key ghosting, because the circuits are
desigend to prevent ghosting on qwerty layout only (with mod keys in
their normal positions). Unless your keyboard is actually full n-key-
roll-over.

maybe some of these are useful info, but maybe you are quite beyond
that. Thanks for your info too. Good luck.

 just Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-18 Thread Xah Lee
On Jun 18, 4:06 am, Dotan Cohen  wrote:
> On Sat, Jun 18, 2011 at 01:09, Xah Lee  wrote:
> > thanks. didn't know about Ducky keyboard. Looks good. Also nice to
> > hear your experience about Truly Ergonomic keyboard.
>
> I like it, see my first-hour review 
> here:http://geekhack.org/showwiki.php?title=Island:18154

very nice review! and on geekhack.org too — the hardcore keyboard mod site!
I enjoyed reading it.

> > no actually i don't know how to make normal letter keys as (ctrl, alt)
> > modifiers. You'll need a usb hid remapper. (there's a couple for mac
> > os x i linked on my site but i couldn't verify cuz am now on a 6 years
> > old powerpc with outdated mac os x) For Windows, Microsoft made a
> > layout maker. I haven't used it so i don't know if it allows mapping
> > letter keys as modifier. Have you tried it?
>
> I use Kubuntu Linux.

i only started to use linux this month, from 10 years hiatus. First
thing to do there is remap keys to the way i like of course. But am
not familiar on how-to there. Seems xmodmap is becoming obsolete and
XKB is in place. There's a couple nice sites about XKB but havn't had
a chance to study them yet. May i ask you a few questions down the
road? (maybe we can add each other on google talk or some social
network)

(off to email)

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


what is the airspeed velocity of an unladen swallow

2011-06-28 Thread Xah Lee
this will be of interest to those bleeding-edge pythoners.

“what… is the airspeed velocity of an unladen swallow?”

xahlee.org/funny/unladen_swallow.html

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


emacs lisp text processing example (html5 figure/figcaption)

2011-07-03 Thread Xah Lee
OMG, emacs lisp beats perl/python again!

Hiya all, another little emacs lisp tutorial from the tiny Xah's Edu
Corner.

〈Emacs Lisp: Processing HTML: Transform Tags to HTML5 “figure” and
“figcaption” Tags〉
xahlee.org/emacs/elisp_batch_html5_tag_transform.html

plain text version follows.

--

Emacs Lisp: Processing HTML: Transform Tags to HTML5 “figure” and
“figcaption” Tags

Xah Lee, 2011-07-03

Another triumph of using elisp for text processing over perl/python.


The Problem

--
Summary

I want batch transform the image tags in 5 thousand html files to use
HTML5's new “figure” and “figcaption” tags.

I want to be able to view each change interactively, while optionally
give it a “go ahead” to do the whole job in batch.

Interactive eye-ball verification on many cases lets me be reasonably
sure the transform is done correctly. Yet i don't want to spend days
to think/write/test a mathematically correct program that otherwise
can be finished in 30 min with human interaction.

--
Detail

HTML5 has the following new tags: “figure” and “figcaption”. They are
used like this:



my cat!


(For detail, see: HTML5 “figure” & “figurecaption” Tags Browser
Support)

On my website, i used a similar structure. They look like this:



my cat!


So, i want to replace them with the HTML5's new tags. This can be done
with a regex. Here's the “find” regex:


??
\([^<]+?\)
?

Here's the replacement string:



\5


Then, you can use “find-file” and dired's “dired-do-query-replace-
regexp” to work on your 5 thousand pages. Nice. (See: Emacs:
Interactively Find & Replace String Patterns on Multiple Files.)

However, the problem here is more complicated. The image file may be
jpg or png or gif. Also, there may be more than one image per group.
Also, the caption part may also contain complicated html. Here's some
examples:




my 2 cats




jamie's cat! Her blog is http://example.com/
jamie/">http://example.com/jamie/


So, a solution by regex is out.


Solution

The solution is pretty simple. Here's the major steps:

Use “find-lisp-find-files” to traverse a dir.
For each file, open it.
Search for the string 
Use “sgml-skip-tag-forward” to jump to its closing tag.
Save the positions of these tag begin/end positions.
Ask user if she wants to replace. If so, do it. (using “delete-
region” and “insert”)
Repeat.

Here's the code:

;; -*- coding: utf-8 -*-
;; 2011-07-03
;; replace image tags to use html5's “figure”  and “figcaption” tags.

;; Example. This:
;; …
;; should become this
;; …

;; do this for all files in a dir.

;; rough steps:
;; find the 
;; use sgml-skip-tag-forward to move to the ending tag.
;; save their positions.


(defun my-process-file (fpath)
  "process the file at fullpath FPATH ..."
  (let (mybuff p1 p2 p3 p4 )
(setq mybuff (find-file fpath))

(widen)
(goto-char 0) ;; in case buffer already open

(while (search-forward "" nil t)
  (progn
(setq p2 (point) )
(backward-char 17) ; beginning of “div” tag
(setq p1 (point) )

(forward-char 1)
(sgml-skip-tag-forward 1) ; move to the closing tag
(setq p4 (point) )
(backward-char 6) ; beginning of the closing div tag
(setq p3 (point) )
(narrow-to-region p1 p4)

(when (y-or-n-p "replace?")
  (progn
(delete-region p3 p4 )
(goto-char p3)
(insert "")

(delete-region p1 p2 )
(goto-char p1)
(insert "")
(widen) ) ) ) )

(when (not (buffer-modified-p mybuff)) (kill-buffer mybuff) )

) )

(require 'find-lisp)


(let (outputBuffer)
  (setq outputBuffer "*xah img/figure replace output*" )
  (with-output-to-temp-buffer outputBuffer
(mapc 'my-process-file (find-lisp-find-files "~/web/xahlee_org/
emacs/" "\\.html$"))
(princ "Done deal!")
) )

Seems pretty simple right?

The “p1” and “p2” variables are the positions of start/end of . The “p3” and “p4” is the start/end of it's closing tag .

We also used a little trick with “widen” and “narrow-to-region”. It
lets me see just the part that i'm interested. It narrows to the
beginning/end of the div.img. This makes eye-balling a bit easier.

The real time-saver is the “sgml-skip-tag-forward” function from “html-
mode”. Without that, one'd have to write a mini-parser to deal with
html's nested ways to be able to locate the proper ending tag.

Using the above code, i can comfortably eye-ball and press “y” at the
rate of about 5 per second. That makes 300 replacements per minute. I
have 5000+ files. If we presume there are 6k replacement to be made,
then at 5 per second means 20 minutes sitting

Re: emacs lisp text processing example (html5 figure/figcaption)

2011-07-05 Thread Xah Lee
On Jul 4, 12:13 pm, "S.Mandl"  wrote:
> Nice. I guess that XSLT would be another (the official) approach for
> such a task.
> Is there an XSLT-engine for Emacs?
>
> -- Stefan

haven't used XSLT, and don't know if there's one in emacs...

it'd be nice if someone actually give a example...

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: emacs lisp text processing example (html5 figure/figcaption)

2011-07-05 Thread Xah Lee
On Jul 5, 12:17 pm, Ian Kelly  wrote:
> On Mon, Jul 4, 2011 at 12:36 AM, Xah Lee  wrote:
> > So, a solution by regex is out.
>
> Actually, none of the complications you listed appear to exclude
> regexes.  Here's a possible (untested) solution:
>
> 
> ((?:\s* height="[0-9]+">)+)
> \s*((?:[^<]|<(?!/p>))+)
> \s*
>
> and corresponding replacement string:
>
> 
> \1
> \2
> 
>
> I don't know what dialect Emacs uses for regexes; the above is the
> Python re dialect.  I assume it is translatable.  If not, then the
> above should at least work with other editors, such as Komodo's
> "Find/Replace in Files" command.  I kept the line breaks here for
> readability, but for completeness they should be stripped out of the
> final regex.
>
> The possibility of nested HTML in the caption is allowed for by using
> a negative look-ahead assertion to accept any tag except a closing
> .  It would break if you had nested  tags, but then that would
> be invalid html anyway.
>
> Cheers,
> Ian

that's fantastic. Thanks! I'll try it out.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: emacs lisp text processing example (html5 figure/figcaption)

2011-07-05 Thread Xah Lee
On Jul 5, 12:17 pm, Ian Kelly  wrote:
> On Mon, Jul 4, 2011 at 12:36 AM, Xah Lee  wrote:
> > So, a solution by regex is out.
>
> Actually, none of the complications you listed appear to exclude
> regexes.  Here's a possible (untested) solution:
>
> 
> ((?:\s* height="[0-9]+">)+)
> \s*((?:[^<]|<(?!/p>))+)
> \s*
>
> and corresponding replacement string:
>
> 
> \1
> \2
> 
>
> I don't know what dialect Emacs uses for regexes; the above is the
> Python re dialect.  I assume it is translatable.  If not, then the
> above should at least work with other editors, such as Komodo's
> "Find/Replace in Files" command.  I kept the line breaks here for
> readability, but for completeness they should be stripped out of the
> final regex.
>
> The possibility of nested HTML in the caption is allowed for by using
> a negative look-ahead assertion to accept any tag except a closing
> .  It would break if you had nested  tags, but then that would
> be invalid html anyway.
>
> Cheers,
> Ian

emacs regex supports shygroup (the 「(?:…)」) but it doesn't support the
negative assertion 「?!…」 though.

but in anycase, i can't see how this part would work
((?:[^<]|<(?!/p>))+)

?

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lisp refactoring puzzle

2011-07-11 Thread Xah Lee

2011-07-11

On Jul 11, 6:51 am, jvt  wrote:
> I might as well toss my two cents in here.  Xah, I don't believe that
> the functional programming idiom demands that we construct our entire
> program out of compositions and other combinators without ever naming
> anything.  That is much more the province of so-called "function-
> level" programming languages like APL/J and to a more limited extent
> concatenative languages where data (but not code) is mostly left
> without names.
>
> Functional programming, in my mind, is about identifying reproducibly
> useful abstractions, _naming them_, and constructing other
> abstractions from them.  Your piece of code above probably needs to be
> factored out into named pieces so that the composition is more
> sensible.  If a piece of code isn't comprehensible, it might be
> because it isn't using the right abstractions in the right way, not
> because the notion of functional programming is itself problematic.
>
> One might instead provide a nightmare nest of procedural code and
> claim that procedural programming has problems. Of course, this
> particular kind of problem might be less common in procedural code,
> since it depends heavily on naming and side effecting values, but it
> isn't hard to find procedural code with a long list of operations and
> namings wherein the chose names are random or otherwise unrelated to
> the problem domain.  My adviser in grad school used to name variables
> after pieces of furniture in dutch, but that didn't cause me to
> impeach the _notion_ of procedural code.

hi jvt,

of course, you are right. But i wasn't criticising functional
programing in anyway.

was just putting out my tale as a caution, to those of us — e.g.
academic scheme lispers and haskell types — who are perpetually
mangling their code for the ultimate elegant constructs.

but speaking on this now... as you guys may know, i was a naive master
of Mathematica while being absolute illiterate in computer science or
any other lang. (see 〈Xah Lee's Computing Experience (Impression Of
Lisp from Mathematica)〉 @ 
http://xahlee.org/PageTwo_dir/Personal_dir/xah_comp_exp.html
) When i didn't know anything about lisp, i thought lisp would be
similar, or even better, as a highlevel lang in comparison to
Mathematica. In retrospect now, i was totally wrong.

lisp, or scheme lisp, is a magnitude more highlevel in comparison to C
or C derivatives such as C++, Java. However, in comparison to
Mathematica, it's one magnitude low level. (it pains me to see lisp
experts here talking about cons and macros all day, even bigshot names
such as one Paul Graham and in lisp books praising lisp macros. Quite
ridiculous.)

over the years, i had curiosity whether perhaps ML/OCaml, Haskell,
would be equivalent high-level as Mathematica as i thought.
Unfortunately, my study of them didn't went far. (best result is my
incomplete 〈OCaml Tutorial〉 @ http://xahlee.org/ocaml/ocaml.html ) Am
not qualified to comment on this, but i think that even Haskell,
OCaml, are still quite low in comparison to Mathematica.

it's funny, in all these supposedly modern high-level langs, they
don't provide even simple list manipulation functions such as union,
intersection, and the like. Not in perl, not in python, not in lisps.
(sure, lib exists, but it's a ride in the wild) It's really
exceedingly curious to me. And it seems that lang authors or its
users, have all sorts of execuse or debate about whether those should
be builtin if you force them to answer. (i.e. they don't get it)
While, we see here regularly questions about implementing union etc
with follow up of wild answers and re-invention the thousandth time.
Of course, Mathematica has Union, Intersection, and a host of others
some 20 years ago, and today it has a complete set of combinatorics
functions as *builtin* functions (as opposed to add-on libs of second-
rate quality). (this is not a question. No need to suggest some
possible reasons why lang might not want to have a whole set of list
manipulation builtin. You (the lisper/python/perl regulars and other
lang fans) are a complete idiot, that's what i'm saying. COMPLETE
IDIOT. (actually, this is not surprising, since genius and true
thinkers are rare and few. (such as myself. As they say, beyond the
times)))

i also wondered, if Mathematica is truely a magnitude higher level
than lisp, why we don't see any computer scientists talk about it? (of
course there are, but almost non-existant in comparison to, say,
academic publications on Scheme, Haskell, even Java) I think the
reason is social, again. Proprietary langs isn't a revue of
academicians, together with the fact that Stephen Wolfram goes about
as if the entire science of computer science comprises of himself.

Want Mathematica? Pay $2k+.

recently i spent several days stu

What Programing Language are the Largest Website Written In?

2011-07-12 Thread Xah Lee

maybe this will be of interest.

〈What Programing Language Are the Largest Website Written In?〉
http://xahlee.org/comp/website_lang_popularity.html

-

i don't remember how, but today i suddenly got reminded that Facebook
is written in PHP. So, on the spur of the moment, i twitted:

  “Remember folks, the world's largest sites {Facebook, Wikipedia,
“Yahoo!”, etc} are written in Pretty Home Page!”

and followed with:

  “To Chinese friends, what's Baido, QQ, Taobao, Sina written in?”

Then, this question piqued me, even i tried to not waste my time. But
it overpowered me before i resisted, becuase i quickly spend 15 min to
write this list (with help of Google):

1 Google ◇ Java
2 Facebook ◇ PHP
3 YouTube ◇ Python
4 Yahoo! ◇ PHP
5 blogger.com ◇ Java
6 baidu.com ◇ C/C++. perl/python/ruby
7 Wikipedia ◇ PHP
8 Windows Live live.com
9 Twitter.com ◇ Scala and Ruby?
10 QQ.com ◇ ?
11 MSN.com ◇ ?
13 LinkedIn ◇ PHP?
15 TaoBao.com ◇ ?
16 sina.com.cn ◇ ?
17 Amazon.com ◇ ?
18 WordPress.com ◇ PHP
22 eBay.com ◇ ?
23 yandex.ru (Russian) ◇ ?
24 Bing ◇ ?
27 Microsoft.com ◇ ?
28 网易 163.com ◇ ?
29 PayPal.com ◇ Java?
31 新浪微博 weibo.com ◇ ?
32 Flickr.com ◇ ?
34 mail.ru ◇ ?
35 Craiglist.org ◇ perl
36 FC2.com ◇ ?
38 Apple.com ◇ Objective J?
39 imdb.com ◇ ?
41 VKontakte.ru ◇ ?
43 搜狐网 sohu.com ◇ ?
44 Ask.com ◇ ?
45 BBC.co.uk ◇ ?
46 tumblr.com ◇ PHP
47 LiveJasmin.com (porn) ◇ ?
48 xvideos.com (porn) ◇ ?
…
56 土豆网 Todou.com ◇ ?
81 YouPorn.com ◇ ?
StumbleUpon.com ◇ PHP, Perl, C++
…

the numbers is site ranking, from alexa.com. (missing ones are mostly
duplicates, such as google japan, google india, etc.)

i think notable interest is that twitter stands out, with Scala and
Ruby.

Those with perl are probably going back to the first dot com era (aka
Web 1.0, ~1995 to ~2002). At that time, perl was basically the only
game in town (secondarily: Java). (i don't recall what amazon and ebay
were in... was it perl or php? how about imdb.com?)

most php follows starting in early 2000s, that's when PHP quietly
surpassed perl in all battle fronts.

it'd be interesting to know what some of the chinese sites uses, and
porn sites (e.g. livejasmin, xvideos, youporn)

as for Microsoft sites... are they in C/C++ and or dotnet?

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


a little parsing challenge ☺

2011-07-17 Thread Xah Lee
2011-07-16

folks, this one will be interesting one.

the problem is to write a script that can check a dir of text files
(and all subdirs) and reports if a file has any mismatched matching
brackets.

• The files will be utf-8 encoded (unix style line ending).

• If a file has mismatched matching-pairs, the script will display the
file name, and the  line number and column number of the first
instance where a mismatched bracket occures. (or, just the char number
instead (as in emacs's “point”))

• the matching pairs are all single unicode chars. They are these and
nothing else: () {} [] “” ‹› «» 【】 〈〉 《》 「」 『』
Note that ‘single curly quote’ is not consider matching pair here.

• You script must be standalone. Must not be using some parser tools.
But can call lib that's part of standard distribution in your lang.

Here's a example of mismatched bracket: ([)], (“[[”), ((, 】etc. (and
yes, the brackets may be nested. There are usually text between these
chars.)

I'll be writing a emacs lisp solution and post in 2 days. Ι welcome
other lang implementations. In particular, perl, python, php, ruby,
tcl, lua, Haskell, Ocaml. I'll also be able to eval common lisp
(clisp) and Scheme lisp (scsh), Java. Other lang such as Clojure,
Scala, C, C++, or any others, are all welcome, but i won't be able to
eval it. javascript implementation will be very interesting too, but
please indicate which and where to install the command line version.

I hope you'll find this a interesting “challenge”. This is a parsing
problem. I haven't studied parsers except some Wikipedia reading, so
my solution will probably be naive. I hope to see and learn from your
solution too.

i hope you'll participate. Just post solution here. Thanks.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-18 Thread Xah Lee

On Jul 17, 12:47 am, Xah Lee  wrote:
> 2011-07-16
>
> folks, this one will be interesting one.
>
> the problem is to write a script that can check a dir of text files
> (and all subdirs) and reports if a file has any mismatched matching
> brackets.
> …

Ok, here's my solution (pasted at bottom). I haven't tried to make it
elegant or terse, yet, seeing that many are already much elegent than
i could possibly do so with my code.

my solution basically use a stack. (i think all of us are doing
similar) Here's the steps:

• Go thru the file char by char, find a bracket char.
• check if the one on stack is a matching opening char. If so remove
it. Else, push the current onto the stack.
• Repeat the above till end of file.
• If the stack is not empty, then the file got mismatched brackets.
Report it.
• Do the above on all files.

Many elegant solutions. Raymond Hettinger is very quick, posted a
solution only after a hour or so when i posted it. Many others are
very short, very nice. Thank you all for writing them. I haven't
studied them yet. I'll run them all and post a summary in 2 days. (i
have few thousands files to run this test thru, many of them have
mismatched brackets. So i have good data to test with.)

PS we still lack a perl, Scheme lisp, tcl, lua versions. These
wouldn't be hard and would be interesting to read.  If you are picking
up one of these lang, this would be a good exercise.  Haskell too. I
particularly would like to see a javascript version ran from command
line. Maybe somebody can put this exercise to Google folks ... they
are like the js gods.

also, now that we have these home-brewed code, how'd a parser expert
do it? Is it possible to make it even simpler by using some parser
tools? (have no idea what those lex yacc do, or modern incarnations)
I've also been thinking whether this can be done with Parsing
Expression Grammar. That would make the code semantics really elegant
(as opposed home-cooked stack logic).

 Xah

;; -*- coding: utf-8 -*-
;; 2011-07-15, Xah Lee
;; go thru a file, check if all brackets are properly matched.
;; e.g. good: (…{…}… “…”…)
;; bad: ( [)]
;; bad: ( ( )

(setq inputDir "~/web/xahlee_org/p/") ; must end in slash

(defvar matchPairs '() "a alist. For each air, the car is opening
char, cdr is closing char.")

(setq matchPairs '(
   ("(" . ")")
   ("{" . "}")
   ("[" . "]")
   ("“" . "”")
   ("‹" . "›")
   ("«" . "»")
   ("【" . "】")
   ("〈" . "〉")
   ("《" . "》")
   ("「" . "」")
   ("『" . "』")
   )
  )

(defvar searchRegex "" "regex string of all pairs to search.")
(setq searchRegex "")
(mapc
 (lambda (mypair) ""
   (setq searchRegex (concat searchRegex (regexp-quote (car mypair))
"|" (regexp-quote (cdr mypair)) "|") )
   )
 matchPairs)

(setq searchRegex (replace-regexp-in-string "|$" "" searchRegex t
t)) ; remove the ending “|”

(setq searchRegex (replace-regexp-in-string "|" "\\|" searchRegex t
t)) ; change | to \\| for regex “or” operation

(defun my-process-file (fpath)
  "process the file at fullpath fpath ..."
  (let (myBuffer (ii 0) myStack ξchar ξpos)

(setq myStack '() ) ; each element is a vector [char position]
(setq ξchar "")

(setq myBuffer (get-buffer-create " myTemp"))
(set-buffer myBuffer)
(insert-file-contents fpath nil nil nil t)

(goto-char 1)
(while (search-forward-regexp searchRegex nil t)
  (setq ξpos (point)  )
  (setq ξchar (buffer-substring-no-properties ξpos (- ξpos 1))  )

  ;; (princ (format "-\nfound char: %s
\n" ξchar) )

  (let ((isClosingCharQ nil) (matchedOpeningChar nil) )
(setq isClosingCharQ (rassoc ξchar matchPairs))
(when isClosingCharQ (setq matchedOpeningChar (car
isClosingCharQ) ) )

;; (princ (format "isClosingCharQ is: %s\n" isClosingCharQ) )
;; (princ (format "matchedOpeningChar is: %s\n"
matchedOpeningChar) )

(if
(and
 (car myStack) ; not empty
 (equal (elt (car myStack) 0) matchedOpeningChar )
 )
(progn
  ;; (princ (format "matched this bottom item on stack: %s
\n" (car myStack)) )
  (setq myStack (cdr myStack) )
  )
  (progn
    ;; (princ (format "did not match this bottom item on
stack: %s\n" (car myStack)) )

Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee
On Jul 18, 7:07 pm, Billy Mays  wrote:
> On 7/18/2011 7:56 PM, Steven D'Aprano wrote:
>
>
>
>
>
>
>
>
>
> > Billy Mays wrote:
>
> >> On 07/17/2011 03:47 AM, Xah Lee wrote:
> >>> 2011-07-16
>
> >> I gave it a shot.  It doesn't do any of the Unicode delims, because
> >> let's face it, Unicode is for goobers.
>
> > Goobers... that would be one of those new-fangled slang terms that the young
> > kids today use to mean its opposite, like "bad", "wicked" and "sick",
> > correct?
>
> > I mention it only because some people might mistakenly interpret your words
> > as a childish and feeble insult against the 98% of the world who want or
> > need more than the 127 characters of ASCII, rather than understand you
> > meant it as a sign of the utmost respect for the richness and diversity of
> > human beings and their languages, cultures, maths and sciences.
>
> TL;DR version: international character sets are a problem, and Unicode
> is not the answer to that problem).
>
> As long as I have used python (which I admit has only been 3 years)
> Unicode has never appeared to be implemented correctly.  I'm probably
> repeating old arguments here, but whatever.
>
> Unicode is a mess.  When someone says ASCII, you know that they can only
> mean characters 0-127.  When someone says Unicode, do the mean real
> Unicode (and is it 2 byte or 4 byte?) or UTF-32 or UTF-16 or UTF-8?
> When using the 'u' datatype with the array module, the docs don't even
> tell you if its 2 bytes wide or 4 bytes.  Which is it?  I'm sure that
> all the of these can be figured out, but the problem is now I have to
> ask every one of these questions whenever I want to use strings.
>
> Secondly, Python doesn't do Unicode exception handling correctly. (but I
> suspect that its a broader problem with languages) A good example of
> this is with UTF-8 where there are invalid code points ( such as 0xC0,
> 0xC1, 0xF5, 0xF6, 0xF7, 0xF8, ..., 0xFF, but you already knew that, as
> well as everyone else who wants to use strings for some reason).
>
> When embedding Python in a long running application where user input is
> received, it is very easy to make mistake which bring down the whole
> program.  If any user string isn't properly try/excepted, a user could
> craft a malformed string which a UTF-8 decoder would choke on.  Using
> ASCII (or whatever 8 bit encoding) doesn't have these problems since all
> codepoints are valid.
>
> Another (this must have been a good laugh amongst the UniDevs) 'feature'
> of unicode is the zero width space (UTF-8 code point 0xE2 0x80 0x8B).
> Any string can masquerade as any other string by placing  few of these
> in a string.  Any word filters you might have are now defeated by some
> cheesy Unicode nonsense character.  Can you just just check for these
> characters and strip them out?  Yes.  Should you have to?  I would say no.
>
> Does it get better?  Of course! international character sets used for
> domain name encoding use yet a different scheme (Punycode).  Are the
> following two domain names the same: tést.com , xn--tst-bma.com ?  Who
> knows!
>
> I suppose I can gloss over the pains of using Unicode in C with every
> string needing to be an LPS since 0x00 is now a valid code point in
> UTF-8 (0x for 2 byte Unicode) or suffer the O(n) look up time to do
> strlen or concatenation operations.
>
> Can it get even better?  Yep.  We also now need to have a Byte order
> Mark (BOM) to determine the endianness of our characters.  Are they
> little endian or big endian?  (or perhaps one of the two possible middle
> endian encodings?)  Who knows?  String processing with unicode is
> unpleasant to say the least.  I suppose that's what we get when we
> things are designed by committee.
>
> But Hey!  The great thing about standards is that there are so many to
> choose from.
>
> --
> Bill

might check out my take

〈Xah's Unicode Tutorial〉
http://xahlee.org/Periodic_dosage_dir/unicode.html

especially good for emacs users.

if you grew up with english, unicode might seem complex or difficult
due to unfamiliarity.

but for asian people, when you dont have alphabets, it's kinda strange
to think that a byte is char. The notion simply don't exist and
impossible to establish. There are many encodings for chinese before
unicode. Even today, unicode isn't used in taiwan or china. Taiwan
uses big5, china uses GB18030, which contains all chars of unicode.

~8 years ago i thought that it'd be great if china adopted unicode
sometimes in the future... so that we all just have one charset to
deal with. But that'

Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee
On Sunday, July 17, 2011 2:48:42 AM UTC-7, Raymond Hettinger wrote:
> On Jul 17, 12:47 am, Xah Lee  wrote:
> > i hope you'll participate. Just post solution here. Thanks.
>
> http://pastebin.com/7hU20NNL

just installed py3.
there seems to be a bug.
in this file

http://xahlee.org/p/time_machine/tm-ch04.html

there's a mismatched double curly quote. at position 28319.

the python code above doesn't seem to spot it?

here's the elisp script output when run on that dir:

Error file: c:/Users/h3/web/xahlee_org/p/time_machine/tm-ch04.html
["“" 28319]
Done deal!

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee

On Jul 18, 10:12 am, Billy Mays
<81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote:
> On 07/17/2011 03:47 AM,XahLee wrote:
>
> > 2011-07-16
>
> I gave it a shot.  It doesn't do any of the Unicode delims, because
> let's face it, Unicode is for goobers.
>
> import sys, os
>
> pairs = {'}':'{', ')':'(', ']':'[', '"':'"', "'":"'", '>':'<'}
> valid = set( v for pair in pairs.items() for v in pair )
>
> for dirpath, dirnames, filenames in os.walk(sys.argv[1]):
>      for name in filenames:
>          stack = [' ']
>          with open(os.path.join(dirpath, name), 'rb') as f:
>              chars = (c for line in f for c in line if c in valid)
>              for c in chars:
>                  if c in pairs and stack[-1] == pairs[c]:
>                      stack.pop()
>                  else:
>                      stack.append(c)
>          print ("Good" if len(stack) == 1 else "Bad") + ': %s' % name
>
> --
> Bill

as Ian Kelly mentioned, your script fail because it doesn't report the
position or line/column number of  first mismatched bracket. This is
rather significant part to this small problem. Avoiding unicode also
lessen the value of this exercise, because handling unicode in python
isn't trivial, at least with respect to this small exercise.

I added other unicode brackets to your list of brackets, but it seems
your code still fail to catch a file that has mismatched curly quotes.
(e.g. http://xahlee.org/p/time_machine/tm-ch04.html )

LOL Billy.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee

On Jul 18, 2:59 pm, Thomas 'PointedEars' Lahn 
wrote:
> Ian Kelly wrote:
> > Billy Mays wrote:
> >> I gave it a shot.  It doesn't do any of the Unicode delims, because let's
> >> face it, Unicode is for goobers.
>
> > Uh, okay...
>
> > Your script also misses the requirement of outputting the index or row
> > and column of the first mismatched bracket.
>
> Thanks to Python's expressiveness, this can be easily remedied (see below).  
>
> I also do not follow Billy's comment about Unicode.  Unicode and the fact
> that Python supports it *natively* cannot be appreciated enough in a
> globalized world.
>
> However, I have learned a lot about being pythonic from his posting (take
> those generator expressions, for example!), and the idea of looking at the
> top of a stack for reference is a really good one.  Thank you, Billy!
>
> Here is my improvement of his code, which should fill the mentioned gaps.
> I have also reversed the order in the report line as I think it is more
> natural this way.  I have tested the code superficially with a directory
> containing a single text file.  Watch for word-wrap:
>
> # encoding: utf-8
> '''
> Created on 2011-07-18
>
> @author: Thomas 'PointedEars' Lahn , based on an idea of
> Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com>
> in <news:j01ph6$knt$1...@speranza.aioe.org>
> '''
> import sys, os
>
> pairs = {u'}': u'{', u')': u'(', u']': u'[',
>          u'”': u'“', u'›': u'‹', u'»': u'«',
>          u'】': u'【', u'〉': u'〈', u'》': u'《',
>          u'」': u'「', u'』': u'『'}
> valid = set(v for pair in pairs.items() for v in pair)
>
> if __name__ == '__main__':
>     for dirpath, dirnames, filenames in os.walk(sys.argv[1]):
>         for name in filenames:
>             stack = [' ']
>
>             # you can use chardet etc. instead
>             encoding = 'utf-8'
>
>             with open(os.path.join(dirpath, name), 'r') as f:
>                 reported = False
>                 chars = ((c, line_no, col) for line_no, line in enumerate(f)
> for col, c in enumerate(line.decode(encoding)) if c in valid)
>                 for c, line_no, col in chars:
>                     if c in pairs:
>                         if stack[-1] == pairs[c]:
>                             stack.pop()
>                         else:
>                             if not reported:
>                                 first_bad = (c, line_no + 1, col + 1)
>                                 reported = True
>                     else:
>                         stack.append(c)
>
>             print '%s: %s' % (name, ("good" if len(stack) == 1 else "bad
> '%s' at %s:%s" % first_bad))

Thanks for the fix.
Though, it seems still wrong.

On the file http://xahlee.org/p/time_machine/tm-ch04.html

there is a mismatched curly double quote at 28319.

the script reports:
tm-ch04.html: bad ')' at 68:2

that doesn't seems right. Line 68 is empty. There's no opening or
closing round bracket anywhere close. Nearest are lines 11 and 127.

Maybe Billy Mays's algorithm is wrong.

 Xah (fairly discouraged now, after running 3 python scripts all
failed)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee
On Jul 17, 8:31 am, Thomas Jollans  wrote:
> On Jul 17, 9:47 am,XahLee  wrote:
>
>
>
>
>
>
>
>
>
> > 2011-07-16
>
> > folks, this one will be interesting one.
>
> > the problem is to write a script that can check a dir of text files
> > (and all subdirs) and reports if a file has any mismatched matching
> > brackets.
>
> > • The files will be utf-8 encoded (unix style line ending).
>
> > • If a file has mismatched matching-pairs, the script will display the
> > file name, and the  line number and column number of the first
> > instance where a mismatched bracket occures. (or, just the char number
> > instead (as in emacs's “point”))
>
> > • the matching pairs are all single unicode chars. They are these and
> > nothing else: () {} [] “” ‹› «» 【】 〈〉 《》 「」 『』
> > Note that ‘single curly quote’ is not consider matching pair here.
>
> > • You script must be standalone. Must not be using some parser tools.
> > But can call lib that's part of standard distribution in your lang.
>
> > Here's a example of mismatched bracket: ([)], (“[[”), ((, 】etc. (and
> > yes, the brackets may be nested. There are usually text between these
> > chars.)
>
> > I'll be writing a emacs lisp solution and post in 2 days. Ι welcome
> > other lang implementations. In particular, perl, python, php, ruby,
> > tcl, lua, Haskell, Ocaml. I'll also be able to eval common lisp
> > (clisp) and Scheme lisp (scsh), Java. Other lang such as Clojure,
> > Scala, C, C++, or any others, are all welcome, but i won't be able to
> > eval it. javascript implementation will be very interesting too, but
> > please indicate which and where to install the command line version.
>
> > I hope you'll find this a interesting “challenge”. This is a parsing
> > problem. I haven't studied parsers except some Wikipedia reading, so
> > my solution will probably be naive. I hope to see and learn from your
> > solution too.
>
> > i hope you'll participate. Just post solution here. Thanks.
>
> I thought I'd have some fun with multi-processing:
>
> https://gist.github.com/1087682

hi Thomas. I ran the program, all cpu went max (i have a quad), but
after i think 3 minutes nothing happens, so i killed it.

is there something special one should know to run the script?

I'm using Python 3.2.1 on Windows 7.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-19 Thread Xah Lee
On Jul 19, 10:33 am, Billy Mays
<81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote:
> On 07/19/2011 01:14 PM,XahLee wrote:
>
> > I added other unicode brackets to your list of brackets, but it seems
> > your code still fail to catch a file that has mismatched curly quotes.
> > (e.g.http://xahlee.org/p/time_machine/tm-ch04.html )
>
> > LOL Billy.
>
> >  Xah
>
> I suspect its due to the file mode being opened with 'rb' mode.  Also,
> the diction of characters at the top, the closing token is the key,
> while the opening one is the value.  Not sure if thats obvious.
>
> Also returning the position of the first mismatched pair is somewhat
> ambiguous.  File systems store files as streams of octets (mine do
> anyways) rather than as characters.  When you ask for the position of
> the the first mismatched pair, do you mean the position as per
> file.tell() or do you mean the nth character in the utf-8 stream?
>
> Also, you may have answered this earlier but I'll ask again anyways: You
> ask for the first mismatched pair, Are you referring to the inner most
> mismatched, or the outermost?  For example, suppose you have this file:
>
> foo[(])bar
>
> Would the "(" be the first mismatched character or would the "]"?

yes i haven't been precise. Thanks for brining it up.

thinking about it now, i think it's a bit hard to define precisely. My
elisp code actually reports the “)”, so it's wrong too. LOL

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-20 Thread Xah Lee
i've just cleaned up my elisp code and wrote a short elisp tutorial.

Here:

〈Emacs Lisp: Batch Script to Validate Matching Brackets〉
http://xahlee.org/emacs/elisp_validate_matching_brackets.html

plain text version follows. Please let me know what you think.

am still working on going thru all code in other langs. Will get to
the ruby one, and that perl regex, and the other fixed python ones.
(possibly also the 2 common lisp codes but am not sure they are
runnable as is or just some non-working showoff. lol)

===
Emacs Lisp: Batch Script to Validate Matching Brackets

Xah Lee, 2011-07-19

This page shows you how to write a elisp script that checks thousands
of files for mismatched brackets.


The Problem


Summary

I have 5 thousands files containing many matching pairs. I want to to
know if any of them contains mismatched brackets.


Detail

The matching pairs includes these: () {} [] “” ‹› «» 〈〉 《》 【】 〖〗 「」
『』.

The program should be able to check all files in a dir, and report any
file that has mismatched bracket, and also indicate the line number or
positon where a mismatch occurs.

For those curious, if you want to know what these brackets are, see:

• Syntax Design: Use of Unicode Matching Brackets as Specialized
Delimiters
• Intro to Chinese Punctuation with Computer Language Syntax
Perspectives

For other notes and conveniences about dealing with brackets in emacs,
see:

• Emacs: Defining Keys to Navigate Brackets
• “extend-selection” at A Text Editor Feature: Extend Selection by
Semantic Unit
• “select-text-in-quote” at Suggestions on Emacs's mark-word
Command


Solution

Here's outline of steps.

• Go thru the file char by char, find a bracket char.
• Check if the one on stack is a matching opening char. If so
remove it. Else, push the current onto the stack.
• Repeat the above till no more bracket char in the file.
• If the stack is not empty, then the file got mismatched
brackets. Report it.
• Do the above on all files.

Here's some interesting use of lisp features to implement the above.


Define Matching Pair Chars as “alist”

We begin by defining the chars we want to check, as a “association
list” (aka “alist”). Like this:

(setq matchPairs '(
   ("(" . ")")
   ("{" . "}")
   ("[" . "]")
   ("“" . "”")
   ("‹" . "›")
   ("«" . "»")
   ("【" . "】")
   ("〖" . "〗")
   ("〈" . "〉")
   ("《" . "》")
   ("「" . "」")
   ("『" . "』")
   )
  )

If you care only to check for curly quotes, you can remove elements
above. This is convenient because some files necessarily have
mismatched pairs such as the parenthesis, because that char is used
for many non-bracketing purposes (e.g. ASCII smiley).

A “alist” in lisp is basically a list of pairs (called key and value),
with the ability to search for a key or a value. The first element of
a pair is called its key, the second element is its value. Each pair
is a “cons”, like this: (cons mykey myvalue), which can also be
written using this syntax: (mykey . myvalue) for more easy reading.

The purpose of lisp's “alist” is similar to Python's dictionary or
Pretty Home Page's array. It is also similar to hashmap, except that
alist can have duplicate keys, can search by values, maintains order,
and alist is not intended for massive number of elements. Elisp has a
hashmap datatype if you need that. (See: Emacs Lisp Tutorial: Hash
Table.)

(info "(elisp) Association Lists")


Generate Regex String from alist

To search for a set of chars in emacs, we can read the buffer char-by-
char, or, we can simply use “search-forward-regexp”. To use that,
first we need to generate a regex string from our matchPairs alist.

First, we defines/declare the string. Not a necessary step, but we do
it for clarity.

(setq searchRegex "")

Then we go thru the matchPairs alist. For each pair, we use “car” and
“cdr” to get the chars and “concat” it to the string. Like this:

(mapc
 (lambda (mypair) ""
   (setq searchRegex (concat searchRegex (regexp-quote (car mypair))
"|" (regexp-quote (cdr mypair)) "|") )
   )
 matchPairs)

Then we remove the ending “|”.

(s

Re: a little parsing challenge ☺

2011-07-21 Thread Xah Lee
On Jul 19, 11:14 am, Thomas Jollans  wrote:
> I thought I'd have some fun with multi-processing:

Nice joke. ☺

> Here's a sane version:
>
> https://gist.github.com/1087682/2240a0834463d490c29ed0f794ad15128849ff8e

hi thomas,

i still cant get your code to work. I have a dir named xxdir with a
single test file xx.txt,with this content:

 foo[(])bar

when i run your code
py3 validate_brackets_Thomas_Jollans_2.py

it simply exit and doesn't seem to do anything. I modded your code to
print the file name it's proccessing. Apparently it did process it.

my python isn't strong else i'd dive in. Thanks.

I'm on Python 3.2.1. Here's a shell log:

 h3@H3-HP 2011-07-21 05:20:30 ~/web/xxst/find_elisp/validate matching
brackets
py3 validate_brackets_Thomas_Jollans_2.py
 h3@H3-HP 2011-07-21 05:20:34 ~/web/xxst/find_elisp/validate matching
brackets
py3 validate_brackets_Thomas_Jollans_2.py
c:/Users/h3/web/xxst/find_elisp/validate matching brackets/xxdir
\xx.txt
 h3@H3-HP 2011-07-21 05:21:59 ~/web/xxst/find_elisp/validate matching
brackets
py3 --version
Python 3.2.1
 h3@H3-HP 2011-07-21 05:27:03 ~/web/xxst/find_elisp/validate matching
brackets

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-21 Thread Xah Lee
On Jul 19, 11:07 am, Thomas Jollans  wrote:
> On 19/07/11 18:54, Xah Lee wrote:
>
>
>
>
>
>
>
>
>
> > On Sunday, July 17, 2011 2:48:42 AM UTC-7, Raymond Hettinger wrote:
> >> On Jul 17, 12:47 am, Xah Lee  wrote:
> >>> i hope you'll participate. Just post solution here. Thanks.
>
> >>http://pastebin.com/7hU20NNL
>
> > just installed py3.
> > there seems to be a bug.
> > in this file
>
> >http://xahlee.org/p/time_machine/tm-ch04.html
>
> > there's a mismatched double curly quote. at position 28319.
>
> > the python code above doesn't seem to spot it?
>
> > here's the elisp script output when run on that dir:
>
> > Error file: c:/Users/h3/web/xahlee_org/p/time_machine/tm-ch04.html
> > ["“" 28319]
> > Done deal!
>
> That script doesn't check that the balance is zero at the end of file.
>
> Patch:
>
> --- ../xah-raymond-old.py       2011-07-19 20:05:13.0 +0200
> +++ ../xah-raymond.py   2011-07-19 20:03:14.0 +0200
> @@ -16,6 +16,8 @@
>          elif c in closers:
>              if not stack or c != stack.pop():
>                  return i
> +    if stack:
> +        return i
>      return -1
>
>  def scan(directory, encoding='utf-8'):

Thanks a lot for the fix Raymond.

Though, the code seems to have a minor problem.
It works, but the report is wrong.
e.g. output:

30068: c:/Users/h3/web/xahlee_org/p/time_machine\tm-ch04.html

that 30068 position is the last char in the file.
The correct should be 28319. (or at least point somewhere in the file
at a bracket char that doesn't match.)

Today, i tried 3 more scripts. 2 fixed python3 versions, 1 ruby, all
failed again. I've reported the problems i encounter at python or ruby
newsgroups. If you are the author, a fix is very much appreciated.
I'll get back to your code and eventually do a blog of summary of all
different lang versions.

Am off to test that elaborate perl regex now... cross fingers.

 Xah. Mood: quite discouraged.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-21 Thread Xah Lee

2011-07-21

On Jul 18, 12:09 am, Rouslan Korneychuk  wrote:
> I don't know why, but I just had to try it (even though I don't usually
> use Perl and had to look up a lot of stuff). I came up with this:
>
> /(?|
>      (\()(?&matched)([\}\]”›»】〉》」』]|$) |
>      (\{)(?&matched)([\)\]”›»】〉》」』]|$) |
>      (\[)(?&matched)([\)\}”›»】〉》」』]|$) |
>      (“)(?&matched)([\)\}\]›»】〉》」』]|$) |
>      (‹)(?&matched)([\)\}\]”»】〉》」』]|$) |
>      («)(?&matched)([\)\}\]”›】〉》」』]|$) |
>      (【)(?&matched)([\)\}\]”›»〉》」』]|$) |
>      (〈)(?&matched)([\)\}\]”›»】》」』]|$) |
>      (《)(?&matched)([\)\}\]”›»】〉」』]|$) |
>      (「)(?&matched)([\)\}\]”›»】〉》』]|$) |
>      (『)(?&matched)([\)\}\]”›»】〉》」]|$))
> (?(DEFINE)(?(?:
>      \((?&matched)\) |
>      \{(?&matched)\} |
>      \[(?&matched)\] |
>      “(?&matched)” |
>      ‹(?&matched)› |
>      «(?&matched)» |
>      【(?&matched)】 |
>      〈(?&matched)〉 |
>      《(?&matched)》 |
>      「(?&matched)」 |
>      『(?&matched)』 |
>      [^\(\{\[“‹«【〈《「『\)\}\]”›»】〉》」』]++)*+))
> /sx;
>
> If the pattern matches, there is a mismatched bracket. $1 is set to the
> mismatched opening bracket. $-[1] is its location. $2 is the mismatched
> closing bracket or '' if the bracket was never closed. $-[2] is set to
> the location of the closing bracket or the end of the string if the
> bracket wasn't closed.
>
> I didn't write all that manually; it was generated with this:
>
> my @open = ('\(','\{','\[','“','‹','«','【','〈','《','「','『');
> my @close = ('\)','\}','\]','”','›','»','】','〉','》','」','』');
>
> '(?|'.join('|',map
> {'('.$open[$_].')(?&matched)(['.join('',@close[0..($_-1),($_+1)..$#close]). 
> ']|$)'}
> (0 .. $#open)).')(?(DEFINE)(?(?:'.join('|',map
> {$open[$_].'(?&matched)'.$close[$_]} (0 ..
> $#open)).'|[^'.join('',@open,@close).']++)*+))'

Thanks for the code.

are you willing to make it complete and standalone? i.e. i can run it
like this:

perl Rouslan_Korneychuk.pl dirPath

and it prints any file that has mismatched pair and line/column number
or the char position?

i'd do it myself but so far i tried 5 codes, 3 fixes, all failed. Not
a complain, but it does take time to gather the code, of different
langs by different people, properly document their authors and
original source urls, etc, and test it out on my envirenment. All
together in the past 3 days i spent perhaps a total of 4 hours running
several code and writing back etc and so far not one really worked.

i know perl well, but your code is a bit out of the ordinary ☺. If
past days have been good experience, i might dive in and study for
fun.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-21 Thread Xah Lee
Ok. Here's a preliminary report.

〈Lisp, Python, Perl, Ruby … Code to Validate Matching Brackets〉
http://xahlee.org/comp/validate_matching_brackets.html

it's taking too much time to go thru.

right now, i consider only one valid code, by Raymond Hettinger (with
minor edit from others).

right now, there's 2 other possible correct solution. One by Robert
Klemme but requires ruby19 but i only have ruby18x. One by Thomas
Jollans in Python 3 but didn't run on my machine perhaps due to some
unix/Windows issue, yet to be done.

the other 3 or 4 seems to be incomplete or just suggestion of ideas.

i haven't done extensive testing on my own code neither.
I'll revisit maybe in a few days.

Feel free to grab my report and make it nice. If you would like to fix
your code, feel free to email.

 Xah

On Jul 21, 7:26 am, Ian Kelly  wrote:
> On Thu, Jul 21, 2011 at 6:58 AM, Xah Lee  wrote:
> > Thanks a lot for the fix Raymond.
>
> That fix was from Thomas Jollans, not Raymond Hettinger.
>
> > Though, the code seems to have a minor problem.
> > It works, but the report is wrong.
> > e.g. output:
>
> > 30068: c:/Users/h3/web/xahlee_org/p/time_machine\tm-ch04.html
>
> > that 30068 position is the last char in the file.
> > The correct should be 28319. (or at least point somewhere in the file
> > at a bracket char that doesn't match.)
> Previously you wrote:
> > If a file has mismatched matching-pairs, the script will display the
> > file name, and the  line number and column number of the first
> > instance where a mismatched bracket occures. (or, just the char number
> > instead (as in emacs's “point”))
>
> I submit that as the file contains no mismatched brackets (only an
> orphan bracket), the output is correct to specification (indeed you
> did not define any output for this case), if not necessarily useful.
>
> In other words, stop being picky.  You may be willing to spend an hour
> or moe on this, but that doesn't mean anybody else is.  Raymond gave
> you a basically working Python solution, but forgot one detail.
> Thomas fixed that detail for you but didn't invest the time to rewrite
> somebody else's function to get the output "correct".  Continuing to
> harp on it at this point is verging on trolling.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a little parsing challenge ☺

2011-07-21 Thread Xah Lee

On Jul 21, 9:43 am, pyt...@bdurham.com wrote:
> Xah,
>
> 1. Is the following string considered legal?
>
> [ { ( ] ) }
>
> Note: Each type of brace opens and closes in the proper sequence. But
> inter-brace opening and closing does not make sense.

nu!

> Or must a closing brace always balance out with the most recent opening
> brace like so?
>
> [ { ( ) } ]

yeah!

> 2. If there are multiple unclosed braces at EOF, is the answer you're
> looking for the position of the first open brace that hasn't been closed
> out yet?

well, as many pointed out, i really haven't thought it out well.

originally, i just want to know the position of a un-matched char.

i haven't taken the time to think about what really should be the
desired behavior. For me, the problem started because i wanted to use
the script to check my 5k html files, in particular, classic novels
that involves double curly quotes and french quotes. So, the desired
behavior is one based on the question of what would best for the user
to see in order to correct a bracket mismatch error in a file. (which,
can get quite complex for nested syntax, because, usually, once you
have one missed, it's all hell from there. I think this is similar to
the problem when a compiler/interpreter encounters a bad syntax in
source code, and thus the poplar situation where error code of
computer programs are hard to understand...)

but anyway, just for this exercise, the requirement needn't be
stringent. I still think that at least the reported position should be
a matching char in the file. (and if we presume this, then only my
code works. LOL)

PS this is a warmup problem for writing a HTML tag validator. I looked
high and lo in past years, but just couldn't find a script that does
simple validation in batch. The w3c one is based on SGML, really huge
amount of un-unstandable irregular historical baggage. XML lexical
validator is much closer, but still not regular. I simply wanted one
just like the match-pair validator in our problem, except the opening
char is not a single char but string of the form  and the
*matching* closing one is of the form , and with just one
exception: when a tag has “/>” in ending such as  then it is
skipped (i.e. not considered as opening or closing).

I'll be writing this soon in elisp… since i haven't studied parsers, i
had hopes that parser expert would show some proper parser solutions…
in particular i think such can be expressed in Parsing Expression
Grammar in just a few lines… but so far no deity came forward to show
the light. lol

getting ranty… it's funny, somehow the tech geekers all want regex to
solve the problem. Regex, regex, regex, a 40 years old deviant bastard
that by some twist of luck became a tool for matching text patterns.
One bloke came forward to show-off a perl regex obfuscation. That's
like, lol. But it might be good for the lulz if his code is actually
complete and worked. Then, you have a few who'd nonchalantly remark
“O, you just need push-down automata”. LOL, unless they show actual
working code, its Automata their asses.

folks, don't get angry with me. I'm a learner. I'm curious. I always
am eager to learn. And there's always things we can learn. Don't get
into a fit and do the troll dance in a pit with me. Nobody's gonna
give a shit if you think u knew it all. If u are not the master of one
thousand and one languages yet, you can learn with me. ☺ troll

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What Programing Language are the Largest Website Written In?

2011-08-02 Thread Xah Lee
On Jul 31, 11:38 am, gavino  wrote:
> On Jul 13, 1:04 pm, ccc31807  wrote:
>
>
>
>
>
>
>
>
>
> > On Jul 12, 7:54 am, Xah Lee  wrote:
>
> > > maybe this will be of interest.
>
> > > 〈What Programing Language Are the Largest Website Written 
> > > In?〉http://xahlee.org/comp/website_lang_popularity.html
>
> > About five years ago, I did some pretty extensive research, and
> > concluded that the biggest sites were written serverside with JSP.
> > Obviously, this wouldn't include The Biggest site, but if you were a
> > big, multinational corporation, or listed on the NYSE, you used JSP
> > for your web programming.
>
> > I doubt very seriously PHP is used for the biggest sites -- I'd still
> > guess JSP, or maybe a MS technology (not VB), but it's only a guess.
>
> > CC.
>
> facebook is php
>
> myspace is microsoft
>
> aol was tcl and aolserver c embedding tcl interp
>
> priceline is lisp
>
> reddit is python was lisp orig
>
> amazon was perl
>
> livejournal was perl

thanks Kevin. Rarely seen you useful. :)

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems of Symbol Congestion in Computer Languages

2011-02-28 Thread Xah Lee
On Feb 28, 7:30 pm, rusi  wrote:
> On Feb 28, 11:39 pm, Dotan Cohen  wrote:
>
> > You miss the canonical bad character reuse case: = vs ==.
>
> > Had there been more meta keys, it might be nice to have a symbol for
> > each key on the keyboard. I personally have experimented with putting
> > the symbols as regular keys and the numbers as the Shifted versions.
> > It's great for programming.
>
> Hmmm... Clever!
> Is it X or Windows?
> Can I have your setup?

hi Russ,

there's a programer's dvorak layout i think is bundled with linux.

or you can do it with xmodmap on X-11 or AutoHotKey on Windows, or
within emacs... On the mac, you can use keyboardMaestro, Quickeys, or
just write a os wide config file yourself. You can see tutorials and
sample files for all these here 
http://xahlee.org/Periodic_dosage_dir/keyboarding.html

i'd be interested to know what Dotan Cohen use too.

i tried the swapping number row with symbols a few years back. didn't
like it so much because numbers are frequently used as well,
especially when you need to enter a series of numbers. e.g. heavy
math, or dates 2010-02-28. One can use the number pad but i use that
as extra programable buttons.

 Xah

> One problem we programmers face is that keyboards were made for
> typists not programmers.
> Another is that when we move from 'hi-level' questions eg code reuse
> -- to lower and lower -- eg ergonomics of reading and writing code --
> the focus goes from the center of consciousness to the periphery and
> we miss how many inefficiencies there are in our semi-automatic
> actions.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems of Symbol Congestion in Computer Languages

2011-03-05 Thread Xah Lee
a,    less,           0x01ac,     0x013c  
>     ]       };
>     key  {  [    period,    greater,        0x01dc,     0x01ae  
>     ]       };
>     key  {  [     slash,    question,       0x01af,     0x013f  
>     ]       };
>
>     key  {  [ backslash,         bar,       0x01dc,     0x01fc  
>     ]       };
>     key  {  [ Caps_Lock     ]       };
>     // End alphanumeric section
>
>     include "level3(win_switch)"
>     include "level3(menu_switch)"
>
> };
>
> In fine.. you fire up an xterm.. issue a ‘setxkbmap apl’ command from
> the shell prompt and you're in business.
>
> I used it daily for about a month before I switched to APLX - aka micro
> APL.. and as I had zero problems.. So, I suspect it is 100% A+
> compatible.
>
> Initially, I thought of writing a python wrapper that would handle
> conversion from Unicode to A+'s peculiar brand of latin1 and back (among
> other things) but never had the time.

hi Chris,

i created a page dedicated to creating math symbol layouts for
different langs.
I linked to your post.

I wonder if you would let me mirror your X code on my site? Or, if you
place it on somewhere more permanent or dedicate page such as git, i'd
link to that. Thanks.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


English Idiom in Unix: Directory Recursively

2011-05-17 Thread Xah Lee
might be of interest.

〈English Idiom in Unix: Directory Recursively〉
http://xahlee.org/comp/idiom_directory_recursively.html

--
English Idiom in Unix: Directory Recursively

Xah Lee, 2011-05-17

Today, let's discuss something in the category of lingustics.

You know how in unix tools, when you want to delete the whole
directory and all sub-directories and files in it, it's referred as
“recursive”?

For example, when you want to delete the whole dir in emacs, it
prompts this message: “Recursive delete of xx? (y or n) ”. (Note: to
be able to delete whole dir in emacs in dired, you'll first need to
turn it on. See: emacs dired tutorial.)

Here's another example. A quote from “rsync” man page:

 …
 This would recursively transfer all files from the directory …
 -r, --recursive recurse into directories
 This tells rsync to copy directories recursively.  See also --
dirs (-d).
 …

Here's a quote from “cp”'s man page:

 -R, -r, --recursive
  copy directories recursively

and lots of other tools has a “-r” option, and they all refer to it as
“recursive”.

Though, if you think about it, it's not exactly a correct description.
“Recursive”, or “recursion”, refers to a particular type of algorithm,
or a implementation using that algorithm. Obviously, to process all
directory's content does not necessarily mean it must be done by a
recursive algorithm. A iteration can do it as well and it's easy to
have the full behavior and properties in the result as a recursive
approach, such as specifying depth order, level to dive into, etc.
(because, dir is a tree, and recursive algorithm is useful for walking
the tree data structure but is not necessary, because a tree can be
laid out flat. Any path order taken by a recursive approach can be
done by just enumerating the nodes in sequence. In fact, iteration
approach can be faster and simpler in many aspects. (i wrote a article
about this some 10 years ago, see: Trees and Indexes.) Note: this
thought about tree and its nodes as a set of node addresses can be
applied to any tree data structure, such as lisp's nested syntax, XML.
See: Programing Language: Fundamental Problems of Lisp.)

If you look at Windows or Mac OS X world, i don't think they ever
refer to dealing with whole dir as “recursive” in user interface. For
example, in Windows Vista, while changing properties of a folder, it
has this message:

Apply changes to this folder only.
Apply changes to this folder, subfolders and files.

Note the second choice. In unix, it would say “Apply changes to this
folder recursively.”

So, the word “recursive” used in unixes may be technically incorrect,
but more so, it's just not the right phrase. Because, we want to
communicate whether the whole content of a directory are processed,
not about certain algorithm or how it is implemented. A simple “all
the dir's branches/contents” or similar would be more apt.

Recently i was chatting in Second Life with someone (Sleeves). She's
typing, while i'm on voice. In part of our conversation, i said “you
sounded fine”. Note that it's technically incorrect, because she's
typing, not on voice. So she didn't actually make any “sound”. But to
say “you typed fine”, or “you chatted fine”, won't get the message
across.

That's idiom. When you interpret a idiom logically, it doesn't make
much sense, but people understand the particular phrase better anyway.
I suspect the “directory recursively” is also a idiom. It seems so
natural and really gets the point across, without any ill effects.
Even if the implementation actually used a iteration, it doesn't seems
to matter.

So the interesting question is, why this idiom works? Or, how it
developed?

I think, among programers (which all unix users are in the 1970s),
every one knows the concept of recursion, and many unix tools on dir
probably are implemented with a recursive algorithm. When you say “…
recursively”, the point gets across, because we all understand it,
even when we are not actually talking about implementation. The phrase
“… directory recursively” is short and memorable, while “… directory
and all its contents” or “… directory and all its branches” or “…
directory and all its sub-directories and files” are wordy and
unwieldy.
✍

Idiocy Of Unix Copy Command
Emacs Lisp Suggestion: Function to Copy/Delete a Directory
Recursively
How to rsync, unison, wget, curl
Hunspell Tutorial
Mac OS X Resource Fork and Command Line Tips
ImageMagick Tutorial
Making System Calls in Perl and Python
Unix And Literary Correlation
The Unix Pestilence
To An Or Not To An
On “I” versus “i” (capitalization of first person pronoun)
On the Postposition of Conjunction in Penultimate Position of a
Sequence
What's Passive Voice? What's Aggressive Voice?
 

Re: English Idiom in Unix: Directory Recursively

2011-05-22 Thread Xah Lee
Xah wrote:
«In the emacs case: “Recursive delete of xx? (y or n) ”, what could it
possibly mean by the word “recursive” there? Like, it might delete the
directory but not delete all files in it?
»

Jonathan de Boyne Pollard wrote:
> It might *try* to delete the directory but not any of its contents,
> yes.

you mean theoretically you see a possibility if the dir is implement
as stilted as unix, but never in your life you find yourself might
want to do it?

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Functional Programing: stop using recursion, cons. Use map & vectors

2011-05-22 Thread Xah Lee

this is important but i think most lispers and functional programers
still don't know it.

Functional Programing: stop using recursion, cons. Use map & vectors.

〈Guy Steele on Parallel Programing〉
http://xahlee.org/comp/Guy_Steele_parallel_computing.html

btw, lists (as cons, car, cdr) in the lisp world has always been some
kinda cult. Like, if you are showing some code example and you
happened to use lisp vector datatype and not cons (lists) and it
doesn't really matter in your case, but some lisper will always rise
up to bug you, either as innocent curious question or attacking you
for not “understanding” lisp. (just as other idiocies happen in other
lang that lispers see but other langs don't see)

it's interesting to me that all other high level langs: Mathematica,
perl, python, php, javascript, all don't have linked list as lisp's
list. It's also curious that somehow lispers never realises this. I've
been having problems with lisp's cons ever since i'm learning Scheme
Lisp in 1998 (but mostly the reason is language design at syntax and
lack of abstraction level in calling “cons, car, cdr” stuff, without
indexing mechanism). Realizing the algorithmic property and parallel-
execution issues of linked list is only recent years.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-22 Thread Xah Lee
On May 22, 3:46 pm, Chris Angelico  wrote:
> On Mon, May 23, 2011 at 6:22 AM, Xah Lee  wrote:
> > Xah wrote:
> > «In the emacs case: “Recursive delete of xx? (y or n) ”, what could it
> > possibly mean by the word “recursive” there? Like, it might delete the
> > directory but not delete all files in it?
> > »
>
> > Jonathan de Boyne Pollard wrote:
> >> It might *try* to delete the directory but not any of its contents,
> >> yes.
>
> > you mean theoretically you see a possibility if the dir is implement
> > as stilted as unix, but never in your life you find yourself might
> > want to do it?
>
> There's a difference between working with a directory itself and
> working with files inside it. Generally, if you copy or delete a
> directory, you will want to recurse. But if you want to, for instance,
> wipe out all files whose names end with a tilde, then you might want
> to recurse and you might not. So it makes sense to offer the user a
> choice, and if recursive action is the only one that makes sense, at
> least acknowledge that the operation might take an arbitrarily long
> time. (Ever done a recursive operation on / on a large file system?
> Takes just a little bit longer than a non-recursive one under the same
> circumstances...)

the context is this: In emacs directory manager (aka dired), when you
call dired-do-delete on a directory, emacs prompts, this way:
“Recursive delete of xx? (y or n)”

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-23 Thread Xah Lee
On May 22, 4:32 pm, Chris Angelico  wrote:
> On Mon, May 23, 2011 at 9:17 AM, Xah Lee  wrote:
> > the context is this: In emacs directory manager (aka dired), when you
> > call dired-do-delete on a directory, emacs prompts, this way:
> > “Recursive delete of xx? (y or n)”
>
> But in order to make your point (such as it is), you are ignoring the
> fact that there are other uses of the term 'recurse' or 'recursive',
> and consistency and clarity are important. I don't see emacs offering
> me a chance to do a non-recursive delete; the only issue here seems to
> be that it's explicit that it is going to destroy an entire branch of
> the directory tree. If this is such a problem, grab the emacs sources
> and change that string - it probably occurs in exactly one place in
> the code. Voila! You now have The One True Perfect Emacs, the ultimate
> text editor, because it no longer tells you that it's working
> recursively.
>
> *removes tongue from cheek after saying that last sentence*
>
> Chris Angelico

why don't you file a bug report? In GNU Emacs 23.2, it's under the
Help menu. I suppose it's the same in other emacs distro.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-24 Thread Xah Lee
On May 23, 9:28 pm, Chris Angelico  wrote:
> On Tue, May 24, 2011 at 2:20 PM, Xah Lee  wrote:
> > why don't you file a bug report? In GNU Emacs 23.2, it's under the
> > Help menu. I suppose it's the same in other emacs distro.
>
> Because I do not consider its behaviour to be errant. And I suspect
> its main developers won't either. That's why I suggested you grab the
> sources and make The Perfect Emacs.

why don't you try http://ergoemacs.org/ ?

 Xah

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-24 Thread Xah Lee
On May 24, 3:06 pm, Rikishi42  wrote:
> On 2011-05-24, Steven D'Aprano  wrote:
>
> >>> I think that is a patronizing remark that under-estimates the
> >>> intelligence of lay people and over-estimates the difficulty of
> >>> understanding recursion.
>
> >> Why would you presume this to be related to intelligence? The point was
> >> not about being *able* to understand, but about *needing* to understand
> >> in order to use.
>
> > Maybe they don't "need" to understand recursion. So what?
>
> I think you should read the earlier posts again, this is drifting so far
> from what I intended.
>
> What I mean is: I'm certain that over the years I've had more than one
> person come to me and ask what 'Do you wish to delete this directory
> recursively?' meant. BAut never have I been asked to explain what 'Do you
> wish to delete this directory and it's subdirs/with all it's contents?'
> meant. Never.
>
> > Recursion is a perfectly good English word, no more technical than
> > "accelerate" or "incinerate" or "dissolve" or "combustion". Do people
> > need to know the word "combustion" when they could say "burn" instead?
>
> It wasn't about the word, but about the nature of the function. Besides, if
> the chance exists of a confusion between a recursive job and the fact the
> job is done using a recursive function... I would try staying away from the
> expression.  
>
> Why not use 'delete a directory'. It's obvious the content gets binned, too.
>
> Do you know many people who incinerate leaves and branches in their garden?
> I burn them.
>
> > Do they need to know the words "microwave oven" when they could be saying
> > "invisible rays cooking thing"?
>
> The word oven has existed for ages, microwave is just a name for the type of
> oven. Not even a description, just a name.
>
> > I wonder whether physicists insist that cars should have a "go faster
> > pedal" because ordinary people don't need to understand Newton's Laws of
> > Motion in order to drive cars?
>
> Gas pedal. Pedal was allraedy known when the car was invented. The simple
> addition of gas solved that need. Oh, and it's break pedal, not
> descellarator. (sp?)
>
> > Who are you to say that people shouldn't be exposed to words you deem
> > that they don't need to know?
>
> I'm one of the 'people'. You say exposed to, I say bothered/bored with.
>
> I have nothing against the use of a proper, precise term. And that word can
> be a complex one with many, many sylables (seems to add value, somehow).
>
> But I'm not an academic, so I don't admire the pedantic use of terms that
> need to be explained to 'lay' people. Especially if there is a widespread,
> usually shorter and much simpler one for it. A pointless effort if
> pointless, even when comming from a physicist.  :-)

very well said, Rikishi42.

this one is probably the most intelligent post in this thread.

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-25 Thread Xah Lee
On May 25, 12:26 am, Thorsten Kampe  wrote:
> * Rikishi42 (Wed, 25 May 2011 00:06:06 +0200)
>
>
>
>
>
>
>
>
>
>
>
> > On 2011-05-24, Steven D'Aprano  wrote:
> > >>> I think that is a patronizing remark that under-estimates the
> > >>> intelligence of lay people and over-estimates the difficulty of
> > >>> understanding recursion.
>
> > >> Why would you presume this to be related to intelligence? The point was
> > >> not about being *able* to understand, but about *needing* to understand
> > >> in order to use.
>
> > > Maybe they don't "need" to understand recursion. So what?
>
> > I think you should read the earlier posts again, this is drifting so far
> > from what I intended.
>
> > What I mean is: I'm certain that over the years I've had more than one
> > person come to me and ask what 'Do you wish to delete this directory
> > recursively?' meant. BAut never have I been asked to explain what 'Do you
> > wish to delete this directory and it's subdirs/with all it's contents?'
> > meant. Never.
>
> Naming something in the terms of its implementation details (in this
> case recursion) is a classical WTF.
>
> On the other hand, it's by far not the only WTF in Unix. For instance,
> how often have you read "unlink" instead of "delete"? Or "directory"
> instead of "folder", pointing out that "directory" is the correct term
> because a directory is just a listing and does not "contain" the actual
> files. Of course these implementation details will never matter to
> anyone except under the rarest conditions.
>
> Thorsten

well said.

half of posts in this thread are from idiots. just incredible, but
again, its newsgroups ... what am i thinking ...

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: English Idiom in Unix: Directory Recursively

2011-05-26 Thread Xah Lee
On May 26, 4:20 am, Thorsten Kampe  wrote:
> Did your mom tell you to "recursively clean up your room"?.

that had me L O L!

i think i'll quote in my unix hating blogs sometimes, if you don't
mind. ☺

 Xah
-- 
http://mail.python.org/mailman/listinfo/python-list


A Moronicity of Guido van Rossum

2005-09-29 Thread Xah Lee
A Moronicity of Guido van Rossum

Xah Lee, 200509

On Guido van Rossum's website:
 http://www.artima.com/weblogs/viewpost.jsp?thread=98196
dated 20050826, he muses with the idea that he would like to remove
lambda, reduce(), filter() and map() constructs in a future version
Python 3000.

Guido wrote:
«filter(P, S) is almost always written clearer as [x for x in S if
P(x)], and this has the huge advantage that the most common usages
involve predicates that are comparisons, e.g. x==42, and defining a
lambda for that just requires much more effort for the reader (plus the
lambda is slower than the list comprehension)»

the form “[x for x in S if P(x)]” is certainly not more clear than
“filter(P, S)”. The latter is clearly a function. What the fuck is
the former? A function every programer in any language can understand
and appreciate its form and function. What the fuck would anyone to
expect everyone to appreciate a Python syntactical idiosyncrasy “[x
for ...]”?

also, the argument that the from “filter(F,S)” being cumbersome
because the first argument is a function and that mostly likely it
would be a function that returns true and false thus most people will
probably use the form “lambda” and that is quite cumbersome than if
the whole thing is written with the syntactical idiosyncrasy “[x for
...]”, is rather inane, as you can now see.

The filter(decision_function,list) form is clean, concise, and helps
thinking. Why it helps thinking? Because it condenses the whole
operation into its mathematical essence with the most clarity. That is,
it filters, of a list, and by a yes/no decision function. Nothing is
more, and nothing can be less. It is unfortunate that we have the
jargon Lambda and Predicate developed by the morons in the tech geekers
of the functional programing community. The lambda could be renamed
Pure Function and the Predicate could be called True/False function,
but the world being the way they are already, it is unwise to rewrite
every existing Perl program just because somebody invented another
language.

If the predicate in lambda in filter() is cumbersome, so would exactly
the same thing appear in the syntactical idiosyncrasy “[x for x in S
if P(x)]”.

Guido added this sting as a afterthought:
«(plus the lambda is slower than the list comprehension)»

Which is faster is really the whim and capacity of Python
implementators. And, just before we were using criterion of simplicity.
The concept of a function every programer understands, what the fuck is
a List Comprehension?
Why don't you scrap list comprehension in Python 3000 and create a
table() function that's simpler in syntax and more powerful in
semantics? ( See http://xahlee.org/perl-python/list_comprehension.html
)

Guido wrote:
«Why drop lambda? Most Python users are unfamiliar with Lisp or
Scheme, so the name is confusing; also, there is a widespread
misunderstanding that lambda can do things that a nested function can't
-- I still recall Laura Creighton's Aha!-erlebnis after I showed her
there was no difference! Even with a better name, I think having the
two choices side-by-side just requires programmers to think about
making a choice that's irrelevant for their program; not having the
choice streamlines the thought process. Also, once map(), filter() and
reduce() are gone, there aren't a whole lot of places where you really
need to write very short local functions; Tkinter callbacks come to
mind, but I find that more often than not the callbacks should be
methods of some state-carrying object anyway (the exception being toy
programs).»

In the outset Guido here assumes a moronitude about the set of Python
users and what they are familiar of. Python users 10 years ago are not
the same Python users today, and will certainly not be the same 10
years later if you chop off lambda. Things change, math literacy
advances, and what users you have changes with what you are. A pure
function (lambda) is the gist of a mathematical idea embodied in
computer languages, not something from LISP or Scheme as tech geeking
morons wont to think.

Guido wrote:
«... there is a widespread misunderstanding that lambda can do things
that a nested function can't...».

One is so insulted by a bigshot in the industry of quoting something so
disparate then shot it down as if showing his perspicacity.

A lambda is a syntax for function or a name for the concept of
function. What the fuck does it mean that a lambda isn't as powerful as
nested function??

The lambda in Python is really ill. It is designed with a built-in
limitation in the first place, and regarded as some foreign substance
in the Imperative crowd such as the Pythoners. If there's any problem
with lambda, it is with lambda in Python and Pythoner's attitude.

Guido wrote:
«Also, once map(), filter() and reduce() are gone, there aren't a
whole lot of places where you really need to write very short local
functions;»

Of course, you begi

Re: A Moronicity of Guido van Rossum

2005-09-29 Thread Xah Lee
addendum:

reduce() in fact embodies a form of iteration/recursion on lists, very
suitable in a functional language environment. If Python's lambda and
other functional facilities are more powerful, reduce() would be a good
addition. For instance, in functional programing, it is a paradigm to
nest or sequence functions. (most readers will be familiar in the form
of unix shell's “pipe”). When you sequence functions, you can't
stop in the middle and do a loop construct. So, reduce() and other
functional forms of iteration are convenient and necessary.

--
For version with slight professionalism (sans “fuck”), see:
 http://xahlee.org/perl-python/python_3000.html

Note: Guido certainly isn't a moron. But, this post of his shows his
haughtiness, and rather unfamiliarity with functional programing. (i.e.
has he, worked in a functional language in any significant length or
project?) However, he's got the audacity to assert things, probably due
to bigshot status.

Guido's stumble isn't a rare instance in the industry, and i don't take
him to be of any sinister nature. (i don't know much about Guido the
person or personality.)

There are quite a lot fucking liers and charlatans in the computing
industry, especially the OpenSourcers, from the fucking
a-dime-a-million students with their “FREE” irresponsible homeworks
on the net to fuckheads like James Gosling of Java , Larry Wall of
Perl, Linus Torvolts of Linux kernel, and that fuckhead C++ Berjo
something, the unix advocating fuckers, and those “gang of four”
Design Patterns shit and the criminals of eXtreme Programing and UML...
with these pundits begets one generation of fucking tech geeking coding
monkeys, thinking that they know something, while creating a mass of
garbage that crashes and fucks us up everyday in the computing world.

(disclaimer: this post is pure opinion.)

"The required techniques of effective reasoning are pretty formal, but
as long as programming is done by people that don't master them, the
software crisis will remain with us and will be considered an incurable
disease. And you know what incurable diseases do: they invite the
quacks and charlatans in, who in this case take the form of Software
Engineering gurus." —Edsger Dijkstra 1930-2002.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: A Moronicity of Guido van Rossum

2005-10-01 Thread Xah Lee
y the list in-place, some
returns a new list.

-

one is quite sorry to read a big shot contemplating on petty issues
with a ambitious name Python THREE THOUSAND.

For the grand Python THREE THOUSAND, what about supporting non-trivial
things such as built-in transparent fractions? What about a smart
exact-arithmetics once for all? What about supporting pattern matching?
(not textual pattern matching (e.g. regex) as Imperative Morons wont to
understand, but patterns of list structures and data types.)

the features of Mathematica mentioned above existed over a decade ago.
But today, OpenSourcing bigwigs can contemplate and dither nothing but
which lipstick to use.

A good number of the industrial dignitaries are just fucking liers. And
today we have the fucking Java and fucking Perl and their bosses
trumpeting their fucking state-of-the-art-ness. Go fuck your wifes.

(disclaimer: all mentions of any real person are just opinion.)

-
See also:
http://xahlee.org/perl-python/python_3000.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

check html file size

2005-10-04 Thread Xah Lee
would anyone like to translate the following perl script to Python or
Scheme (scsh)?

the file takes a inpath, and report all html files in it above certain
size. (counting inline images)
also print a sorted report of html files and their size.

(a copy of the script is here:
http://xahlee.org/_scripts/check_file_size.pl
)

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/


# perl

# Tue Oct  4 14:36:48 PDT 2005
# given a dir, report all html file's size. (counting inline images)
# XahLee.org

use Data::Dumper;
use File::Find;
use File::Basename;

$inpath = '/Users/t/web/mydirectory/';
$sizeLimit = 800 * 1000;

# $inpath = $ARGV[0]; # should give a full path; else the
$File::Find::dir won't give full path.
while ($inpath =~ [EMAIL PROTECTED](.+)/$@) { $inpath = $1;} # get rid of 
trailing
slash

die "dir $inpath doesn't exist! $!" unless -e $inpath;


##
# subroutines


# getInlineImg($file_full_path) returns a array that is a list of
inline images. For example, it may return ('xx.jpg','../image.png')
sub getInlineImg ($) { $full_file_name= $_[0];
@linx =(); open (FF, "<$full_file_name") or die "error: can not open
$full_file_name $!";
while () { @txt_segs = split(m/img/, $_); shift @txt_segs;
for $lin (@txt_segs) { if ($lin =~ m@ src\s*=\s*\"([^\"]+)\"@i) 
{
push @linx, $1; }}
} close FF;
return @linx;
}

# linkFullPath($dir,$locallink) returns a string that is the full path
to the local link. For example,
linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
'Users/t/public_html/a/image/t.png'. The returned result will not
contain double slash or '../' string.
sub linkFullPath($$){ $result=$_[0] . $_[1]; while ($result =~
[EMAIL PROTECTED]/\/@\/@) {}; while ($result =~ s@/[^\/]+\/\.\.@@) {}; return
$result;}


# listLocalLinks($html_file_full_path) returns a array where each
element is a full path of local links in the html.
sub listLocalLinks($) {
my $htmlfile= $_[0];

my ($name, $dir, $suffix) = fileparse($htmlfile, ('\.html') );
my @aa = getlinks($htmlfile);
@aa = grep(!m/\#/, @aa);
  @aa = grep (!m/^mailto:/, @aa);
  @aa = grep (!m/^http:/, @aa);

my @linkedFiles=();
foreach my $lix (@aa) { push @linkedFiles, linkFullPath($dir,$lix);}
return @linkedFiles;
}


# listInlineImg($html_file_full_path) returns a array where each
element is a full path to inline images in the html.
sub listInlineImg($) {
my $htmlfile= $_[0];

my ($name, $dir, $suffix) = fileparse($htmlfile, ('\.html') );
my @aa = getInlineImg($htmlfile);

my @result=();
foreach my $ele (@aa) { push @result, linkFullPath($dir,$ele);}
return @result;
}

##
sub checkLink {
if (
-T $File::Find::name
&& $File::Find::name =~ [EMAIL PROTECTED]@
) {
$total= -s $File::Find::name;
@h2 = listInlineImg($File::Find::name);
for my $ln (@h2) {$total += -s $ln;};
if ( $total > $sizeLimit) {print "problem: file:
$File::Find::name, size: $total\n";}

push (@result, [$total, $File::Find::name]);
};
}

find(\&checkLink, $inpath);

@result = sort { $b->[0] <=> $a->[0]} @result;

print Dumper([EMAIL PROTECTED]);
print "done reporting. (any file above size are printed above.)";

__END__

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: check html file size

2005-10-07 Thread Xah Lee
Xah Lee wrote: « would anyone like to translate the following perl
script to Python or Scheme (scsh)?»

Here's the Python version.

# -*- coding: utf-8 -*-
# Python


# Wed Oct  5 15:50:31 PDT 2005
# given a dir, report all html file's size. (counting inline images)
# XahLee.org

import re, os.path, sys

inpath= '/Users/t/web/'

while inpath[-1] == '/': inpath = inpath[0:-1] # get rid of trailing
slash

if (not os.path.exists(inpath)):
print "dir " + inpath + " doesn't exist!"
sys.exit(1)

##
# subroutines


def getInlineImg(file_full_path):
'''getInlineImg($file_full_path) returns a array that is a list of
inline images. For example, it may return ['xx.jpg','../image.png']'''

FF = open(file_full_path,'rb')
txt_segs = re.split( r'src', unicode(FF.read(),'utf-8'))
txt_segs.pop(0)
FF.close()
linx=[]
for linkBlock in txt_segs:
matchResult = re.search(r'\s*=\s*\"([^\"]+)\"', linkBlock)
if matchResult: linx.append( matchResult.group(1) )
return linx


def linkFullPath(dir,locallink):
'''linkFullPath(dir, locallink) returns a string that is the full
path to the local link. For example,
linkFullPath('/Users/t/public_html/a/b', '../image/t.png') returns
'Users/t/public_html/a/image/t.png'. The returned result will not
contain double slash or '../' string.'''
result = dir + '/' + locallink
result = re.sub(r'//+', r'/', result)
while re.search(r'/[^\/]+\/\.\.', result): result =
re.sub(r'/[^\/]+\/\.\.', '', result)
return result

def listInlineImg(htmlfile):
'''listInlineImg($html_file_full_path) returns a array where each
element is a full path to inline images in the html.'''
dir=os.path.dirname(htmlfile)
imgPaths = getInlineImg(htmlfile)
result = []
for aPath in imgPaths:
result.append(linkFullPath( dir, aPath))
return result


##
# main

fileSizeList=[]
def checkLink(dummy, dirPath, fileList):
for fileName in fileList:
if '.html' == os.path.splitext(fileName)[1] and
os.path.isfile(dirPath+'/'+fileName):
totalSize = os.path.getsize(dirPath+'/'+fileName)
imagePathList = listInlineImg(dirPath+'/'+fileName)
for imgPath in imagePathList: totalSize +=
os.path.getsize(imgPath)
fileSizeList.append([totalSize, dirPath+'/'+fileName])


os.path.walk(inpath, checkLink, 'dummy')

fileSizeList.sort(key=lambda x:x[0],reverse=True)

for it in fileSizeList: print it
print "done reporting."



-----
This Python version is a direct translation of the Perl version. They
match pretty much line by line.

for both the Python version and the Perl version, see:
 http://xahlee.org/perl-python/check_html_size.html

Would any lisper provides a Scheme version? i don't think i'll do a
Scheme version anytime soon. Please, Schemers, show us some fanfare.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Daisy Daisy, give me your answer do

2005-10-08 Thread Xah Lee
there is a MacPerl program posted in 1998 that uses Mac's speech synth
to sing Daisy Bell.
See:

http://bumppo.net/lists/macperl/1998/11/msg00412.html

can anyone modify it so it runs out of the box on today's OS X?

PS i'm posting this also in python and lisp group, i hope it'd be some
general interest. For some background of this song, see
  http://xahlee.org/Periodic_dosage_dir/sanga_pemci/daisy_bell.html

i'm interested in getting versions that can sing the song in Windows,
Mac, Linux using whatever speech synth each OS may provide. Thanks.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Daisy Daisy, give me your answer do

2005-10-08 Thread Xah Lee
Dear Michael Goettsche,

why don't you lead the pack to be on-topic for a change, huh?

 Xah

Michael Goettsche wrote:
> On Saturday 08 October 2005 22:10, Xah Lee wrote:
> > there is a MacPerl program posted in 1998 that uses Mac's speech synth
> > to sing Daisy Bell.
> > See:
> >
> > http://bumppo.net/lists/macperl/1998/11/msg00412.html
> >
> > can anyone modify it so it runs out of the box on today's OS X?
> >
> > PS i'm posting this also in python and lisp group, i hope it'd be some
> > general interest. For some background of this song, see
> >   http://xahlee.org/Periodic_dosage_dir/sanga_pemci/daisy_bell.html
> >
> > i'm interested in getting versions that can sing the song in Windows,
> > Mac, Linux using whatever speech synth each OS may provide. Thanks.
> >
> >  Xah
> >  [EMAIL PROTECTED]
> > ∑ http://xahlee.org/
>
> You're asking "tech geekers" and "morons" to do this job? Isn't that a task
> for somebody more professional like you?

-- 
http://mail.python.org/mailman/listinfo/python-list

Pythot doc problem: lambda keyword...

2005-10-09 Thread Xah Lee
i'm trying to lookup on the detail of language Python's “lambda”
function feature. I've seen it before, but today i need to read about
it again since i'm writing. I quickly went to the index page:
http://python.org/doc/2.4.1/lib/genindex.html
but all i got is a LambdaType.

i'm thinking, maybe built-in functions are not listed there. Lo, but
map() and friends are there. O, maybe i'm confused and “lambda” is
actually a keyword by this particular language's feelings, but Lo,
“while” is certainly a keyword but it is there.

Fuck the Python liers and ignorant fuckheads. Motherfucking don't know
shit and yet lying thru their teeth with fanfare.

(for the technical context and justification of this message, please
see the essays at the bottom of:
http://xahlee.org/perl-python/python.html )

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Perl-Python-a-Day: Sorting

2005-10-10 Thread Xah Lee
Sort a List

Xah Lee, 200510

In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.

To sort a list in Python, use the “sort” method. For example:

li=[1,9,2,3];
li.sort();
print li;


Note that sort is a method, and the list is changed in place.

Suppose you have a matrix, and you want to sort by second column.
Example Solution:

li=[[2,6],[1,3],[5,4]]
li.sort(lambda x, y: cmp(x[1],y[1]))
print li; # prints [[1, 3], [5, 4], [2, 6]]


The line “li.sort(lambda x, y: cmp(x[1],y[1]))” can also be written
as “li.sort(cmp=lambda x, y: cmp(x[1],y[1]))”

The argument to sort is a function of two arguments, that returns -1,
0, 1. This function is a decision function that tells sort() how to
decide the order of any two elements in your list. If the first
argument is “less” then second argument, the function should return
-1. If equal, then 0. Else, 1.

Here's a more complex example. Suppose you have a list of strings.

'my283.jpg'
'my23i.jpg'
'web7-s.jpg'
'fris88large.jpg'
...


You want to sort them by the number embedded in them. What you have to
do, is to provide sort() method a function, that takes two strings, and
compares the integer inside the string. Here's the solution:

li=[
'my283.jpg',
'my23i.jpg',
'web7-s.jpg',
'fris88large.jpg',
]

def myComp (x,y):
import re
def getNum(str): return float(re.findall(r'\d+',str)[0])
return cmp(getNum(x),getNum(y))

li.sort(myComp)
print li # returns ['web7-s.jpg', 'my23i.jpg', 'fris88large.jpg',
'my283.jpg']


Here, we defined a function myComp to tell sort about the ordering.
Normally, one would use the “lambda” construct, but Python's lambda
construct can only represent the simplest functions.

Some Math about Sorting

In general, the function f used to determine the order of any two
element must satisfy some constraints:

• f(a,a)==0
• if f(a,b)==0 then f(b,a)==0
• if f(a,b)==0 and f(b,c)==0, then f(a,c)==0.
• if f(a,b)==-1 and f(b,c)==-1, then f(a,c)==-1.
• if f(a,b)==-1, then f(b,a)==1.


If the comparison function does not behave as the above, then it is not
consistent, meaning that the result “ordered” list is may actually
be different depending how the language happens to implement sort.

The significance of all these is that in real software you may want to
sort a list of non-simple entities by a specialized ordering. For
example, you may want to sort a list of polygonal surfaces in 3D space,
for particular reasons in implementing some computer graphics features.
Say, you want to sort these polygons by their spacial orientations. It
is in advanced cases like these, understanding the basic math about
ordering is important. Otherwise, you might have a bewildering result
yet unable to locate any flaws in your code.

Python's “sort” method's optional parameters: “key” and
“reverse”

Most of the time, sorting is done for a list of atomic element such as
[3,2,4]. This is simply done by myList.sort() without any argument.
Other than simple list, sort is frequently used on matrixes (e.g.
[[2,6],[1,3],[5,4]]). For matrixes, almost always a particular column
is used for the basis of ordering. For example, if we want to sort by
second column, we do: “li.sort(lambda x, y: cmp(x[1],y[1]))”. Since
this is frequently used, Python provides a somewhat shorter syntax for
it, by specifying the column used as the ordering “key”. For
example:

li=[[2,6],[1,3],[5,4]]
li.sort(key=lambda x:x[1] ) # is equivalent to the following
#li.sort(lambda x, y: cmp(x[1],y[1]))
print li; # prints [[1, 3], [5, 4], [2, 6]]


Because Python's implementation is not very refined , this specialized
syntax is actually much speedier than the general form “lambda x, y:
cmp(x[1],y[1])”. It is a burden on the programer to always use the
“key” syntax idiosyncrasy if he is sorting a large matrix.

Another idiosyncratic provision is the optional “reverse” argument.
This parameter is somewhat necessary when using the “key”
parameter. One can reverse the ordering by using the “reverse”
keyword as a argument to sort. Example:

The following are equivalent:

li.sort(key=lambda x:x[1], reverse=True )
li.sort(lambda x, y: cmp(x[1],y[1]), reverse=True)
li.sort(lambda x, y: cmp(y[1],x[1]))


The official doc on Python's sort method is at (bottom):
http://python.org/doc/2.4/lib/typesseq-mutable.html

Sorting in Perl

(to be posted in a couple of days)

This post is archived at:
http://xahlee.org/perl-python/sort_list.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Python Doc Problem Example: sort() (reprise)

2005-10-11 Thread Xah Lee
Python Doc Problem Example: sort()

Xah Lee, 200503
Exhibit: Incompletion & Imprecision

Python doc “3.6.4 Mutable Sequence Types” at
http://python.org/doc/2.4/lib/typesseq-mutable.html

in which contains the documentation of the “sort” method of a list.
Quote:

«
Operation   Result  Notes
s.sort([cmp[, key[, reverse]]]) sort the items of s in place(7),
(8), (9), (10)

(7) The sort() and reverse() methods modify the list in place for
economy of space when sorting or reversing a large list. To remind you
that they operate by side effect, they don't return the sorted or
reversed list.

(8) The sort() method takes optional arguments for controlling the
comparisons.

cmp specifies a custom comparison function of two arguments (list
items) which should return a negative, zero or positive number
depending on whether the first argument is considered smaller than,
equal to, or larger than the second argument: "cmp=lambda x,y:
cmp(x.lower(), y.lower())"

key specifies a function of one argument that is used to extract a
comparison key from each list element: "cmp=str.lower"

reverse is a boolean value. If set to True, then the list elements
are sorted as if each comparison were reversed.

In general, the key and reverse conversion processes are much
faster than specifying an equivalent cmp function. This is because cmp
is called multiple times for each list element while key and reverse
touch each element only once.

Changed in version 2.3: Support for None as an equivalent to
omitting cmp was added.

Changed in version 2.4: Support for key and reverse was added.

(9) Starting with Python 2.3, the sort() method is guaranteed to be
stable. A sort is stable if it guarantees not to change the relative
order of elements that compare equal -- this is helpful for sorting in
multiple passes (for example, sort by department, then by salary
grade).

(10) While a list is being sorted, the effect of attempting to
mutate, or even inspect, the list is undefined. The C implementation of
Python 2.3 and newer makes the list appear empty for the duration, and
raises ValueError if it can detect that the list has been mutated
during a sort.
»


As a piece of documentation, this is a lousy one.

The question Python doc writers need to ask when evaluating this piece
of doc are these:

• can a experienced programer who is expert at several languages but
new to Python, and also have read the official Python tutorial, can he,
read this doc, and know exactly how to use sort with all the options?

• can this piece of documentation be rewritten fairly easily, so that
the answer to the previous question is a resounding yes?

To me, the answers to the above questions are No and Yes. Here are some
issues with the doc:

• In the paragraph about the “key” parameter, the illustration
given is: “cmp=str.lower”. It should be be “key=str.lower”

• This doc lacks examples. One or two examples will help a lot,
especially to less experienced programers. (which comprises the
majority of readers) In particular, it should give a full example of
using the comparison function and one with the “key” parameter.
Examples are particularly needed here because these parameteres are
functions, often with the “lambda” construct. These are unusual and
advanced constructs among imperative programers.

• This doc fails to mention what happens when the predicate and the
shortcut version conflicts. e.g. “myList.sort(cmp=lambda x,y:
cmp(x[0], y[0]), key=lambda x: str(x[1]) )”

• The syntax notation Python doc have adopted for indicating optional
parameters, does not give a clear view just exactly what combination of
optional parameters can be omitted. The notation: “s.sort([cmp[,
key[, reverse]]])” gives the impression that only trailing arguments
can be omitted, which is not true.

• The doc gives no indication of how to omit a optional arg. Should
it be “nul”, “Null”, 0, or left empty? Since it doesn't give
any examples, doc reader who isn't Python experts is left to guess at
how true/false values are presented in Python.

• On the whole, the way this doc is written does not give a clear
picture of the roles of the supplied options, nor how to use them.

Suggested Quick Remedy: add a example of using the cmp function. And a
example using the “key” function. Add a example of Using one of
them and with reverse. (the examples need not to come with much
explanations. One sentence annotation is better than none.)

Other than that, the way the doc is layed out with a terse table and
run-on footnotes (employed in several places in Python doc) is not
inductive. For a better improvement, there needs to be a overhaul of
the organization and the attitude of the entire doc. The organization
needs to be programing based, as opposed to implementation or computer
science based. (in this regard, one can learn from the Perl folks). As
to attitude, the writing needs to be Python-as-is, as op

Re: Python Doc Problem Example: sort() (reprise)

2005-10-11 Thread Xah Lee
Addendum, 200510

Here's further example of Python's extreme low quality of
documentation. In particular, what follows focuses on the bad writing
skill aspect, and comments on some language design and quality issues
of Python.

>From the Official Python documentation of the sort() method, at:
http://python.org/doc/2.4.2/lib/typesseq-mutable.html, Quote:

«The sort() method takes optional arguments for controlling the
comparisons.»

It should be “optional parameter” not “optional argument”.
Their difference is that “parameter” indicates the variable, while
“argument” indicates the actual value.

«... for controlling the comparisons.»

This is a bad writing caused by lack of understanding. No, it doesn't
“control the comparison”. The proper way to say it is that “the
comparison function specifies an order”.

«The sort() and reverse() methods modify the list in place for economy
of space when sorting or reversing a large list. To remind you that
they operate by side effect, they don't return the sorted or reversed
list. »

This is a example of tech-geeking drivel. The sort() and reverse()
methods are just the way they are. Their design and behavior are really
not for some economy or remind programers of something. The Python doc
is bulked with these irrelevant drivels. These littered inanities
dragged down the whole quality and effectiveness of the doc implicitly.

«Changed in version 2.4: Support for key and reverse was added.»

«In general, the key and reverse conversion processes are much faster
than specifying an equivalent cmp function. This is because cmp is
called multiple times for each list element while key and reverse touch
each element only once.»

When sorting something, one needs to specify a order. The easiest way
is to simply list all the elements as a sequence. That way, their order
is clearly laid out. However, this is in general not feasible and
impractical. Therefore, we devised a mathematically condensed way to
specify the order, by defining a function f(x,y) that can take any two
elements and tell us which one comes first. This, is the gist of
sorting a list in any programing language.

The ordering function, being a mathematically condensed way of
specifying the order, has some constraints. For example, the function
should not tell us x < y and y < x. (For a complete list of these
constraints, see http://xahlee.org/perl-python/sort_list.html )

With this ordering function, it is all sort needed to sort a list.
Anything more is interface complexity.

The optional parameters “key” and “reverse” in Python's sort
method is a interface complexity. What happened here is that a compiler
optimization problem is evaded by moving it into the language syntax
for programers to worry about. If the programer does not use the
“key” syntax when sorting a large matrix (provided that he knew in
advance of the list to be sorted or the ordering function), then he is
penalized by a severe inefficiency by a order of magnitude of execution
time.

This situation, of moving compiler problems to the syntax surface is
common in imperative languages.

«Changed in version 2.3: Support for None as an equivalent to omitting
cmp was added.»

This is a epitome of catering towards morons. “myList.sort()” is
perfect but Pythoners had to add “myList.sort(None)” interface
complexity just because idiots need it.

The motivation here is simple: a explicit “None” gives coding
monkeys a direct sensory input of the fact that “there is no
comparison function”. This is like the double negative in black
English “I ain't no gonna do it!”. Logically, “None” is not
even correct and leads to bad thinking. What really should be stated in
the doc, is that “the default ordering function to sort() is the
‘cmp’ function.”.

«Starting with Python 2.3, the sort() method is guaranteed to be
stable. A sort is stable if it guarantees not to change the relative
order of elements that compare equal -- this is helpful for sorting in
multiple passes (for example, sort by department, then by salary
grade).»

One is quite surprised to read this. For about a decade of a language's
existence, its sort functionality is not smart enough to preserve
order?? A sort that preserves original order isn't something difficult
to implement. What we have here is sloppiness and poor quality common
in OpenSource projects.

Also note the extreme low quality of the writing. It employes the
jargon “stable sort” then proceed to explain what it is, and the
latch on of “multiple passes” and the mysterious “by department,
by salary”.

Here's a suggested rewrite: “Since Python 2.3, the result of sort()
no longer rearrange elements where the comparison function returns
0.”
---
This post is archived at:
http://xahlee.org/perl-python/python_doc_sort.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Perl-Python-a-Day: Sorting

2005-10-12 Thread Xah Lee
Sorting in Perl

In Perl, to sort a list, do like this:

@li=(1,9,2,3);
@li2 = sort {$a <=> $b} @li;
print join(' ', @li2);


In Perl, sort is a function, not some Object Oriented thing. It returns
the sorted result as another list. This is very simple and nice.

It works like this: sort takes the form “sort {...} @myList”.
Inside the enclosing braces is the body of the ordering function, where
variables $a and $b inside are predefined by the language to represent
two elements in the list. The operator “<=>” returns -1 if left
element is less than right element. If equal, it returns 0, else 1. It
is equivalent to Python's “cmp” function.

Perl provides another comparison operator “cmp”, which treats its
operands as strings. So, for example:

print "3" <=> "20"; # prints -1
print "3" cmp "20"; # prints 1


In Perl, numbers and strings are mutually automatically converted if
needed.

Another form of sort is “sort orderFunctionName @list”, which uses
a function name in place of the comparison block

Just for completeness, let's define a Perl equivalent of a Python
example above.

@li=(
'my283.jpg',
'my23i.jpg',
'web7-s.jpg',
'fris88large.jpg',
);

# sorts a list of strings by their embedded number
@li2 = sort { ($a=~m/(\d+)/)[0] <=> ($b=~m/(\d+)/)[0];} @li;
print join(' ', @li2);  # prints web7-s.jpg my23i.jpg fris88large.jpg
my283.jpg


Note, that Perl also has pure functions (lambda) construct. In Perl, a
pure function is simply done as “def {...}”, and applied to
argument by the form “pureFunction->(args)”. Unlike Python, Perl
has no limitation in its pure function construct. Because of this, Perl
supports a limited form of functional programing. Here's a simple
example:

$result = sub($) {$_[0]+1}->(3);
print $result; # prints 4
# a value obtained by a pure function that adds 1 to its argument,
# applied to a argument of 3.


Perl, like Python, whose compiler is not very smart. In sorting, the
ordering function is called unnecessarily repetitiously. Like Python,
Perlers have sought means to avoid this penalty. If the programer knew
in advance that his matrix is huge or knew in advance his ordering
function, then he can code his sort by writing it using a very complex
workaround.:

Here's how this work around works: Suppose you want to sort a huge list
with a expensive ordering function. If you simply do “sort
orderingFunction @myList”, then Perl is going to call your
orderingFunction wantonly, and you lose. To beat Perl, you first
generate a copy newList, whose elements are pairs (x,y), where x is
from the original list, and y is the sorting key extracted from x.
Then, you call Perl's sort function on this newList and using a
ordering function based on the second list element (the y). Once this
newList is sorted, you construct your original list by extracting the
first element from this sorted newList. Here's the code example:

# the following two lines are equivalent
@li2 = sort { ($a=~m/(\d+)/)[0] <=> ($b=~m/(\d+)/)[0];} @li;
@li2 = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_,
($_=~m/(\d+)/)[0] ] } @li;


In the above Perl code, the part “map { [ $_, ($_=~m/(\d+)/)[0] ] }
@li;” generates the intermediate newList. Then, sort is applied to
it, then, another map “map { $_->[0] }” gets the original list
sorted.

In this way, the cost of the internals of the ordering function is
avoided. (it runs on each list element once) However, your huge list is
copied 1 extra time. So, there are pros and cons. Because this work
around is very complex in both its semantics and syntax, it has
acquired a coolness factor among Perl coders, and is given the name
Schwartzian Transform.

It is interesting to note what compiler flaws can do to imperative
languages and its people. In Python, the language syntax is tainted. In
Perl, a complex construct is invented. In both camps, the basic
mathematics of sorting and its implementation aspects are completely
belied.

For the official doc of Perl's sort, type on the command line:
“perldoc -f sort”.
-
this post is archived at:
http://xahlee.org/perl-python/sort_list.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Perl-Python-a-Day: Sorting

2005-10-12 Thread Xah Lee

Lasse Vågsæther Karlsen wrote:

> Like the sorted function in Python ?
>
> li2 = sorted(li)
>
> you can also specify a key and a cmp function if you need to.

Thanks. I didn't know there's also a sort function in Python (2.4),
besides the method. (i've mentioned your name as acknowledgement at my
website essay)

The Python doc really should mention it at the place where the sort
method is documented.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Microsoft Hatred FAQ

2005-10-14 Thread Xah Lee
Microsoft Hatred, FAQ

Xah Lee, 20020518

Question: U.S. Judges are not morons, and quite a few others are
not morons. They find MS guilty, so it must be true.

Answer: so did the German population thought Jews are morons by
heritage, to the point that Jews should be exterminated from earth.
Apparently, the entire German population cannot be morons, they must be
right.

Judge for yourself, is a principle i abide by. And when you judge, it
is better to put some effort into it.

How much you invest in this endearvor depends on how important the
issue is to you. If you are like most people, for which the issue of
Microsoft have remote effect on your personal well-being, then you can
go out and buy a case of beer on one hand and pizza on the other, and
rap with your online confabulation buddies about how evil is MS. If you
are an author writing a book on this, then obviously its different
because your reputation and ultimately daily bread depend on what you
put down. If you are a MS competitor such as Apple or Sun, then
obviously you will see to it with as much money as you can cough out
that MS is guilty by all measures and gets put out of business. If you
are a government employee such as a judge, of course it is your
interest to please your boss, with your best accessment of the air.

When i judge things, i like to imagine things being serious, as if my
wife is a wager, my daughter is at stake, that any small factual error
or mis-judgement or misleading perspective will cause unimaginable
things to happen. Then, my opinions becomes better ones.

Q: Microsoft's Operating System is used over 90% of PCs. If that's
not monopoly, i don't know what is.

A: Now suppose there is a very ethical company E, whose products have
the best performance/price ratio, and making all the competitors
looking so majorly stupid and ultimately won over 90% of the market as
decided by consumers. Is E now a monopoly? Apparently, beer drinkers
and pizza eaters needs to study a bit on the word monopoly, from the
perspectives of language to history to law. If they have some extra
time, they can sharpen views from philosophy & logic contexts as well.

Q: What about all the people in the corporate environments who are
forced to use MS products and aren't allowed the option/choice to use
Mac/Linux/UNIX?

A: Kick your boss's ass, or, choose to work for a company who have
decisions that you liked.

Q: What about MS buying out all competitors?

A: Microsoft offered me $1 grand for saying good things about them.
They didn't put a gunpoint on my head. I CHOOSE to take the bribe.
Likewise, sold companies can and have decided what's best for them.
It's nothing like under gunpoint.

Q: Microsoft forced computer makers to not install competitor's
applications or OSes.

A: It is free country. Don't like MS this or that? Fuck MS and talk to
the Solaris or BeOS or AIX or HP-UX or Apple or OS/2 or Amiga or NeXT
or the Linuxes with their free yet fantastically easy-to-use and
network-spamming X-Windows. Bad business prospects? Then grab the
opportunity and become an entrepreneur and market your own beats-all
OS. Too difficult? Let's sue Microsoft!

Q: Microsoft distributed their Internet Explorer web browser free,
using their “monopoly” power to put Netscape out of business.

A: entirely inane coding monkeys listen: It takes huge investment to
give away a quality software free. Netscape can give away Operating
Systems free to put MS out of business too. Nobody is stopping Sun
Microsystem from giving Java free, or BeOS a browser free, or Apple to
bundle QuickTime deeply with their OS free.

Not to mention that Netscape is worse than IE in just about every
version till they become the OpenSource mozilla shit and eventually
bought out by AOL and still shit.

• Netscape struggles, announced open browser source code in 1998-01,
industry shock
http://wp.netscape.com/newsref/pr/newsrelease558.html

• Netscape browser code released in 1998-03. Mozilla FAQ.
http://mozilla.org/docs/mozilla-faq.html

• AOL buys Netscape in 1998-11 for 4.2 billion.
http://news.com.com/2100-1023-218360.html?legacy=cnet

• Jamie Zawinski, resignation and postmortem, 1999-04
http://www.jwz.org/gruntle/nomo.html

• suck.com, Greg Knauss & Terry Colon, 2000-04, Netscape 6 mockery
http://www.suck.com/daily/2000/04/10/
http://xahlee.org/UnixResource_dir/_/24,greg_knauss_netscape.zip

• Xah Lee, Netscape Crap
http://xahlee.org/Writ_dir/macos-talk/58.txt

Q: Microsoft implemented extra things to standard protocols in
their OS so that other OS makers cannot be compatible with their OS
while their OS can be compatible with all. They used this Embrace &
Extend to lock out competitors.

A: My perspective is this: suppose you are now a company who's OS sits
over 90% of computers (regardless how this come to be for the moment).
Now, lots of “standard” protocols in the industry is a result o

tuple versus list

2005-10-16 Thread Xah Lee
suppose i'm going to have a data structure like this:

[
[imgFullPath,(width, height)],
[imgFullPath,(width, height)],
[imgFullPath,(width, height)],
[imgFullPath,(width, height)],
...
]

should i use (width,height) or [width,height]?
what advantage i get to use n-tuple instead of the generic list?

Thanks.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Perl-Python-a-Day: split a file full path

2005-10-16 Thread Xah Lee
Split File Fullpath Into Parts

Xah Lee, 20051016

Often, we are given a file fullpath and we need to split it into the
directory name and file name. The file name is often split into a core
part and a extension part. For example:

'/Users/t/web/perl-python/I_Love_You.html'
becomes

'/Users/t/web/perl-python/'  (directory name)
'I_Love_You' (file's base name)
'.html'  (file's “extension”)


Depending on the language, some language will remove the trailing slash
after the dir name, and some will omit the dot before the suffix.

In Python, to split a full path into parts is done with the os.path
module. Example:

# python
import os.path

myPath = '/Users/t/web/perl-python/I_Love_You.html'
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)

print dirName # /Users/t/web/perl-python
print fileName# I_Love_You.html
print fileBaseName# I_Love_You
print fileExtension   # .html


The official doc of the os.path module is at:
http://www.python.org/doc/2.4.1/lib/module-os.path.html

In Perl, spliting a full path into parts is done like this:

# perl
use File::Basename;

$myPath = '/Users/t/web/perl-python/I_Love_You.html';

($fileBaseName, $dirName, $fileExtension) = fileparse($myPath,
('\.html') );

print $fileBaseName, "\n";   # I_Love_You
print $dirName, "\n";# /Users/t/web/perl-python/
print $fileExtension, "\n";  # .html


Note: the second argument to fileparse() is a list of regex. In
particular, you need to escape the dot.

For the official doc, type in command line: “perldoc File::Path”.
--
This post is archived at
  http://xahlee.org/perl-python/split_fullpath.html

Schemers, a scsh version will be appreciated.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: bizarro world (was Re: Python Doc Problem Example: sort() (reprise))

2005-10-17 Thread Xah Lee

Bryan wrote:
> mr. xah... would you be willing to give a lecture at pycon 2006?  i'm sure you
> would draw a huge crowd and a lot of people would like to meet you in 
> person...
>
> thanks.

I'd be delight to.

My requirements are: 1 cup of fat-free milk, free, and free pizza.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Perl-Python-a-Day: split a file full path

2005-10-17 Thread Xah Lee
Xah Lee wrote:
> > In Perl, spliting a full path into parts is done like this:

Dr.Ruud wrote:
> And then follows Perl-code that only works with an optional .html
> "extension",

Thanks for the note. I've corrected it here:
http://xahlee.org/perl-python/split_fullpath.html

namely:
Note: the second argument to fileparse() is a list of regex. In
particular, you need to escape the dot. Normally, one gives it a value
such as ('\.html', '\.HTML', '\.jpg', '\.JPG'). Yes, it is case
sensitive. If you want to match any extension (that is, the string
after the last dot), use ('\.[^.]+$').

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

write a loopin one line; process file paths

2005-10-18 Thread 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
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: write a loopin one line; process file paths

2005-10-18 Thread Xah Lee

Xah Lee wrote:
> 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
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Python Doc Error: os.makedirs

2005-10-18 Thread Xah Lee
Python doc problem:

http://python.org/doc/2.4.2/lib/os-file-dir.html

makedirs(   path[, mode])
Recursive directory creation function. Like mkdir(), but makes all
intermediate-level directories needed to contain the leaf directory.
Throws an error exception if the leaf directory already exists or
cannot be created. The default mode is 0777 (octal). This function does
not properly handle UNC paths (only relevant on Windows systems;
Universal Naming Convention paths are those that use the `\\host\path'
syntax). New in version 1.5.2.


The “Throws an error exception” should be “Throws an OSError
exception”.



i think the function shouldn't complain if dir already exists. How is a
programer to distinguish if the dir already exists, or if there's a
problem creating the dir?


 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: write a loopin one line; process file paths

2005-10-18 Thread Xah Lee

Peter Hansen wrote:
> Xah Lee wrote:
> > If you think i have a point, ...
>
> 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
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Jargons of Info Tech industry

2005-10-18 Thread Xah Lee
> Xah Lee, on Aug 22, 2:43 pm wrote:
> Unix, RFC, and Line Truncation
> http://xahlee.org/UnixResource_dir/writ/truncate_line.html

Steve wrote:
> I've seen this argument before.  There's at least one VERY good reason
> to hard-code linebreaks in text:  to preserve a covert channel.  It's
> really easy to structure plain text in such a way to include super
> sekret messages that can only be properly decoded when the original
> formatting of the text is preserved.  Assuming that all of us are
> agreed that plain text is the correct lowest-common denominator in
> email and Usenet communications, it makes sense to allow for additional
> personal expression by way of enabling users to encode additional
> information in the formatting of their messages.

Rethink what you are saying. You'll see that what you propose as
reasons for one, is actually for the other.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: write a loopin one line; process file paths

2005-10-18 Thread 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
 [EMAIL PROTECTED]
∑ http://xahlee.org/

[EMAIL PROTECTED] wrote:
> 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).
>

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: write a loopin one line; process file paths

2005-10-19 Thread Xah Lee

[EMAIL PROTECTED] wrote:
> it will be added in 2.5 I beleve. At the moment, you can:
>
> predicate and  or 

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
 [EMAIL PROTECTED]
∑ http://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.
>

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: write a loopin one line; process file paths

2005-10-19 Thread 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
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

Perl-Python-a-Day: one-liner loop Functional Style

2005-10-20 Thread Xah Lee
One-Liner Loop in Functional Style

Xah Lee, 200510

Today we show a example of a loop done as a one-liner of Functional
Programing style.

Suppose you have a list of file full paths of images:

/Users/t/t4/oh/DSCN2059m-s.jpg
/Users/t/t4/oh/DSCN2062m-s.jpg
/Users/t/t4/oh/DSCN2097m-s.jpg
/Users/t/t4/oh/DSCN2099m-s.jpg
/Users/t/Icons_dir/icon_sum.gif

For those ending with -s (indicate a smaller version), you want to
change it to the full version without the -s, if it exists. Here's the
code:

# -*- 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'.
imgPaths2=[]
for myPath in imgPaths:
p=myPath
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)
if(fileBaseName[-2:] == '-s'):
p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
if os.path.exists(p2): p=p2
imgPaths2.append(p)

print imgPaths2

But how do you do it in a functional programing style? Namely,
something of the from imgPath2=f(imgPath), where the f is some
function. Normally, the f would be a pure function construct made up on
the spot, that is, lambda. But Python's lambda is limited to only a
expression as its body. Nevertheless, one can achieve a functional
style with some workaround, using map twice. Here's the code:

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))

The first map:

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

generate a list of pairs (x, y), where x is the same element in
imgPaths, and y is one without the -s. Then, a second map:

map( lambda x: os.path.exists(x[1]) and x[1] or x[0], newList,
imgPaths))

checks if the path y exists, if so, use that, else, use the x part. The
function body is essentially of a conditional EXPRESSION, of the from
(test, if true return result1, else return result2). This form of a
test that returns a expression is very important in functional
programing. Because, in functional programing a common pattern is to
sequence functions and passing values. One cannot stop in the middle
and use a block structure. Here's how this form's syntax in several
languages:

test? trueExpression: falseExpression  (C, Perl)
If[test, trueExpression, falseExpression]  (Mathematica)
(test trueExpression falseExpression)  (LISP)

In Python, there's no such form for this, but a semantically equivalent
workaround is to sequence boolean expressions as used in our example.
Namely:

test and trueExpression or falseExpression  (Python)

This works because it exploits a particular behavior of how Python
treats boolean sequences. In Python, “x and y” returns y if x is
true. And, “x or y” returns x if x is true. This behavior is
compatible with the mathematical sense of booleans. For example,
mathematically “x and y” should be true if both are true, yet in
Python if x is true then y is returned, and if y is true then this is
compatible with the math sense, but if y is false then the whole result
is also false, so it also satisfies the math sense. Similar is the case
of “x or y”. The point here is that one of the element is returned,
instead of a real True or False value. Therefore, in a twisted way this
can be used for the If[test, trueExpression, falseExpression] form.
Example.

result1= True and 4 or 5# returns 4
result2= False and 4 or 5   # returns 5

print result1
print result2

Such language behavior is a result of the laziness of the compiler
implementation, particular seen in unix shells (&& and ||). The problem
with this design is that codes relying on the the returned element of a
boolean sequence does not clearly indicate the programer's intention.
It works by language pecularities, instead of expressed program logic.

For the official doc on evaluating booleans, see:
http://python.org/doc/2.4.2/lib/boolean.html

Here's the Perl code of the same loop block:

# perl
use File::Basename;

@imgPaths=(
'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg',
'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg',
'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg',
'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg',
'/Users/t/web/Icons_dir/icon_sum.gif');

# change the image path to the full si

Re: Python Doc Error: os.makedirs

2005-10-20 Thread Xah Lee

Thomas Bellman wrote:
>try:
>   os.makedirs("/tmp/trh/spam/norwegian/blue/parrot/cheese")
>except os.error, e:
>   if e.errno != errno.EEXIST:
>   raise

This is what i want. Thanks.

(the doc needs quite some improvement...)

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

a Haskell a Day

2005-10-26 Thread Xah Lee
A Haskell A Day: Manifesto

This is my learning notes on Haskell. I call it a-Haskell-a-day. I've
been programing since 1992, and am a top expert at the Mathematica↗
language. I've long wanted to learn Haskell. It is my habit to write
down what i'm learning. I will send out a small tip of what i have
learned every day. If you are experienced industrial programer,
especially if you also have experience in a functional language, this
group may be useful to you. I hope you will join me in learning
Haskell.

Note: this is not a tutorial. The daily notes serve best as a
motivation for your own learning processes. Links to appropriate
sections in other reliable texts will usually be included daily.

Daily tips will be gradually collected and organized at:
http://xahlee.org/haskell/haskell.html My goal is to eventually form a
example-based tutorial.

I'm running a mailing list at Yahoo.com. To see the mailing list, go
to: http://groups.yahoo.com/group/haskell-a-day

To subscribe, send a email to: haskell-a-day-subscribe @
yahoogroups.com

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

tool for syntax coloring in html

2005-10-26 Thread Xah Lee
in some online documentations, for examples:

http://perldoc.perl.org/perlref.html
http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-17.html
http://www.haskell.org/hawiki/HaskellDemo

the codes are syntax colored.

Is there a tool that produce codes in html with syntax coloring?

Thanks.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

-- 
http://mail.python.org/mailman/listinfo/python-list

  1   2   3   4   5   6   >