[Tutor] get/set attributes

2008-05-25 Thread Eric Abrahamsen

Hello,

I'm trying to create a Dummy class of objects, something that will  
return an empty string for any attribute. I'm making a very simple CMS  
with Cherrypy/Sqlalchemy/Cheetah, and I have an HTML form template  
that I use for editing/creating objects to save to the database. I'm  
using just one form template, and I want to either pass an object from  
the database to the template (for editing existing objects), or a  
Dummy object which outputs empty strings for all the HTML form fields,  
creating a blank form. Additionally, I'd like to be able to pass  
keyword arguments to the Dummy() initialization which get turned into  
attributes, so I can output various default values for the form fields.


What I've got so far handles non-existent attributes fine, but using  
keywords to create attributes isn't working (attributes thus set still  
output an empty string), and I'm not sure why. Here's the class:


class Dummy(object):
def __init__(self,**atts):
for k,v in atts.items():
self.k = v

def __repr__(self):
return ''

def __getattr__(self, attr):
return ''

>>> dum = Dummy(name='John')
>>> dum

>>> dum.nationality
''
>>> dum.name
''

__getattr__ says it only comes into play when an attribute is NOT  
found, so I must be doing something wrong in the __init__ method...


Thanks in advance,
Eric
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading only a few specific lines of a file

2008-05-25 Thread Kent Johnson
On Sat, May 24, 2008 at 7:09 PM, Jason Conner <[EMAIL PROTECTED]> wrote:

> def loadItem(self, objectToLoad):
> wordList = []
> fobj = open('/home/jason.conner/Documents/Python/objectconfig.txt', 'r')
>
> for line in fobj:
> if line == objectToLoad:
> wordList.append(line)
> for i in range(5):
> wordList.append(fobj.next())
>
> Though it did take me a few minutes to figure out that I needed to include
> the \n in the objectToLoad variable to get it to work in its present form,
> it works great. Thanks for all your help!

Yes, the lines you get from iterating the file contain the trailing
newlines. A simple fix (which strips *all* whitespace from both ends
of the line) is to say
  line = line.strip()
You can be more restrictive if you want, for example
  line = line.rstrip('\r\n')
will strip only carriage returns and line feeds from the end (right
side) of line.

Note that wordList will also include newlines in its entries. You
might want to use some variation of
  wordList.append(fobj.next().strip())

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


Re: [Tutor] get/set attributes

2008-05-25 Thread Kent Johnson
On Sun, May 25, 2008 at 6:30 AM, Eric Abrahamsen
<[EMAIL PROTECTED]> wrote:

> What I've got so far handles non-existent attributes fine, but using
> keywords to create attributes isn't working (attributes thus set still
> output an empty string), and I'm not sure why. Here's the class:
>
> class Dummy(object):
>def __init__(self,**atts):
>for k,v in atts.items():
>self.k = v

This creates an attribute named k; it does not create an attribute
whose name is the *value* of k. For that you need setattr:
  setattr(self, k, v)

There is a shortcut that replaces the entire loop:
  self.__dict__.update(atts)

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


Re: [Tutor] get/set attributes

2008-05-25 Thread Alan Gauld


"Eric Abrahamsen" <[EMAIL PROTECTED]> wrote

return an empty string for any attribute. I'm making a very simple 
CMS  with Cherrypy/Sqlalchemy/Cheetah,


Those are the components of TurboGears.
You might save a lot of bother by using TG instead of trying to
write all the glue code yourself. Form handling for one would
come for free, as would logins, help pages etc etc.

OTOH maybe you are doing this as a learning exercise in
which case have at it! :-)

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


Re: [Tutor] get/set attributes

2008-05-25 Thread Eric Abrahamsen

This creates an attribute named k; it does not create an attribute
whose name is the *value* of k. For that you need setattr:
 setattr(self, k, v)

There is a shortcut that replaces the entire loop:
 self.__dict__.update(atts)


Thanks Kent, that explains a lot about how things work.


return an empty string for any attribute. I'm making a very simple  
CMS  with Cherrypy/Sqlalchemy/Cheetah,


Those are the components of TurboGears.
You might save a lot of bother by using TG instead of trying to
write all the glue code yourself. Form handling for one would
come for free, as would logins, help pages etc etc.

OTOH maybe you are doing this as a learning exercise in
which case have at it! :-)


I am doing this as a learning exercise, though I hadn't realized those  
are precisely TG's components. I'm actually coming at this from  
Django, and wanted to approach things at a lower level (every time a  
friend asks me to make a website for them I use it as an excuse to  
learn some new framework components). I wasn't really missing Django  
until I got to the 'CMS backend' stage of things – forms for models,  
validation, etc. I'll stick through the learning exercise, but it will  
be *very* nice to return to a full framework package...


Eric





--
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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading only a few specific lines of a file

2008-05-25 Thread Kent Johnson
Forwarding to the list with my reply...

On Sun, May 25, 2008 at 2:20 PM, Chester <[EMAIL PROTECTED]> wrote:
> Python uses  \n  as the newline character (which is a UNIX/Linux newline
> character). I see you have Python for Windows installed and running it on
> the Windows OS. The file objectconfig.txt has the Windows newline character
> \r\n  at the end of each physical line, which confuses the Python parser. I
> think you need to look at the linesep function of the os module; that is,
> use os.linesep (check how to use it) and you should be good. ;)

Umm...no.

\n does double-duty, in a way. It is the linefeed character, and it
represents a newline.

On Unix-like OSes such as Unix, Linux and MacOS X the newline
character is a single linefeed so there is no difference between these
two meanings.

Windows uses two characters - carriage return + line feed - to
represent a newline. When you read a text file in Python on Windows,
the CRLF pairs are converted to a single LF char, or \n. So in this
sense \n does represent a newline in a portable fashion.

Note that this has nothing to do with the Python *parser*, which reads
Python source files. It is the Python runtime that makes the
translation.

One every platform, when you read lines from a file using "for line in
f", f.readline() or f.readlines(), the newline character is included
in the returned line. This is the source of the OP's problem, not any
confusion on the part of the parser or the runtime.

Kent

PS Please use Reply All to reply to the list.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading only a few specific lines of a file

2008-05-25 Thread Chester
I don't know then. Just hack your way through and tell us how did you manage
to do it. ;)


On Sun, May 25, 2008 at 9:32 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> Forwarding to the list with my reply...
>
> On Sun, May 25, 2008 at 2:20 PM, Chester <[EMAIL PROTECTED]> wrote:
> > Python uses  \n  as the newline character (which is a UNIX/Linux newline
> > character). I see you have Python for Windows installed and running it on
> > the Windows OS. The file objectconfig.txt has the Windows newline
> character
> > \r\n  at the end of each physical line, which confuses the Python parser.
> I
> > think you need to look at the linesep function of the os module; that is,
> > use os.linesep (check how to use it) and you should be good. ;)
>
> Umm...no.
>
> \n does double-duty, in a way. It is the linefeed character, and it
> represents a newline.
>
> On Unix-like OSes such as Unix, Linux and MacOS X the newline
> character is a single linefeed so there is no difference between these
> two meanings.
>
> Windows uses two characters - carriage return + line feed - to
> represent a newline. When you read a text file in Python on Windows,
> the CRLF pairs are converted to a single LF char, or \n. So in this
> sense \n does represent a newline in a portable fashion.
>
> Note that this has nothing to do with the Python *parser*, which reads
> Python source files. It is the Python runtime that makes the
> translation.
>
> One every platform, when you read lines from a file using "for line in
> f", f.readline() or f.readlines(), the newline character is included
> in the returned line. This is the source of the OP's problem, not any
> confusion on the part of the parser or the runtime.
>
> Kent
>
> PS Please use Reply All to reply to the list.
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to get a string from a DOM Text node ?

2008-05-25 Thread Zameer Manji
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA512

I'm trying to extract some information from the following xml file:
http://pastebin.ca/1029125

This is the code that I have so far:

import xml.dom.minidom
file = "Music.xml"
doc = xml.dom.minidom.parse(file)

plist = doc.childNodes[1]

dict = plist.childNodes[1] #dict contains App version and stuff, as well
as tracks and playlist.

tdict = dict.childNodes[21] #dict which contains dicts for each track,
those dicts contain the info

print tdict.childNodes[1]
print tdict.childNodes[1].toxml()
print tdict.childNodes[2]

This code prints out the following:


42


The problem is that the Text node that is printed out is blank, when it
should be 42. What do I need to do so I can get tdict.childNodes[2] to
become a string being "42" ?
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)

iQEcBAEBCgAGBQJIOdGWAAoJEA759sZQuQ1B6l8H/2i5FTlsSHPMzlc5ch2/DLiY
0yS1VWNQNuVh40WfK0SVFgsmFxNJ0IFQ+kkJsQPH/mcxQHbP+8iuzKiMuZIpIgUY
Gx/DwfIUzg60W0cCe9gfguvnR/rOXFk/5PuoktWHe9/8bb1BoV+RAFyc9mEWzRzJ
K6+/Rb7ISvQ/ptZ13PNgmX3UkLZU+qIFqlZZ+9rDld4iuyc+toqhdf4nUm7Bemen
XivPf4H/n8OpckYeYKXuKyotacsIZQQraAJevSeiV/EWWrkgHyslktnRwIGVJlmJ
CC8fgKE7DUVrI1PFBa2W48xlTOcpLtg9beB86J5cjNlsbVkSMC+gMiVVAGPooZc=
=yPBg
-END PGP SIGNATURE-


0x50B90D41.asc
Description: application/pgp-keys
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: How to get a string from a DOM Text node ?

2008-05-25 Thread Moishy Gluck
-- Forwarded message --
From: Moishy Gluck <[EMAIL PROTECTED]>
Date: Sun, May 25, 2008 at 5:54 PM
Subject: Re: [Tutor] How to get a string from a DOM Text node ?
To: Zameer Manji <[EMAIL PROTECTED]>


xml does not allow text in a  node. So you kneed to place a  node
inside the  node. Like such

42

xml is also case sensitive. So the T in Text must be capital.

If you do not want to edit your xml file this will get you what you want.

print tdict.childNodes[1].firstChild
print tdict.childNodes[1].firstChild.nodeValue

will output


42

The xml turns white space into a text node. So the whitespace between
"42" and " ... " got parsed into a text node. Which
is "tdict.childNodes[2]".

" ... " Would be the value of "tdict.childNodes[3]".
On Sun, May 25, 2008 at 4:52 PM, Zameer Manji <[EMAIL PROTECTED]> wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA512
>
> I'm trying to extract some information from the following xml file:
> http://pastebin.ca/1029125
>
> This is the code that I have so far:
>
> import xml.dom.minidom
> file = "Music.xml"
> doc = xml.dom.minidom.parse(file)
>
> plist = doc.childNodes[1]
>
> dict = plist.childNodes[1] #dict contains App version and stuff, as well
> as tracks and playlist.
>
> tdict = dict.childNodes[21] #dict which contains dicts for each track,
> those dicts contain the info
>
> print tdict.childNodes[1]
> print tdict.childNodes[1].toxml()
> print tdict.childNodes[2]
>
> This code prints out the following:
>
> 
> 42
> ">
>
> The problem is that the Text node that is printed out is blank, when it
> should be 42. What do I need to do so I can get tdict.childNodes[2] to
> become a string being "42" ?
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.7 (MingW32)
>
> iQEcBAEBCgAGBQJIOdGWAAoJEA759sZQuQ1B6l8H/2i5FTlsSHPMzlc5ch2/DLiY
> 0yS1VWNQNuVh40WfK0SVFgsmFxNJ0IFQ+kkJsQPH/mcxQHbP+8iuzKiMuZIpIgUY
> Gx/DwfIUzg60W0cCe9gfguvnR/rOXFk/5PuoktWHe9/8bb1BoV+RAFyc9mEWzRzJ
> K6+/Rb7ISvQ/ptZ13PNgmX3UkLZU+qIFqlZZ+9rDld4iuyc+toqhdf4nUm7Bemen
> XivPf4H/n8OpckYeYKXuKyotacsIZQQraAJevSeiV/EWWrkgHyslktnRwIGVJlmJ
> CC8fgKE7DUVrI1PFBa2W48xlTOcpLtg9beB86J5cjNlsbVkSMC+gMiVVAGPooZc=
> =yPBg
> -END PGP SIGNATURE-
>
> -BEGIN PGP PUBLIC KEY BLOCK-
> Version: GnuPG v1.4.7 (MingW32)
>
> mQELBEM2+ggBCAC0L91VmTkAiJ20R652OR9kBjeJyirFBiG1/hPOVbMih9qp5VWh
> a+iHzhmn90fl4gQpo4na1MW6mkTfYeD5ZJxKpS5YCZ7hAl54LJACAMcAaYqKgsMM
> DaPZVtM6rKkE5DbKlny8G8OC8vP0cz2pQ7ONkPYTOtBx6PxeMDGmckunRgbbEI7r
> 2Yv0BIP7GbfG59sxa12N+ekvOUFE7lrzsOUyLy+kANgpFBqaPABaP3qvV5NFWIba
> DbU6jGja7cvz7NCnb/sZt7Jlw8ueJW41szN8z58n3E0HXYPqR+6FOexHsimAu9f+
> 7dl0JSxliXvD1JeXyDlZ7SMsbwQjQECREG/lAAYptC5aYW1lZXIgTWFuamkgKE5l
> dyBNYWluIEtleSkg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RuXFqjkitPGZ81NeQpZ6HTYa
> WZvDV2Vr006w0ZFY9/ttGuq43htv8+3zJDid1stX3BNXuYipN9gOy6ilgPlVD3B9
> yRp6m8h7Rc6EoWHqUM418d4PIJmulcUa4c4dAl16N6LsLnVidnhj
> =wMDx
> -END PGP PUBLIC KEY BLOCK-
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get/set attributes

2008-05-25 Thread Alan Gauld

"Eric Abrahamsen" <[EMAIL PROTECTED]> wrote


>> CMS  with Cherrypy/Sqlalchemy/Cheetah,
>
> Those are the components of TurboGears.
I am doing this as a learning exercise, though I hadn't realized 
those  are precisely TG's components.


TG tries to use a set of "best practice" components and glue
them together into a framework.

It started off using Kid/CherryPy/SQLObject  then acquired
Alchemy as an alternative ORM and has now adopted Cheeetah
for templating so you can mix n' match by setting  your config
options to select your favourite components, but the upper level
framework stuff works fairly transparently - obviously the templating
syntax depends on the engine you use and the ORM layer is
restricted to whatever features your chosen mapper supports but
the translation to python code is pretty seamless and portable.

I like the concept of TG but there are a few niggles with it. I'm
just learning Django at the moment so can't really make any
proper comparisons yet.

Alan G.



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


Re: [Tutor] How to get a string from a DOM Text node ?

2008-05-25 Thread Alan Gauld


"Zameer Manji" <[EMAIL PROTECTED]> wrote in


I'm trying to extract some information from the following xml file:
http://pastebin.ca/1029125

This is the code that I have so far:

import xml.dom.minidom



This code prints out the following:


42


The problem is that the Text node that is printed out is blank, when 
it
should be 42. What do I need to do so I can get tdict.childNodes[2] 
to


I may be off base here; I only used DOM once. I usually use
ElementTree for parsing XML(and only rarely do that!). But isn't there
a data or content field on the tag object that you have to use?

Just a vague memory and too late/lazy to look it up!

It might be a clue till somebody who really knows minidom replies!

Alan G. 



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


[Tutor] What lib should I use?

2008-05-25 Thread Julia
Hi!

I need to write a program what can do two things:

1) get data from the website
2) send information from a textfield (e.g. like a google search)

Any tips on what lib I should use and any good tutorial covering it?

Thanks!

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


Re: [Tutor] How to get a string from a DOM Text node ?

2008-05-25 Thread Martin Walsh
Zameer Manji wrote:
> I'm trying to extract some information from the following xml file:
> http://pastebin.ca/1029125
> 

> file = "Music.xml"

> dict = plist.childNodes[1] #dict contains App version and stuff, as well

You really should try to avoid rebinding built-in names, like dict and
file, this is just asking for trouble.

> The problem is that the Text node that is printed out is blank, when it
> should be 42. What do I need to do so I can get tdict.childNodes[2] to
> become a string being "42" ?

I have not used minidom at all really, but here is an example using
BeautifulSoup, perhaps you'll find it helpful:

from BeautifulSoup import BeautifulStoneSoup

class MusicSoup(BeautifulStoneSoup):
NESTABLE_TAGS = {
'dict': [], 'array': []
}


bsoup = MusicSoup(file('Music.xml'))
print bsoup.dict.dict.key.string
# 42
print bsoup.dict.dict.key.findNextSibling('key').string
# 183

... or ...

tracks = bsoup.dict.dict.findChildren('key', recursive=False)
print tracks

for track in tracks:
print track
details = track.findNextSibling('dict')


HTH,
Marty


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


Re: [Tutor] What lib should I use?

2008-05-25 Thread Alan Gauld


"Julia" <[EMAIL PROTECTED]> wrote


I need to write a program what can do two things:

1) get data from the website
2) send information from a textfield (e.g. like a google search)


There are lots of web frameworks for Python that you could use.

The most basic is the standard cgi module in the standard library.
Moving up frfrom there something like CherryPy is a good bet. 
But for what you describe vanilla cgi is fine. Read the docs and 
topic guide on the Python web site.


http://wiki.python.org/moin/CgiScripts

HTH,

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


Re: [Tutor] What lib should I use?

2008-05-25 Thread python nutter
On Mon, 26 May 2008 01:51:45 +0200, "Julia" <[EMAIL PROTECTED]> said:
> Hi!
>
> I need to write a program what can do two things:
>
> 1) get data from the website
> 2) send information from a textfield (e.g. like a google search)

Sounds like you are web scraping or wanting to make a web spider.

Check out
a) urllib
b) urllib2
c) beautifulsoup

a + b are built-ins. c you have to download and install from
http://www.crummy.com/software/BeautifulSoup/

Cheers,
Python Nutter
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What lib should I use?

2008-05-25 Thread bob gailer

Julia wrote:


I need to write a program what can do two things:

1) get data from the website
2) send information from a textfield (e.g. like a google search)


I presume you want to automatically submit a form?

For both see

http://docs.python.org/lib/node577.html 

Forms are submitted by either GET or POST. Examine the HTML of the form 
to see which.


--
Bob Gailer
919-636-4239 Chapel Hill, NC

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


Re: [Tutor] What lib should I use?

2008-05-25 Thread W W
However, something to note is that Google prohibits web scraping, at
least via urllib.

If you try to GET or POST (I can't remember off the top of my head),
Google will not return the actual search.

HTH,
Wayne

On Sun, May 25, 2008 at 8:06 PM, bob gailer <[EMAIL PROTECTED]> wrote:
> Julia wrote:
>>
>> I need to write a program what can do two things:
>>
>> 1) get data from the website
>> 2) send information from a textfield (e.g. like a google search)
>
> I presume you want to automatically submit a form?
>
> For both see
>
> http://docs.python.org/lib/node577.html
> Forms are submitted by either GET or POST. Examine the HTML of the form to
> see which.
>
> --
> Bob Gailer
> 919-636-4239 Chapel Hill, NC
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor