Skip to content

Commit

Permalink
feat: implement python-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
rasendubi committed Aug 21, 2024
1 parent 9db1233 commit 107b037
Show file tree
Hide file tree
Showing 28 changed files with 1,178 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"eppo_core",
"rust-sdk",
"python-sdk",
"ruby-sdk/ext/eppo_client",
]

Expand Down
10 changes: 9 additions & 1 deletion eppo_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eppo_core"
version = "2.0.0"
version = "2.1.0"
edition = "2021"
description = "Eppo SDK core library"
repository = "https://github.com/Eppo-exp/rust-sdk"
Expand All @@ -9,6 +9,10 @@ keywords = ["eppo", "feature-flags"]
categories = ["config"]
rust-version = "1.71.1"

[features]
# Add implementation of `FromPyObject`/`ToPyObject` for some types.
pyo3 = ["dep:pyo3", "dep:serde-pyobject"]

[dependencies]
chrono = { version = "0.4.38", features = ["serde"] }
derive_more = "0.99.17"
Expand All @@ -23,6 +27,10 @@ serde_json = "1.0.116"
thiserror = "1.0.60"
url = "2.5.0"

# pyo3 dependencies
pyo3 = { version = "0.22.0", optional = true, default-features = false }
serde-pyobject = { version = "0.4.0", optional = true}

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
env_logger = "0.11.3"
Expand Down
31 changes: 31 additions & 0 deletions eppo_core/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,34 @@ impl From<&str> for AttributeValue {
Self::String(value.to_owned())
}
}

#[cfg(feature = "pyo3")]
mod pyo3_impl {
use pyo3::{exceptions::PyTypeError, prelude::*, types::*};

use super::*;

impl<'py> FromPyObject<'py> for AttributeValue {
fn extract_bound(value: &Bound<'py, PyAny>) -> PyResult<AttributeValue> {
if let Ok(s) = value.downcast::<PyString>() {
return Ok(AttributeValue::String(s.extract()?));
}
// In Python, Bool inherits from Int, so it must be checked first here.
if let Ok(s) = value.downcast::<PyBool>() {
return Ok(AttributeValue::Boolean(s.extract()?));
}
if let Ok(s) = value.downcast::<PyFloat>() {
return Ok(AttributeValue::Number(s.extract()?));
}
if let Ok(s) = value.downcast::<PyInt>() {
return Ok(AttributeValue::Number(s.extract()?));
}
if let Ok(_) = value.downcast::<PyNone>() {
return Ok(AttributeValue::Null);
}
Err(PyTypeError::new_err(
"invalid type for subject attribute value",
))
}
}
}
17 changes: 17 additions & 0 deletions eppo_core/src/eval/eval_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,20 @@ impl From<EvaluationError> for BanditEvaluationCode {
}
}
}

#[cfg(feature = "pyo3")]
mod pyo3_impl {
use pyo3::prelude::*;

use crate::pyo3::TryToPyObject;

use super::EvaluationDetails;

impl TryToPyObject for EvaluationDetails {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
serde_pyobject::to_pyobject(py, self)
.map(|it| it.unbind())
.map_err(|err| err.0)
}
}
}
17 changes: 17 additions & 0 deletions eppo_core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ pub struct BanditEvent {
pub action_categorical_attributes: HashMap<String, String>,
pub meta_data: HashMap<String, String>,
}

#[cfg(feature = "pyo3")]
mod pyo3_impl {
use pyo3::{PyObject, PyResult, Python};

use crate::pyo3::TryToPyObject;

use super::AssignmentEvent;

impl TryToPyObject for AssignmentEvent {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
serde_pyobject::to_pyobject(py, self)
.map(|it| it.unbind())
.map_err(|err| err.0)
}
}
}
2 changes: 2 additions & 0 deletions eppo_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod configuration_store;
pub mod eval;
pub mod events;
pub mod poller_thread;
#[cfg(feature = "pyo3")]
pub mod pyo3;
pub mod sharder;
pub mod ufc;

Expand Down
30 changes: 30 additions & 0 deletions eppo_core/src/pyo3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use pyo3::prelude::*;

/// Similar to [`pyo3::ToPyObject`] but allows the conversion to fail.
pub trait TryToPyObject {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject>;
}

// Implementing on `&T` to allow dtolnay specialization[1] (e.g., for `Option<T>` below).
//
// [1]: https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md
impl<T: ToPyObject> TryToPyObject for &T {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
Ok(self.to_object(py))
}
}

impl<T> TryToPyObject for Py<T> {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
Ok(self.to_object(py))
}
}

impl<T: TryToPyObject> TryToPyObject for Option<T> {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
match self {
Some(it) => it.try_to_pyobject(py),
None => Ok(().to_object(py)),
}
}
}
25 changes: 25 additions & 0 deletions eppo_core/src/ufc/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,28 @@ impl AssignmentValue {
}
}
}

#[cfg(feature = "pyo3")]
mod pyo3_impl {
use pyo3::prelude::*;

use crate::pyo3::TryToPyObject;

use super::*;

impl TryToPyObject for AssignmentValue {
fn try_to_pyobject(&self, py: Python) -> PyResult<PyObject> {
let obj = match self {
AssignmentValue::String(s) => s.to_object(py),
AssignmentValue::Integer(i) => i.to_object(py),
AssignmentValue::Numeric(n) => n.to_object(py),
AssignmentValue::Boolean(b) => b.to_object(py),
AssignmentValue::Json(j) => match serde_pyobject::to_pyobject(py, j) {
Ok(it) => it.unbind(),
Err(err) => return Err(err.0),
},
};
Ok(obj)
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"test": "npm run with-server 'npm-run-all test:*'",
"test:rust": "cargo test",
"test:python": "cd python-sdk && maturin develop && pytest",
"test:ruby": "cd ruby-sdk && bundle exec rake test",
"with-server": "start-server-and-test start-mock-server http://127.0.0.1:8378",
"start-mock-server": "npm start --prefix ./mock-server"
Expand Down
72 changes: 72 additions & 0 deletions python-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/target

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
15 changes: 15 additions & 0 deletions python-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "eppo_py"
version = "5.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
eppo_core = { version = "2.1.0", features = ["pyo3"] }
log = "0.4.22"
pyo3 = { version = "0.22.0" }
pyo3-log = "0.11.0"
serde-pyobject = "0.4.0"
21 changes: 21 additions & 0 deletions python-sdk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Eppo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 107b037

Please sign in to comment.