Bridge and python keyword arguments

222 views
Skip to first unread message

Justin

unread,
Jul 16, 2012, 4:12:31 PM7/16/12
to bridge...@googlegroups.com
Is there a special trick to use keyword arguments with bridge?

$ cat server.py 
from configit import conf_from_file
config
= conf_from_file('../development.py')

from BridgePython import Bridge

class SimpleHandler(object):
   
def simple(self, simple=None, callback=None):
       
if simple and callback:
            callback
('Simple is {0}'.format(simple))
       
elif callback:
            callback
('Simple not passed')

bridge
= Bridge(api_key=config.private_api_key)
bridge
.publish_service('simple', SimpleHandler())

bridge
.connect()

$ cat client
.py
from configit import conf_from_file
config
= conf_from_file('../development.py')

from BridgePython import Bridge

bridge
= Bridge(api_key=config.public_api_key)
client
= None

def service_response(resp):
   
print(resp)

service
= bridge.get_service('simple')
try:
    service
.simple(simple='simple text', callback=service_response)
except TypeError as e:
   
print(e)

service
.simple('simple text', service_response)

bridge
.connect()

Results:

$ python client.py 
<lambda>() got an unexpected keyword argument 'simple'

Simple is simple text

Sridatta Thatipamala

unread,
Jul 16, 2012, 4:17:17 PM7/16/12
to bridge...@googlegroups.com
Not at the moment. It would be hard to map the concept of keyword arguments to any of the other languages we support (except maybe Ruby).

Are you using Bridge for primarily Python to Python communication?
--Sridatta

--
You received this message because you are subscribed to the Google Groups "Bridge" group.
To post to this group, send email to bridge...@googlegroups.com.
To unsubscribe from this group, send email to bridge-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/bridge-users/-/dKuBYdpBM7MJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Justin

unread,
Jul 16, 2012, 5:45:02 PM7/16/12
to bridge...@googlegroups.com
yup... all python.


On Monday, July 16, 2012 4:17:17 PM UTC-4, Sridatta Thatipamala wrote:
Not at the moment. It would be hard to map the concept of keyword arguments to any of the other languages we support (except maybe Ruby).

Are you using Bridge for primarily Python to Python communication?
--Sridatta

To unsubscribe from this group, send email to bridge-users+unsubscribe@googlegroups.com.

Steve Wang

unread,
Jul 16, 2012, 6:35:22 PM7/16/12
to bridge...@googlegroups.com
I've hacked together a very rudimentary wrapper for Bridge that
enables . Just use via `bridge = Wrap(Bridge(**kwargs))`. Entirely
experimental (and unsupported), but it seems usable.

class Wrap():
def __init__(self, bridge):
self.bridge = bridge
def get_service(self, svcName):
return RemoteService(self.bridge.get_service(svcName))
def publish_service(self, svcName, svc):
return self.bridge.publish_service(svcName, WrappedService(svc))
def join_channel(self, name, handler, writeable=True, callback=None):
return self.bridge.joinChannel(name, WrappedService(handler),
writeable, callback)
def get_channel(self, channelName):
return RemoteService(self.bridge.get_channel(svcName))
def connect(self):
self.bridge.connect()
def __getattr__(self, attr):
return getattr(self.bridge, attr)


class RemoteService():
def __init__(self, svc):
self.svc = svc
def __getattr__(self, attr):
if getattr(self.svc, attr):
return lambda **kwargs: getattr(self.svc, attr)(kwargs)
raise AttributeError(attr)

class WrappedService():
def __init__(self, svc):
self.svc = svc
def __getattr__(self, attr):
if getattr(self.svc, attr):
return lambda kwargs: getattr(self.svc, attr)(**kwargs)
raise AttributeError(attr)
>>> bridge-users...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msg/bridge-users/-/dKuBYdpBM7MJ.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Bridge" group.
> To post to this group, send email to bridge...@googlegroups.com.
> To unsubscribe from this group, send email to
> bridge-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/bridge-users/-/7tVo-8wK0McJ.

Justin

unread,
Jul 16, 2012, 6:57:28 PM7/16/12
to bridge...@googlegroups.com
Very cool. I'll play with it. Currently not working in my 'Simple' example.

Thanks

Steve Wang

unread,
Jul 16, 2012, 7:22:46 PM7/16/12
to bridge...@googlegroups.com
Hmm, looked into using both kwargs and args simultaneously. For the
two wrapper services, try:

class RemoteService():
def __init__(self, svc):
self.svc = svc
def __getattr__(self, attr):
if getattr(self.svc, attr):
return lambda *args, **kwargs: getattr(self.svc,
attr)(*(args + (kwargs,)))
raise AttributeError(attr)

class WrappedService():
def __init__(self, svc):
self.svc = svc
def __getattr__(self, attr):
if getattr(self.svc, attr):
return lambda *args: getattr(self.svc, attr)(*args[:-1], **args[-1])
raise AttributeError(attr)
> --
> You received this message because you are subscribed to the Google Groups
> "Bridge" group.
> To post to this group, send email to bridge...@googlegroups.com.
> To unsubscribe from this group, send email to
> bridge-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/bridge-users/-/cQ1fx3cjXZkJ.

Justin

unread,
Jul 16, 2012, 8:05:30 PM7/16/12
to bridge...@googlegroups.com
Steve you are the man. Took me a minute to wrap my head around the lambda's but that totally did the trick.

Thanks!


On Monday, July 16, 2012 7:22:46 PM UTC-4, steve wrote:
Hmm, looked into using both kwargs and args simultaneously. For the
two wrapper services, try:

class RemoteService():
    def __init__(self, svc):
        self.svc = svc
    def __getattr__(self, attr):
        if getattr(self.svc, attr):
            return lambda *args, **kwargs: getattr(self.svc,
attr)(*(args + (kwargs,)))
        raise AttributeError(attr)

class WrappedService():
    def __init__(self, svc):
        self.svc = svc
    def __getattr__(self, attr):
        if getattr(self.svc, attr):
            return lambda *args: getattr(self.svc, attr)(*args[:-1], **args[-1])
        raise AttributeError(attr)


Reply all
Reply to author
Forward
0 new messages