Re: [Tutor] __new__ over __init__

2010-09-01 Thread Payal
On Tue, Aug 31, 2010 at 08:27:10AM +0200, Peter Otten wrote:
> Subclasses of immutable types, e. g. tuple:

That was one great example, thanks. Some doubts,

a. I have seen this cls before, what does it mean?
b. What does type(_) mean?

Thanks a lot in advance.

With warm regards,
-Payal
-- 


> >>> class A(tuple):
> ... def __new__(cls, a, b):
> ... return tuple.__new__(cls, (a, b))
> ...
> >>> A(1, 2)
> (1, 2)
> >>> type(_)
> 
> 
> Peter
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __new__ over __init__

2010-09-01 Thread Alan Gauld

"Payal"  wrote


>>> class A(tuple):
... def __new__(cls, a, b):
... return tuple.__new__(cls, (a, b))



a. I have seen this cls before, what does it mean?


It is an abbreviation for class. The first parameter to new() must be 
a refernce to the class.
It is similar to self in an instance method, where the first parameter 
is a reference

to the instance.



b. What does type(_) mean?


The _ refers to the last evaluated result, in this case the tuple 
(1,2).
Its a shorthand trick, I think it only works in the interpreter, I 
don't like

it and never use it, but many do. (FWIW Perl has a similar shortcut
and Perl fans use it a lot!)

Try:

5+5

10

A = 1+2
print _

10

A

3

print _

3

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Alan Gauld

"Ranjith Kumar"  wrote

I`m using ubuntu how to find and print the installed web 
browsers using

python scripting.


How would you do it without Python scripting?
Is it even possible?

And on a multiuser system like Linux would you print out all the 
browsers

installed for the current user or for all users?

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Nick Raptis

On 09/01/2010 11:17 AM, Alan Gauld wrote:

"Ranjith Kumar"  wrote

I`m using ubuntu how to find and print the installed web browsers 
using

python scripting.


How would you do it without Python scripting?
Is it even possible?

And on a multiuser system like Linux would you print out all the browsers
installed for the current user or for all users?


Alan, let me make a wild guess here.

Ubuntu does have little "Preferred applications" config tool. I don't 
know how or where it stores this data, but my guess is it's the same 
place xdg (as in xdg-open) gets it's configuration from. This article 
might help http://www.crystalorb.net/mikem/xdg-settings.html


Quote from above article (near the end): "...especially as there is 
(currently) no single, portable, authoritative way for applications to 
query the values of these settings..."


Ranjith, get ready for some configuration file parsing.
But if you just want to open a url with the default browser, you can 
just execute "xdg-open your-url" as a subprocess.


Hope I shifted you to the right direction.

Nick

PS-trivia: I got to guess these just because I've read the source from 
"import antigravity"

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Ranjith Kumar
On Wed, Sep 1, 2010 at 1:47 PM, Alan Gauld wrote:

> "Ranjith Kumar"  wrote
>
>
> I`m using ubuntu how to find and print the installed web browsers using
>> python scripting.
>>
>
> How would you do it without Python scripting?
> Is it even possible?
>
> And on a multiuser system like Linux would you print out all the browsers
> installed for the current user or for all users?
>
for all users

>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Cheers
Ranjith,
Software Engineer,
Sedin Technologies,
Chennai

http://ranjith10z.wordpress.com
http://ranjithtenz.wordpress.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Nick Raptis

On 09/01/2010 11:46 AM, Nick Raptis wrote:


Alan, let me make a wild guess here.

Ubuntu does have little "Preferred applications" config tool. I don't 
know how or where it stores this data, but my guess is it's the same 
place xdg (as in xdg-open) gets it's configuration from. This article 
might help http://www.crystalorb.net/mikem/xdg-settings.html


Quote from above article (near the end): "...especially as there is 
(currently) no single, portable, authoritative way for applications to 
query the values of these settings..."


Ranjith, get ready for some configuration file parsing.
But if you just want to open a url with the default browser, you can 
just execute "xdg-open your-url" as a subprocess.


Hope I shifted you to the right direction.

Nick

PS-trivia: I got to guess these just because I've read the source from 
"import antigravity"


Ooops! Sorry if I caused any confusion, I thought the goal was to print 
the default browser, not all of the installed ones. Silly me.
Still, the "Preferred applications" tool seems to know that info (so to 
give you a choice) so it might be something to dig into.


Nick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Steven D'Aprano
On Wed, 1 Sep 2010 03:24:58 pm Ranjith Kumar wrote:
> Hi all,
>  I`m using ubuntu how to find and print the installed web
> browsers using python scripting.


You already asked this question on the 9th of August, in an email 
titled "Need a mentor":


4) Lastly I need to know is how to print the list of web browsers 
installed on a machine.


The answer is the same now as it was then:

You can't. 

I gave a much longer reply back then. But in summary:

* there's no canonical list of web browsers you could look for;
* there's no reliable way of recognizing a web browser short of human 
intelligence;
* it's not even clear what a web browser is.

Obviously a web browser is something that can browse the WWW, but that's 
much, much broader than just Firefox and IE. Python can browse the web. 
Does that mean Python is a web browser? Adobe Acrobat can download 
upgrades over the web. Does that make it a web browser? How about curl 
or wget?

Many applications can read files remotely over http. Some of them can 
follow embedded hyperlinks. Are they web browsers?




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Installation problem: Python 2.6.6 (32-Bit) on Windows 7 (32-Bit)

2010-09-01 Thread Steven D'Aprano
On Wed, 1 Sep 2010 02:59:18 pm Tony Cappellini wrote:
> Has anyone else had problems running the msi for Python 2.6.6 on
> Windows 7?

Sorry, I'm not a Windows guy, I can't help.

You might have more luck on the python-l...@python.org mailing list, 
which is also available on comp.lang.python:

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


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] polymorphism for file-like objects

2010-09-01 Thread Steven D'Aprano
On Wed, 1 Sep 2010 09:24:23 am Gregory, Matthew wrote:
> Hi all,
>
> In the 2nd edition of Python Cookbook, Mark Lutz writes the intro to
> Chapter 2 (Files) and gives the following example of polymorphism for
> file like objects (adapted for brevity):
[...]
> I understand this code and the polymorphic behavior of scanner.  But
> then he says you can extend this idea to other file like objects (SQL
> databases, XML, interactive user, etc.).  My question is how do you
> extend the idea when 'lines' aren't so clearly defined such as this
> XML snippet:
>
> 
>   a b c
>   d
>   
> a x
>   
> 

I expect that iterating over lines would be equivalent to:

a b c
d
a x

If you agree, then just walk the XML, and every time you see a  
item, yield it.

Things get tricky if you can nest lines...

a bdc

I don't know what to expect if I see that.



> I assume you need to write a MyXMLParser that somehow defines the
> iteration behavior that you want such that it will work with the
> scanner function?  But it doesn't necessarily need to subclass file,
> correct?

Correct.

Forget about inheritance in this case, what you care about is the 
interface. You want an object that iterates over "lines", whatever that 
happens to be.

Inheritance is useful, but it's just one way of making objects with the 
same interface.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] P2PU Beginning Python Webservices

2010-09-01 Thread pa yo
I thought some of you might be interested in this course:

http://www.p2pu.org/webcraft/beginning-python-webservices
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Exec function problem

2010-09-01 Thread Currall, Andrew
Using Python 2.5. 
DemandModel.py is a valid, working python script. If I create another
script file, then:

execfile('DemandModel.py')

works fine. However, the apparently similar:

def runfile():
execfile('DemandModel.py')
runfile()

doesn't, for no apparent reason: I get 

  File "DemandModel.py", line 12, in DemandModel
Parameters=ReadDmData(segname)
NameError: global name 'ReadDmData' is not defined

Any ideas why? 

Use of 

CodeFile=open('DemandModel.py', 'r')
exec(CodeFile)

instead of execfile, doesn't make any difference either way, nor does it
make any difference if I read the file using read() and then execute the
string- in all cases the top-level command works; placed in a function
it doesn't.
Regards and thanks,
Andrew Currall MPhys
Senior Consultant, Transportation
D +44 (0)1727 535612
andrew.curr...@aecom.com
AECOM
AECOM House
63-77 Victoria Street
St Albans, Herts AL1 3ER
T +44 (0)1727 535000 F +44 (0)1727 535099
www.aecom.com  



 
This email is confidential and is for the intended recipient only. If you are 
not the intended recipient, please contact the author and you must not disclose 
or use the contents in any way. The author bears responsibility for any legal 
action or disputes arising from views or professional advice expressed which do 
not relate to the business of AECOM Ltd.
 
AECOM Limited Registered in England No: 1846493
Registered Office: AECOM House, 63-77 Victoria Street, St Albans, Herts, AL1 3ER
 
Please consider the environment before printing this e-mail
 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Alan Gauld


"Nick Raptis"  wrote

Ooops! Sorry if I caused any confusion, I thought the goal was to 
print the default browser, not all of the installed ones. Silly me.
Still, the "Preferred applications" tool seems to know that info (so 
to give you a choice) so it might be something to dig into.


The problem remains that the config tool will only know about
those browsers that have been "politely" installeed. If someone just
built the source in a local folder then its unlikely to be rgisterd,
but its still a valid browser, and may even be that users default.

You can only ever get an approximation and its a dangerous
assumption to think you can get more than that. But depending
on the reason for asking it may be good enough.

HTH,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exec function problem

2010-09-01 Thread Alan Gauld


"Currall, Andrew"  wrote

DemandModel.py is a valid, working python script. 
If I create another script file, then:


execfile('DemandModel.py')


You probably shouldn't. You should probably be importing it. 
Is there a reason why you want to use execfile()? 
Its nearly always the wrong solution...



def runfile():
execfile('DemandModel.py')

runfile()



  File "DemandModel.py", line 12, in DemandModel
Parameters=ReadDmData(segname)
NameError: global name 'ReadDmData' is not defined


So it works on a line by itself but not in a function?
And it is running the file but getting an undefined name. odd.
What happens if you run execfile() from a new interpreter prompt?


CodeFile=open('DemandModel.py', 'r')
exec(CodeFile)


What happens if you use import (assuming you store in 
somewhere in sys.path...)



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Mark Weil
Not perfect, but you could check for each browser's binary.

import os
os.path.isfile("/usr/bin/firefox")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Steve Willoughby

On 01-Sep-10 13:10, Mark Weil wrote:

Not perfect, but you could check for each browser's binary.

import os
os.path.isfile("/usr/bin/firefox")


You'd probably be better off at least looking at the user's PATH 
variable, which would likely catch platform variations in where the 
browser would be located, and catch local installations by that user. 
Of course, you're still playing a guessing game of what to even look for 
or what the browser binary is called.  Maybe it's 
/usr/local/bin/firefox3, for example.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to print the installed web browser

2010-09-01 Thread Alan Gauld


"Steve Willoughby"  wrote


Not perfect, but you could check for each browser's binary.

import os
os.path.isfile("/usr/bin/firefox")


But then you have to know of all browsers and thats almost impossible.
And what if the user has built their own browser - I've written at 
least

3 web browsers of varying quality over the years! Including one batch
oriented one that ran overnight in the days when I only had a
14400 baud modem connection...

You'd probably be better off at least looking at the user's PATH 
variable, which would likely catch platform variations in where the 
browser would be located, and catch local installations by that 
user.


But only if they used path.
When I was a Unix user I used to just set up aliases to many of
the binaries that I installed locally. Or I would use a shell script 
to

launch them after setting up environment variables etc. The shell
script would be in my PATH but not the binary...

It really is an impossible task to get right. The best you will get is
a check against a small set of the most common browsers

firefox, opera, konqueror, chrome, lynx, safari(is it on Linux?) etc.

And as Steven pointed out many office type programs include the
ability to act as a basic browser nowadays. Even trhe Eclipse IDE
can display HTML pages. Does that count?


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor