Interacting with Python code from R has become very convenient via the reticulate package which also takes care of most standard data types used by R users. On top of that, arrow facilitates exchange of vectors and especially data.frame (or in its parlance RecordBatch) objects. Using nanoarrow to do so takes the (considerable) pain of building with arrow away.
But sometimes we want bespoke custom objects from a specific library. R does well with external pointer objects, so a recent question in the Rcpp context was: how do we do this with Python?
This repository has one answer and working demonstration. It uses a very small but clever class: a ‘stopwatch’ implementation taken (with loving appreciation and a nod) from the lovely spdlog library, and specifically the already simplified version in RcppSpdlog presented by this ‘spdlog_stopwatch.h’ file.
It is used by the demo in the corresponding R package from the sibbling repository chronometre-r.
So once installed this minimal demo in main.py does
this:
#!/usr/bin/env python3
import chronometre
import time
def main():
s = chronometre.Stopwatch()
print(s)
print(s.elapsed())
time.sleep(0.1)
print(s.elapsed())
s2 = chronometre.bake(s)
print(s2)
print(s2.elapsed())
time.sleep(0.1)
print(s2.elapsed())
print(s.elapsed())
if __name__ == "__main__":
main()It allocates a stopwatch object, and then ‘clones’ it via a helper function into a second object still pointing at the same memory location and hence sharing the object. (From R we can also use an additional constructor, but when we use that from Python ‘lifetime’ behavior gets in the way; the factory helper function gets around that. A small nuance.)
This generated sample output such as the following:
(chronometre) $ ./main.py
<chronometre._chronometre.Stopwatch object at 0x7ba8071abc70>
0:00:00.000016
0:00:00.100226
<chronometre._chronometre.Stopwatch object at 0x7ba8071abc70>
0:00:00.100287
0:00:00.200371
0:00:00.200408
(chronometre) $demonstrating that the memory address is in fact the same, and the behavior is shared.
Dirk Eddelbuettel for the sample Python package
Gabi Melman for the initial stopwatch class
The package as a whole is licensed under the GPL (version 2 or later).
The stopwatch C++ class by Gabi Melman is MIT licensed.