Re: Dictionaries

2006-10-18 Thread Rob De Almeida
Lad wrote:
> Let's suppose I have
>
>  a={'c':1,'d':2}
>  b={'c':2}
> but
>  a.update(b)
> will make
> {'c': 2, 'd': 2}
>
> and I would need
> {'c': 3, 'd': 2}
>
> (because `c` is 1 in `a` dictionary and `c` is 2 in `b` dictionary, so
> 1+2=3)
>
> How can be done that?

dict([(k, a.get(k, 0) + b.get(k,0)) for k in set(a.keys() + b.keys())])

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


Re: Where to find fpconst?

2006-09-04 Thread Rob De Almeida
> Anybody know where I can find fpconst?

I uploaded the lastest copy I could find to the Cheese Shop
(http://www.python.org/pypi/fpconst/).

I'm not affiliated in any way with fpconst, btw.

Rob

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


Re: ancestor class' __init__ doesn't call other methods

2006-09-15 Thread Rob De Almeida
Luis P. Mendes wrote:
> Method a() is not called.  Why is this?  What is the best option to
> solve this? Have Cotacoes returning values and not to be an ancestor
> class of CruzaEmas?

It works for me, after rearranging your code a little bit:


class Ema:
pass

class Sistema:
def __init__(self, par):
cruza_ema = CruzaEmas(par)

class Cotacoes:
def __init__(self, par):
print "par: ", par
self.a()
def a(self):
print "ff"

class CruzaEmas(Ema, Cotacoes):
def __init__(self, par):
Cotacoes.__init__(self, par)

s = Sistema("par")


--Rob

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


Compile AST to bytecode?

2006-09-19 Thread Rob De Almeida
Hi,

I would like to compile an AST to bytecode, so I can eval it later. I
tried using parse.compileast, but it fails:

>>> import compiler, parser
>>> ast = compiler.parse("42")
>>> parser.compileast(ast)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: compilest() argument 1 must be parser.st, not instance

Any hints?

TIA,
--Rob

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


Re: Compile AST to bytecode?

2006-09-19 Thread Rob De Almeida
Duncan Booth wrote:
> > I would like to compile an AST to bytecode, so I can eval it later.
> I'm not sure there are any properly documented functions for converting an
> AST to a code object, so your best bet may be to examine what a
> pycodegen class like Expression or Module actually does.

Thanks, Duncan. It worked perfectly. :-)

For arbitrary nodes I just had to wrap them inside an Expression node:

>>> ast = compiler.ast.Expression(node)
>>> ast.filename = 'dummy'
>>> c = compiler.pycodegen.ExpressionCodeGenerator(ast)
>>> obj = eval(c.getCode(), scope)

--Rob

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


Re: What value should be passed to make a function use the default argument value?

2006-10-03 Thread Rob De Almeida
LaundroMat wrote:
> Suppose I have this function:
>
> def f(var=1):
> return var*2
>
> What value do I have to pass to f() if I want it to evaluate var to 1?
> I know that f() will return 2, but what if I absolutely want to pass a
> value to f()? "None" doesn't seem to work..

If you *absolutely* want to pass a value and you don't know the default
value (otherwise you could just pass it):

>>> import inspect
>>> v = inspect.getargspec(f)[3][0] # first default value
>>> f(v)
2

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


Re: WSGI with mod_python (was: Python, WSGI, legacy web application)

2006-11-23 Thread Rob De Almeida
Ben Finney wrote:
> I was under the impression that WSGI in mod_python was a rather kludgy
> way to do WSGI, but I don't know what the alternatives are. CGI?
> Python http server (e.g. CherryPy)? Something else?

You can use FastCGI or SCGI too, with Apache, lighttpd or Cherokee. I
have a short description of different ways to run a WSGI app here:

http://pydap.org/docs/server.html

Though it's focused on a specific WSGI app I wrote it uses Paste
Deploy, so you can generalize it easily.

--Rob

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


Re: What was that web interaction library called again?

2007-06-22 Thread Rob De Almeida
On Jun 22, 11:19 am, Harald Korneliussen <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I remember I came across a python library that made it radically
> simple to interact with web sites, connecting to gmail and logging in
> with four or five lines, for example. I thought, "that's interesting,
> I must look into it sometime". Now there's this child I know who asked
> me about programming, especially programs that could do things like
> this, how difficult it was, and so on. I mentioned how I though Python
> was a good intro to programming, and there was a library which was
> perfect for what he wanted.

httplib2?

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


Re: Rappresenting infinite

2007-06-27 Thread Rob De Almeida
On Jun 27, 6:41 am, andrea <[EMAIL PROTECTED]> wrote:
> I would like to have a useful rappresentation of infinite, is there
> already something??

from numpy import inf

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