[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] [OT] pyOSC question






On 29 December 2012 21:31, nescivi <nescivi@xxxxxxxxx> wrote:
Hiho,

On Saturday 29 December 2012 08:40:15 Nathaniel Virgo wrote:
> I'm sorry for posting an off-topic question, but there doesn't seem to be
> an official palce to ask pyOSC questions and I thought there might be some
> expertise here.

I've used it quite a bit :)

> I'm writing a Python program that will intercommunicate with SuperCollider
> via OSC. I originally started using pyliblo, but I realised that it doesn't
> quite meet my needs (there's no way to remove a callback once it's been
> added), so I've switched to pyOSC.

pyOSC also is more easy to work cross platform, as you don't depend on C-
libraries being installed...

> My question is, in pyOSC, how can I filter messages by OSC type tags? It
> seems from the example code that callbacks are added with something like
>
> server.addMsgHandler("/my/path", callback_func)
>
> but then callback_func has to know how to handle an OSC message of any
> format, e.g.
>
> ['/my/path', 1.0]
> ['/my/path', 42, 64, 6.283]
> ['/my/path', "foo", "bar", "baz"]
> etc.
>
> This seems to mean that I have to put error checking code in every single
> callback, to make sure that the message has the right format, which is very
> annoying. But then again, there's a lot of stuff in pyOSC that isn't
> demonstrated in the example code, so I wondered whether anyone knows of a
> better way?

It seems to me this is a question of defining your address namespace well, and
the types of data that you send with each.
If you use the same address tag '/my/path' for a lot of different types of
messages, I think the namespace is not well defined.

Many thanks for the reply. I agree with this - I don't want to use the same address tag for more than one thing - it's just that I didn't want all my callbacks to have to check the type tags explicitly, like this:

def my_callback(path, tags, args, source)
   if tags=='ff':
     # do stuff with args
   else:
     # let the message fall through to the default callback

it seemed as if this would be such a typical use case that the library might include some way of dealing with it. In pyliblo you don't have to do this, because you specify the type tags when you register the callback, and it doesn't get called unless the tags are correct.

For now I've solved it by using some decorator magic to add that logic to all my callbacks automatically, but it does seem a strange thing not to have in the API.

Nathaniel


You do however also get the types of the arguments, so you could filter based
on the type tags of the message.

A default fallback function is:

def fallback(self, path, args, types, src):
    print( "got unknown message '%s' from '%s'" % (path, src.get_url()) )
    for a, t in zip(args, types):
      print( "argument of type '%s': %s" % (t, a) )

sincerely,
Marije

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/



--
Nathaniel Virgo
http://nathanielvirgo.com