Gregory, Matthew | 1 Sep 2010 01:24
Picon
Favicon

polymorphism for file-like objects

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):

def firstword(line):
    print line.split()[0]

def scanner(fileobject, linehandler):
    for line in fileobject:
        linehandler(line)

my_file = open('foo.txt')
scanner(my_file, firstword)

from cStringIO import StringIO
my_string = StringIO('one\ntwo xxx\nthree\n')
scanner(my_string, firstword)

class MyStream(object):
    def __iter__(self):
        return iter(['a\n', 'b c d\n'])
my_stream = MyStream()
scanner(my_stream, firstword)

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:

<root>
(Continue reading)

Tony Cappellini | 1 Sep 2010 06:59
Picon

Installation problem: Python 2.6.6 (32-Bit) on Windows 7 (32-Bit)

Has anyone else had problems running the msi for Python 2.6.6 on Windows 7?

If I don't check "Compile .py to byte code", the installer completes
without error.
Checking "Compile .py to byte code" causes the following to be displayed

"There is a problem with the windows installer package. A program run
as part of setup did not complete as expected"

1. Yes I have plenty of disk space.
2. Yes I have admin privileges
3. Yes, the MD5 checksum of the downloaded installer matches the MD5
checksum on python.org
4. Run As Adminsitrator is not available when I Shift-Right Click
(probably because my user already has admin privileges)

I'm also having a similar issue with the PythonWin32 extensions
installer on the same machine.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Ranjith Kumar | 1 Sep 2010 07:24
Picon
Gravatar

How to print the installed web browser

Hi all,

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

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

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Payal | 1 Sep 2010 03:45

Re: __new__ over __init__

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(_)
> <class '__main__.A'>
> 
> Peter
> 
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Sep 2010 10:14

Re: __new__ over __init__

"Payal" <payal-tutor <at> scriptkitchen.com> 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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Sep 2010 10:17

Re: How to print the installed web browser

"Ranjith Kumar" <ranjithtenz <at> gmail.com> 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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Nick Raptis | 1 Sep 2010 10:46
Picon

Re: How to print the installed web browser

On 09/01/2010 11:17 AM, Alan Gauld wrote:
> "Ranjith Kumar" <ranjithtenz <at> gmail.com> 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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Ranjith Kumar | 1 Sep 2010 10:48
Picon
Gravatar

Re: How to print the installed web browser



On Wed, Sep 1, 2010 at 1:47 PM, Alan Gauld <alan.gauld <at> btinternet.com> wrote:
"Ranjith Kumar" <ranjithtenz <at> gmail.com> 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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



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

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Nick Raptis | 1 Sep 2010 10:51
Picon

Re: How to print the installed web browser

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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 1 Sep 2010 12:49

Re: How to print the installed web browser

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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane