[CentralOH] DoJo Mumbilings for March 28, 2019
Thomas Winningham
winningham at gmail.com
Thu Mar 28 17:41:36 EDT 2019
1. Fixes row 9 to have category 2
2. Creates an EAV "facts" table, sort of like a graph
3. Assumes only one matching value, no aggregate sums, etc
4. Extendable to other similar problems, although report generation is
rather hard coded for this dataset
import uuid
fields = ['id','category','type','value']
data = [
[1,1,"A",2],
[2,3,"D",5],
[3,4,"B",7],
[4,2,"C",8],
[5,4,"D",9],
[6,4,"C",1],
[7,1,"D",3],
[8,3,"C",9],
[9,2,"A",5],
[10,3,"A",4],
[11,1,"B",6],
[12,4,"A",2],
[13,2,"D",6],
[14,3,"B",3],
[15,2,"B",8],
[16,1,"C",0]
]
# Create fact table
facts = []
for x in data:
this_fact=str(uuid.uuid1())
for f in range(len(fields)):
facts.append((this_fact, fields[f], x[f]))
# Create some query functions for EAV structure
E,A,V = 0,1,2
q_e4av = lambda a,v : [x[E] for x in facts if x[A]==a and x[V]==v]
q_v4a = lambda a : [x[V] for x in facts if x[A]==a]
q_v4ea = lambda e,a : [x[V] for x in facts if x[E]==e and x[A]==a]
# Generate report
uniq_cats = sorted(list(set(q_v4a('category'))))
uniq_typs = sorted(list(set(q_v4a('type'))))
result=[]
for c in uniq_cats:
row=[]
for t in uniq_typs:
cat_ids = set(q_e4av('category',c))
typ_ids = set(q_e4av('type',t))
result_entity = list(cat_ids & typ_ids)
if len(result_entity) > 0:
row.append(q_v4ea(result_entity[0],'value')[0])
else:
row.append(None)
result.append(row)
print('\t'+'\t'.join(uniq_typs))
for c in range(len(uniq_cats)):
print('\t'.join([str(uniq_cats[c])]+[repr(x) for x in result[c]]))
#-----------------------
$ python3 test1.py
A B C D
1 2 6 0 3
2 5 8 8 6
3 4 3 9 5
4 2 7 1 9
On Thu, Mar 28, 2019 at 3:53 PM Travis Risner <deeppunster at gmail.com> wrote:
> Hi folks,
>
> Here is a problem proposed by one of our own members. (The problem is
> in the attached PDF.)
>
> Travis
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20190328/e2abf45e/attachment.html>
More information about the CentralOH
mailing list