Skip to content

Instantly share code, notes, and snippets.

@yszou
Created Apr 29, 2022
Embed
What would you like to do?
py code can run as micropython
# -*- coding: utf-8 -*-
import random
import json
import js
class RandomData():
def __init__(self, rows=5, dimensions=1, metrics=2):
self.data = []
self.rows = rows
self.dimensions = dimensions
self.metrics = metrics
def randnum(self, count=1):
c = '0123456789'
s = ''.join([random.choice(c) for x in range(count)])
return s
def randchar(self, count=1):
c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s = ''.join([random.choice(c) for x in range(count)])
return s
def randhan(self, count=1):
return self.randchar(count)
def get_data(self):
dimension_name_list = [self.randchar(5) for x in range(self.dimensions)]
metric_name_list = [self.randchar(8) for x in range(self.metrics)]
self.data.append(dimension_name_list + metric_name_list)
for i in range(self.rows):
row = []
for j in range(len(dimension_name_list)):
row.append(self.randhan(2))
for j in range(len(metric_name_list)):
row.append(str(int(self.randnum(3))))
self.data.append(row)
csv = '\n'.join([' | '.join(row) for row in self.data])
return csv
class BaseChart():
type = ''
rows = 1
dimensions = 1
columns = 1
def randchar(self, count=1):
c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s = ''.join([random.choice(c) for x in range(count)])
return s
def __init__(self):
self.name = self.randchar(8)
def get_data_name(self):
return self.name
def get_data(self):
return RandomData(self.rows, self.dimensions, self.columns).get_data()
def gen_expr(self):
params = self.get_params()
params_str = ['{}: {}'.format(key, value) for (key, value) in params.items()]
return '+{} {} {}'.format(self.type, '|' if params_str else '', ' | '.join(params_str))
def get_params(self):
return {}
class Title(BaseChart):
type = 'Title'
def get_params(self):
return {'title': RandomData(0, 1, 0).get_data()}
class Table(BaseChart):
type = 'Table'
def get_data(self):
return RandomData(5, random.choice([1,2,3]), random.choice([1,2,3])).get_data()
def get_params(self):
return {'data': '${}'.format(self.name)}
class Space(BaseChart):
type = 'Space'
def get_params(self):
return {'height': 10}
class Hr(BaseChart):
type = 'Hr'
class Series(BaseChart):
type = 'Series'
def get_data(self):
return RandomData(10, 1, random.choice([1,2,3,4])).get_data()
def get_params(self):
return {'data': '${}'.format(self.name), 'type': random.choice(['bar', 'line'])}
class Pie(BaseChart):
type = 'Pie'
def get_data(self):
return RandomData(5, 1, 1).get_data()
def get_params(self):
return {'data': '${}'.format(self.name)}
class Radar(BaseChart):
type = 'Radar'
def get_data(self):
return RandomData(3, 1, 3).get_data()
def get_params(self):
return {'data': '${}'.format(self.name)}
class Funnel(BaseChart):
type = 'Funnel'
def get_data(self):
return RandomData(5, 1, 3).get_data()
def get_params(self):
return {'data': '${}'.format(self.name)}
class Report():
source = { 'transform': [] }
component_list = []
def add_data(self, name, data):
self.source['transform'].append({
'id': name,
'type': 'csv',
'template': data,
})
def add_component(self, expr):
self.component_list.append(expr)
def as_json(self):
return json.dumps({'source': self.source, 'componentList': self.component_list})
if __name__ == '__main__':
report = Report()
count = 0
while count < 3:
report.add_component(Title().gen_expr())
Chart = random.choice([Table, Series, Pie, Radar, Funnel])
chart = Chart()
report.add_data(chart.get_data_name(), chart.get_data())
report.add_component(chart.gen_expr())
report.add_component(Space().gen_expr())
report.add_component(Hr().gen_expr())
count += 1
config = report.as_json()
print(config)
js.emit(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment