Python client to manage PrimedIO
Project description
# pyprimed: a python library to manage PrimedIO
Create a personalized web application that is unique and relevant for each and every user with [Primed.io](https://www.primed.io/).
## Installation
```
pip install pyprimed
```
## Quickstart
### Import the SDK and initiate the connection
```python
from pyprimed.pio import Pio
pio = Pio(uri='http://<user>:<password>@<api_url>:<port>')
```
### Create a Universe, and attach a few Targets
```python
# create a new universe and attach a single target
pio\
.universes\
.create(name='myfirstuniverse')\
.targets\
.upsert([{'key':'ARTICLE-1', 'value':{'url': 'www.example.com/article-1'}}])
# retrieving the newly created universe
u = pio.universes.filter(name='myfirstuniverse').first
# list all targets currently associated with this universe
for target in u.targets.all():
print(target.key, target.created_at)
# prepare a list of new targets
new_targets = [
{'key': 'ARTICLE-2', 'value': {'url': 'www.example.com/article-2'}},
{'key': 'ARTICLE-3', 'value': {'url': 'www.example.com/article-3'}}
]
# upsert the new targets
u.targets.upsert(new_targets)
# targets are upserted, which means that for a given key there
# can be only one instance in the database. Trying to create an
# instance with the same key will update the value of the record
# in the database
u.targets.upsert([{'key':'ARTICLE-1', 'value':{'url': 'THIS IS NEW!'}}])
u.targets.filter(key='ARTICLE-1').first.value
```
### Create a Model, and attach a few Signals
```python
# create a new model and attach a single signal
pio\
.models\
.create(name='myfirstmodel')\
.signals\
.upsert([{'key':'ALICE'}])
# retrieving the created model
m = pio.models.filter(name='myfirstmodel').first
# list all signals currently associated with this model
for signal in m.signals.all():
print(signal.key, signal.created_at)
# prepare a list of new signals
new_signals = [
{'key': 'BOB'},
{'key': 'CHRIS'}
]
# create the new signals
m.signals.upsert(new_signals)
# prepare a set of predictions (sk stand for signal.key, and tk for target.key)
# WARNING: `sk` and `tk` should always be a string!
predictions = [
{'sk': 'ALICE', 'tk': 'ARTICLE-1', 'score': 0.35},
{'sk': 'BOB', 'tk': 'ARTICLE-1', 'score': 0.75},
{'sk': 'CHRIS', 'tk': 'ARTICLE-1', 'score': 0.15}
]
# create the new predictions
u = pio.universes.filter(name='myfirstuniverse').first
pio\
.predictions\
.on(model=m, universe=u)\
.upsert(predictions)
```
### Create a Campaign, Experiment and set up an AB test to start using the Predictions
```python
from pyprimed.models.abvariant import CustomAbvariant, CONTROL
# create a campaign
c = u.campaigns.create(key='test.campaign', name='myfirstcampaign')
# create an experiment for which we will define abvariants
e = c.experiments.create(name='myfirstexperiment')
# define an abvariant with only one model
ab = CustomAbvariant(label='A', models={m: 1.0})
# attach the abvariant to the experiment, ensuring 90% of traffic will flow to
# the abvariant, and 10% will flow to CONTROL
e.abvariants.create({ab: 0.9, CONTROL: 0.1})
# get personalized!
c.personalize(
pubkey='mypubkey',
secretkey='mysecretkey',
signals={'userid': 'BOB'}
)
```
## Developer
Build the documentation:
```
cd docs && pydocmd build
```
Preview documentation on http://localhost:8000
```
cd docs && pydocmd serve
```
Create a personalized web application that is unique and relevant for each and every user with [Primed.io](https://www.primed.io/).
## Installation
```
pip install pyprimed
```
## Quickstart
### Import the SDK and initiate the connection
```python
from pyprimed.pio import Pio
pio = Pio(uri='http://<user>:<password>@<api_url>:<port>')
```
### Create a Universe, and attach a few Targets
```python
# create a new universe and attach a single target
pio\
.universes\
.create(name='myfirstuniverse')\
.targets\
.upsert([{'key':'ARTICLE-1', 'value':{'url': 'www.example.com/article-1'}}])
# retrieving the newly created universe
u = pio.universes.filter(name='myfirstuniverse').first
# list all targets currently associated with this universe
for target in u.targets.all():
print(target.key, target.created_at)
# prepare a list of new targets
new_targets = [
{'key': 'ARTICLE-2', 'value': {'url': 'www.example.com/article-2'}},
{'key': 'ARTICLE-3', 'value': {'url': 'www.example.com/article-3'}}
]
# upsert the new targets
u.targets.upsert(new_targets)
# targets are upserted, which means that for a given key there
# can be only one instance in the database. Trying to create an
# instance with the same key will update the value of the record
# in the database
u.targets.upsert([{'key':'ARTICLE-1', 'value':{'url': 'THIS IS NEW!'}}])
u.targets.filter(key='ARTICLE-1').first.value
```
### Create a Model, and attach a few Signals
```python
# create a new model and attach a single signal
pio\
.models\
.create(name='myfirstmodel')\
.signals\
.upsert([{'key':'ALICE'}])
# retrieving the created model
m = pio.models.filter(name='myfirstmodel').first
# list all signals currently associated with this model
for signal in m.signals.all():
print(signal.key, signal.created_at)
# prepare a list of new signals
new_signals = [
{'key': 'BOB'},
{'key': 'CHRIS'}
]
# create the new signals
m.signals.upsert(new_signals)
# prepare a set of predictions (sk stand for signal.key, and tk for target.key)
# WARNING: `sk` and `tk` should always be a string!
predictions = [
{'sk': 'ALICE', 'tk': 'ARTICLE-1', 'score': 0.35},
{'sk': 'BOB', 'tk': 'ARTICLE-1', 'score': 0.75},
{'sk': 'CHRIS', 'tk': 'ARTICLE-1', 'score': 0.15}
]
# create the new predictions
u = pio.universes.filter(name='myfirstuniverse').first
pio\
.predictions\
.on(model=m, universe=u)\
.upsert(predictions)
```
### Create a Campaign, Experiment and set up an AB test to start using the Predictions
```python
from pyprimed.models.abvariant import CustomAbvariant, CONTROL
# create a campaign
c = u.campaigns.create(key='test.campaign', name='myfirstcampaign')
# create an experiment for which we will define abvariants
e = c.experiments.create(name='myfirstexperiment')
# define an abvariant with only one model
ab = CustomAbvariant(label='A', models={m: 1.0})
# attach the abvariant to the experiment, ensuring 90% of traffic will flow to
# the abvariant, and 10% will flow to CONTROL
e.abvariants.create({ab: 0.9, CONTROL: 0.1})
# get personalized!
c.personalize(
pubkey='mypubkey',
secretkey='mysecretkey',
signals={'userid': 'BOB'}
)
```
## Developer
Build the documentation:
```
cd docs && pydocmd build
```
Preview documentation on http://localhost:8000
```
cd docs && pydocmd serve
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyprimed-2.1.0.tar.gz
(23.4 kB
view hashes)