Skip to content

Commit

Permalink
Python fixes (#71)
Browse files Browse the repository at this point in the history
* * James is masquerading as Stallone
* Added to_pydoubles, to_pylongs, from_pydoubles, from_pylongs to the Python port

* Changed python port to support various methods already available
in various container .cpp files and Madaraknowledge.cpp in main
Madara directory.

* Added NativeCircularBufferConsumer class to MadaraKnowledgeContainers.cpp
Changed function defaults to read_file function
Added .staticmethod to certain KnowledgeRecord class (get_precision,
set_fixed, set_precision, set_scientific)

* Added NativeCircularBufferConsumer class
  • Loading branch information
jredmondson committed Sep 14, 2018
1 parent f14a48d commit 1cc4d47
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 11 deletions.
15 changes: 9 additions & 6 deletions port/python/src/FunctionDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@
********************************************************/

/********************************************************
* Knowledge Record overloads
* Class members (functions inside of classes)
********************************************************/

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (m_to_string_0_of_1,
to_string, 0, 1)


/********************************************************
* Knowledge Base overloads
********************************************************/

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (m_get_1_of_2,
get, 1, 2)
Expand Down Expand Up @@ -102,6 +97,14 @@ BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (m_create_vector_2_of_3,
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (m_from_kb_2_of_3,
from_kb, 2, 3)

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (m_read_file_1_of_2,
read_file, 1, 2)


/********************************************************
* Static functions (standalone functions)
********************************************************/

BOOST_PYTHON_FUNCTION_OVERLOADS (file_from_fragments_2_of_4,
madara::utility::file_from_fragments, 2, 4)

Expand Down
18 changes: 13 additions & 5 deletions port/python/src/MadaraKnowledge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,17 +470,20 @@ void define_knowledge (void)
// clears the value to a 0 integer
.def ("clear_value", &madara::knowledge::KnowledgeRecord::clear_value,
"Sets the value to 0 and type to integer")

// sets the contents of the record to a jpeg
// sets the contents of the record as a file
.def ("read_file", &madara::knowledge::KnowledgeRecord::read_file,
"Reads the contents of a file into the record")
m_read_file_1_of_2 (
args("filename", "read_as_type"),
"Reads the contents of a file into the record. Type "
"is an optional field that can force a type for reading into."))

// gets the double precision
.def ("get_precision", &madara::knowledge::KnowledgeRecord::get_precision,
"Gets the double precision used in to_string")

// decrements an index of an array
.def ("dec_index", &madara::knowledge::KnowledgeRecord::inc_index,
.def ("dec_index", &madara::knowledge::KnowledgeRecord::dec_index,
"Decrements an array element at a particular index")

// increments an index of an array
Expand Down Expand Up @@ -611,7 +614,7 @@ void define_knowledge (void)
.def ("exists", &madara::knowledge::KnowledgeRecord::exists,
"Returns whether the knowledge has been set/modified/created")

// convert to a string
// show record information
.def ("status", &madara::knowledge::KnowledgeRecord::status,
"Returns the status of the record")

Expand Down Expand Up @@ -790,6 +793,11 @@ void define_knowledge (void)
.def ("to_any", MADARA_MEMB(Any, KnowledgeRecord, to_any, () const),
"Convert this record to an Any")

.staticmethod("get_precision")
.staticmethod("set_fixed")
.staticmethod("set_precision")
.staticmethod("set_scientific")

; // end of KnowledgeRecord


Expand Down
107 changes: 107 additions & 0 deletions port/python/src/MadaraKnowledgeContainers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "madara/knowledge/KnowledgeBase.h"
#include "madara/knowledge/containers/CircularBuffer.h"
#include "madara/knowledge/containers/CircularBufferConsumer.h"
#include "madara/knowledge/containers/NativeCircularBufferConsumer.h"
#include "madara/knowledge/containers/Double.h"
#include "madara/knowledge/containers/Integer.h"
#include "madara/knowledge/containers/NativeDoubleVector.h"
Expand Down Expand Up @@ -329,6 +330,112 @@ void define_knowledge_containers (void)

;

class_<madara::knowledge::containers::NativeCircularBufferConsumer> (
"NativeCircularBufferConsumer",
"References a circular buffer created by a CircularBuffer container."
" This provides a personalized consumer with a local index for iteration",
init <> ())

.def (init <const std::string &, madara::knowledge::KnowledgeBase &> ())

// methods

// retrieves the number of records
.def ("count",
&madara::knowledge::containers::NativeCircularBufferConsumer::count,
"Returns the number of records in the buffer")

// get latest
.def ("consume",
static_cast<
madara::knowledge::KnowledgeRecord
(madara::knowledge::containers::NativeCircularBufferConsumer::*)(
void
) const
> (&madara::knowledge::containers::NativeCircularBufferConsumer::consume),
"Consumes earliest record in the buffer")

// gets the underlying prefix/name
.def ("get_dropped",
&madara::knowledge::containers::NativeCircularBufferConsumer::get_dropped,
"Returns the number of records dropped since last consume")

// gets the underlying prefix/name
.def ("get_name",
&madara::knowledge::containers::NativeCircularBufferConsumer::get_name,
"Returns the underlying name of the container")

// get latest
.def ("inspect",
static_cast<
madara::knowledge::KnowledgeRecord
(madara::knowledge::containers::NativeCircularBufferConsumer::*)(
madara::knowledge::KnowledgeRecord::Integer
) const
> (&madara::knowledge::containers::NativeCircularBufferConsumer::inspect),
"Inspects the record at the indicated position in the buffer. "
"This position can be positive or negative from current position")

// gets the latest n records
.def ("inspect",
static_cast<
std::vector <madara::knowledge::KnowledgeRecord>
(madara::knowledge::containers::NativeCircularBufferConsumer::*)(
madara::knowledge::KnowledgeRecord::Integer, size_t
) const
> (&madara::knowledge::containers::NativeCircularBufferConsumer::inspect),
"Inspects the records at the indicated position in the buffer. "
"This position can be positive or negative from current position")

// returns the remaining records
.def ("remaining",
&madara::knowledge::containers::NativeCircularBufferConsumer::remaining,
"Returns the remaining records in the buffer")

// sets the index
.def ("set_index",
&madara::knowledge::containers::NativeCircularBufferConsumer::set_index,
"Sets the buffer index to an arbitrary position")

// sets the name
.def ("set_name",
static_cast<
void (madara::knowledge::containers::NativeCircularBufferConsumer::*)(
const std::string &, madara::knowledge::KnowledgeBase &
)
> (&madara::knowledge::containers::NativeCircularBufferConsumer::set_name),
"Sets the name inside of the Knowledge Base for the container to use")

// sets the name
.def ("set_name",
static_cast<
void (madara::knowledge::containers::NativeCircularBufferConsumer::*)(
const std::string &, madara::knowledge::Variables &
)
> (&madara::knowledge::containers::NativeCircularBufferConsumer::set_name),
"Sets the name inside of the Knowledge Base for the container to use")

// sets the name
.def ("set_name",
static_cast<
void (madara::knowledge::containers::NativeCircularBufferConsumer::*)(
const std::string &, madara::knowledge::ThreadSafeContext &
)
> (&madara::knowledge::containers::NativeCircularBufferConsumer::set_name),
"Sets the name inside of the Knowledge Base for the container to use")

// returns the size of the buffer
.def ("size",
&madara::knowledge::containers::NativeCircularBufferConsumer::size,
"Returns the size of the buffer")

// .....
.def ("get_record",
&madara::knowledge::containers::NativeCircularBufferConsumer::get_record,
".....")

;


class_<madara::knowledge::containers::Integer> (
"Integer",
Expand Down

0 comments on commit 1cc4d47

Please sign in to comment.