Groups Code Example Types

1 view
Skip to first unread message

Jay Herrmann

unread,
Sep 10, 2010, 1:36:48 AM9/10/10
to pysage
Hi John,

I am now moving forward with my project using pysage. As I am using
it more, I have another question.

class MainGroupMessage(Message):
properties = ['TenSecCount','ThreeSecCount']
types = ['i','i']
packet_type = 101

The types property is still a little an clear. I am sure it is
because I am learning Python as I do this project as well. So thank
for your patience. So can I assume that the types follows the format
characters found under pack in python docs.

http://docs.python.org/library/struct.html?highlight=pack_#format-characters

Just double checking. Also does pack/unpack support dicts as a type?

Thanks for pysage,
Jay

John Yang

unread,
Sep 10, 2010, 1:55:16 AM9/10/10
to pys...@googlegroups.com
Hi Jay:

Glad to hear your project is moving forward. You are on the right
track. Besides the "C types", there is also an array and an arbitrary
string type built into pysage.

Please see:

http://www.bigjstudio.com/pysage/network.html#network-messages

You should be able to simulate a dictionary with an array. The
provided automatic packing/unpacking can support arbitrary types.
However, you do need to provide the functions that actually do the
packing/unpacking. What is the type of the key and value for the
dictionary? I'll be happy to provide you with an example if you give
me some more details.

HTH,

John

> --
> You received this message because you are subscribed to the Google Groups "pysage" group.
> To post to this group, send email to pys...@googlegroups.com.
> To unsubscribe from this group, send email to pysage+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pysage?hl=en.
>
>

Jay Herrmann

unread,
Sep 10, 2010, 2:18:02 AM9/10/10
to pys...@googlegroups.com
Hi John,
I was looking at a way to easily pass info to my MainGroup
MainGroup
SubGroup1
SubGroup2
etc...

Where MainGroup would have properties like
(Action='DoSomething',Dict={'Data': 'to work with'})
So SubGroupX could pass data to MainGroup for some action

As I did a little more research, I did find at Python 2.6>= supports
JSON. So I could do something like below that used a type of 'S'.

>>> import json
>>> json.dumps(c)
'{"date": {"BTID": {"action": [1, 2], "date": "now"}}}'
>>> js = json.dumps(c)
>>> js
'{"date": {"BTID": {"action": [1, 2], "date": "now"}}}'
>>> d = json.loads(js)
>>> d
{u'date': {u'BTID': {u'action': [1, 2], u'date': u'now'}}}

The SubGroupX would encode dict as JSON string pass to MainGroup then
Decode JSON string back to dict. Speed of encoding and decoding maybe
an issue in the future as I am looking at putting this on a embedding
ARM brd. But for testing and design on an x86 I think this should
work. I am hoping. :)

Thanks,
Jay

John Yang

unread,
Sep 10, 2010, 3:24:19 AM9/10/10
to pys...@googlegroups.com
Hey Jay:

First of all, update pysage :). Small patch to enable atomic type
packing/unpacking (only had packing/unpacking for composite types
before).

Here I have a small example on how to use the (un)packing with JSON.

from pysage import *
import json
import time, random

class FoodAvailableMessage(Message):
properties = ['food']
types = ['S']
packet_type = 101
def pack_food(self, food):
return json.dumps(food)
def unpack_food(self, food_s):
return json.loads(food_s)

class Consumer(Actor):
subscriptions = ['FoodAvailableMessage']
def handle_FoodAvailableMessage(self, msg):
print 'Yummy! I had %d %s pancakes!' %
(msg.get_property('food')['amount'],
msg.get_property('food')['color'])

import time, random

class Chef(Actor):
def __init__(self):
self.last_sent = time.time()
def update(self):
'''every 2 seconds, this chef makes a random amount of pancakes'''
if time.time() - self.last_sent > 2.0:
mgr.queue_message_to_group(mgr.PYSAGE_MAIN_GROUP,
FoodAvailableMessage(food={'amount': random.randint(0,10), 'color':
'red'}))
self.last_sent = time.time()

mgr = ActorManager.get_singleton()
mgr.register_actor(Consumer())

if __name__ == '__main__':
mgr.enable_groups()
mgr.add_process_group('chefs', Chef) # spawns a new process
that "ticks" independently
while True:
processed = mgr.tick()
time.sleep(.03)

HTH. You will incur the cost of JSON serialization. However, you do
have small enough message now. In the future, you can take advantage
of the composite type and array type to easily mimic this behaviour
without using JSON. Let me know if I can provide an example for that
as well should you need it.

John

John Yang

unread,
Sep 10, 2010, 3:34:01 AM9/10/10
to pys...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages