Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
123 lines (102 sloc) 5.23 KB
from pydomo.datasets import DataSetRequest, Schema, Column, ColumnType, Policy
from pydomo.datasets import PolicyFilter, FilterOperator, PolicyType, Sorting
def datasets(domo):
'''DataSets are useful for data sources that only require
occasional replacement. See the docs at
'''"\n**** Domo API - DataSet Examples ****\n")
datasets = domo.datasets
# Define a DataSet Schema
dsr = DataSetRequest() = 'Leonhard Euler Party'
dsr.description = 'Mathematician Guest List'
dsr.schema = Schema([Column(ColumnType.STRING, 'Friend')])
# Create a DataSet with the given Schema
dataset = datasets.create(dsr)"Created DataSet " + dataset['id'])
# Get a DataSets's metadata
retrieved_dataset = datasets.get(dataset['id'])"Retrieved DataSet " + retrieved_dataset['id'])
# List DataSets
dataset_list = list(datasets.list(sort=Sorting.NAME))"Retrieved a list containing {} DataSet(s)".format(
# Update a DataSets's metadata
update = DataSetRequest() = 'Leonhard Euler Party - Update'
update.description = 'Mathematician Guest List - Update'
update.schema = Schema([Column(ColumnType.STRING, 'Friend'),
Column(ColumnType.STRING, 'Attending')])
updated_dataset = datasets.update(dataset['id'], update)"Updated DataSet {}: {}".format(updated_dataset['id'],
# Import Data from a string
csv_upload = '"Pythagoras","FALSE"\n"Alan Turing","TRUE"\n' \
'"George Boole","TRUE"'
datasets.data_import(dataset['id'], csv_upload)"Uploaded data to DataSet " + dataset['id'])
# Export Data to a string
include_csv_header = True
csv_download = datasets.data_export(dataset['id'], include_csv_header)"Downloaded data from DataSet {}:\n{}".format(
dataset['id'], csv_download))
# Export Data to a file (also returns a readable/writable file object)
csv_file_path = './math.csv'
include_csv_header = True
csv_file = datasets.data_export_to_file(dataset['id'], csv_file_path,
csv_file.close()"Downloaded data as a file from DataSet {}".format(
# Import Data from a file
csv_file_path = './math.csv'
datasets.data_import_from_file(dataset['id'], csv_file_path)"Uploaded data from a file to DataSet {}".format(
# Personalized Data Policies (PDPs)
# Build a Policy Filter (hide sensitive columns/values from users)
pdp_filter = PolicyFilter()
pdp_filter.column = 'Attending' # The DataSet column to filter on
pdp_filter.operator = FilterOperator.EQUALS
pdp_filter.values = ['TRUE'] # The DataSet row value to filter on
# Build the Personalized Data Policy (PDP)
pdp_request = Policy() = 'Only show friends attending the party'
# A single PDP can contain multiple filters
pdp_request.filters = [pdp_filter]
pdp_request.type = PolicyType.USER
# The affected user ids (restricted access by filter)
pdp_request.users = [998, 999]
# The affected group ids (restricted access by filter)
pdp_request.groups = [99, 100]
# Create the PDP
pdp = datasets.create_pdp(dataset['id'], pdp_request)"Created a Personalized Data Policy (PDP): "
"{}, id: {}".format(pdp['name'], pdp['id']))
# Get a Personalized Data Policy (PDP)
pdp = datasets.get_pdp(dataset['id'], pdp['id'])"Retrieved a Personalized Data Policy (PDP):"
" {}, id: {}".format(pdp['name'], pdp['id']))
# List Personalized Data Policies (PDP)
pdp_list = datasets.list_pdps(dataset['id'])"Retrieved a list containing {} PDP(s) for DataSet {}"
.format(len(pdp_list), dataset['id']))
# Update a Personalized Data Policy (PDP)
# Negate the previous filter (logical NOT). Note that in this case you
# must treat the object as a dictionary - `pdp_filter.not` is invalid
# syntax.
pdp_filter['not'] = True = 'Only show friends not attending the party'
# A single PDP can contain multiple filters
pdp_request.filters = [pdp_filter]
pdp = datasets.update_pdp(dataset['id'], pdp['id'], pdp_request)"Updated a Personalized Data Policy (PDP): {}, id: {}"
.format(pdp['name'], pdp['id']))
# Delete a Personalized Data Policy (PDP)
datasets.delete_pdp(dataset['id'], pdp['id'])"Deleted a Personalized Data Policy (PDP): {}, id: {}"
.format(pdp['name'], pdp['id']))
# Delete a DataSet
datasets.delete(dataset['id'])"Deleted DataSet {}".format(dataset['id']))