Re: [Tutor] Problems with Python Modules

2006-09-16 Thread Dave Kuhlman
On Sat, Sep 16, 2006 at 08:10:21PM -0700, Will Shattuck wrote:
> Hi all,
> 
> I'm trying to install some python mud engines, but I keep running into
> problems with the software not seeing python modules.  Could this be
> due to environment variables missing on my system?  Here is an example
> of the most recent error message using the InnerSpace mud engine:
> 
> 
> C:\muds\InnerSpace-0.9>python bin\bootstrap_server.py --verdir verbs\
> Traceback (most recent call last):
>   File "bin\bootstrap_server.py", line 13, in ?
> from twisted.scripts import mktap, twistd
>   File "C:\Python24\lib\site-packages\twisted\scripts\twistd.py", line 6, in ?
> from twisted.python import log, syslog
>   File "C:\Python24\lib\site-packages\twisted\python\syslog.py", line 5, in ?
> syslog = __import__('syslog')
> ImportError: No module named syslog
> 

Try adding this for debugging:

import sys
print sys.path

That's a list of the directories where Python looks for modules to
be imported.

Are you sure that you want to import syslog from twisted.python? 
syslog is a module in the python standard library.  Is there also a
module named syslog in twisted.python?

Dave

[snip]

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problems with Python Modules

2006-09-16 Thread Will Shattuck
Hi all,

I'm trying to install some python mud engines, but I keep running into
problems with the software not seeing python modules.  Could this be
due to environment variables missing on my system?  Here is an example
of the most recent error message using the InnerSpace mud engine:


C:\muds\InnerSpace-0.9>python bin\bootstrap_server.py --verdir verbs\
Traceback (most recent call last):
  File "bin\bootstrap_server.py", line 13, in ?
from twisted.scripts import mktap, twistd
  File "C:\Python24\lib\site-packages\twisted\scripts\twistd.py", line 6, in ?
from twisted.python import log, syslog
  File "C:\Python24\lib\site-packages\twisted\python\syslog.py", line 5, in ?
syslog = __import__('syslog')
ImportError: No module named syslog


My system is new Dell D620 Latitude running WindowsXP Pro SP2, Python
2.4, I have installed the Twisted binary.  I have another mud engine
(ErisMUD) that doesn't see the SQLite modules I installed either.
Here is my PATH at a command prompt in case you need it.  Yes I have a
lot of crud in my path :)

==
PATH:
PATH=C:\Python24;C:\orant\bin;C:\Perl\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program
Files\Microsoft SQL
Server\80\Tools\Binn\;C:\PROGRA~1\WONDER~1\Avantis\Common;C:\Program
Files\Crimson Editor\;C:\Program Files\QuickTime\QTSystem\;C:\Program
Files\VDMSound;c:\Program Files\Microsoft SQL Server\90\Tools\binn\
==

Thanks for any help. I just don't know where to start looking.

Will

-- 
Will Shattuck  ( willshattuck.at.gmail.com )
Home Page:  http://www.thewholeclan.com/will

When you get to your wit's end, you'll find God lives there.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Cookie help

2006-09-16 Thread federico ramirez
Hey, im working on a cgi script, just a simple news system.And im stucked with the login system.GOD! I hate being a noob!Well, this is the login page#!/usr/bin/python
import cgi, dbm, string, Cookieimport configrequest = cgi.FieldStorage()def loginform():    print '''    
        Username                Password
                 
            '''def main():    C = Cookie.SimpleCookie()    if(C.has_key("admin_user") and C.has_key("admin_pass")):        admin = 
config.getadmin()        if(C["admin_user"].value == admin[0] and C["admin_pass"].value == admin[1]):            config.makepage("You are already logged!")        else:            
config.makepage("Wrong cookies...")    elif(request.has_key("Submit")):        admin = config.getadmin()        username = config.clean(request["user"].value)        password = 
config.clean(request["pass"].value)        if(username == admin[0] and password == admin[1]):                C["admin_user"] = username                C["admin_pass"] = password
                print C                print "Content-Type: text/html\n\n"                config.startpage()                print "Bienvenido",username,'!'                config.endpage
()        else:            print "Content-Type: text/html\n\n"            config.startpage()            print "Incorrect username and password combination"            config.endpage
()    else:        print "Content-Type: text/html\n\n"        config.startpage()        loginform()        config.endpage()main()That seems to work..but i cant get the cookies, i tried making this to get them but i have a 500 error -.- i hate it so much
##!/usr/bin/pythontry:    import Cookie    print "Content-Type: text/html\n\n"    C = Cookie.SimpleCookie()    print C.["admin_user"].value
except:    cgi.print_exception()###i tried that in different ways but i cant get it to workHelp please :(-- Best Regards.fedekiller
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] folder and module

2006-09-16 Thread linda.s
Hi there,
i have test.py under c:\try1\new;
I want to import b and b.py is under c:\trytry. How to do it?
Also, I import c and c.py is in the desktop folder but no error reported.
Why?
Thanks,
Linda



On 9/2/06, linda. s <[EMAIL PROTECTED]> wrote:
> I read something about random.seed() but still confused. i can
> understand random.random() but it is very hard for me to understand
> random.seed(0... can anyone explain an example?
> Thanks a lot!
> Linda
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Kent Johnson
Brian van den Broek wrote:
> Kent Johnson said unto the world upon 16/09/06 04:35 PM:
>> Brian van den Broek wrote:

>>> You say you are new to Python. Well, it might not now be obvious why 
>>> dictionaries are especially useful, but they are *central* to the 
>>> pythonic approach. The sooner you become comfortable with them, the 
>>> better (IMHO).
>> I agree that dicts are extremely useful, but I don't think they add 
>> anything in this case unless there is actually a need for keyed access. 
>> A list of lists (or tuples) seems very appropriate to me. A good 
>> alternative might be a list of Bunches.
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
>>
>> Kent
> 
> 
> Hi Kent and all,
> 
> I should have included the reason why I thought a dict might be better 
> here. (I did send it in a private email after the post.)
> 
> A lot of ways I could imagine the time-line data being used might 
> involve wanting to access some one year, rather than the entire time-line.

Yes, I was a bit hasty in denouncing dicts, the best data structure does 
depend entirely on how it is to be used, and we don't know enough about 
this application to know.

>  >>> print timeline_data[800][0]
> 
> seems *way* better than something like:
> 
>  >>> for year_data in timeline_data_as_list_of_lists:
> ...if year_data[0] == 800:
> ...   print year_data[1]
> ...   break
> 
> which would be what the original list structure seems to require.

The thing is, though, how will you know that 800 is a valid year? You 
need a list of valid years. If you get that list from the dict keys, and 
iterate that, you haven't really gained anything over a list of tuples. 
Maybe you have a lot of items and the user enters a year and you want to 
print out the data you have on the year...

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread PythonIn.dk
Thank you for your tips on my list list.
I see how there are advantages to the different approaches.
I expect to simply output the text and pipe it somewhere else - so, it
is not actually an issue to access the individual posts.

Yours, (probably back later)
Morten
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Brian van den Broek
Kent Johnson said unto the world upon 16/09/06 04:35 PM:
> Brian van den Broek wrote:
>> Morten Juhl Johansen said unto the world upon 16/09/06 08:29 AM:
>>> # Newbie warning
>>> I am making a timeline program. It is fairly simple.
>>> I base it on appending lists to a list.
>>> Ex.
>>> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
>>>
>>> This seemed like a brilliant idea when I did it. It is easy to sort.
>>> Now, if I want to OUTPUT it, how do I indicate that I want to extract
>>> first entry in a list in a list? How do I print the separate entries?
>>>
>>> Yours,
>>> Morten
>>>
>> Hi Morten,
>>
>> Andrei answered the question you asked; I'd like to make a suggestion 
>> involving a bit of reworking.
>>
>> You might think about structuring your timeline data as a dictionary, 
>> rather than a list. So:
>>
>>  >>> timeline_data = {
>> ...  800: ["Charlemagne Crowned Holy Roman Emperor", 'event_text'],
>> ... 1066: ["Battle at Hastings", 'event_text']}
>>
>>
>> This makes it very easy to access a given year's data:
>>
>>  >>> timeline_data[800]
>> ['Charlemagne Crowned Holy Roman Emperor', 'event_text']
>>
>> and
>>
>>  >>> timeline_data[800][0]
>> 'Charlemagne Crowned Holy Roman Emperor'
>>
>> will get you the headline alone.
>>
>> You expressed a liking for the lists as they are easy to sort. On 
>> recent versions of python one can easily obtain a sorted list of 
>> dictionary keys, too:
>>
>>  >>> d = {1:2, 3:4, 43545:32, -3434:42}
>>  >>> d
>> {1: 2, 3: 4, -3434: 42, 43545: 32}
>>  >>> sorted(d)
>> [-3434, 1, 3, 43545]
>>  >>>
>>
>> (Older versions of Python can do the same, but with a bit more 
>> keyboard action.)
>>
>> So, if you wanted to print the headlines in increasing year order:
>>
>>  >>> for year in sorted(timeline_data):
>> ... print timeline_data[year][0]
>> ...
>> Charlemagne Crowned Holy Roman Emperor
>> Battle at Hastings
>>  >>>
>>
>>
>> You say you are new to Python. Well, it might not now be obvious why 
>> dictionaries are especially useful, but they are *central* to the 
>> pythonic approach. The sooner you become comfortable with them, the 
>> better (IMHO).
> 
> I agree that dicts are extremely useful, but I don't think they add 
> anything in this case unless there is actually a need for keyed access. 
> A list of lists (or tuples) seems very appropriate to me. A good 
> alternative might be a list of Bunches.
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
> 
> Kent


Hi Kent and all,

I should have included the reason why I thought a dict might be better 
here. (I did send it in a private email after the post.)

A lot of ways I could imagine the time-line data being used might 
involve wanting to access some one year, rather than the entire time-line.

So, if you wanted to get the headline for the year 800,

 >>> print timeline_data[800][0]

seems *way* better than something like:

 >>> for year_data in timeline_data_as_list_of_lists:
...if year_data[0] == 800:
...   print year_data[1]
...   break

which would be what the original list structure seems to require.

It may be a case of over-design for needs that won't arise, though.

Best to all,

Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Kent Johnson
Kent Johnson wrote:
> It's handy to assign names to the individual elements of the list. A 
> variable named 'year' is a lot easier to understand than 'event[0]'. 
> This is easy to do with tuple unpacking:
> 
> In [6]: for event in events:
> ...: year, head, text = event
> ...: print 'In', year, head, text
> ...:
> ...:
> In 1863 headline1 event text1
> In 1992 headline2 event text2

The for loop and tuple unpacking can also be combined into the very elegant:

for year, head, text in events:
   print 'In', year, head, text

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Kent Johnson
Brian van den Broek wrote:
> Morten Juhl Johansen said unto the world upon 16/09/06 08:29 AM:
>> # Newbie warning
>> I am making a timeline program. It is fairly simple.
>> I base it on appending lists to a list.
>> Ex.
>> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
>>
>> This seemed like a brilliant idea when I did it. It is easy to sort.
>> Now, if I want to OUTPUT it, how do I indicate that I want to extract
>> first entry in a list in a list? How do I print the separate entries?
>>
>> Yours,
>> Morten
>>
> 
> Hi Morten,
> 
> Andrei answered the question you asked; I'd like to make a suggestion 
> involving a bit of reworking.
> 
> You might think about structuring your timeline data as a dictionary, 
> rather than a list. So:
> 
>  >>> timeline_data = {
> ...  800: ["Charlemagne Crowned Holy Roman Emperor", 'event_text'],
> ... 1066: ["Battle at Hastings", 'event_text']}
> 
> 
> This makes it very easy to access a given year's data:
> 
>  >>> timeline_data[800]
> ['Charlemagne Crowned Holy Roman Emperor', 'event_text']
> 
> and
> 
>  >>> timeline_data[800][0]
> 'Charlemagne Crowned Holy Roman Emperor'
> 
> will get you the headline alone.
> 
> You expressed a liking for the lists as they are easy to sort. On 
> recent versions of python one can easily obtain a sorted list of 
> dictionary keys, too:
> 
>  >>> d = {1:2, 3:4, 43545:32, -3434:42}
>  >>> d
> {1: 2, 3: 4, -3434: 42, 43545: 32}
>  >>> sorted(d)
> [-3434, 1, 3, 43545]
>  >>>
> 
> (Older versions of Python can do the same, but with a bit more 
> keyboard action.)
> 
> So, if you wanted to print the headlines in increasing year order:
> 
>  >>> for year in sorted(timeline_data):
> ... print timeline_data[year][0]
> ...
> Charlemagne Crowned Holy Roman Emperor
> Battle at Hastings
>  >>>
> 
> 
> You say you are new to Python. Well, it might not now be obvious why 
> dictionaries are especially useful, but they are *central* to the 
> pythonic approach. The sooner you become comfortable with them, the 
> better (IMHO).

I agree that dicts are extremely useful, but I don't think they add 
anything in this case unless there is actually a need for keyed access. 
A list of lists (or tuples) seems very appropriate to me. A good 
alternative might be a list of Bunches.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Kent Johnson
Morten Juhl Johansen wrote:
> # Newbie warning
> I am making a timeline program. It is fairly simple.
> I base it on appending lists to a list.
> Ex.
> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
> 
> This seemed like a brilliant idea when I did it. It is easy to sort.

It's a fine idea

> Now, if I want to OUTPUT it, how do I indicate that I want to extract
> first entry in a list in a list? How do I print the separate entries?

There are a couple of things to do. First, at the top level, you have a 
list and you want to process each element of the list. A for loop works 
well for this; it assigns each member of the list in turn to a named 
variable:

In [2]: events=[[1863, "headline1", "event text1"], [1992, "headline2", 
"event text2"]]

In [3]: for event in events:
...: print event
...:
...:
[1863, 'headline1', 'event text1']
[1992, 'headline2', 'event text2']

Within the for loop the variable 'event' contains a simple list which 
can be accessed with normal subscripting:
In [5]: for event in events:
...: print 'In', event[0], event[1], event[2]
...:
...:
In 1863 headline1 event text1
In 1992 headline2 event text2

It's handy to assign names to the individual elements of the list. A 
variable named 'year' is a lot easier to understand than 'event[0]'. 
This is easy to do with tuple unpacking:

In [6]: for event in events:
...: year, head, text = event
...: print 'In', year, head, text
...:
...:
In 1863 headline1 event text1
In 1992 headline2 event text2


One final note - in Python it is more idiomatic - or at least closer to 
the intentions of the language designer - to use tuples for lists of 
dissimilar items. So it is a bit more idiomatic to express your initial 
list as a list of tuples:

events=[(1863, "headline1", "event text1"), (1992, "headline2", "event 
text2")]

It's not a big deal and it won't change the rest of the code at all, 
it's just a stylistic note.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] From byte[] to Image

2006-09-16 Thread Alan Gauld
Hi Ziad,

Can you give us a bit mor4e detail.

> #**
> # fileName is the path of the file
> stream = service.getFile(fileName)
> 
> file = open("pic.jpeg", 'w')
> 
> img = Image.frombuffer("1", (128, 128), stream)
> img.save(file)

Which library are you using here? Is it the PIL?

> The Java method is: "public byte[] getFile(String path)"
> 
> This is not working. I am not able to get all the data of the image
> and for sure not able to form the complete image.

Are you saying the Java code is not working?
Or that the value you get back from the web service is not 
what you expected? Or that the resultant byte array is what 
you expected but it is not being transformed to an image correctly?

I'm not 100% sure what the problem is here.

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Brian van den Broek
Morten Juhl Johansen said unto the world upon 16/09/06 08:29 AM:
> # Newbie warning
> I am making a timeline program. It is fairly simple.
> I base it on appending lists to a list.
> Ex.
> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
> 
> This seemed like a brilliant idea when I did it. It is easy to sort.
> Now, if I want to OUTPUT it, how do I indicate that I want to extract
> first entry in a list in a list? How do I print the separate entries?
> 
> Yours,
> Morten
>

Hi Morten,

Andrei answered the question you asked; I'd like to make a suggestion 
involving a bit of reworking.

You might think about structuring your timeline data as a dictionary, 
rather than a list. So:

 >>> timeline_data = {
...  800: ["Charlemagne Crowned Holy Roman Emperor", 'event_text'],
... 1066: ["Battle at Hastings", 'event_text']}


This makes it very easy to access a given year's data:

 >>> timeline_data[800]
['Charlemagne Crowned Holy Roman Emperor', 'event_text']

and

 >>> timeline_data[800][0]
'Charlemagne Crowned Holy Roman Emperor'

will get you the headline alone.

You expressed a liking for the lists as they are easy to sort. On 
recent versions of python one can easily obtain a sorted list of 
dictionary keys, too:

 >>> d = {1:2, 3:4, 43545:32, -3434:42}
 >>> d
{1: 2, 3: 4, -3434: 42, 43545: 32}
 >>> sorted(d)
[-3434, 1, 3, 43545]
 >>>

(Older versions of Python can do the same, but with a bit more 
keyboard action.)

So, if you wanted to print the headlines in increasing year order:

 >>> for year in sorted(timeline_data):
... print timeline_data[year][0]
...
Charlemagne Crowned Holy Roman Emperor
Battle at Hastings
 >>>


You say you are new to Python. Well, it might not now be obvious why 
dictionaries are especially useful, but they are *central* to the 
pythonic approach. The sooner you become comfortable with them, the 
better (IMHO).

Best wishes,

Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problems serving up PDF

2006-09-16 Thread Tim Johnson
* Python <[EMAIL PROTECTED]> [060915 04:35]:
> 
> There is a Firefox extension called TamperData that allows you to
> examine and change the headers.  This extension is very handy for
> debugging these kinds of issues.  My Apache web server returns these
> headers when I request a pdf file:
> 
> Status=OK - 200
> Date=Fri, 15 Sep 2006 12:03:37 GMT
> Server=Apache/2.0.53 (Fedora)
> Last-Modified=Tue, 06 Jun 2006 16:50:30 GMT
> Etag="68061-6a29d-c70c180"
> Accept-Ranges=bytes
> Content-Length=434845
> Connection=close
> Content-Type=application/pdf
> X-Pad=avoid browser bug
> 
> The last two should be of interest to you.  I did not try to google X-
> Pad or "avoid browser bug", but google might provide more background.  
> 
> In any case, Tamper Data allows you to see what a working web site does
> to succeed.  Imitation is the sincerest form of flattery.

   thanks. that is a very helpful tool. and I'm seeing that headers are
   consistant with what I've sent.
   Good tip!
   tim
-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data structure question / PyGoogle

2006-09-16 Thread Python
(forwarding to list)
On Sat, 2006-09-16 at 10:31 -0500, Brian Edward wrote:
> Thanks for the quick reply!  I really appreciate your assistance.  Of
> course, it will take some time to get this worked out, but your
> explanation is very clear.  
>  
> Best,
> Brian
> 
>  
> On 9/16/06, Python <[EMAIL PROTECTED]> wrote: 
> On Sat, 2006-09-16 at 09:44 -0500, Brian Edward wrote:
> > Hello all,
> >
> > I am new to Python (and programming in general) and am
> trying to get 
> > PyGoogle figured out for some specific research
> interests.  Basically,
> > I have done a simple search using PyGoogle and have some
> sitting in
> > memory.  I have an object data.results, which is apparently
> a list: 
> >
> > >>> type(data.results)
> > 
> >
> > In this list, I have ten URL saved, which I can access by
> using the
> > brackets and noting the specific elements.  For example: 
> >
> > >>> data.results[0].URL
> > 'http://www.psychguides.com/gl-
> treatment_of_schizophrenia_1999.html'
> >
> > >>> data.results [1].URL
> > 'http://www.psychguides.com/sche.pdf'
> >
> > My question is, how can I access all ten URLs in a single
> command.
> > Specifically, why does the following statement not work: 
> >
> > >>> data.results[0:10].URL
> You need to extract the URL from each item in the result list.
> Something like:
> 
>urls = [r.URL for r in data.results]
> 
> will extract a list of urls from your list of results. 
> >
> > Traceback (most recent call last):
> >   File "", line 1, in -toplevel-
> > data.results[0:10].URL
> > AttributeError: 'list' object has no attribute 'URL'
> > 
> >
> > Again, I am new to Python, so a watered-down, conceptual
> response to
> > this would be greatly appreciated.  Thanks in advance.
> 
> data.results[0:10] simply copies the first 10 results from
> your original 
> list into a new list.  The new list does not have a URL
> attribute, as
> the error message tells us.  The URL attribute belongs to the
> individual
> items in the list.
> 
> You need to process each result in data.results to extract the
> URL.
> Your choices boil down to:
>for statement
> or the more functionally oriented
>map
>list comprehension
>generator expression (python 2.4 or later)
> 
> For creating a new list from an existing list, a list
> comprehension is 
> usually the best bet.  The for statement approach would look
> something
> like:
>urls = []
>for r in data.results:
>urls.append(r.URL)
> 
> list comprehensions provide a simpler, more direct syntax. 
> 
> 
> 
> >
> > Brian
> >
> >
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> --
> Lloyd Kvam
> Venix Corp
> 
> 
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] From byte[] to Image

2006-09-16 Thread Ziad Rahhal
Hi,

I convert an Image to bye array in Java (web-services) and send it to a python client.

At the client side (python code) i receive the bytes, and then i need to transform them into
the image again.

Here is what I am doing:

#**
# fileName is the path of the file
stream = service.getFile(fileName)   

file = open("pic.jpeg", 'w')

img = Image.frombuffer("1", (128, 128), stream)
img.save(file)

file.close()
#**

The Java method is: "public byte[] getFile(String path)"

This is not working. I am not able to get all the data of the image
and for sure not able to form the complete image.

Any solution for that?

Thank you in advance,

Ziad

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data structure question / PyGoogle

2006-09-16 Thread Python
On Sat, 2006-09-16 at 09:44 -0500, Brian Edward wrote:
> Hello all,
> 
> I am new to Python (and programming in general) and am trying to get
> PyGoogle figured out for some specific research interests.  Basically,
> I have done a simple search using PyGoogle and have some sitting in
> memory.  I have an object data.results, which is apparently a list:
> 
> >>> type(data.results)
> 
> 
> In this list, I have ten URL saved, which I can access by using the
> brackets and noting the specific elements.  For example: 
> 
> >>> data.results[0].URL
> 'http://www.psychguides.com/gl-treatment_of_schizophrenia_1999.html'
> 
> >>> data.results [1].URL
> 'http://www.psychguides.com/sche.pdf'
> 
> My question is, how can I access all ten URLs in a single command.
> Specifically, why does the following statement not work: 
> 
> >>> data.results[0:10].URL
You need to extract the URL from each item in the result list.
Something like:

urls = [r.URL for r in data.results]

will extract a list of urls from your list of results.
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> data.results[0:10].URL
> AttributeError: 'list' object has no attribute 'URL' 
> 
> 
> Again, I am new to Python, so a watered-down, conceptual response to
> this would be greatly appreciated.  Thanks in advance.

data.results[0:10] simply copies the first 10 results from your original
list into a new list.  The new list does not have a URL attribute, as
the error message tells us.  The URL attribute belongs to the individual
items in the list.

You need to process each result in data.results to extract the URL.
Your choices boil down to:
for statement
or the more functionally oriented
map
list comprehension
generator expression (python 2.4 or later)

For creating a new list from an existing list, a list comprehension is
usually the best bet.  The for statement approach would look something
like:
urls = []
for r in data.results:
urls.append(r.URL)

list comprehensions provide a simpler, more direct syntax.



> 
> Brian
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Data structure question / PyGoogle

2006-09-16 Thread Alan Gauld
> In this list, I have ten URL saved, which I can access by using the 
> brackets
> and noting the specific elements.  For example:
>
 data.results[0].URL
> 'http://www.psychguides.com/gl-treatment_of_schizophrenia_1999.html'

> My question is, how can I access all ten URLs in a single command.
> Specifically, why does the following statement not work:
>
 data.results[0:10].URL

The list slice returns another list. And as the error says lists do
not have a URL attribute. You can either write a loop to return
the URLs

urls = []
for url in data.results:
 urls.append(url.URL)

or more Pythonically use a List Comprehension, which
combines all of that in one line:

urls = [url.URL for url in data.results]

In general when you want to convert a list of something
to another list of something, either a subset of the original
or a transformed version, like here, use a list comprehension.

newlist = [f(x) for x in oldlist ]

> Again, I am new to Python, so a watered-down, conceptual response to 
> this
> would be greatly appreciated.  Thanks in advance.

You can find more on list comrehensions in the functional
programming topic of my tutor - about half way down the page...

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Data structure question / PyGoogle

2006-09-16 Thread Brian Edward
Hello all,I am new to Python (and programming in general) and am trying to get PyGoogle figured out for some specific research interests.  Basically, I have done a simple search using PyGoogle and have some sitting in memory.  I have an object 
data.results, which is apparently a list:>>> type(data.results)In this list, I have ten URL saved, which I can access by using the brackets and noting the specific elements.  For example:  
>>> data.results[0].URL'http://www.psychguides.com/gl-treatment_of_schizophrenia_1999.html'>>> data.results
[1].URL'http://www.psychguides.com/sche.pdf'My question is, how can I access all ten URLs in a single command.  Specifically, why does the following statement not work:
>>> data.results[0:10].URLTraceback (most recent call last):  File "", line 1, in -toplevel-    data.results[0:10].URLAttributeError: 'list' object has no attribute 'URL'
Again, I am new to Python, so a watered-down, conceptual response to this would be greatly appreciated.  Thanks in advance.Brian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in lists

2006-09-16 Thread Andrei
Morten Juhl Johansen wrote:
> # Newbie warning
> I am making a timeline program. It is fairly simple.
> I base it on appending lists to a list.
> Ex.
> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
> 
> This seemed like a brilliant idea when I did it. It is easy to sort.
> Now, if I want to OUTPUT it, how do I indicate that I want to extract
> first entry in a list in a list? How do I print the separate entries?

Just append the position indicators, e.g. "print MyList[0][1]" will take 
item #0 in MyList and request its item #1. It's equivalent to saying

   MySubList = MyList[0]
   print MySubList[1]

In an interactive session:

 >>> li = [[1,2], [3,4]]
 >>> li[0]
[1, 2]
 >>> li[0][0]
1
 >>> li[0][1]
2
 >>> li[1][1]
4


Yours,

Andrei

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Lists in lists

2006-09-16 Thread Morten Juhl Johansen
# Newbie warning
I am making a timeline program. It is fairly simple.
I base it on appending lists to a list.
Ex.
[[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]

This seemed like a brilliant idea when I did it. It is easy to sort.
Now, if I want to OUTPUT it, how do I indicate that I want to extract
first entry in a list in a list? How do I print the separate entries?

Yours,
Morten

__
http://mjj.slacking.dk
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ANN: pyfuzzylib 0.1.3 Released

2006-09-16 Thread nelson -
PyFuzzyLib is a library for fuzzy inference engine building. Using
pyfuzzylib you can add fuzzy logic to your programs. The program is in
it early stage of development, but it is still usable. Every sort of
feedback is appreciated!

the project homepage is

http://sourceforge.net/projects/pyfuzzylib


good afternoon,
   nelson
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of range() function in Walking problem

2006-09-16 Thread Andrei
Srinivas Iyyer wrote:

> Coordinates of a variable G1:
> a1 : 3-8
> b2 : 10-25
> c3 : 7-18
> d4 : 10-13
> Now, I have variables G1Gn.  Each variable may
> have individual pieces a1(n),b2(n),c3(n).z(n).
> 
> my kind of solution, which is very crappy (i assume):
> 
> Sort the list:
> [3,8] [3,8]
> [10,25] sort  [7,18]
> [7,18] >  [10,13]
> [10,13]   [10,25]
> 

> The question to the forum is, I am using range()
> function to solve this. Since the numbers in the
> example are small, it worked. in the real world
> example , it did not work, because computer slowed
> down and eventually crashed.  The range(81393417) is
> really big. 

I'm not sure I fully understand the question - do you NEED to loop over 
the possible locations, or do you only need to determine the extremes of 
the 4 ranges? If you need a loop, xrange is probably the way to go, as 
Luke mentions.
If you just need the extremes, you don't need a range(), because it can 
be done using zip:

 >>> a,b,c,d = [3,8], [10,25], [7,18], [10,13]
 >>> candidates = zip(a,b,c,d) # 'merges' the elements of the lists
 >>> print candidates # first item contains potential minimums
[(3, 10, 7, 10), (8, 25, 18, 13)] # second item potential maxes
 >>> maxrange = [ min(candidates[0]), max(candidates[1]) ]
 >>> print maxrange
[3, 25]

Now if you have a non-continuous range, this will still give you the 
overlapping area. E.g. if you'd drop c3, it would not affect the result. 
If that's a problem, you could loop over the lists directly:

def getMaxRange(locs):
 locs.sort() # sorts 'naturally'
 #print "sorted locs:", locs
 maxrange = locs[0][:] # guaranteed to start with min
 for loc in locs[1:]: # loop over rest of locs
 if loc[0] <= maxrange[1]:
 maxrange[1] = loc[1]
 else:
 print "   discontinuity found for", loc
 return None
 return maxrange

It's similar to your solution, but without the range - they're superfluous.

Yours,

Andrei

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Limitation of range() function in Walking problem

2006-09-16 Thread Alan Gauld
> I loop through each list and check if list[0] is in
> range of previous list list[1] AND if it is less or
> greater than. If less, I retain the greater number and
> loop through. ...
> example are small, it worked. in the real world
> example , it did not work, because computer slowed
> down and eventually crashed.  The range(81393417) is
> really big. 

Rather than use 'in' just compare against the limits.

if num in range(min, max):

becomes

if  min < num < max:

The first version has to construct a list size max 
then it has to cycle through the list comparing 
every entry with num. Because its a contiguous 
range you only want to compare with the bottom 
and top, which is much faster.

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor