[Tutor] CGI script to create an XML document from an HTML form

2005-03-22 Thread Gabriel Farrell
Hi,

I'm just starting to get my feet wet with Python.  I'm trying to write
a CGI script to create an XML document using input from a web form.
The created document would be a MODS record[1], so I already have a
Schema for it.  I think I could make it work if I just feed the input
into a long string as variables, then write that out to a new
document, but I'd like to start investigating some of Python's XML
modules because that's a sexier solution and I'll probably be doing
more work in XML in the future.

I've been looking around at various resources such as the Python/XML
Howto[2], some of the articles by Uche Ogbuji[3], and elsewhere, and
frankly I'm a little overwhelmed by the many seemingly overlapping
methods.  Which one would the wise tutors recommend for my situation?

gabe

[1] http://www.loc.gov/standards/mods/
[2] http://pyxml.sourceforge.net/topics/howto/xml-howto.html
[3] http://www.xml.com/pub/au/84
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining functions

2005-03-25 Thread Gabriel Farrell
So, as a newbie, I see this thread and I check out the PEP and I see
that for future compatibility we should use sys.stdin.readline().  So
I import sys to see how it works.  Of course, sys.stdin.readline('type
anything: ') doesn't work in quite the same way as raw_input('type
anything: ') does.  The closest I can get after a few newbie stabs is:

>>>print 'type anything: ', sys.stdin.readline()
type anything: hello
 hello

>>>

What is the easiest way to get the exact functionality of raw_input()
(i.e. a prompt, no whitespace at the front, and no trailing \n) using
sys.stdin.readline()?

gabe


On Fri, Mar 25, 2005 at 11:02:43AM -0500, Jacob S. wrote:
> Yeah. And they're thinking of removing raw_input() too.  I think it's good 
> to have a __builtin__ user input function.  Why should we have to import 
> sys everytime we want user input? Almost every program that newbies write 
> uses it, and advanced programmers also if they're using console programs.  
> IMHO, I see no reason to remove it.
> ## end rant
> 
> Jacob
> 
> 
> >Michael Dunn wrote:
> >>Something I've always wondered: if input() is so dangerous, why is it
> >>there? What valid uses does it have in the wild?
> >
> >It's a mistake planned to be removed in Python 3.0, the "hypothetical 
> >future release of Python that can break backwards compatibility with the 
> >existing body of Python code."
> >
> >Python tries very hard to maintain backward compatibility so things like 
> >input() are not removed.
> >
> >http://www.python.org/peps/pep-3000.html#built-ins
> >
> >Kent
> >
> >___
> >Tutor maillist  -  Tutor@python.org
> >http://mail.python.org/mailman/listinfo/tutor
> >
> >
> 
> ___
> 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] a shorter way to write this

2005-03-25 Thread Gabriel Farrell
how about

manyones = [ 1 for x in range(96) ]


On Sat, Mar 26, 2005 at 03:02:34AM +0800, jrlen balane wrote:
> basically, i'm going to create a list with 96 members but with only one value:
> 
> list1[1,1,1,1...,1]
> 
> is there a shorter way to write this one???
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sys.path.append issues with cgi

2005-04-14 Thread Gabriel Farrell
Hello all,

I'm trying to modify the sys.path in my cgi scripts to import modules
that I've installed in my home directory.  The top of the script reads
as follows:


#!/usr/local/bin/python

import cgi, sys
sys.path.append('/net/u/16/g/gsf/lib/python2.4/site-packages')
from ElementTree import Element, SubElement, tostring


But my traceback reads:

ImportError: No module named ElementTree


The appended path is the same I've added to my PYTHONPATH variable and
it works fine from the python interactive prompt.  I thought it might
be a permissions issue since it's a cgi script so I chmodded
everything up to the ElementTree directory 755 but still no luck.

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


Re: [Tutor] sys.path.append issues with cgi

2005-04-14 Thread Gabriel Farrell
Sorry, addendum to that post.  The line 

from ElementTree import Element, SubElement, tostring

should read 

from elementtree.ElementTree import Element, SubElement, tostring

but that still doesn't work.


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


Re: [Tutor] sys.path.append issues with cgi

2005-04-18 Thread Gabriel Farrell
A follow-up to this post for future reference:

It appears that on my web host the cgi scripts run on a web server
that doesn't have access to the same python and python libs that are
available when I ssh in to my shell account.  So it doesn't matter if
I change the sys.path, because the libs are simply not accessible.
This situation is particular to my web hosting service, so it may not
apply to others who are running python cgi scripts and importing
modules.

gabe


On Thu, Apr 14, 2005 at 06:43:26PM -0400, Gabriel Farrell wrote:
> Hello all,
> 
> I'm trying to modify the sys.path in my cgi scripts to import modules
> that I've installed in my home directory.  The top of the script reads
> as follows:
> 
> 
> #!/usr/local/bin/python
> 
> import cgi, sys
> sys.path.append('/net/u/16/g/gsf/lib/python2.4/site-packages')
> from ElementTree import Element, SubElement, tostring
> 
> 
> But my traceback reads:
> 
> ImportError: No module named ElementTree
> 
> 
> The appended path is the same I've added to my PYTHONPATH variable and
> it works fine from the python interactive prompt.  I thought it might
> be a permissions issue since it's a cgi script so I chmodded
> everything up to the ElementTree directory 755 but still no luck.
> 
> TIA,
> gabe
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] making a cgi search safe(r)

2005-05-04 Thread Gabriel Farrell
Greetings,

I'm setting up a search for an online catalog.  It works right now by
passing the cgi search query to the command-line swish-e via os.popen.
I'll do it via a direct interface as soon as I figure out how to do
that with swig or the swishe module.  In the meantime, I'm trying to
sanitize the query so it's safer on the shell.  The swish-e
documentation says it's better to include only the characters that you
want rather than knock out the known offenders.  It seems like there
should be a simple way with string methods or the re module.  Right
now I've got:

def getquery():
kosher = re.compile(r'\w|"|\'|[ ]')
sanitized = ''.join(kosher.findall(form['query'].value))


Is this about the best way to do this?

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


Re: [Tutor] XML to Python

2005-05-06 Thread Gabriel Farrell
On Thu, 5 May 2005, Smith, Jeff wrote:
> I'm able to use the built in XML parser to effect "normal" XML parsing
> usage but frequently, I'm not doing anything to complicated and would
> simply like to translate the XML file into a more "Pythonic" structure.  
> What's the best way to do this?  Something from the standard libraries
> would be preferable.

I just went through the same process of trying to find a quick and
easy way to parse XML in Python, and ended up choosing ElementTree.
Out of everything I tried (including Amara and minidom), ElementTree
seemed to be the one that made the most intuitive sense.  

The only downside is that I don't like the way ElementTree handles
namespaces.  My quick and dirty solution was to strip the default
namespace out with re before parsing the file with ElementTree, then
put it back in again after the altered document was written out.  I
would rather the default namespace be ignored than an ns0 or ns1 added
in front of every element.

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


Re: [Tutor] output of dictionaries

2005-05-10 Thread Gabriel Farrell
take a look at pickle and shelve.  info about both can be found at
http://docs.python.org/lib/python.html

On Tue, May 10, 2005 at 08:46:50AM -0700, Tom Cloyd wrote:
> I'm creating a small database using a dictionary of dictionaries, and I  
> want to output it to a file. It seems that only strings can be output to  
> files, and I cannot quite figure out how to quickly and simply convert my  
> dictionary to a list of strings or whatever. Or maybe I'm going about this  
> all wrong.
> 
> So, the most general form of my question: how can I store a dictionary  
> containing some data records stored as dictionaries - a dictionary of  
> dictionaries - in a file, so I can read it back in later and process its  
> contents?
> 
> Thank you very much for your generosity in giving some of your time to  
> this service. I'm very grateful.
> 
> -- t.
> 
> ==
> Tom Cloyd
> Bellingham, Washington, U.S.A: (360) 920-1226
> << BestMindHealth.com >>
> ==
> 
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
> 
> ___
> 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] Python versions and Debian?

2005-05-11 Thread Gabriel Farrell
Hi William,

The short answer is go with the default until you run into something
you can't do without 2.4.

For the skinny on all the changes you'll want to check out
http://python.org/doc/2.4/whatsnew/whatsnew24.html .  That document
lays it out about as well as can be done.  If you find something in
there that really makes a difference for you, then go to 2.4.  

I've only installed the default python (2.3 for now) on my Debian box
because the changes don't really affect my programs.

There is a short, not-too-informative chat about Python 2.4 and Debian
that starts at
http://lists.debian.org/debian-python/2005/04/msg5.html .

gabe


On Wed, May 11, 2005 at 04:10:37PM -0400, William O'Higgins wrote:
> I am just beginning to experiment with Python, and I'm looking for
> advice about versions.  Debian testing provides version 2.3 by default,
> (/usr/bin/python is linked to /usr/bin/python2.3) but I am able to 
> install 2.4 as well.  What gains do I realize by using the more modern 
> version?  Are there gotchas based on that minor-version change?  Your
> insight is appreciated, thanks.
> -- 
> 
> yours,
> 
> William
> 



> ___
> 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] Building an SQL query

2005-06-03 Thread Gabriel Farrell
On Thu, Jun 02, 2005 at 10:41:20PM +0100, Alan G wrote:
> Why not convert the list to a tuple before applying str():
> 
> str(tuple(ids_to_process))

I'm just getting started with Python and PostgreSQL but I found that
str(tuple(valueList)) wouldn't work for me because I had a few values
with apostrophes.  PostgreSQL needed 'Lion''s Mane' but Python was
sending it "Lion's Mane", so I ended up writing this little function:

def sqlNice(valueList):
count = 1
y = '('
for x in valueList:
x = x.replace("'", "''")
y = y + "'" + x + "'"
if count < len(valueList):
y = y + ', '
count = count + 1
y = y + ')'
y = y.replace("'NULL'", 'NULL')
return y

Does anyone see any major stumbling blocks in that?  

On a side note, I've struggled with PyGreSQL.  At first I was using
the pg module, but I switched to pgdb when insert() wasn't working for
me and I thought I would have less trouble using something that's
DB-API compliant.  There seemed to be more documentation there, and I
figured it's a good idea to go with the standard.  However, it does
seem like I'm covering ground I'm sure someone else has already
crossed when I create these re functions to manipulate queries.  For
inserts, at least, it seems a Python dictionary should be able to do
the job nicely.

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


Re: [Tutor] Building an SQL query (Gabriel Farrell)

2005-06-03 Thread Gabriel Farrell
On Fri, Jun 03, 2005 at 03:50:09PM -0400, Lloyd Kvam wrote:
> The code to update the database should look something like:
> the_cursor.execute( sql_cmd, data)
> 
In PyGreSQL/pgdb it's cursor.execute(query[, params]) but it means
more or less the same thing because pgdb's paramstyle (I knew from the
DB-API[1] to look in help(pgdb) for "paramstyle") is "pyformat".  I
googled that and found some explanation of pyformat in a message[2] on
the DB-SIG mailing list.  To quickly summarize that message, pyformat
means the string fed to cursor.execute() should follow all the usual
rules of Python string formatting.

Knowing this, I can now execute my query thusly:

>>> import pgdb
>>> db = pgdb.connect(database='asdc')
>>> cursor = db.cursor()
>>> data = {
... 'noteType': None,
... 'note': "Lion's Mane",
... 'recordIdentifier': 'gsf136'
... }
>>> cursor.execute("INSERT INTO notes (notetype, note, recordidentifier) \
... VALUES (%(noteType)s, %(note)s, %(recordIdentifier)s)", data)
>>> db.commit()

Note that the re matching I had to do before is now taken care of by
pgdb (in the _query() function Danny Yoo was kind enough to track
down).  Before the query gets to PostgreSQL, the None value turns into
a NULL and "Lion's Mane" transforms into 'Lion''s Mane'.  No re
incantations necessary!

gabe

[1] http://www.python.org/peps/pep-0249.html
[2] http://aspn.activestate.com/ASPN/Mail/Message/db-sig/1632007
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with pgbd & datetime

2005-07-27 Thread Gabriel Farrell
Are you using PyGreSQL?  import pgdb works fine for me with PyGreSQL.
I'm on Debian but I assume there's a package for it for SUSE.  

gsf


On Wed, Jul 27, 2005 at 10:13:22AM -0400, Don Parris wrote:
> O.k., I'm running SUSE Linux 9.2, Python 2.3.4, 
> 
> I have changed my DB back-end from MySQL to Postgres for the larger
> feature set.  However, in attempting to load the pgdb module for use
> in my script, I got this message (same when I try it at the
> command-line):
> 
> >>> pgdb.connect('localhost:chaddb_a_test:donp:d1o1l2l4')
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'pgdb' is not defined
> >>> import pgdb
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "/usr/lib/python2.3/site-packages/pgdb.py", line 65, in ?
> except ImportError: import DateTime
> ImportError: No module named DateTime
> 
> 
> In the pgdb module, I found this (which matches with the traceback):
> # Marc-Andre is changing where DateTime goes.  This handles it either way.
> try: from mx import DateTime
> except ImportError: import DateTime
> 
> Do I just need to modify pgdb.py to import datetime instead of
> DateTime?  Or is there something more to this?  I could spend a fair
> amount of time searching the archives.
> 
> Don
> -- 
> DC Parris GNU Evangelist
> http://matheteuo.org/
> [EMAIL PROTECTED]
> "Hey man, whatever pickles your list!"
> ___
> 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] anything to do???

2005-07-27 Thread Gabriel Farrell
I assume you have checked out
http://wiki.python.org/moin/VolunteerOpportunities .

I've improved a lot by just reading this list and following along with
the successes and failures (always temporary, I hope) of others.

gsf


On Wed, Jul 27, 2005 at 10:20:43PM +0500, Mustafa wrote:
> i hav just finished learning pythob from "A byte of python"(an online 
> book) so i wanted to apply my new skills. to learn and to have some fun.
> is there any place which lists jobs to be done...you know minor jobs and 
> requests thats nobody has found time to do.
> i would point out that i am not looking for a JOB as job with payment.
> i am looking for a JOB that is to be done and nobody has done it yet.
> 
> also aside from this is there any other way i could use python and 
> improve. i figure this practice will make me a lot better. but if anybody
> else has any ideas than that would be good too .
> ___
> 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] Problem with pgbd & datetime

2005-07-28 Thread Gabriel Farrell
On Wed, Jul 27, 2005 at 07:13:49PM -0400, Don Parris wrote:
> pg/pgdb are both part of the PyGreSQL distribution.  You can choose
> whichever you want to use, but pgdb supports the 2.0 API, whereas
> (according to the readme) pg does not.  I will try pg when I get back
> to my box tonight, but would prefer to use pgdb.  

I use pgdb as well, for the same reason.  

> All of this should have been configured properly when I installed
> SUSE Linux 9.2 last Fall.  The MySQLdb  module works fine - the pgdb
> should as well.  Perhaps I should post this over on the DB Sig list.

Debian lists a package called python-egenix-mxdatetime as a dependency
for python-pygresql, and that in turn depends on
python-egenix-mxtools.  I believe you need to install these modules
for pygresql to work, as mxDateTime is not the same as datetime.  The
site for mxDateTime is
http://www.egenix.com/files/python/mxDateTime.html if you need to
install it manually, but there should be a SUSE package for it.
*Quick look through SUSE site.*  Yep, here it is:
http://www.novell.com/products/linuxpackages/professional/python-egenix-mx-base.html

I hope that gets it working.

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


Re: [Tutor] Python Editors (particualrly Vim)

2005-09-21 Thread Gabriel Farrell
Uh oh, looks like you're begging for an editor war. 

That said, I'm in the vim camp.  It can do everything you specified
for all of the languages you mention (well, I'm not sure about
collapsible code...web search...aha![1]).  After using it for four
years, I'm still learning new tricks (see, for example, this page I
found today on indentation[2]).

vim's extendable with python scripts, but a lot of what you need for
coding is already built in.  I find most of my info either in the help
manual that comes with it (:h) or at vim.org.

gsf

[1] http://www.dgp.toronto.edu/~mjmcguff/learn/vim/folding.txt
[2] http://www.vim.org/tips/tip.php?tip_id=83


On Wed, Sep 21, 2005 at 02:00:20PM +0100, Ed Singleton wrote:
> I've been trying to decide which editor to use to edit Python (on
> Windows mostly).
> 
> My wishlist of features would be:
> 
> - automatic code formatting (indentation etc)
> - collapsible code (to collapse def's etc)
> - automatic code coloring (that's easily changeable)
> - auto-completion of namespaces
> - easy to run scripts from the editor (a shortcut key to run them or 
> something)
> 
> As I also do a lot of html, css and javascript it would be cool to
> have an editor that could also handle them, in which case I would want
> the same features for those languages, as well as the ability to write
> macros, scripts, etc in python.
> 
> Having looked at loads of editors, I've ended up looking at emacs and vim.
> 
> Emacs seems too difficult with not enough support for using the mouse.
> 
> Vim seemed better, and I get the impression that it is possible to use
> python to script the editor, but I can't find much information on
> using vim as a python editor on windows.
> 
> My various questions are:
> 
> What other features should I be looking for?
> 
> What would be the best editor for a beginner to start using, with a
> view to the long term?
> 
> Where can I find some authoritative information about setting emacs or
> vim up as a fully featured python editor?
> 
> Thanks
> 
> Ed
> ___
> 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] Linking with C programs

2005-09-27 Thread Gabriel Farrell
You can read some of what Guido has to say about Python and C at
http://python.org/doc/essays/omg-darpa-mcc-position.html
and see SWIG documentation (a few pertain to Python) at
http://www.swig.org/doc.html

gsf


On Tue, Sep 27, 2005 at 08:41:31AM -0600, Eric Walker wrote:
> There is a program called swig. this generates python interfaces to external 
> C/C++ libraries. I never used it as I am a very new python programmer but I 
> took the Mark Lutz class and he pushes it.
> 
> Python Newbie
> 
> 
> On Tuesday 27 September 2005 07:49 am, Matt Williams wrote:
> > Dear List,
> >
> > Could someone explain how, in very general terms, one would use python
> > to wrap some C libraries/ API.
> >
> > I ask because there are a few bits of C software that look quite
> > interesting, and I know that Python can be used to wrap the C - but how
> > does it work?
> >
> > Thanks,
> >
> > Matt
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> 
> -- 
> Eric Walker
> EDA/CAD Engineer
> ___
> 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] greetings...

2005-10-21 Thread Gabriel Farrell
Welcome to the list, Carl.  Feel free to browse the list archive at
http://mail.python.org/pipermail/tutor/ or
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor to
get some idea of the kinds of questions asked and answers given.
You'll find a lot of suggestions for beginners among the threads there
as well.

There's also http://wiki.python.org/moin/BeginnersGuide, which is
always a good place to start.

gsf


On Thu, Oct 20, 2005 at 05:09:07PM -0400, [EMAIL PROTECTED] wrote:
> Greetings to list,
> Tomorrow is my last day in class with Lutz. I am not only new to Python but
> new to programming in general, this is my first language. Looking forward to
> your help in the days and weeks to come.
> Any suggestions for self tutorial, whether on python.org
> or not, would be greatly appreciated. I am going to
> finish his Learning...
> book first then hack open some open code for a simple project in order to
> see how it works.
> peace
> Carl

> ___
> 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] file permissions

2005-12-07 Thread Gabriel Farrell
On Wed, Dec 07, 2005 at 11:52:20AM -0800, Danny Yoo wrote:
> On Wed, 7 Dec 2005, Eric Walker wrote:
> 
> > Looking for easiest way to get a files permissions in linux.
> 
> Hi Eric,
> 
> What have you looked at so far?

I agree with Danny, the question lacks any evidence of prior
investigation on the part of the poster, and that makes it a lousy
candidate for the helpful guidance of the python tutor list.  The
documentation on this aspect of python, however, is pretty sparse, and
the answer's not exactly intuitive, so I thought I'd mention the
effbot's page about the os module as a good place to look, in addition
to the documentation Danny listed.

http://effbot.org/librarybook/os.htm

What he's written there should be able to answer your question.

If I may hijack the thread, does anyone more knowledgeable than me
know why os.stat and stat are so low-level and esoteric?  Am I crazy
for wanting to replace

oct(stat.S_IMODE(os.stat(thefile)[stat.ST_MODE]))

with, say,

stat.getmode(os.stat(thefile))

or even

os.getmode(thefile)

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


Re: [Tutor] file permissions

2005-12-07 Thread Gabriel Farrell
On Wed, Dec 07, 2005 at 02:51:55PM -0700, Eric Walker wrote:
> After 4+ hours 
> of playing around with this I made the futile post.

Don't let the post be futile!  Post what you've tried so far and
you'll get help.

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


Re: [Tutor] ElementTree in Python 2.5!

2005-12-16 Thread Gabriel Farrell
This is great news.  And the thread on comp.lang.python is awesome.
The eff-bot and the martelli-bot and everyone's just talking about how
great it would be to have it in the core, and, then, it just ...
happens.  Wow!

gsf


On Wed, Dec 14, 2005 at 03:20:38PM -0500, Kent Johnson wrote:
> By some miracle of the gods smiling and the planets aligning, a
> comp.lang.python thread that started with the question "ElementTree -
> Why not part of the core?" has actually resulted in ElementTree
> *becoming* part of the core for Python 2.5! Pretty cool! So the core
> Python distribution will finally have a Pythonic XML processor.
> 
> Kent
> 
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/e095cc79d1efb99/a4523a6e9b7061af?rnum=1#a4523a6e9b7061af
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] tempfile and passing files around

2006-07-11 Thread Gabriel Farrell
I have an issue with the tempfile module that I can't figure out.

Snippet of relevant script looks like so:

1   bob = tempfile.NamedTemporaryFile()
2   bob.write('Hallo!')
3   bob.read()
4   sam = tempfile.NamedTemporaryFile()
5   bobHandle = file(bob.name)
6   bobHandle.read()

As you can see, I'm creating two NamedTemporaryFiles and then reading
from the first.  Line 3 doesn't really do anything, there's no output
because we're reading at the end of the file, but as long as I leave
it in, line 6 will output the expected 'Hallo!'.  If I remove it,
there's no output from line 6.

I really stumbled upon this, and it looks like, if you create a
tempfile and write to it, then create another tempfile, you can only
open up the first to read it again if you do a read method on the
first file before you open it.  I dunno, just kinda baffled.

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


Re: [Tutor] tempfile and passing files around

2006-07-11 Thread Gabriel Farrell
On Tue, Jul 11, 2006 at 03:04:02PM -0400, Kent Johnson wrote:
> Try bob.flush() instead of bob.read(), my guess is the read() is forcing 
> a flush().

That works!  Thanks, Kent.  If I understand flush (looking at [1]), I
got no output because the data for the stream wasn't written to the
file before I tried to read it.  Is that right?

[1] http://www.opengroup.org/pubs/online/7908799/xsh/fflush.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] piping to system commands

2006-07-13 Thread Gabriel Farrell
I'm trying to create a master dictionary in aspell, and I can do so
with the following:

inFile = tempfile.NamedTemporaryFile()
inFile.write(wordList)
inFile.flush()
command = 'aspell --lang=en --dont-validate-words create master %s < %s' % \
('./dict.cwl', inFile.name)
os.system(command)

or, in 2.4, instead of os.system(command):

retcode = subprocess.call(command, shell = True)

I'm wondering, is there a way to do this more directly, without the
need for a temp file, and in a way where I the stdout coming from
aspell won't go to sys.stdout?  The following seems to work, but the
end of the input gets cut off.

command = 'aspell --lang=en --dont-validate-words create master ./dict.cwl'
pipe = subprocess.Popen(command, stdin = subprocess.PIPE, shell=True).stdin
pipe.write()
pipe.flush()
pipe.close()

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


[Tutor] cartesian product recursive loop

2006-07-18 Thread Gabriel Farrell
In trying to combine a set of lists together, I landed upon
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 , which
does the job beautifully.  The problem is I really want to understand
how it does it, and I can't quite get my brain wrapped around it.
I'll try to explain, step by step, what I think it's doing, and if
someone would be so kind as to try to show me where I'm missing
something, I'd be grateful.

Okay, so here's the function: 

1   def combine(*seqin):
2   def rloop(seqin, listout, comb):
3   if seqin:
4   for item in seqin[0]:
5   newcomb = comb + [item]
6   rloop(seqin[1:], listout, newcomb)
7   else:
8   listout.append(comb)
9   listout = []
10  rloop(seqin, listout, [])
11  return listout

We'll start with the example Mr. Klaffenbach gives in the
documentation on the function: combine((1, 2), (3, 4)) .  Line 10 is
the first thing to act upon the list of seqin, looking like this:

rloop(((1, 2), (3, 4)), [], [])

So we go into the rloop function.  There is something left in the
seqin, so we pass the if statement and hit the for loop.  seqin[0] ==
(1, 2), and comb == [], so newcomb == [1], the first item, and we're
sent back into the rloop with the following new setup:

rloop(((3, 4)), [], [1])

This time around we pass the if statement again and the first item in
(3, 4) is 3.  comb == [1], so newcomb == [1, 3].  We get sent back to
the top of the rloop with the following new setup:

rloop((), [], [1, 3])

Now we fail the if statement, so comb is appended to listout and
listout == [[1, 3]], which is the first combination to be returned.

Great.  But, as far as I can see, the whole function should stop right
here.  How does it produce the rest of the combinations?  I suspect
I'm misunderstanding something about the for loop, or the way seqin is
handed into the rloop function.  Any help is much appreciated.

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


Re: [Tutor] cartesian product recursive loop

2006-07-18 Thread Gabriel Farrell
On Tue, Jul 18, 2006 at 01:33:27PM -0400, Kent Johnson wrote:
> Each call to rloop() has its own local variables and its own state.
> Returning from one call doesn't pop all the way up the stack, it
> resumes execution at the point of call with the local state restored
> to what it was before the call.

Okay, this was the main abstraction I was missing, and you've spelled
it out clearly.  It seems like such an "intelligent" way to handle the
flow -- I think that's what threw me off.

> It might help to put in some print statements to help you track the 
> flow, or run the program in a debugger and step through it.

I *think* I get it.  I read
http://www.ferg.org/papers/debugging_in_python.html , imported pdb,
and inserted pdb.set_trace() after

def rloop(seqin, listout, comb):

I'm seeing now, by stepping through the program, how it flows.  Pdb is
pretty awesome!  I had a feeling there was something like different
levels of loops going on, but I didn't get it before.  

Thanks, Kent!

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


Re: [Tutor] lists and recursion

2006-07-22 Thread Gabriel Farrell
On Sat, Jul 22, 2006 at 06:20:12AM -0400, Kent Johnson wrote:
> We just had a discussion of recursion, you might want to look at it - 
> though the code is a bit more complex than this example.
> http://mail.python.org/pipermail/tutor/2006-July/048081.html

Glad to see I'm not the only one to get tripped up by stack frames.  I
guess that's what happens to us non-CS majors who get into Python.
After I got Kent's explanation in the discussion he refers to above, I
did a little research.  The best shortish description of the call
stack is, not surprisingly, at Wikipedia:
http://en.wikipedia.org/wiki/Call_stack

I understood things a bit better once I took the time to digest that
article.

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


Re: [Tutor] What after Learning Python 2nd

2006-10-09 Thread Gabriel Farrell
On Mon, Oct 09, 2006 at 10:51:26AM -0700, josip wrote:
> Hi.
>
>   Hi I have finished Learning Python 2nd ed.
>   What to do next? 
>   Should I go with Programming Python book or Python Cookbook or?
>   Or maybe something else?
>
>   Thanks people!
>

I went from Learning Python to the Python Cookbook and have been very
happy.  The Cookbook is loaded with great tips in pretty much every
area of programming, and it's great to get so many insights.
I've also been pleasantly surprised by the usefulness of the intros to
each section, written by some of the superstars of the Python
community.

gabe

> 
>   
> -
> Do you Yahoo!?
>  Get on board. You're invited to try the new Yahoo! Mail.
> ___
> 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] XML parsing in Python.. resources needed

2006-10-27 Thread Gabriel Farrell
On Fri, Oct 27, 2006 at 11:24:00AM +0100, Asrarahmed Kadri wrote:
> Hi Folks,
> 
> ANy comments about ELementTree module...
> 
> I have heard that it is more 'Pythonic'...
> 
> I need some basic examples to start learning the stuff.. If anyone has got
> the information, please pass it along..
> 

ElementTree is great, and will be in Python2.5, so it's good to know.
Go through the documentation at http://effbot.org/zone/element.htm, as
Kent mentioned, and return to the list with specific questions.  

I recommend you also take a look at Amara[1], which I enjoy using
because of it's clear API and clean output.

gabe

[1] http://uche.ogbuji.net/tech/4suite/amara/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Elementtree ...how to access the attributes..

2006-11-09 Thread Gabriel Farrell
On Thu, Nov 09, 2006 at 06:23:50PM +, Asrarahmed Kadri wrote:
> Hi Folks,
> 
> 
> I am trying to parse XML documents using elementtree.
> I have just started with it. Can somebody help me with how to select nodes
> with a particular atribute set to some value. For example, I have the
> following file. Now I want to access the founder element of the company
> whose attribute is set to 'ndtv'. Can somebody help me with this?
> 

with elementtree:

from elementtree import ElementTree as et

tree = et.parse('temp.xml')
company_list = tree.findall('company')
for company in company_list:
if company.attrib['name'] == 'ndtv':
for founder in company.findall('founder'):
for child in founder.getchildren():
print child.tag, child.text

with lxml (note the difference a full xpath implementation makes):

from lxml import etree

tree = etree.parse('temp.xml')
ndtv_founder_list = tree.xpath('[EMAIL PROTECTED]"ndtv"]/founder')
for founder in ndtv_founder_list:
for child in founder.getchildren():
print child.tag, child.text

I'll leave the modifying of tags and the writing of new xml files up
to you.  A note on your sample.xml: why is it


  larry page
  sergey brin


when it could just be

larry page
sergey brin

?  That would make it easier to parse the text out.  (Probably easier
to produce as well.)

gabe


> sample.xml
> 
> 
> 
> 
>   larry page 
>  sergey brin
> 
>  search engine 
> 
> 
> 
> 
>  Pranoy Roy 
>  Radhika Roy
> 
>  news 
> 
> 
> 
>  Azeem Premje 
>  IT services 
> 
> 
> 
> -- 
> To HIM you shall return.

> ___
> 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] Help with Elementtree ...how to access the attributes..

2006-11-09 Thread Gabriel Farrell
On Fri, Nov 10, 2006 at 10:23:49AM +1300, John Fouhy wrote:
> On 10/11/06, Asrarahmed Kadri <[EMAIL PROTECTED]> wrote:
> > I am trying to parse XML documents using elementtree.
> > I have just started with it. Can somebody help me with how to select nodes
> > with a particular atribute set to some value. For example, I have the
> > following file. Now I want to access the founder element of the company
> > whose attribute is set to 'ndtv'. Can somebody help me with this?
> 
> Here's some code to get you started:
> 
> >>> from elementtree import ElementTree
> >>> tree = ElementTree.parse('sample.xml')
> 
> # Make a list of all  elements.
> >>> companies = tree.findall('company')
> 
> # Make a dict, mapping the name attrib of  elements to the
> elements themselves.
> >>> companiesByName = dict((c.get('name'), c) for c in companies)
> 
> # Access the founder element in the ndtv company.
> >>> ndtvFounder = companiesByName['ndtv'].find('founder')
> 

Oops, I guess John's email and mine zipped by each other on the wires.
I like his use of a dictionary to simplify things.  Funny that we had
nearly the same comment about the  element!

> I notice that sometimes, your  element contains text, and
> sometimes it contains other elements ( and ).  This will
> make extracting founder information a bit fiddly.. You might want to
> consider changing the structure -- perhaps:
> 
> 
>  
>   Bill Gates
>   Steve Jobs
>   Larry Wall
>  
> 
> 
> That way, you could do:
> 
> >>> founderElements = companiesByName['ndtv'].findall('founder')
> >>> founders = [f.text for f in founderElements]
> 
> and that would give you the founder names in a list, regardless of
> whether you had one or many.
> 
> HTH!
> 
> -- 
> John.
> ___
> 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] Best IDE for Python

2007-01-29 Thread Gabriel Farrell
On Sat, Jan 27, 2007 at 02:58:38AM -0800, Dick Moores wrote:
> 
> 
> At 07:12 PM 1/24/2007, Shadab Sayani wrote:
> Hi,
> I am using vim editor to code my project in python.Is there a good
> IDEĀ  where in I type the name of the class object and then dot then
> all the attributes of the object are displayed so on.
> Ulipad can do this.
> Dick Moores
> 
> UliPad <>:
> http://wiki.woodpecker.org.cn/moin/UliPad";>
> http://wiki.woodpecker.org.cn/moin/UliPad 
> 
> 

If you want to stick with VIM, you can get the functionality you're
looking for if it's 7.0+ and compiled with +python.  A web search for
"omnicomplete python vim" (no quotes) brings up some useful pages.

Gabe

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


[Tutor] A complete, functioning small web app: code for review

2007-02-09 Thread Gabriel Farrell
2
dumpurl = DUMPURL % username
dump = urllib2.urlopen(dumpurl)
for line in dump:
if 'REC INFO' in line:
return True

new_userlist = []
for userpass in self.getList():
if not innocheck(userpass[0]):
# the flag is len(userpass) == 3
userpass = userpass + (1,)
new_userlist.append(userpass)
return new_userlist

def _cleanup(self, max=10):
'delete backups in excess of max'
backup_list, ldap_dir = self.getBackups()
backup_list.reverse()
def cleanup_rec():
if len(backup_list) > max:
oldest = os.path.join(ldap_dir, backup_list.pop())
self.ss.remove(oldest)
cleanup_rec()
cleanup_rec()

def sync(self):
'sync MAIN_SERVER and SECONDARY_SERVER'
self._cleanup()
ss2 = Session(SECONDARY_SERVER, USERNAME, PASSWORD)
def remoteCopy(path):
file1 = self.ss.open(path)
file1_str = file1.read()
file1.close()
file2 = ss2.open(path, 'w')
file2.write(file1_str)
file2.close()
# copy ldap.usr from main to secondary
remoteCopy(LDAP_USR_PATH)

# remove old backups and copy new
backup_list, ldap_dir = self.getBackups()
file_list2 = ss2.listdir(ldap_dir)
backup_list2 = [x for x in file_list2 if x.startswith('ldap.usr.') 
and x.endswith('.bak')]
for backup in backup_list2:
backup_path = os.path.join(ldap_dir, backup)
ss2.remove(backup_path)
for backup in backup_list:
backup_path = os.path.join(ldap_dir, backup)
remoteCopy(backup_path)

ss2.close()

'''
sftp_wrapper is a wrapper class for the sftp portion of the 
paramiko library, located in lib/python/
'''

import sys
sys.path.insert(0, 'lib/python')

import paramiko

class Session(object):
def __init__(self, hostname, username, password):
self.make_sftp(hostname, username, password)
# uncomment to save log -- for testing
# hmm, doesn't seem to be working -- gsf 20061220
#paramiko.util.log_to_file('sftp.log')

def make_sftp(self, hostname, username, password):
self.tunnel = paramiko.Transport((hostname, 22))
hostkeys = paramiko.util.load_host_keys('known_hosts')
hostkey = hostkeys[hostname]['ssh-rsa']
self.tunnel.connect(username=username, password=password, 
hostkey=hostkey)
self.sftp = paramiko.SFTPClient.from_transport(self.tunnel)

def open(self, filename, mode='r'):
return self.sftp.open(filename, mode=mode)

def chdir(self, dir):
self.sftp.chdir(dir)

def get(self, remote, local=None):
if not local:
local = remote
self.sftp.get(remote, local)

def put(self, local, remote=None):
if not remote:
remote = local
self.sftp.put(local, remote)

def listdir(self, remote):
return self.sftp.listdir(remote)

def remove(self, remote):
self.sftp.remove(remote)

def close(self):
self.tunnel.close()

Title: EZProxy User List Editor




  
  

  EZProxy User List Editor(beta)
  Manually created users must be added to this EZProxy User List in order to get access to online journals.
  
% for status in form.getlist('username_status'):
  % if status == 'empty':
Please supply a username.
  % endif
  % if status == 'illegal':
The username contains illegal characters.
  % endif
  % if status == 'in_list':
User ${form.getvalue('username') | h} is already in the list.
  % endif
  % if status == 'added':
User ${form.getvalue('added') | h} has been added the list.
  % endif
% endfor
% for status in form.getlist('password_status'):
  % if status == 'empty':
Please supply a password.
  % endif
  % if status == 'illegal':
The password contains illegal characetrs.
  % endif
% endfor
% if form.getvalue('none_removed'):
  Please select user(s) to remove.
% endif
% for username in form.getlist('removed'):
  User ${username | h} has been removed.
% endfor
% if form.getvalue('no_backups'):
  No backups to revert to.
% endif
% if form.getvalue('bak_days'):
  % if form.getvalue('bak_days') == 1:
Reverted to