Skip to content

Instantly share code, notes, and snippets.

@sunahsuh
Created June 25, 2018 18:56
Show Gist options
  • Select an option

  • Save sunahsuh/07abea4d099872818db59d26bf485c54 to your computer and use it in GitHub Desktop.

Select an option

Save sunahsuh/07abea4d099872818db59d26bf485c54 to your computer and use it in GitHub Desktop.
import argparse
import csv
import json
BASE_SCHEMA = {
"eventGroupName": "dt",
"filters": {
"docType": [
"main"
],
"appName": [
"Firefox",
],
},
"events": []
}
def get_event(name, categories, methods, objects, values, amplitude_props, user_props, descr=""):
props = {
"timestamp": {
"type": "number",
"minimum": 0
},
"category": {
"type": "string",
"enum": categories
},
"method": {
"type": "string",
"enum": methods
},
"object": {
"type": "string",
"enum": objects
}
}
if values:
props["value"] = {
"type": "string",
"enum": values
}
return {
"name": name,
"description": descr,
"amplitudeProperties": amplitude_props,
"userProperties": user_props,
"schema": {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": props,
"required": list(props.keys())
}
},
def sep(val):
return [e.strip() for e in val.split(",") if e.strip()]
def convert(filename):
default = BASE_SCHEMA
with open(filename, 'r') as f:
events = list(csv.reader(f, delimiter=',', quotechar='"'))
for e in events:
name, desc, cs, ms, os, vs, aps, ups = e
amplitude_props = {}
if aps:
separated = sep(aps)
for e in separated:
k, v = e.split("=")
amplitude_props[k] = v
user_props = {}
if ups:
separated = sep(ups)
for e in separated:
k, v = e.split("=")
user_props[k] = v
default["events"].append(
get_event(
name,
sep(cs),
sep(ms),
sep(os),
sep(vs),
amplitude_props,
user_props
)
)
return default
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="CSV to Schema converter for Events to Amplitude",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"filename",
help = "CSV file with the data to convert"
)
args = parser.parse_args()
print json.dumps(convert(args.filename), sort_keys = True, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment