I built a nice html templater!

2008-09-30 Thread Derick van Niekerk
Ok - so it's not really an awesome achievement and only handles basic
templating needs (no loops and other programming constructs) but maybe
someone will find it useful.

It replaces any xml block where the id attribute is specified with
contents provided - a description is provided in the comments.

http://pastebin.com/m76f57ae2

My knowledge of python is limited and I would like someone to help me
with wrapping a command line interface around this function. I need
switches to specify input file, output file, key/value pairs for
variable substitution and key/value pairs for substituting blocks with
file contents (there is some clarification in the comments)

Any help would be appreciated.

Of course - feel free to use the code as you wish when you wish if you
wish :)

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


Re: Zope 3

2006-04-26 Thread Derick van Niekerk
This looks more or less like what I am looking for to learn Zope3! As
is mentioned elsewhere in this thread Zope3 is *nothing* like Zope2 and
after starting to learn the one, I knew nothing about the other.
*Everything* is different - from the interface to the design
methodologies.

One thing Zope seems to keep pushing is extreme programming - until
recently, I thought it is a joke, like extreme ironing, but it seems
like a very popular style of programming. I am astounded by how much I
need to learn to call myself a programmer!

Anyway - If some of you can give me a little insight to what you use to
develop on the web using Python, I'd appreciate it. I've heard good
things about cherrypy, django, mod_python, zope, etc., etc. There is
just so little time - I'd gladly sacrifice a little power / flexibility
for an easier learning curve. This will be my first python web
project...

Thanks for the feedback - it helps a lot :)
Derick

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


Zope 3

2006-04-25 Thread Derick van Niekerk
I have been developing in PHP for some time now and needed to look into
application frameworks to speed up my development. I was looking into
Horde and CakePHP before I was introduced to Python. I started learing
python and within a few *hours* I already wrote my first small program
and I still use it!

I love Python! Then I was introduced to Zope by freak accident. While
Zope looked like the answer to my dillemma, I still can't get my head
wrapped around it. Is it because I don't know Python well enough? Or is
it just that difficult to learn?

I would like to start my next web project (a database of demographic
info on scientists in Africa)  on Zope 3. Could anybody point me in the
right direction? Where should I start?

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


Re: Dictionary inserts into MySQL (each key in its own field)

2006-01-27 Thread Derick van Niekerk
[quote]
d = {spam: 1, egg: 2}

cols = d.keys()
vals = d.values()

stmt = INSERT INTO table (%s) VALUES(%s) % (
,.join(cols), ,.join([?]*len(vals))
)

cursor.execute(stmt, tuple(vals))
[/quote]

I will be using the python-mysql API. This looks like what I am looking
for. I just have a question about the last join statment. In this case
it would just create a string = '?,?' - wouldn't it? Other than that,
it is pretty easy to understand. Now - how do I escape the string for
characters that might break the script e.g. ['  \ ) ( ...]?

Is there a python function to do it? part of the mysql-python module,
maybe?

-d-

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


Re: Dictionary inserts into MySQL (each key in its own field)

2006-01-27 Thread Derick van Niekerk
[quote]
(just curious, but from where do people get the idea that arbitrary
data
just have to be inserted into the the SQL statement text all the time?
is
this some PHP misfeature?)
[/quote]

I've never seen it done in this way before, but I do come from a PHP
point of view.

I've only started with python this week, so a lot of the way it does
things are new to me, So far, all of the differences are good.

Thanks for the help

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


Re: Mining strings from a HTML document.

2006-01-26 Thread Derick van Niekerk
I'm battling to understand this. I am switching to python while in a
production environment so I am tossed into the deep end. Python seems
easier to learn than other languages, but some of the conventions still
trip me up. Thanks for the link - I'll have to go through all the
previous chapters to understand this one though...

I suppose very few books on python start off with HTML processing in
stead of 'hello world' :p

Could you give me an example of how to use it to extract basic
information from the web page? I need a bit of a hit-the-ground-running
approach to python. You'll see that the data in my example isn't
encapsulated in tags - is there still an easy way to extract it using
the parser module?

Thanks

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


Re: Mining strings from a HTML document.

2006-01-26 Thread Derick van Niekerk
Runsun Pan helped me out with the following:

You can also try the following very primitive solution that I
sometimes
use to extract simple information in a quick and dirty way:

def extract(text,s1,s2):
''' Extract strings wrapped between s1 and s2.

 t=this is a spantest/span for spanextract()/span
that spandoes multiple extract/span 
 extract(t,'span','/span')
['test', 'extract()', 'does multiple extract']

'''
beg = [1,0][text.startswith(s1)]
tmp = text.split(s1)[beg:]
end = [len(tmp), len(tmp)+1][ text.endswith(s2)]
return [ x.split(s2)[0] for x in tmp if
len(x.split(s2))1][:end]


This will help out a  *lot*! Thank you. This is a better bet than the
parser in this particular implementation because the data I need is not
encapsulated in tags! Field names are within b/b tags followed by
plain text data and ended with a br tag. This was my main problem
with a parser, but your extract fuction solves it beautifully!

I'm posting back to the NG in just in case it is of value to anyone
else.

Could you/anyone explain the 4 lines of code to me though? A crash
course in Python shorthand? What does it mean when you use two sets of
brackets as in : beg = [1,0][text.startswith(s1)] ?

Thanks for the help!
-d-

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


Re: Mining strings from a HTML document.

2006-01-26 Thread Derick van Niekerk
Thanks Guys!

I've written several functions yesterday to import from different types
of raw data including html and different text formats. In the end I
never used the extract function or the parser module, but your advice
put me on the right track. All these functions are now in a single
object and the inner workings are abstracted (as much as python
allows). So a single object can now import from any file without me
having to worry about what file it is!

Might not sound like much, but the whole OOP thing is new to me too, so
I am very happy with what python could do for me.

Now just to get this stuff into MySQL...new topic :)

Thanks for all your help!
-d-

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


Mining strings from a HTML document.

2006-01-25 Thread Derick van Niekerk
Hi,

I am new to Python and have been doing most of my work with PHP until
now. I find Python to be *much* nicer for the development of local apps
(running on my machine) but I am very new to the Python way of thinking
and I don't realy know where to start other than just by doing it...so
far I'm just through the tut :)

My problem is as follows:
I have an html file with a list of records from a database. The list of
records is delimited with a comment and the format is as follows:

!-- comment first--
a href=slfdhksah kkshdfksahdfRecord 1/a
bField1/bData data databrbField2/bData data
databrbField3/bData data databrbField4/bData data databr

a href=slfdhksah kkshdfksahdfRecord 2/a
bField1/bData data databrbField2/bData data
databrbField3/bData data databrbField4/bData data databr

a href=slfdhksah kkshdfksahdfRecord 3/a
bField1/bData data databrbField2/bData data
databrbField3/bData data databrbField4/bData data databr
!-- comment last--

The data fields could be up to 2 or 3 paragraphs each. The number and
names of fields may differ between records (some info in one, but not
the other - ie null values do not show up in the html)

What are the string functions I would use and how would I use them? I
saw something about html parsing in python, but that might be overkill.
Babysteps.

Thanks

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


Re: Mining strings from a HTML document.

2006-01-25 Thread Derick van Niekerk
Thanks, Jay!

I'll try this out today. Trying to write my own parser is such a pain.
This BeatifullSoup script is very nice! I'll give it a try.

If you can help me out with an example of how to do what I explained, I
would appreciate it. I actually finished doing an import last night,
but there is no way I'm creating another parser from scratch!

I tried figuring out what to do by going through the code, but I am
still waay too fresh to understand generators and some of the coding
conventions.

Thanks again

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