Re: Python doc problem example: gzip module (reprise)

2005-11-08 Thread Xah Lee
Newsgroups: comp.lang.perl.misc From: "Veli-Pekka Tätilä" Date: Sat, 5 Nov 2005 17:25:35 +0200 Subject: Re: Python doc problem example: gzip module (reprise) Xah Lee wrote: > Today i need to use Python to compress/decompress gzip files. > However, scanning the doc after 20 se

Xah's Edu Corner: Sophie's World

2005-11-24 Thread Xah Lee
Recommended: Sophie's World, by Jostein Gaarder, 1995. http://en.wikipedia.org/wiki/Sophie%27s_World http://www.amazon.com/gp/product/0425152251/ Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list

Xah's Edu Corner: Examples of Quality Technical Writing

2005-12-05 Thread Xah Lee
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality of its documentation, it is a impeccability. it has or possesses properties of: • To the point and use

Post-modernism, Academia, and the Tech Geeking fuckheads

2005-12-08 Thread Xah Lee
Post-modernism, Academia, and the Tech Geeking fuckheads • the Sokal Affair http://en.wikipedia.org/wiki/Sokal_Affair • SCIGen and World Multi-Conference on Systemics, Cybernetics and Informatics   http://pdos.csail.mit.edu/scigen/ • What are OOP's Jargons and Complexities, Xah Lee

PHP = Perl Improved

2005-12-09 Thread Xah Lee
recently i got a project that involves the use of php. In 2 days, i read almost the entirety of the php doc. Finding it a breeze because it is roughly based on Perl, of which i have mastery. i felt a sensation of neatness, as if php = Perl Improved, for a dedicated job of server-side scripting. Ev

20050111: list basics

2005-01-11 Thread Xah Lee
# in Python, list can be done this way: a = [0, 1, 2, 'more',4,5,6] print a # list can be joined with plus sign b = a + [4,5,6] print b # list can be extracted by appending a square bracket with index # negative index counts from right. print b[2] print b[-2] # sublist extraction print 'element

[perl-python] 20050112 while statement

2005-01-13 Thread Xah Lee
# here's a while statement in python. a,b = 0,1 while b < 20: print b a,b = b,a+b --- # here's the same code in perl ($a,$b)=(0,1); while ($b<20) { print $b, "\n"; ($a,$b)= ($b, $a+$b); } Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/m

how to stop google from messing Python code

2005-01-13 Thread Xah Lee
i'm using groups-beta.google.com to post python code. Is there a way to stop google from messing with my format? it seems to have eaten the spaces in my python code. thanks. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

Re: how to stop google from messing Python code

2005-01-13 Thread Xah Lee
gmane is great! its renaming of newsgroups is quite a headache. i found that comp.lang.python corresponds to gmane.comp.python.general. do you know which one corresponds to comp.lang.perl.misc? there's no .misc or .general... -- i thought there a strick like preceding a line by -- or something tha

[perl-python] 20050113 looking up syntax

2005-01-14 Thread Xah Lee
while programing in Python, one can lookup syntax or info for keywords or modules within Python. In the command line, type python to get into the python interactive program. then type help() >From there one can type any keyword or module name to find out the syntax or info. Everything is self-co

[perl-python] 20050114 if statement

2005-01-14 Thread Xah Lee
. # here's an example of if statement in python. . . x=-1 . if x<0: . print 'neg' . elif x==0: . print 'zero' . elif x==1: . print 'one' . else: . print 'other' . . # the elif can be omitted. . -- . # here's an example of if statement in perl . . $x=3

[perl-python] 20050115, for statement

2005-01-15 Thread Xah Lee
© # this is an example of for statement © # the % symbol calculates the remainder © # of division. © # the range(m,n) function © # gives a list from m to n-1. © © a = range(1,51) © for x in a: © if x % 2 == 0: © print x, 'even' © © # note that in this example, for goes over a list. © # e

python mode indentation problem

2005-01-15 Thread Xah Lee
does anyone know why the Python mode in emacs uses spaces for first level indentation but one tab for second level? i'm using emacs 21.3.50.1. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

Re: python mode indentation problem

2005-01-15 Thread Xah Lee
© ok, here's the ordeal. © © for i in range(5): © print i © for i in range(2): © print i, 'tt' © for i in [3]: © print i © for i in [32]: © print i © © # 1 level, 4 space © # 2 level, 1 tab © # 3 level, 1 tab, 4 spaces © # 4 level, 2 tabs. © © w

[perl-python] 20050116 defining a function

2005-01-16 Thread Xah Lee
© # the following is a example of defining © # a function in Python. © © def fib(n): © """This prints n terms of a sequence © where each term is the sum of previous two, © starting with terms 1 and 1.""" © result=[];a=1;b=1 © for i in range(n): © result.append(b) ©

[perl-python] 20050117, filter, map

2005-01-16 Thread Xah Lee
© # -*- coding: utf-8 -*- © # Python © © # the "filter" function can be used to © # reduce a list such that unwanted © # elements are removed. © # example: © © def even(n): return n % 2 == 0 © print filter( even, range(11)) © © © # the "map" function applies a function © # to all elements of a list

Re: [perl-python] 20050116 defining a function

2005-01-17 Thread Xah Lee
errata: * the variables in the perl section should be declared inside the subroutine. * the @_[0] should've been $_[0] thanks for Dave Cross for pointing them out. * the Mathematica Apply should be Select... Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.

Re: [perl-python] 20050117, filter, map

2005-01-17 Thread Xah Lee
erratum: the Mathematica Apply should've been Select. ... Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

Re: [perl-python] 20050117, filter, map

2005-01-17 Thread Xah Lee
erratum: the Mathematica Apply should've been Select. ... Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

[perl-python] 20050118 keyed list

2005-01-17 Thread Xah Lee
© # -*- coding: utf-8 -*- © © # in Python, there's a special type of © # data structure called keyed list. it © # is a unordered list of pairs, each © # consists of a key and a value. It is © # also known as dictionary. © © # define a keyed list © aa = {'john':3, 'mary':4, 'jane':5, 'vicky':7} © p

[perl-python] 20050119 writing modules

2005-01-18 Thread Xah Lee
© # -*- coding: utf-8 -*- © # Python © © # one can write functions, © # save it in a file © # and later on load the file © # and use these functions. © © # For example, save the following line in © # a file and name it module1.py © # def f1(n): returns range(n) © © # to load the file, use import ©

iteritems() and enumerate()

2005-01-19 Thread Xah Lee
Python has iteritems() and enumerate() to be used in for loops. can anyone tell me what these are by themselves, if anything? are they just for idiom? thanks. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

[perl-python] 20050120 find functions in modules

2005-01-19 Thread Xah Lee
© # -*- coding: utf-8 -*- © # Python © © # once a module is loaded © # import mymodule © # one can find all the names it © # export with dir() © © import sys © print dir(sys) © © # without argument it gives the names © # you've defined © print dir() © © # to find a list of built-in names © # import

how to write a tutorial

2005-01-21 Thread Xah Lee
i've started to read python tutorial recently. http://python.org/doc/2.3.4/tut/tut.html Here are some quick critique: quick example: If the input string is too long, they don't truncate it, but return it unchanged; this will mess up your column lay-out but that's usually better than the alternati

[perl-python] 20050121 file reading & writing

2005-01-22 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be # 'w' for write (overwrite exsiting file) # 'a' for append (ditto) # 'r' or read only # to actually print to file

Re: how to write a tutorial

2005-01-23 Thread Xah Lee
adding to my previosu comment... In the Python tutorial: http://python.org/doc/2.3.4/tut/node11.html the beginning two paragraphs should be deleted. Nobody gives a shit except a few smug academicians where the author wrote it for pleasing himself. For 99% of readers, it is incomprehensible and irr

[perl-python] 20050124 classes & objects

2005-01-24 Thread Xah Lee
© # -*- coding: utf-8 -*- © # Python © © # in Python, one can define a boxed set © # of data and functions, which are © # traditionally known as "class". © © # in the following, we define a set of data © # and functions as a class, and name it xxx © class xxx: © "a class extempore! (^_^)" ©

[perl-python] 20050125 standard modules

2005-01-25 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # some venture into standard modules import os # print all names exported by the module print dir(os) # print the module's online manual print help(os) # the above is also available in # interactive mode in Python terminal # example of using a function print 'c

Re: how to write a tutorial

2005-01-25 Thread Xah Lee
in my previous two messages, i've criticized the inanity of vast majority of language documentations and tutorials in the industry. I've used the Python tutorial's chapter on class as an example. I've indicated that proper tutorial should be simple, covering just common cases, be self-contained, an

[perl-python] 20050126 find replace strings in file

2005-01-26 Thread Xah Lee
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv[0] © else: © stext = sys.argv[1] © rtext = sys.argv[2] © input = open(sys.argv[3]) © output = open(sys.argv[4],'w

how to comment out a block of code

2005-01-26 Thread Xah Lee
is there a syntax to comment out a block of code? i.e. like html's or perhaps put a marker so that all lines from there on are ignored? thanks. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

[perl-python] 20050127 traverse a dir

2005-01-27 Thread Xah Lee
# -*- coding: utf-8 -*- # Python suppose you want to walk into a directory, say, to apply a string replacement to all html files. The os.path.walk() rises for the occasion. © import os © mydir= '/Users/t/Documents/unix_cilre/python' © def myfun(s1, s2, s3): © print s2 # current dir © pr

[perl-python] daily tip website

2005-01-28 Thread Xah Lee
for those interested, i've created a webpage for these perl-python daily tips. The url is: http://xahlee.org/perl-python/python.html Thanks to those who have made useful comments. They will be assimilated. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html > Hey all, I have seen no

what's OOP's jargons and complexities?

2005-01-28 Thread Xah Lee
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon to define functions inside a function. For example: subroutine f (x1, x2, ...) { variables... subroutine

[perl-python] sending email

2005-01-29 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # Suppose you want to spam your friend, and you have lots of # friends. The solution is to write a program to do it. After a gander # at python docs, one easily found the module for the job. # see http://python.org/doc/2.3.4/lib/SMTP-example.html # the code is a b

Q: quoting string without escapes

2005-01-31 Thread Xah Lee
in Python, is there a way to quote a string as to avoid escaping ' or " inside the string? i.e. like Perl's q or qq. thanks. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

[perl-python] find & replace strings for all files in a dir

2005-01-31 Thread Xah Lee
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web' © ©findStr='' ©repStr='' © ©def replaceStringInFile(findStr,repStr,filePath): ©"replaces all findStr by repStr in file file

[perl-python] string pattern matching

2005-02-01 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images files from gif # format to png format. Now you need to change the # html code to use the .png files. So, ess

Re: how to write a tutorial

2005-02-02 Thread Xah Lee
in the doc for re module http://python.org/doc/lib/module-re.html 4.2.2 on Matching vs Searching http://python.org/doc/lib/matching-searching.html Its mentioning of Perl is irrelevant, since the majority reading that page will not have expertise with Perl regex. The whole section should be delete

Re: how to write a tutorial

2005-02-02 Thread Xah Lee
i've noticed that in Python official doc http://python.org/doc/lib/module-re.html and also How-To doc http://www.amk.ca/python/howto/regex/ both mentions the book "Mastering Regular Expressions" by Jeffrey Friedl. I suggest it be dropped in both places. The mentioning of this book in the Perl/Pyt

[perl-python] get web page programatically

2005-02-04 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # suppose you want to fetch a webpage. from urllib import urlopen print urlopen('http://xahlee.org/Periodic_dosage_dir/_p2/russell-lecture.html').read() # note the line # from import # it reads the library and import the function name # to see available function

Re: functions with unlimeted variable arguments...

2005-06-19 Thread Xah Lee
oops... it is in the tutorial... sorry. though, where would one find it in the python reference? i.e. the function def with variable/default parameters. This is not a rhetorical question, but where would one start to look for it in the python ref? a language is used by programers. Subroutine def

references/addrresses in imperative languages

2005-06-19 Thread Xah Lee
in coding Python yesterday, i was quite stung by the fact that lists appened to another list goes by as some so-called "reference". e.g. t=range(5) n=range(3) n[0]='m' t.append(n) n[0]='h' t.append(n) print t in the following code, after some 1 hour, finally i found the solution of h[:]. (and th

Re: functions with unlimited variable arguments...

2005-06-20 Thread Xah Lee
Dear Chinook Lee, Thank you very much. That seems a godsend. I'd like to also thank its author Richard Gruet. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Chinook wrote: > ... > I don't get to the reference docs much. Mostly I use the quick reference > guide and it's noted there in an easy to f

Re: references/addrresses in imperative languages

2005-06-20 Thread Xah Lee
Dear Andrea Griffini, Thanks for explaning this tricky underneath stuff. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Andrea Griffini wrote: > On Sun, 19 Jun 2005 22:25:13 -0500, Terry Hancock > <[EMAIL PROTECTED]> wrote: > > >> PS is there any difference between > >> t=t+[li] > >> t.append(li)

Re: tree functions daily exercise: Table

2005-06-21 Thread Xah Lee
here's the Python spec for the Table function: '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns [f(3),f(5),f(7),f(9)] Table(f,[iStart,iEnd,iStep], [jStart,jEnd,jStep], ...) returns a nested list of f(i,j,...)

eval() in python

2005-06-21 Thread Xah Lee
is it possible to eval a string like the following? m=''' i0=[0,1] i1=[2,3] i2=[4,'a'] h0=[] for j0 in i0: h1=[] for j1 in i1: h2=[] for j2 in i2: h2.append(f(j0,j1,j2)) h1.append( h2[:] ) h0.append( h1[:] ) return h0''' perhaps i'm tired, but why can't i run: t='m=3' pr

Re: tree functions daily exercise: Table

2005-06-21 Thread Xah Lee
the example in the spec of previous post is wrong. Here's corrected version: here's the Python spec for the Table function: '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns [f(3),f(5),f(7),f(9)] Table(f,[iStart,

Re: tree functions daily exercise: Table

2005-06-21 Thread Xah Lee
ge, so they share syntax form. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Duncan Booth wrote: > Xah Lee wrote: > > > '''Table(f,[iStart,iEnd,iStep]) returns a list of f applied to the > > range range(iStart,iEnd,iStep). Example: Table(f,[3,10,2]) returns &g

Re: tree functions daily exercise: Table

2005-06-21 Thread Xah Lee
der if i can even do it in a few days. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ David Van Horn wrote: > Xah Lee wrote: > > here's the Python spec for the Table function: > ... > > References: > > > > • for a context of this message, see: http://xahlee.org/tre

turn text lines into a list

2005-06-27 Thread Xah Lee
i have a large number of lines i want to turn into a list. In perl, i can do @corenames=qw( rb_basic_islamic sq1_pentagonTile sq_arc501Tile sq_arc503Tile ); use Data::Dumper; print Dumper([EMAIL PROTECTED]); -- is there some shortcut to turn lines into list in Python? Xah [EMAIL PROTE

What is Expresiveness in a Computer Language?

2005-07-10 Thread Xah Lee
What is Expresiveness in a Computer Language 20050207, Xah Lee. In languages human or computer, there's a notion of expressiveness. English for example, is very expressive in manifestation, witness all the poetry and implications and allusions and connotations and dictions. There are a m

Re: What is Expresiveness in a Computer Language?

2005-07-12 Thread Xah Lee
Most participants in the computering industry should benefit in reading this essay: George Orwell's “Politics and the English Language”, 1946. Annotated: http://xahlee.org/p/george_orwell_english.html Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/pyth

Xah's edu corner: on Microsoft hatred

2005-07-18 Thread Xah Lee
oriented communist/socialist nations or sovereignly ruled kingdoms & queendoms. this post is archived at: http://xahlee.org/UnixResource_dir/writ/mshatred155.html © copyright 2003 by Xah Lee. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list

Jargons of Info Tech industry

2005-08-11 Thread Xah Lee
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like extraneous jargons, such as in the Unix & Perl community. Unlike mathematicians, where in mathematics t

Re: Jargons of Info Tech industry

2005-08-22 Thread Xah Lee
Unix, RFC, and Line Truncation [Note: unix tradition requires that a return be inserted at every 70 characters in email messages or so so that each line are less than 80 characters. Unixers made this as a requirement into an RFC document.] Xah Lee, 20020511 This truncation of lines business is

OpenSource documentation problems

2005-08-27 Thread Xah Lee
previously i've made serious criticisms on Python's documentations problems. (see http://xahlee.org/perl-python/re-write_notes.html ) I have indicated that a exemplary documentation is Wolfram Research Incorporated's Mathematica language. (available online at http://documents.wolfram.com/mathemati

Python doc problems example: gzip module

2005-08-31 Thread Xah Lee
today i need to use Python to decompress gzip files. since i'm familiar with Python doc and have 10 years of computing experience with 4 years in unix admin and perl, i have quickly located the official doc: http://python.org/doc/2.4.1/lib/module-gzip.html but after a minute of scanning, please

Re: Python doc problems example: gzip module

2005-08-31 Thread Xah Lee
Today i need to use Python to compress/decompress gzip files. I quickly found the official doc: http://python.org/doc/2.4.1/lib/module-gzip.html I'd imagine it being a function something like GzipFile(filePath, comprress/decompress, outputPath) however, scanning the doc after 20 seconds there's

change date format

2005-08-31 Thread Xah Lee
Apache by default uses the following format for date: 30/Aug/2005 is there a module that turn this directly into mmdd? Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list

Re: OpenSource documentation problems

2005-09-01 Thread Xah Lee
By the way, i have sent my criticisms to the proper python doc maintainer or mailing list several months ago. - i'm very sorry to say, that the Python doc is one of the worst possible in the industry. I'm very sick of Perl and its intentional obfuscation and juvenile drivel style of i

Re: OpenSource documentation problems

2005-09-01 Thread Xah Lee
On Python's Documentation Xah Lee, 20050831 I'm very sorry to say, that the Python doc is one of the worst possible in the industry. I'm very sick of Perl and its intentional obfuscation and juvenile drivel style of its docs. I always wanted to learn Python as a replacement of

Re: Python doc problems example: gzip module

2005-09-01 Thread Xah Lee
nothing personal my friend. But just in case you are interested about getting it: the question here is about quality of documentation, not about whether you got it. http://xahlee.org/UnixResource_dir/writ/python_doc.html Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Sybren Stuvel wrote: >

python logo

2005-09-02 Thread Xah Lee
i noticed that Python uses various logos: http://python.org/pics/pythonHi.gif http://python.org/pics/PyBanner038.gif http://python.org/pics/PyBanner037.gif http://python.org/pics/PythonPoweredSmall.gif http://wiki.python.org/pics/PyBanner057.gif is this some decision that python should use vario

Re: python logo

2005-09-03 Thread Xah Lee
identity and as well as advertisement. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Steve Holden wrote: > Xah Lee wrote: > > i noticed that Python uses various logos: > > > > http://python.org/pics/pythonHi.gif > > http://python.org/pics/PyBanner038.gif > > http://

os.system(r"ls") prints to screen??

2005-09-03 Thread Xah Lee
does anyone know why the folllowing prints to the screen? # python import os os.system(r"ls") Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list

Re: os.system(r"ls") prints to screen??

2005-09-04 Thread Xah Lee
Steve Holden wrote: > This is all pretty basic stuff. Perhaps you should stop your verbal > assault on the computer science community and start to learn the > principles of what you are doing. is this a supressed behavior that a human animal can finally instinctively and justifiably release at ano

Re: os.system(r"ls") prints to screen??

2005-09-04 Thread Xah Lee
do you know what the Muses do when a mortal challenged them? And, please tell me exactly what capacity you hold under the official Python organization so that i can calculate to what degree i can kiss your ass or feign mum of your ignorance. Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Steve Hol

Python Doc Problem Example: os.system

2005-09-04 Thread Xah Lee
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the the function due to its well-named-ness: import os os.system("ls") however, according

Re: os.system(r"ls") prints to screen??

2005-09-06 Thread Xah Lee
Xah Lee wrote: > does anyone know why the folllowing prints to the screen? > # python > import os > os.system(r"ls") Steve Holden wrote: > It only prints to the screen when standard output of the invoking > process is the screen. The sub-process forked by os.system

determine if os.system() is done

2005-09-07 Thread Xah Lee
suppose i'm calling two system processes, one to unzip, and one to “tail” to get the last line. How can i determine when the first process is done? Example: subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]); last_line=subprocess.Popen([r"/usr/bin/tail","-n 1","access_log.4"], stdout=sub

Re: determine if os.system() is done

2005-09-07 Thread Xah Lee
Thanks all. I found the answer, rather easily. To make a system call and wait for it, do: subprocess.Popen([r"/sw/bin/gzip","-d","access_log.4.gz"]).wait(); -- this post is archived at: http://xahlee.org/perl-python/system_calls.html Xah [EMAIL PROTECTED]

reading the last line of a file

2005-09-08 Thread Xah Lee
Martin Franklin wrote: > import gzip > log_file = gzip.open("access_log.4.gz") > last_line = log_file.readlines()[-1] > log_file.close() does the log_file.readlines()[-1] actually read all the lines first? i switched to system call with tail because originally i was using a pure Python solution

Re: reading the last line of a file

2005-09-08 Thread Xah Lee
isn't there a way to implement tail in python with the same class of performance? how's tail implemented? Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Fredrik Lundh wrote: > Fredrik Lundh wrote: > > > zcat|tail is a LOT faster. > > and here's the "right way" to use that: > > from subprocess

regex re.search and re.match

2005-09-11 Thread Xah Lee
in the regex module re: Note: Match() is not exactly equivalent to Search() with "^". For example: re.search(r'^B', 'A\nB',re.M) # succeeds re.match(r'B', 'A\nB',re.M) # fails if without the re.M, would re.search and re.match be equivalent? i wish to spruce up the rewritten re

Python Doc Problem Example: os.path.split

2005-09-18 Thread Xah Lee
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html -- split( path) Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash

Re: Python Doc Problem Example: os.path.split

2005-09-19 Thread Xah Lee
left out. --- This post is archived at: http://xahlee.org/perl-python/python_doc_os_path_split.html Xah [EMAIL PROTECTED] ∑ http://xahlee.org/ Xah Lee wrote: > Python Doc Problem Example > > Quote from: > http://docs.python.org/lib/module-os.path.html > -- > s

Perl's documentation come of age

2005-09-21 Thread Xah Lee
Perl's documentation has come of age: http://perldoc.perl.org/ Python morons really need to learn: • ample example codes. • example codes are linked to the appropriate doc location for each code word in the example. • written in a task-oriented style, or manifest-functionality style. That is, i

[perl-python] text pattern matching, and expressiveness

2005-02-07 Thread Xah Lee
20050207 text pattern matching # -*- coding: utf-8 -*- # Python # suppose you want to replace all strings of the form # # to # # in your html files. # you can use the "re" module. import re text = r''' blab blab look at this pict and this one , both are beautiful, but also look: , and sequ

python code with indention

2005-02-07 Thread Xah Lee
is it possible to write python code without any indentation? Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

Re: python code with indention

2005-02-09 Thread Xah Lee
i thought it is trivial for the Python parser to spit out a version with matching brackets. Similarly, perhaps some opensourcing student has modified a parser to read in a matching brackets delimited version of Python. Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://ma

[perl-python] combinatorics fun

2005-02-10 Thread Xah Lee
a year ago i wrote this perl program as part of a larger program. as a exercise of fun, let's do a python version. I'll post my version later today. =pod combo(n) returns a collection with elements of pairs that is all possible combinations of 2 things from n. For example, combo(4) returns {'3,4

Re: [perl-python] combinatorics fun

2005-02-10 Thread Xah Lee
David Eppstein's code is very nice. Here's the python version of the perl code: ©# -*- coding: utf-8 -*- ©# Python © ©def combo (n): ©'''returns all possible (unordered) pairs out of n numbers 1 to n. © ©Returns a dictionary. The keys are of the form "n,m", ©and their values are tuple

[perl-python] 20050211 generating expression by nested loops

2005-02-12 Thread Xah Lee
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return dict([('%d,%d'%(i+1,j+1),(i+1,j+1)) for j in range(n) for i in range(j)]) print combo2(5) # this constr

[perl-python] problem: reducing comparison

2005-02-14 Thread Xah Lee
here's a interesting real-world algoritm to have fun with. attached below is the Perl documentation that i wrote for a function called "reduce", which is really the heart of a larger software. The implementation is really simple, but the key is to understand what the function should be. I'll post

Re: [perl-python] problem: reducing comparison

2005-02-15 Thread Xah Lee
here are the answers: Perl code: sub reduce ($$) { my %hh= %{$_[0]}; # e.g. {'1,2'=>[1,2],'5,6'=>[5,6],...} my ($j1,$j2)=($_[1]->[0],$_[1]->[1]); # e.g. [3,4] delete $hh{"$j1,$j2"}; foreach my $k (keys %hh) { $k=~m/^(\d+),(\d+)$/; my ($k1,$k2)=($1,$2); if ($k1==$j1) {

Re: problem: reducing comparison

2005-02-15 Thread Xah Lee
my Python coding is not experienced. In this case, is ps.pop("%d,%d"%(j[1],k[1]),0) out of ordinary? if i have long paragraphs of documentation for a function, do i still just attach it below the fun def? Xah Xah Lee wrote: > here are the answers: > > ©Python code. > ©

Re: problem: reducing comparison

2005-02-16 Thread Xah Lee
ps.pop("%d,%d"%(j[1],k[0]),0) ©return ps © ©is reduce2 more efficient? It works entirely differently. I'll have to think about it... besides algorithmic... onto the minute academic diddling, i wonder if it is faster to delete entries in dict or add entries... Xah [EMAIL

Re: [perl-python] problem: reducing comparison (correction)

2005-02-16 Thread Xah Lee
Xah Lee wrote: > In imperative languages such as Perl and Python and Java, in general it > is not safe to delete elements when looping thru a list-like entity. > (it screws up the iteration) One must make a copy first, and work with > the copy. Correction: When looping thru a list

Re: [perl-python] problem: reducing comparison (erratum)

2005-02-16 Thread Xah Lee
Xah Lee wrote: > In imperative languages such as Perl and Python > and Java, in general it is not safe to delete > elements when looping thru a list-like entity. > (it screws up the iteration) One must make a > copy first, and work with the copy. Correction: When looping thru a l

Re: [perl-python] problem: reducing comparison (erratum)

2005-02-16 Thread Xah Lee
Xah Lee wrote: > In imperative languages such as Perl and Python > and Java, in general it is not safe to delete > elements when looping thru a list-like entity. > (it screws up the iteration) One must make a > copy first, and work with the copy. Correction: When looping thru a l

[perl-python] exercise: partition a list by equivalence

2005-02-17 Thread Xah Lee
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes. For

[perl-python] exercise: partition a list by equivalence

2005-02-19 Thread Xah Lee
here's the answer to the partition by equivalence exercise. --- # Perl code sub merge($) { my @pairings = @{$_[0]}; my @interm; # array of hashs # chop the first value of @pairings into @interm $interm[0]={$pairings[0][0]=>'x'}; ${interm[0]}{$pairings[0]

Re: exercise: partition a list by equivalence

2005-02-19 Thread Xah Lee
The GOTO statement from Perl has been messed up. This block: ©for group in interm: ©for newcoup in fin: ©for k in group.keys(): ©if newcoup.has_key(k): ©for kk in group.keys(): newcoup[kk]='x'; ©break ©break ©

Re: exercise: partition a list by equivalence

2005-02-19 Thread Xah Lee
an interesting problem so developed now is to write a function that generate test cases for the purpose of testing performance. (just for fun) the design of this function could be interesting. We want to be able to give parameters in this function so as to spit out all possible screw test cases. F

[perl-python] exercise: partition a list by equivalence

2005-02-20 Thread Xah Lee
when i try to run the following program, Python complains about some global name frozenset is not defined. Is set some new facility in Python 2.4? ©# from Reinhold Birkenfeld ©def merge(pairings): ©sets = {} ©for x1, x2 in pairings: ©newset = (sets.get(x1, frozenset([x1])) ©

Re: exercise: partition a list by equivalence

2005-02-23 Thread Xah Lee
I started to collect i believe the 4 or so solutions by different people... but seems it's gonna take some an hour or more... So far the only other one i've run and find alright is Reinhold Birkenfeld's original. Others worth noting i'm aware of is David Epsteinn, improved versions from Reinhold Bi

[perl-python] generic equivalence partition

2005-02-24 Thread Xah Lee
another functional exercise with lists. Here's the perl documentation. I'll post a perl and the translated python version in 48 hours. =pod parti(aList, equalFunc) given a list aList of n elements, we want to return a list that is a range of numbers from 1 to n, partition by the predicate funct

Re: generic equivalence partition

2005-02-26 Thread Xah Lee
# the following solution is submitted by # Sean Gugler and David Eppstein independently # 20050224. @def parti(aList, equalFunc): @result = [] @for i in range(len(aList)): @for s in result: @if equalFunc( aList[i], aList[s[0]] ): @s.append(i) @

function expression with 2 arguments

2005-02-26 Thread Xah Lee
is there a way to write a expression of a function with more than 1 argument? e.g., i want a expression that's equivalent to def f(x,y) return x+y Xah [EMAIL PROTECTED] http://xahlee.org/PageTwo_dir/more.html -- http://mail.python.org/mailman/listinfo/python-list

<    1   2   3   4   5   6   >