[Rprotobuf-commits] r720 - in pkg/inst: . opencpu
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Jan 5 03:40:12 CET 2014
Author: jeroenooms
Date: 2014-01-05 03:40:05 +0100 (Sun, 05 Jan 2014)
New Revision: 720
Added:
pkg/inst/opencpu/
pkg/inst/opencpu/ocpu-getdata.R
pkg/inst/opencpu/ocpu-getdata.py
pkg/inst/opencpu/ocpu-rpc.R
pkg/inst/opencpu/ocpu-rpc.py
pkg/inst/opencpu/readme.txt
pkg/inst/opencpu/rexp_pb2.py
Log:
adding examples/tests with protobuf over https using OpenCPU
Added: pkg/inst/opencpu/ocpu-getdata.R
===================================================================
--- pkg/inst/opencpu/ocpu-getdata.R (rev 0)
+++ pkg/inst/opencpu/ocpu-getdata.R 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,10 @@
+# Jeroen Ooms
+#
+# HTTPS+ProtoBuf RPC POC using OpenCPU
+# Script below downloads MASS::Animals using protobuf
+library(RProtoBuf)
+library(httr)
+
+req <- GET ('https://public.opencpu.org/ocpu/library/MASS/data/Animals/pb')
+output <- unserialize_pb(req$content)
+identical(output, MASS::Animals)
Added: pkg/inst/opencpu/ocpu-getdata.py
===================================================================
--- pkg/inst/opencpu/ocpu-getdata.py (rev 0)
+++ pkg/inst/opencpu/ocpu-getdata.py 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# Jeroen Ooms
+#
+# HTTPS+ProtoBuf RPC POC using OpenCPU
+# Script below downloads MASS::Animals using protobuf
+import urllib2;
+from rexp_pb2 import *;
+
+#HTTP GET
+req = urllib2.Request('https://public.opencpu.org/ocpu/library/MASS/data/Animals/pb');
+res = urllib2.urlopen(req);
+
+#parse output pb
+msg = REXP();
+msg.ParseFromString(res.read());
+
+#the return value is a double vector in this case
+print(msg);
Added: pkg/inst/opencpu/ocpu-rpc.R
===================================================================
--- pkg/inst/opencpu/ocpu-rpc.R (rev 0)
+++ pkg/inst/opencpu/ocpu-rpc.R 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,27 @@
+# Jeroen Ooms
+#
+# HTTPS+ProtoBuf RPC POC using OpenCPU
+# The call below maps to: do.call(stats::rnorm, list(n=42, mean=100))
+
+# !! This requires httr (>= 0.2.99). Version 0.2 has a bug.
+# library(devtools)
+# install_github("httr")
+
+# Actual code
+library(RProtoBuf)
+library(httr)
+
+args <- list(n=42, mean=100)
+payload <- serialize_pb(args, NULL)
+
+req <- POST (
+ url = "https://public.opencpu.org/ocpu/library/stats/R/rnorm/pb",
+ body = payload,
+ add_headers(
+ "Content-Type" = "application/x-protobuf"
+ )
+)
+
+#This is the output of stats::rnorm(n=42, mean=100)
+output <- unserialize_pb(req$content)
+print(length(output))
Added: pkg/inst/opencpu/ocpu-rpc.py
===================================================================
--- pkg/inst/opencpu/ocpu-rpc.py (rev 0)
+++ pkg/inst/opencpu/ocpu-rpc.py 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# Jeroen Ooms
+#
+# HTTPS+ProtoBuf RPC POC using OpenCPU
+# The call below maps to: do.call(stats::rnorm, list(n=42, mean=100))
+import urllib2;
+from rexp_pb2 import *;
+
+#create the post payload, i.e. list(n=42, mean=100)
+payload = REXP(
+ rclass = 5,
+ rexpValue = [
+ REXP(rclass = 2, realValue = [42]),
+ REXP(rclass = 2, realValue = [100])
+ ],
+ attrName = [
+ "names"
+ ],
+ attrValue = [
+ REXP(rclass = 0, stringValue = [STRING(strval="n"), STRING(strval="mean")])
+ ]
+);
+
+#HTTP POST
+req = urllib2.Request(
+ 'https://public.opencpu.org/ocpu/library/stats/R/rnorm/pb',
+ data = payload.SerializeToString(),
+ headers = {
+ 'Content-type': 'application/x-protobuf'
+ }
+);
+res = urllib2.urlopen(req);
+
+#parse output pb
+msg = REXP();
+msg.ParseFromString(res.read());
+
+#the return value is a double vector in this case
+print(msg.realValue);
+
+
+##### To debug:
+#f = open("payload.msg", "wb")
+#f.write(payload.SerializeToString())
+#f.close()
+#
+# Then do in R do:
+# library(RProtoBuf)
+# payload <- unserialize_pb("payload.msg")
+# do.call(stats::rnorm, payload)
\ No newline at end of file
Added: pkg/inst/opencpu/readme.txt
===================================================================
--- pkg/inst/opencpu/readme.txt (rev 0)
+++ pkg/inst/opencpu/readme.txt 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,2 @@
+These scripts illustrate how protocol buffers can be used as a data interchange format
+or as the basis of an RPC protocol.
\ No newline at end of file
Added: pkg/inst/opencpu/rexp_pb2.py
===================================================================
--- pkg/inst/opencpu/rexp_pb2.py (rev 0)
+++ pkg/inst/opencpu/rexp_pb2.py 2014-01-05 02:40:05 UTC (rev 720)
@@ -0,0 +1,281 @@
+# Generated by the protocol buffer compiler. DO NOT EDIT!
+
+from google.protobuf import descriptor
+from google.protobuf import message
+from google.protobuf import reflection
+from google.protobuf import descriptor_pb2
+# @@protoc_insertion_point(imports)
+
+
+
+DESCRIPTOR = descriptor.FileDescriptor(
+ name='rexp.proto',
+ package='rexp',
+ serialized_pb='\n\nrexp.proto\x12\x04rexp\"\xb3\x03\n\x04REXP\x12!\n\x06rclass\x18\x01 \x02(\x0e\x32\x11.rexp.REXP.RClass\x12\x15\n\trealValue\x18\x02 \x03(\x01\x42\x02\x10\x01\x12\x14\n\x08intValue\x18\x03 \x03(\x11\x42\x02\x10\x01\x12)\n\x0c\x62ooleanValue\x18\x04 \x03(\x0e\x32\x13.rexp.REXP.RBOOLEAN\x12!\n\x0bstringValue\x18\x05 \x03(\x0b\x32\x0c.rexp.STRING\x12\x10\n\x08rawValue\x18\x06 \x01(\x0c\x12!\n\x0c\x63omplexValue\x18\x07 \x03(\x0b\x32\x0b.rexp.CMPLX\x12\x1d\n\trexpValue\x18\x08 \x03(\x0b\x32\n.rexp.REXP\x12\x10\n\x08\x61ttrName\x18\x0b \x03(\t\x12\x1d\n\tattrValue\x18\x0c \x03(\x0b\x32\n.rexp.REXP\"f\n\x06RClass\x12\n\n\x06STRING\x10\x00\x12\x07\n\x03RAW\x10\x01\x12\x08\n\x04REAL\x10\x02\x12\x0b\n\x07\x43OMPLEX\x10\x03\x12\x0b\n\x07INTEGER\x10\x04\x12\x08\n\x04LIST\x10\x05\x12\x0b\n\x07LOGICAL\x10\x06\x12\x0c\n\x08NULLTYPE\x10\x07\" \n\x08RBOOLEAN\x12\x05\n\x01\x46\x10\x00\x12\x05\n\x01T\x10\x01\x12\x06\n\x02NA\x10\x02\"-\n\x06STRING\x12\x0e\n\x06strval\x18\x01 \x01(\t\x12\x13\n\x04isNA\x18\x02 \x01(\x08:\x05\x66\x61lse\"&\n\x05\x43MPLX\x12\x0f\n\x04real\x18\x01 \x01(\x01:\x01\x30\x12\x0c\n\x04imag\x18\x02 \x02(\x01\x32(\n\x04ocpu\x12 \n\x06\x64oCall\x12\n.rexp.REXP\x1a\n.rexp.REXP')
+
+
+
+_REXP_RCLASS = descriptor.EnumDescriptor(
+ name='RClass',
+ full_name='rexp.REXP.RClass',
+ filename=None,
+ file=DESCRIPTOR,
+ values=[
+ descriptor.EnumValueDescriptor(
+ name='STRING', index=0, number=0,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='RAW', index=1, number=1,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='REAL', index=2, number=2,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='COMPLEX', index=3, number=3,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='INTEGER', index=4, number=4,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='LIST', index=5, number=5,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='LOGICAL', index=6, number=6,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='NULLTYPE', index=7, number=7,
+ options=None,
+ type=None),
+ ],
+ containing_type=None,
+ options=None,
+ serialized_start=320,
+ serialized_end=422,
+)
+
+_REXP_RBOOLEAN = descriptor.EnumDescriptor(
+ name='RBOOLEAN',
+ full_name='rexp.REXP.RBOOLEAN',
+ filename=None,
+ file=DESCRIPTOR,
+ values=[
+ descriptor.EnumValueDescriptor(
+ name='F', index=0, number=0,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='T', index=1, number=1,
+ options=None,
+ type=None),
+ descriptor.EnumValueDescriptor(
+ name='NA', index=2, number=2,
+ options=None,
+ type=None),
+ ],
+ containing_type=None,
+ options=None,
+ serialized_start=424,
+ serialized_end=456,
+)
+
+
+_REXP = descriptor.Descriptor(
+ name='REXP',
+ full_name='rexp.REXP',
+ filename=None,
+ file=DESCRIPTOR,
+ containing_type=None,
+ fields=[
+ descriptor.FieldDescriptor(
+ name='rclass', full_name='rexp.REXP.rclass', index=0,
+ number=1, type=14, cpp_type=8, label=2,
+ has_default_value=False, default_value=0,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='realValue', full_name='rexp.REXP.realValue', index=1,
+ number=2, type=1, cpp_type=5, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=descriptor._ParseOptions(descriptor_pb2.FieldOptions(), '\020\001')),
+ descriptor.FieldDescriptor(
+ name='intValue', full_name='rexp.REXP.intValue', index=2,
+ number=3, type=17, cpp_type=1, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=descriptor._ParseOptions(descriptor_pb2.FieldOptions(), '\020\001')),
+ descriptor.FieldDescriptor(
+ name='booleanValue', full_name='rexp.REXP.booleanValue', index=3,
+ number=4, type=14, cpp_type=8, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='stringValue', full_name='rexp.REXP.stringValue', index=4,
+ number=5, type=11, cpp_type=10, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='rawValue', full_name='rexp.REXP.rawValue', index=5,
+ number=6, type=12, cpp_type=9, label=1,
+ has_default_value=False, default_value="",
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='complexValue', full_name='rexp.REXP.complexValue', index=6,
+ number=7, type=11, cpp_type=10, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='rexpValue', full_name='rexp.REXP.rexpValue', index=7,
+ number=8, type=11, cpp_type=10, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='attrName', full_name='rexp.REXP.attrName', index=8,
+ number=11, type=9, cpp_type=9, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='attrValue', full_name='rexp.REXP.attrValue', index=9,
+ number=12, type=11, cpp_type=10, label=3,
+ has_default_value=False, default_value=[],
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ ],
+ extensions=[
+ ],
+ nested_types=[],
+ enum_types=[
+ _REXP_RCLASS,
+ _REXP_RBOOLEAN,
+ ],
+ options=None,
+ is_extendable=False,
+ extension_ranges=[],
+ serialized_start=21,
+ serialized_end=456,
+)
+
+
+_STRING = descriptor.Descriptor(
+ name='STRING',
+ full_name='rexp.STRING',
+ filename=None,
+ file=DESCRIPTOR,
+ containing_type=None,
+ fields=[
+ descriptor.FieldDescriptor(
+ name='strval', full_name='rexp.STRING.strval', index=0,
+ number=1, type=9, cpp_type=9, label=1,
+ has_default_value=False, default_value=unicode("", "utf-8"),
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='isNA', full_name='rexp.STRING.isNA', index=1,
+ number=2, type=8, cpp_type=7, label=1,
+ has_default_value=True, default_value=False,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ ],
+ extensions=[
+ ],
+ nested_types=[],
+ enum_types=[
+ ],
+ options=None,
+ is_extendable=False,
+ extension_ranges=[],
+ serialized_start=458,
+ serialized_end=503,
+)
+
+
+_CMPLX = descriptor.Descriptor(
+ name='CMPLX',
+ full_name='rexp.CMPLX',
+ filename=None,
+ file=DESCRIPTOR,
+ containing_type=None,
+ fields=[
+ descriptor.FieldDescriptor(
+ name='real', full_name='rexp.CMPLX.real', index=0,
+ number=1, type=1, cpp_type=5, label=1,
+ has_default_value=True, default_value=0,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ descriptor.FieldDescriptor(
+ name='imag', full_name='rexp.CMPLX.imag', index=1,
+ number=2, type=1, cpp_type=5, label=2,
+ has_default_value=False, default_value=0,
+ message_type=None, enum_type=None, containing_type=None,
+ is_extension=False, extension_scope=None,
+ options=None),
+ ],
+ extensions=[
+ ],
+ nested_types=[],
+ enum_types=[
+ ],
+ options=None,
+ is_extendable=False,
+ extension_ranges=[],
+ serialized_start=505,
+ serialized_end=543,
+)
+
+_REXP.fields_by_name['rclass'].enum_type = _REXP_RCLASS
+_REXP.fields_by_name['booleanValue'].enum_type = _REXP_RBOOLEAN
+_REXP.fields_by_name['stringValue'].message_type = _STRING
+_REXP.fields_by_name['complexValue'].message_type = _CMPLX
+_REXP.fields_by_name['rexpValue'].message_type = _REXP
+_REXP.fields_by_name['attrValue'].message_type = _REXP
+_REXP_RCLASS.containing_type = _REXP;
+_REXP_RBOOLEAN.containing_type = _REXP;
+DESCRIPTOR.message_types_by_name['REXP'] = _REXP
+DESCRIPTOR.message_types_by_name['STRING'] = _STRING
+DESCRIPTOR.message_types_by_name['CMPLX'] = _CMPLX
+
+class REXP(message.Message):
+ __metaclass__ = reflection.GeneratedProtocolMessageType
+ DESCRIPTOR = _REXP
+
+ # @@protoc_insertion_point(class_scope:rexp.REXP)
+
+class STRING(message.Message):
+ __metaclass__ = reflection.GeneratedProtocolMessageType
+ DESCRIPTOR = _STRING
+
+ # @@protoc_insertion_point(class_scope:rexp.STRING)
+
+class CMPLX(message.Message):
+ __metaclass__ = reflection.GeneratedProtocolMessageType
+ DESCRIPTOR = _CMPLX
+
+ # @@protoc_insertion_point(class_scope:rexp.CMPLX)
+
+# @@protoc_insertion_point(module_scope)
More information about the Rprotobuf-commits
mailing list