Code: Alles auswählen
import sys
class TagIndexer(object):
"""
class to help to organize list of comma separated tags
"""
def __init__(self):
self.__tagDic = {}
self.__groups = {}
def add(self, key, taglist):
"""add new entry to list: key is a string, taglist contains all tags as comma separated list"""
tags = taglist.split(',')
for t in tags:
try:
tmp = self.__tagDic[t]
self.__tagDic[t]['keys'].append(key)
except:
if (t != ''):
self.__tagDic[t] = {}
self.__tagDic[t]['keys'] = []
self.__tagDic[t]['keys'].append(key)
def update_groups(self):
""" Update Reverse Dictionary to get tag-groups by key"""
max = 0
maxtag = ''
keylist = []
for t in self.__tagDic.items():
print(t)
keylist.append(t[1]['keys'])
"""
Missing code to create self.__groups in the following way (based on current static input in main)
Ethernet: A,C,E,F,G
USB: F,G
Ethernet,USB: F,G
Ethernet,MOST: C,E,F
...
"""
self.__groups['Ethernet']=['A','C','E','F','G'] # static code for demo
self.__groups['Ethernet,USB']=['F','G'] # static code for demo (example only!)
@property
def MaxTag(self):
"""Return tag with maximum occurance"""
max = 0
maxtag = ''
for t in self.__tagDic:
c = self.__tagDic[t]['count']
if c > max:
max = c
maxtag = t
return((max, maxtag))
def dump(self):
print('--- Dump Tags Start ---')
for t in self.__tagDic:
print('Tag: ', t)
print(' Keys: ', self.__tagDic[t]['keys'])
print('--- Dump Tags End ---')
print('--- Dump Groups Start ---')
for g in self.__groups:
print('Group: ', g, self.__groups[g])
print('--- Dump Groups End ---')
def main(args):
ti = TagIndexer()
ti.add('A', 'Ethernet')
ti.add('B', 'MOST')
ti.add('C', 'Ethernet,MOST')
ti.add('D', 'CAN,MOST')
ti.add('E', 'Ethernet,MOST,CAN')
ti.add('F', 'Ethernet,MOST,USB')
ti.add('G', 'Ethernet,USB')
ti.dump()
ti.update_groups()
ti.dump()
if __name__ == '__main__':
main(sys.argv)