no pass-values calling?

2008-01-15 Thread J. Peng
Hello,

I saw this statement in Core Python Programming book,

All arguments of function calls are made by reference, meaning that
any changes to these parameters within the function
affect the original objects in the calling function.


Does this mean there is not pass-values calling to a function in
python? only pass-reference calling? Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no pass-values calling?

2008-01-15 Thread J. Peng
On Jan 16, 2008 1:45 PM, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Wed, 16 Jan 2008 11:09:09 +0800, "J. Peng" <[EMAIL PROTECTED]>
>
> alist = []
> anint = 2
> astr = "Touch me"
>
> dummy(alist, anint, astr)
>
> "dummy" can only modify the contents of the first argument -- the
> integer and string can not be mutated.

Hi,

How to modify the array passed to the function? I tried something like this:

>>> a
[1, 2, 3]
>>> def mytest(x):
...   x=[4,5,6]
...
>>> mytest(a)
>>> a
[1, 2, 3]

As you see, a was not modified.
Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no pass-values calling?

2008-01-15 Thread J. Peng
On Jan 16, 2008 2:30 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Wed, 16 Jan 2008 13:59:03 +0800, J. Peng wrote:
>
> > Hi,
> >
> > How to modify the array passed to the function? I tried something like
> > this:
> >
> >>>> a
> > [1, 2, 3]
> >>>> def mytest(x):
> > ...   x=[4,5,6]
>
>
> This line does NOT modify the list [1, 2, 3]. What it does is create a
> new list, and assign it to the name "x". It doesn't change the existing
> list.
>

Sounds strange.
In perl we can modify the variable's value like this way:

$ perl -le '
> $x=123;
> sub test {
> $x=456;
> }
> test;
> print $x '
456
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: no pass-values calling?

2008-01-15 Thread J. Peng
On Jan 16, 2008 3:03 PM, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Wed, 16 Jan 2008 13:59:03 +0800, "J. Peng" <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
>
>
> > How to modify the array passed to the function? I tried something like this:
> >
> > >>> a
> > [1, 2, 3]
> > >>> def mytest(x):
> > ...   x=[4,5,6]
>
> x is unqualified (in my terms), so you have just disconnected it
> from the original argument and connected it to [4,5,6]
>

Ok, thanks.
But there is a following question,when we say,

>>> x=[1,2,3]

we create a list.then we say,

>>> x=[4,5,6]

we create a new list and assign it to x for future use.
How to destroy the before list [1,2,3]? does python destroy it automatically?
-- 
http://mail.python.org/mailman/listinfo/python-list


assigning values in python and perl

2008-01-16 Thread J. Peng
I just thought python's way of assigning value to a variable is really
different to other language like C,perl. :)

Below two ways (python and perl) are called "pass by reference", but
they get different results.
Yes I'm reading 'Core python programming', I know what happened, but
just a little confused about it.

$ cat t1.py
def test(x):
x = [4,5,6]

a=[1,2,3]
test(a)
print a

$ python t1.py
[1, 2, 3]

$ cat t1.pl
sub test {
my $ref = shift;
@$ref = (4,5,6);
}

my @a = (1,2,3);
test([EMAIL PROTECTED]);

print "@a";

$ perl t1.pl
4 5 6
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assigning values in python and perl

2008-01-16 Thread J. Peng
May I ask, python's pass-by-reference is passing the object's
reference to functions, but perl, or C's pass-by-reference is passing
the variable itself's reference to functions. So althought they're all
called pass-by-reference,but will get different results.Is it?

On Jan 17, 2008 11:34 AM, J. Peng <[EMAIL PROTECTED]> wrote:
> I just thought python's way of assigning value to a variable is really
> different to other language like C,perl. :)
>
> Below two ways (python and perl) are called "pass by reference", but
> they get different results.
> Yes I'm reading 'Core python programming', I know what happened, but
> just a little confused about it.
>
> $ cat t1.py
> def test(x):
> x = [4,5,6]
>
> a=[1,2,3]
> test(a)
> print a
>
> $ python t1.py
> [1, 2, 3]
>
> $ cat t1.pl
> sub test {
> my $ref = shift;
> @$ref = (4,5,6);
> }
>
> my @a = (1,2,3);
> test([EMAIL PROTECTED]);
>
> print "@a";
>
> $ perl t1.pl
> 4 5 6
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assigning values in python and perl

2008-01-16 Thread J. Peng
On Jan 17, 2008 2:03 PM, Christian Heimes <[EMAIL PROTECTED]> wrote:
> George Sakkis wrote:
> > Python's parameter passing is like passing a pointer in C/C++.
> [snip]
>
> It's not (I repeat NOT) like passing a pointer in C. Please read
> http://effbot.org/zone/call-by-object.htm
>

Yes I agree. Not the same at all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assigning values in python and perl

2008-01-16 Thread J. Peng
On Jan 17, 2008 1:54 PM, Mel <[EMAIL PROTECTED]> wrote:
>
> test(a) (along with def test(x)) takes the object named 'a' in the
> current namespace and binds it with the name 'x' in function test's
> local namespace.  So, inside test, the name 'x' starts by referring to
>the list that contains [1,2,3].  But the only use test makes of the
> name 'x' is to re-bind it to a new list [4,5,6].  Exiting test, the
> local namespace is thrown away.
>

Hi,

Yes I know it pretty well now, but thanks for the pointer.
What I asked is that, if we re-write that code with perl or C, we'll
get different results.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: assigning values in python and perl

2008-01-16 Thread J. Peng
On Jan 17, 2008 2:55 PM, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
>
> @$ref = (4, 5, 6) intentionally assigns to the same list pointed to by
> the reference.  That would be spelled as x[:] = [4, 5, 6] in Python.
> What Python does in your example is assign the same as Perl's $ref =
> [4, 5, 6].  So they're not so different after all.
>

Yup,you're so right.This test below in perl is the same as in python.
So at before I may got mistaken by myself.Thanks all.

$ cat t1.pl
sub test {
my $ref = shift;
$ref = [4,5,6];
}

my @a = (1,2,3);
test([EMAIL PROTECTED]);

print "@a";

$ perl t1.pl
1 2 3
-- 
http://mail.python.org/mailman/listinfo/python-list


array and list

2008-01-17 Thread J. Peng
what's the difference between an array and a list in python?
I see list has all features of array in C or perl.
so please tell me.thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


too long float

2008-01-17 Thread J. Peng
hello,

why this happened on my python?

>>> a=3.9
>>> a
3.8999

I wanted 3.9 but got 3.89
How to avoid it? thanks.

this is my python version:

>>> sys.version
'2.3.4 (#1, Feb  6 2006, 10:38:46) \n[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)]'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: too long float

2008-01-18 Thread J. Peng
thanks all!

On Jan 18, 2008 3:49 PM, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Fri, 18 Jan 2008 13:55:17 +0800, "J. Peng" <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
>
> >
> > why this happened on my python?
> >
> > >>> a=3.9
> > >>> a
> > 3.8999
> >
> > I wanted 3.9 but got 3.89
> > How to avoid it? thanks.
> >
> Avoid it? You don't... You alleviate the concern by understanding
> that floating point is only precise if the value is a fraction of 2: 1,
> 0.5, 0.25, 0.125...
>
> Computer Science recommends that one does NOT compare two floats for
> equality -- instead one should compare the absolute value of the
> difference of the two floats against some required epsilon (ie, how far
> apart two floats can be and still be considered equal...
> abs(f1 - f2) < 0.01
> for example)
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED]   [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array and list

2008-01-18 Thread J. Peng

> On Jan 18, 3:23 am, "J. Peng" <[EMAIL PROTECTED]> wrote:
>   
>> what's the difference between an array and a list in python?
>> I see list has all features of array in C or perl.
>> so please tell me.thanks.
>> 
>
> If you are new to Python, then where other languages may reach for an
> 'array', Python programs might organise data as lists. Lists are used
> much more than arrays in Python. you should find that is the case in
> tutorials/books too.
>
> http://wiki.python.org/moin/PerlPhrasebook?highlight=%28perl%29
>
>
> - Paddy.
>   
Hi,

 From Core Python Programming book (btw I like this book) I know the 
difference is that array can hold only one type (the C standard?), but 
list can hold every type of object. But hmm, perl's array also can hold 
every type of variables, why perl call it array and python call it list? 
Also, if I understand it correctly, python's tuple is called as 'list' 
in perl, so I'm somewhat confused about them.

 > P.S. if you know Perl then try:

Yes I know some Perl. I have registered module on CPAN.

thanks!

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


dynamic type variable

2008-01-20 Thread J. Peng
Python's variable is dynamic type,is it?
But why this can't work?

>>> 3 + 'a'
Traceback (most recent call last):
File "", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'


So I see the number 3 can't be converted to string type automacially.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: object scope

2008-01-20 Thread J. Peng
J. Peng 写道:
> Please see the code below,what's the scope for object "name"?
> I thought it should be located in the while block, but it seems not
> really,it can be accessed out of while (the db[name] statement).Thanks
> in advance.
> 
> 

sorry the before code seems be disordered,re-posted it.

db = {}
def newuser():
   prompt = 'login desired: '
   while 1:
 name = raw_input(prompt)
 if db.has_key(name):
   prompt = 'name taken, try another: '
   continue
 else:
   break
   pwd = raw_input('passwd: ')
   db[name] = pwd
-- 
http://mail.python.org/mailman/listinfo/python-list

object scope

2008-01-20 Thread J. Peng
Please see the code below,what's the scope for object "name"?
I thought it should be located in the while block, but it seems not
really,it can be accessed out of while (the db[name] statement).Thanks
in advance.


db = {}
def newuser():
prompt = 'login desired: '
while 1:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')
db[name] = pwd
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
J. Peng 写道:

>k = (i.split())[3]
>y = (i.split())[1]

btw, why can't I write the above two into one statement?

(k,y) = (i.split())[3,1]
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: object scope

2008-01-20 Thread J. Peng
Dennis Lee Bieber 写道:
>   The scope of "name" is the entire function; lacking a "global name"
> statement, AND being on the left side of an assignment, it is a function
> local name.

Thank you. Does python have so-called 'block scope' object?
or if you can,please show me the doc for python's object scope.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
I tried to write it below,it can work,:)

v= """preference 10 host mx1.domain.com
preference 30 host anotherhost.domain.com
preference 20 host mx2.domain.com"""

x=v.split("\n")

li =[]
for i in x:
   k = (i.split())[3]
   y = (i.split())[1]
   li.append((y,k))

li.sort()
print li


the output is:
[('10', 'mx1.domain.com'), ('20', 'mx2.domain.com'), ('30', 
'anotherhost.domain.com')]



Santiago Romero 写道:
>  Hi ...
> 
>  I have the following DNS MX records info:
> 
> domain.com
> preference 10 host mx1.domain.com
> preference 30 host anotherhost.domain.com
> preference 20 host mx2.domain.com
> 
>  I'm storing this info in 2 lists:
> 
> preferences = [10, 30, 20]
> hosts = [ "mx1.domain.com", "anotherhost.domain.com",
> "mx2.domain.com"]
> 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list depending of the indexes of another sorted list

2008-01-21 Thread J. Peng
Steven D'Aprano 写道:
> On Mon, 21 Jan 2008 16:23:50 +0800, J. Peng wrote:
> 
>> J. Peng 写道:
>>
>>>k = (i.split())[3]
>>>y = (i.split())[1]
>> btw, why can't I write the above two into one statement?
>>
>> (k,y) = (i.split())[3,1]
> 
> I don't know. What's "i"?
> 
> I'm guessing "i" is a string (and what a horrible choice of a name for a 
> string!) So i.split() will return a list. List indexing with multiple 
> arguments isn't defined, which is why you can't write 
> 
> k, y = (i.split())[3,1]
> 

Thanks.
Then one have to split the list twice.Given the list is large,it's maybe 
not good for performance.Is it a more effective split way?
-- 
http://mail.python.org/mailman/listinfo/python-list

read files

2008-01-21 Thread J. Peng
first I know this is the correct method to read and print a file:

fd = open("/etc/sysctl.conf")
done=0
while not done:
line = fd.readline()
if line == '':
done = 1
else:
print line,

fd.close()


I dont like that flag of "done",then I tried to re-write it as:

fd = open("/etc/sysctl.conf")
while line = fd.readline():
print line,
fd.close()


this can't work.why?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read files

2008-01-21 Thread J. Peng
Thank you. That gave so much solutions.
And thanks all.


Steven D'Aprano 写道:
> On Tue, 22 Jan 2008 11:00:53 +0800, J. Peng wrote:
> 
>> first I know this is the correct method to read and print a file:
>>
>> fd = open("/etc/sysctl.conf")
>> done=0
>> while not done:
>> line = fd.readline()
>> if line == '':
>> done = 1
>> else:
>> print line,
>>
>> fd.close()
> 
> 
> The evolution of a Python program.
> 
> # Solution 2:
> 
> fd = open("/etc/sysctl.conf")
> done = False
> while not done:
> line = fd.readline()
> if line:
> print line,
> else:
> done = True
> fd.close()
> 
> 
> 
> # Solution 3:
> 
> fd = open("/etc/sysctl.conf")
> while True:
> line = fd.readline()
> if line:
> print line,
> else:
> break
> fd.close()
> 
> 
> # Solution 4:
> 
> fd = open("/etc/sysctl.conf")
> lines = fd.readlines()
> for line in lines:
> print line,
> fd.close()
> 
> 
> # Solution 5:
> 
> fd = open("/etc/sysctl.conf", "r")
> for line in fd.readlines():
> print line,
> fd.close()
> 
> 
> # Solution 6:
> 
> for line in open("/etc/sysctl.conf").readlines():
> print line,
> # garbage collector will close the file (eventually)
> 
> 
> # Solution 7:
> 
> fd = open("/etc/sysctl.conf", "r")
> line = fd.readline()
> while line:
> print line,
> line = fd.readline()
> fd.close()
> 
> 
> # Solution 8:
> 
> fd = open("/etc/sysctl.conf", "r")
> for line in fd:
> print line,
> fd.close()
> 
> 
> # Solution 9:
> 
> for line in open("/etc/sysctl.conf"):
> print line,
> 
> 
> # Solution 10:
> # (the paranoid developer)
> 
> try:
> fd = open("/etc/sysctl.conf", "r")
> except IOError, e:
> log_error(e)  # defined elsewhere
> print "Can't open file, please try another."
> else:
> try:
> for line in fd:
> print line,
> except Exception, e:
> log_error(e)
> print "Reading file was interrupted by an unexpected error."
> try:
> fd.close()
> except IOError, e:
> # Can't close a file??? That's BAD news.
> log_error(e)
> raise e
> 
> 
> 
> 

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

Re: read files

2008-01-21 Thread J. Peng
Gabriel Genellina 写道:
> En Tue, 22 Jan 2008 02:03:10 -0200, Paul Rubin  
> <"http://phr.cx"@NOSPAM.invalid> escribió:
> 
>> "J. Peng" <[EMAIL PROTECTED]> writes:
>>> print line,
>> Do you really want all the lines crunched together like that?  If not,
>> leave off the comma.
> 
> Remember that Python *keeps* the end-of-line charater at the end of each  
> line; if you leave out the comma, you'll print them doubly spaced.
> 

Most languages (AFAIK) keep the newline character at the end of lines 
when reading from a file.But python's difference is its 'print' built-in 
function add a newline automatically for you. So without that ',' we 
will get double newlines.
-- 
http://mail.python.org/mailman/listinfo/python-list

what's this instance?

2008-01-21 Thread J. Peng
def safe_float(object):
  try:
retval = float(object)
  except (ValueError, TypeError), oops:
retval = str(oops)
  return retval

x=safe_float([1,2,3,4])
print x


The code above works well.But what's the instance of "oops"? where is it
coming from? I'm totally confused on it.thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's this instance?

2008-01-22 Thread J. Peng
Bruno Desthuilliers 写道:
> J. Peng a écrit :
>> def safe_float(object):
>>   try:
>> retval = float(object)
>>   except (ValueError, TypeError), oops:
>> retval = str(oops)
>>   return retval
> 
>> The code above works well.
> 
> For which definition of "works well" ?
> 

I got it from Core Python Programming book I bought.You may ask it to
Westley Chun.:)
-- 
http://mail.python.org/mailman/listinfo/python-list

python modules collection

2008-01-30 Thread J. Peng
Hello,

Is there a site for python,which collects most kinds of python modules?
like CPAN for Perl.
Sometime I want to use a module,like the time/date modules,don't know
where I should search from.
Sorry if I have repeated this question on the list.
Thanks!

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


urllib supports javascript

2008-02-01 Thread J. Peng
hello,

Which useragent lib supports javascript?
I know something about these libs: urllib,urllib2,cookielib,httplib
But I'm not sure which one of them can support javascript scripts.
Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib supports javascript

2008-02-01 Thread J. Peng
js 写道:
> AFAIK, nothing.
> How abount letting a browser do it?
> By using pamie [1] or selenium, you can drive a browser from python.
> 
> [1] http://pamie.sourceforge.net/
> 
> On Feb 2, 2008 11:07 AM, J. Peng <[EMAIL PROTECTED]> wrote:
>> hello,
>>
>> Which useragent lib supports javascript?
>> I know something about these libs: urllib,urllib2,cookielib,httplib
>> But I'm not sure which one of them can support javascript scripts.
>> Thanks!
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

Ok thanks.
I'm running the script on linux host (commands line mode),so can't run a 
  X-mode browser.
I'm familiar with WWW::Mechanize but this famous Perl module also 
doesn't support a JS plugin,though there is someone else has developed 
that a plugin.
-- 
http://mail.python.org/mailman/listinfo/python-list