import

2006-12-17 Thread Gert Cuykens
I would like to lauch a server from outside the side package directory
how do i specify a path with import

#/home/gert/Desktop/www/db/py/start-server.py
import cherrypy

class HelloWorld:
 def index(self):
  return #external htm file Hello world!
 index.exposed = True

if __name__ == '__main__':  # ??? dont know what is this for
 import os.path
 conf = os.path.join(os.path.dirname(__file__), 'conf-server.py')
 cherrypy.config.update(conf)
 cherrypy.quickstart(HelloWorld())
else:
 cherrypy.tree.mount(HelloWorld()) # ??? dont know what is this for

#/home/gert/Desktop/www/db/py/conf-server.py
[global]

server.socket_port = 8080

server.thread_pool = 10
-- 
http://mail.python.org/mailman/listinfo/python-list


def index(self):

2006-12-18 Thread Gert Cuykens
Is there a difference between


class HelloWorld:
 def index(self):
  index.exposed = True
  return "Hello world!"


and


class HelloWorld:
 def index(self):
  self.exposed = True
  return "Hello world!"

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


Re: def index(self):

2006-12-18 Thread Gert Cuykens
> FWIW, the first version raises an exception (unless of course the name
> 'index' is already bound in the enclosing scope). And the second won't
> probably work as expected with CherryPy.


class HelloWorld:
 def index(self):
  return "Hello world!"
 index.exposed = True #DOOH!


i skipped reading the chapter about 2.1.8 Indentation. Guess how many
hours it took to realize 2 spaces isn't the same as 1 space lol

Any other chapters i should read horizontal instead of vertical for a
php5 htm jso wane be snake user :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Http server

2006-12-19 Thread Gert Cuykens
so far this works


import cherrypy
import os.path

class Http:

def index(self):
f = open(os.path.join(os.path.dirname(__file__), '../htm/index.htm'))
xml = f.read()
f.close()
return xml
index.exposed = True

cherrypy.tree.mount(Http())

if __name__ == '__main__':
cherrypy.config.update(os.path.join(os.path.dirname(__file__),
'server.conf'))
cherrypy.server.quickstart()
cherrypy.engine.start()


I would like a cute secretary for Christmas but i dont think i will
get one. So i need to find a way to scan a directory. And map all
files inside the directory and generate a class member like


def filename(self):
f = open(os.path.join(os.path.dirname(__file__), '../htm/filename'))
xml = f.read()
f.close()
return xml
filename.exposed = True


Any idea or cute secretary maybe ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Http server

2006-12-19 Thread Gert Cuykens
> The cute secretary's name is "cherrypy.tools.staticdir".
> Check out her resume at http://www.cherrypy.org/wiki/StaticContent

I think i am in love :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Http server

2006-12-19 Thread Gert Cuykens
> > The cute secretary's name is "cherrypy.tools.staticdir".
> > Check out her resume at http://www.cherrypy.org/wiki/StaticContent
>
> I think i am in love :)

Cant believe this just works out


import os.path
import cherrypy
pwd = os.path.dirname(os.path.abspath(__file__))

class Http:

_cp_config = {'tools.sessions.on': True}
@cherrypy.expose
def index(self):
f = open(os.path.join(pwd, '../htm/index.htm'))
xml = f.read()
f.close()
return xml

if __name__ == '__main__':
cherrypy.config.update({'server.socket_port': 8080,
'server.thread_pool': 10,
'environment': 'production',
'log.screen': True})
conf = {'/': {'tools.staticdir.root': os.path.join(pwd, '../htm')},
'/css': {'tools.staticdir.on': True,
 'tools.staticdir.dir': os.path.join(pwd, '../css')},
'/js': {'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(pwd, '../js')}}
cherrypy.quickstart(Http(), '/', config=conf)

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


Re: Http server

2006-12-19 Thread Gert Cuykens
Does anybody know how to redirect a post request ?

i have a js file that does a post request to a /php/action.php file
and i would like for the secretary to just do the action method
instead that is defined in her python Http class book, so i can run
both php and python without changing the static source code at 2
different ports head to head to see witch one can handle the most
requests ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def index(self):

2006-12-20 Thread Gert Cuykens
> > class HelloWorld(object):
> > @cherrypy.exposed
> > def index(self):
> >return "Hello World"

do i have to write @cherrypy.exposed before every def or just once for
all the def's ? and why not write something like @index.exposed ?

in other words i have no idea what @ actually does i only know i have
too write it to make it work :) Am guessing @ is something like
prototyping in javascript but then def index is not a object but a
method ? So that would not make sense ?

oh and self stands more or less for private method right ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def index(self):

2006-12-21 Thread Gert Cuykens
On 21 Dec 2006 09:44:48 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote:
> "George Sakkis" <[EMAIL PROTECTED]> wrote:
>
> @expr
> def fn(...): ...
>
> is exactly equivalent to:
>
> def fn(...): ...
> fn = (expr)(fn)
>

ok i did my homework reading about decorators
http://www.python.org/doc/2.4.4/whatsnew/node6.html

could it be the example above needs to be like

@expr
def fn(...): ...

is exactly equivalent to:

def fn(...): ...
fn = expr(fn)

> > oh and self stands more or less for private method right ?
>
> if you have to ask that question about 'self' you really do need to read
> some introductory texts on Python.
>
> Methods get passed their instance as their first parameter (usually this is
> implicit in the call, but it is always explicitly shown in the method) and
> self is simply the conventional name. Many other languages use 'this'
> instead of self and make the passing of 'this' implicit inside the method
> as well as outside. There are good reasons why it is explicit in Python,
> but just remember that the first parameter a method receives is always the
> instance and you won't go far wrong.

in all the things i every read about self i end up with blablabla
scoping blabla blabla self:: blablabla public static methods blablabla
:)

So when reading 'self' is the same as 'this' it was worth asking the
question for confusion sake :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def index(self):

2006-12-22 Thread Gert Cuykens
Ok thx i think i understand it now

>>> class C:
... @staticmethod
... def fn():
... return 'whohoo'
...
>>> C.fn()
'whohoo'
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


attribute decorators

2006-12-22 Thread Gert Cuykens
would it not be nice if you could assign decorators to attributes too ?
for example

class C:
@staticattribute
data='hello'

or

class C:
@privateattribute
data='hello'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: attribute decorators

2007-01-05 Thread Gert Cuykens
sorry for repost i just found out the news group comp.lang.python is
the same as python-list@python.org :)

On 5 Jan 2007 20:34:54 -0800, gert <[EMAIL PROTECTED]> wrote:
> Would it not be nice if you could assign decorators to attributes too ?
> for example
>
> class C:
>   @staticattribute
>   data='hello'
>
> or
>
> class C:
>   @privateattribute
>   data='hello'
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


whats wrong with my reg expression ?

2007-01-15 Thread Gert Cuykens
 rex2=re.compile('^"(?P[^]*)"$',re.M)

  File "/usr/lib/python2.5/re.py", line 180, in compile
return _compile(pattern, flags)
  File "/usr/lib/python2.5/re.py", line 233, in _compile
raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression

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


Re: whats wrong with my reg expression ?

2007-01-15 Thread Gert Cuykens
thx

PS i also cant figure out what is wrong here ?

rex=re.compile('^"(?P[^"]*)"$',re.M)
for v in l:
v=rex.match(v).group('value')
v=v.replace('""','"')
return(l)

v=rex.match(v).group('value')
AttributeError: 'NoneType' object has no attribute 'group'
-- 
http://mail.python.org/mailman/listinfo/python-list


for v in l:

2007-01-15 Thread Gert Cuykens
is there a other way then this to loop trough a list and change the values

i=-1
for v in l:
i=i+1
l[i]=v+x

something like

for v in l:
l[v]=l[v]+x
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: whats wrong with my reg expression ?

2007-01-16 Thread Gert Cuykens
thx it works now
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for v in l:

2007-01-16 Thread Gert Cuykens
ok thx this was just what i was looking for

http://docs.python.org/tut/node7.html#SECTION00760
-- 
http://mail.python.org/mailman/listinfo/python-list


instancemethod

2007-01-21 Thread Gert Cuykens
import MySQLdb

class Db:

_db=-1
_cursor=-1

@classmethod
def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._cursor=self._db.cursor()

@classmethod
def excecute(self,cmd):
self._cursor.execute(cmd)
self._db.commit()

@classmethod
def rowcount(self):
return int(self._cursor.rowcount)

@classmethod
def fetchone(self):
return self._cursor.fetchone()

@classmethod
def close(self):
self._cursor.close()
self._db.close()

if __name__ == '__main__':
gert=Db('localhost','root','**','gert')
gert.excecute('select * from person')
for x in range(0,gert.rowcount):
print gert.fetchone()
gert.close()

[EMAIL PROTECTED]:~$ python ./Desktop/svn/db/Py/db.py
Traceback (most recent call last):
  File "./Desktop/svn/db/Py/db.py", line 35, in 
for x in range(0,gert.rowcount):
TypeError: range() integer end argument expected, got instancemethod.
[EMAIL PROTECTED]:~$

Can anybody explain what i must do in order to get integer instead of
a instance ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instancemethod

2007-01-21 Thread Gert Cuykens
On 21 Jan 2007 14:35:19 -0800, Nanjundi <[EMAIL PROTECTED]> wrote:
> >
> > if __name__ == '__main__':
> > gert=Db('localhost','root','**','gert')
> > gert.excecute('select * from person')
> > for x in range(0,gert.rowcount):
> > print gert.fetchone()
> > gert.close()
> >
> > [EMAIL PROTECTED]:~$ python ./Desktop/svn/db/Py/db.py
> > Traceback (most recent call last):
> >   File "./Desktop/svn/db/Py/db.py", line 35, in 
> > for x in range(0,gert.rowcount):
> > TypeError: range() integer end argument expected, got instancemethod.
> > [EMAIL PROTECTED]:~$
> >
> > Can anybody explain what i must do in order to get integer instead of
> > a instance ?
>
> Gert,
> > for x in range(0,gert.rowcount):
> gert.rowcount is the method (and not a data attribute).
> gert.rowcount() is the method call, which get the return value from
> method.
>
> So try this.
> for x in range( 0,gert.rowcount() ):
>

Doh! :)

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


Re: instancemethod

2007-01-21 Thread Gert Cuykens
On 1/22/07, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> "Gert Cuykens" <[EMAIL PROTECTED]> escribió en el mensaje
> news:[EMAIL PROTECTED]
>
> > class Db:
> >
> >_db=-1
> >_cursor=-1
> >
> >@classmethod
> >def __init__(self,server,user,password,database):
> >self._db=MySQLdb.connect(server , user , password , database)
> >self._cursor=self._db.cursor()
> >
> >@classmethod
> >def excecute(self,cmd):
> >self._cursor.execute(cmd)
> >self._db.commit()
> >
> >
> > if __name__ == '__main__':
> >gert=Db('localhost','root','**','gert')
> >gert.excecute('select * from person')
> >for x in range(0,gert.rowcount):
> >print gert.fetchone()
> >gert.close()
>
> Besides your specific question that was already answered, why are you using
> classmethods at all?
> You are constructing a Db instance and using it as if all were normal
> instance methods... Just remove all those @classmethod declarations and use
> it in a standard way.
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

i thought @classmethod was the normal method ?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: instancemethod

2007-01-21 Thread Gert Cuykens
On 1/22/07, Gert Cuykens <[EMAIL PROTECTED]> wrote:
> On 1/22/07, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> > "Gert Cuykens" <[EMAIL PROTECTED]> escribió en el mensaje
> > news:[EMAIL PROTECTED]
> >
> > > class Db:
> > >
> > >_db=-1
> > >_cursor=-1
> > >
> > >@classmethod
> > >def __init__(self,server,user,password,database):
> > >self._db=MySQLdb.connect(server , user , password , database)
> > >self._cursor=self._db.cursor()
> > >
> > >@classmethod
> > >def excecute(self,cmd):
> > >self._cursor.execute(cmd)
> > >self._db.commit()
> > >
> > >
> > > if __name__ == '__main__':
> > >gert=Db('localhost','root','**','gert')
> > >gert.excecute('select * from person')
> > >for x in range(0,gert.rowcount):
> > >print gert.fetchone()
> > >gert.close()
> >
> > Besides your specific question that was already answered, why are you using
> > classmethods at all?
> > You are constructing a Db instance and using it as if all were normal
> > instance methods... Just remove all those @classmethod declarations and use
> > it in a standard way.
> >
> > --
> > Gabriel Genellina
> >
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
> i thought @classmethod was the normal method ?
>

did some reading and i think i know why now :)

http://www.faqts.com/knowledge_base/view.phtml/aid/16824

python always seems to amaze me how other languages make a mess of
things that suppose to be simple
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: instancemethod

2007-01-21 Thread Gert Cuykens
import MySQLdb

class Db:

_db=-1
_cursor=-1
rowcount=-1

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._cursor=self._db.cursor()

def excecute(self,cmd):
self._cursor.execute(cmd)
self._db.commit()
self.rowcount=int(self._cursor.rowcount())

def fetchone(self):
return self._cursor.fetchone()

def close(self):
self._cursor.close()
self._db.close()

if __name__ == '__main__':
gert=Db('localhost','root','***','gert')
gert.excecute('select * from person')
for x in range(0,gert.rowcount):
print gert.fetchone()
gert.close()

Traceback (most recent call last):
  File "Desktop/svn/db/Py/db.py", line 28, in 
gert.excecute('select * from person')
  File "Desktop/svn/db/Py/db.py", line 17, in excecute
self.rowcount=int(self._cursor.rowcount())
TypeError: 'long' object is not callable

I guess i did something wrong again ? :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instancemethod

2007-01-21 Thread Gert Cuykens
never mind i think i need some sleep lol i did the exact opposite this
time .rowcount() -> .rowcount
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instancemethod

2007-01-22 Thread Gert Cuykens
Reading all of the above this is the most simple i can come too.

import MySQLdb

class Db:

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def excecute(self,cmd):
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

def close(self):
self.cursor.close()
self._db.close()

def __del__(self):
try:
self.close()
except:
pass

if __name__ == '__main__':
gert=Db('localhost','root','**','gert')
gert.excecute('select * from person')
for row in gert.cursor:
print row

This must be the most simple it can get right ?

PS i didn't understand the __getattr__ quit well but i thought it was
just to overload the privies class
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instancemethod

2007-01-23 Thread Gert Cuykens
import MySQLdb

class Db(object):

def __enter__(self):
pass

def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._db.autocommit(True)
self.cursor=self._db.cursor()

def execute(self,cmd):
self.cursor.execute(cmd)
self.rowcount=int(self.cursor.rowcount)

def close(self):
self.cursor.close()
self._db.close()

def __getattr__(self, name):
attr = getattr(self._cursor, name,getattr(self._db, name,  None))
if attr is None:
raise AttributeError("object %s has no attribute %s"
%(self.__class__.__name__, name))
return attr

def __del__(self):
try:
self.close()
finally:
pass
except:
pass

def __exit__(self):
pass

if __name__ == '__main__':
gert = Db('localhost','root','*','gert')
gert.execute('select * from person')
for row in gert.cursor:
print row

with Db('localhost','root','*','gert') as gert:
gert.excecute('select * from person')
for row in gert.cursor:
print row

Desktop/svn/db/Py/db.py:45: Warning: 'with' will become a reserved
keyword in Python 2.6
  File "Desktop/svn/db/Py/db.py", line 45
with Db('localhost','root','*','gert') as gert:
  ^
SyntaxError: invalid syntax

I was thinking if it would be possible to create a object that uses
it's own instance name as a atribute.

For example instead of
gert = Db('localhost','root','*','gert')

you would do this
gert = Db('localhost','root','*')

and the name of the object itself 'gert' get's assigned to database somehow ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instancemethod

2007-01-26 Thread Gert Cuykens
 > class Obj(object):
>pass
>
> toto = tutu = tata = titi = Obj()
>
> What's an "instance name" ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list

i would say __object__.__name__[3] == toto

And if your obj is a argument like

something(Obj())

i would say __object__.__name__[0] == 0x2b7bd17e9910
-- 
http://mail.python.org/mailman/listinfo/python-list


def obj()

2007-02-08 Thread Gert Cuykens
def obj():
return {'data':'hello',
'add':add(v)}

def add(v):
data=data+v

if  __name__ == '__main__':
test=obj()
test.add('world')
print test.data

I don't know why but i have one of does none class c programing style
moods again. I was wondering if the following was possible without
using a class ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def obj()

2007-02-08 Thread Gert Cuykens
On 2/8/07, Leif K-Brooks <[EMAIL PROTECTED]> wrote:
> def obj():
>  result = {'data': 'hello'}
>  result['add'] = adder(result)
>  return result
>
> def adder(obj):
>  def add(value):
>  obj['data'] += value
>  return add
>
> if __name__ == '__main__':
>  test = obj()
>  test['add']('world')
>  print test['data']

Nice :) Does anybody know how this would look in c code ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: def obj()

2007-02-08 Thread Gert Cuykens
#include 

function hello(){
struct obj = { char *data = 'hello'}
obj.add = obj_add(obj);
return obj;
}

function obj_add(obj){
function add(value){
obj.data += value;
return obj;
}
}

main(){
test = hello();
test.add('world');
printf(test.data);
}

I was thinking something like this maybe ?
-- 
http://mail.python.org/mailman/listinfo/python-list