jps package¶
Module contents¶
-
class
jps.Publisher(topic_name, host=None, pub_port=None, serializer='DEFAULT')[source]¶ Bases:
objectPublishes data for a topic.
Example:
>>> pub = jps.Publisher('special_topic') >>> pub.publish('{"name": "hoge"}')
Parameters: - topic_name – Topic name
- host – host of subscriber/forwarder
- pub_port – port of subscriber/forwarder
- serializer – this function is applied before publish (default: None)
-
publish(payload)[source]¶ Publish payload to the topic
Note
If you publishes just after creating Publisher instance, it will causes lost of message. You have to add sleep if you just want to publish once.
>>> pub = jps.Publisher('topic') >>> time.sleep(0.1) >>> pub.publish('{data}')
Parameters: payload – data to be published. This is ok if the data is not json.
-
class
jps.Subscriber(topic_name, callback=None, host=None, sub_port=None, deserializer='DEFAULT')[source]¶ Bases:
objectSubscribe the topic and call the callback function
Example:
>>> def callback(msg): ... print msg ... >>> sub = jps.Subscriber('topic_name', callback) >>> sub.spin()
or you can use python generator style
>>> import jps >>> for msg in jps.Subscriber('/hoge1'): ... print msg
Parameters: - topic_name – topic name
- host – host name of publisher/forwarder
- sub_port – port of publisher/forwarder
- deserializer – this function is applied after received (default: None)
-
spin(use_thread=False)[source]¶ call callback for all data forever (until C-c)
Parameters: use_thread – use thread for spin (do not block)
-
spin_once(polling_sec=0.01)[source]¶ Read the queued data and call the callback for them. You have to handle KeyboardInterrupt (C-c) manually.
Example:
>>> def callback(msg): ... print msg >>> sub = jps.Subscriber('topic_name', callback) >>> try: ... while True: ... sub.spin_once(): ... time.sleep(0.1) ... except KeyboardInterrupt: ... pass
-
class
jps.ArgumentParser(subscriber=True, publisher=True, service=False, *args, **kwargs)[source]¶ Bases:
argparse.ArgumentParserCreate ArgumentParser with args (host/subscriber_port/publisher_port)
Example:
>>> parser = jps.ArgumentParser(description='my program') >>> args = parser.parse_args() >>> args.host 'localhost' >>> args.subscriber_port 54321 >>> args.publisher_port 54320
Parameters: - add subscriber_port (default (subscriber) – True)
- add publisher_port (default (publisher) – True)
-
class
jps.ServiceServer(callback, host=None, res_port=54323)[source]¶ Bases:
objectExample:
>>> def callback(req): ... return 'req = {req}'.format(req=req) ... >>> service = jps.ServiceServer(callback) >>> service.spin()
-
class
jps.ActionServer(base_topic_name, callback, host=None, pub_port=None, sub_port=None, serializer='DEFAULT', deserializer='DEFAULT')[source]¶ Bases:
objectserve the service which takes some long time
Example:
>>> import jps >>> import time >>> def callback(req): ... time.sleep(1) ... return req + ' received' >>> s = jps.ActionServer('move_to', callback) # subscribe 'move_to/request', publish 'move_to/response' >>> s.spin()
-
class
jps.ActionClient(base_topic_name, host=None, pub_port=None, sub_port=None, serializer='DEFAULT', deserializer='DEFAULT')[source]¶ Bases:
objectCall an action
Example:
>>> import jps >>> import json >>> c = jps.ActionClient('move_to') >>> future = c(json.dumps({'x': 10.0, 'y': 0.1})) # do something if you are busy to do something during waiting. >>> result = future.wait()
Submodules¶
jps.utils module¶
jps.launcher module¶
jps.args module¶
-
class
jps.args.ArgumentParser(subscriber=True, publisher=True, service=False, *args, **kwargs)[source]¶ Bases:
argparse.ArgumentParserCreate ArgumentParser with args (host/subscriber_port/publisher_port)
Example:
>>> parser = jps.ArgumentParser(description='my program') >>> args = parser.parse_args() >>> args.host 'localhost' >>> args.subscriber_port 54321 >>> args.publisher_port 54320
Parameters: - add subscriber_port (default (subscriber) – True)
- add publisher_port (default (publisher) – True)
jps.tools module¶
-
jps.tools.echo(topic_name, num_print=None, out=<open file '<stdout>', mode 'w'>, host='localhost', sub_port=54321)[source]¶ print the data for the given topic forever
-
jps.tools.play(file_path, host='localhost', pub_port=54320)[source]¶ replay the recorded data by record()
-
jps.tools.pub(topic_name, json_msg, repeat_rate=None, host='localhost', pub_port=54320)[source]¶ publishes the data to the topic
Parameters: - topic_name – name of the topic
- json_msg – data to be published
- repeat_rate – if None, publishes once. if not None, it is used as [Hz].
-
jps.tools.record(file_path, topic_names=[], host='localhost', sub_port=54321)[source]¶ record the topic data to the file