├── testMD.py └── MD.py /testMD.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Tests for Simple Liquid representation test for Mesh Democracy 3 | Basic concept is to form a heirachy of groups 4 | Allow for representation in groups 5 | Pass representation into subgroups 6 | ''' 7 | import MD 8 | 9 | #main function to perform a small test 10 | def main(): 11 | #make topic tree (topic, parent) 12 | MD.addTopic("science") 13 | MD.addTopic("physics", "science") 14 | MD.addTopic("chemistry", "science") 15 | MD.addTopic("biology", "science") 16 | 17 | MD.addTopic("computing") 18 | MD.addTopic("programming", "computing") 19 | MD.addTopic("python", "programming") 20 | MD.addTopic("c++", "programming") 21 | MD.addTopic("java", "programming") 22 | 23 | MD.addTopic("linux", "computing") 24 | MD.addTopic("GNU", "computing") 25 | MD.addTopic("windows", "computing") 26 | MD.addTopic("apple", "computing") 27 | MD.addTopic("android", "computing") 28 | 29 | 30 | #in science bob represents alice 31 | MD.selectRepresentitive("science","bob", "alice") 32 | MD.selectRepresentitive("computing","alex", "alice") 33 | MD.selectRepresentitive("science","bob", "alice") 34 | 35 | #in physics eve represents bob 36 | MD.selectRepresentitive("physics","eve","bob") 37 | MD.selectRepresentitive("programming","alex","bob") 38 | 39 | 40 | MD.selectRepresentitive("python","alex","dave") 41 | 42 | 43 | MD.selectRepresentitive("programming","alex","sara") 44 | 45 | 46 | MD.selectRepresentitive("computing","alex","jane") 47 | 48 | 49 | MD.selectRepresentitive("computing","sara","mike") 50 | 51 | 52 | MD.selectRepresentitive("programming","dave","sally") 53 | 54 | 55 | MD.selectRepresentitive("computing","sally","franko") 56 | 57 | 58 | MD.selectRepresentitive("computing","bob","john") 59 | 60 | #imagine a post in physics where only eve votes 61 | votes={"alex":1,} 62 | 63 | #or eve and bob 64 | #votes={"eve":1, "bob":1} 65 | 66 | #or alice 67 | #votes={"alice":1} 68 | 69 | MD.countVotes("python",votes) 70 | 71 | 72 | if __name__ == "__main__": 73 | main() -------------------------------------------------------------------------------- /MD.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Simple Liquid representation test for Mesh Democracy 3 | Basic concept is to form a heirachy of groups 4 | Allow for representation in groups 5 | Pass representation into subgroups 6 | ''' 7 | import time 8 | #3 people in this test - bob knows alice and eve. alice and eve do not know each other. 9 | #people = {"alice":{}, "bob":{}, "eve":{}} 10 | 11 | #simple dict of topics 12 | topicDict = {} 13 | 14 | #store a representation with timestamp 15 | def selectRepresentitive(topic, representer, representee): 16 | topicReps = topicDict[topic]["reps"] 17 | if representee not in topicReps:topicReps[representee]={} 18 | topicReps[representee]["representer"]= representer; 19 | topicReps[representee]["timestamp"]= time.time(); 20 | 21 | #recursive function to get all representees of a voter using the "topic tree" 22 | #could do with optimisation and clarification 23 | def getRepVotes(voter, topic): 24 | print "getRepVotes(",voter, ",",topic,")" 25 | topicScore=0 26 | 27 | if "parent" in topicDict[topic]: 28 | topicScore+=getRepVotes(voter, topicDict[topic]["parent"]) 29 | #print topicDict[topic] 30 | topicReps = topicDict[topic]["reps"] 31 | for representee in topicReps: 32 | if representee in liquidVoted: 33 | pass 34 | #print "Representee: "+representee+" already counted, skipping (check timestamps?)" 35 | else: 36 | if representee not in directVoted: 37 | representer = topicReps[representee]["representer"] 38 | if representer == voter: 39 | topicScore+=1 40 | liquidVoted[representee]=True 41 | if "parent" in topicDict[topic]: 42 | topicScore+=getRepVotes(representee, topicDict[topic]["parent"]) 43 | return topicScore 44 | 45 | directVoted={}#dict of people who have already voted directly 46 | liquidVoted={}#dict of people who have been counted as a representee 47 | 48 | #get result of liquid votes 49 | def countVotes(vTopic,tVotes): 50 | #reset for recount 51 | directVoted={} 52 | liquidVoted={} 53 | directscore = 0 54 | liquidscore = 0 55 | 56 | #count direct votes 57 | for v in tVotes: 58 | directscore+=tVotes[v] 59 | directVoted[v]=True 60 | print "directscore",directscore 61 | 62 | #count representees recursivly 63 | for v in tVotes: 64 | liquidscore+=getRepVotes(v, vTopic) 65 | print "liquidscore",liquidscore 66 | 67 | print "FINALSCORE",directscore+liquidscore 68 | 69 | def clearTopics(): 70 | topicDict = {} 71 | 72 | def addTopic(key, parent=None): 73 | topicDict[key] = {"reps":{}} 74 | if parent: 75 | topicDict[key]["parent"] = parent 76 | 77 | 78 | 79 | #main function to perform a small test 80 | def main(): 81 | #make topic tree (topic, parent) 82 | addTopic("science") 83 | addTopic("physics", "science") 84 | 85 | #in science bob represents alice 86 | selectRepresentitive("science","bob", "alice") 87 | 88 | #in physics eve represents bob 89 | selectRepresentitive("physics","eve","bob") 90 | 91 | #imagine a post in physics where only eve votes 92 | votes={"eve":1,} 93 | 94 | #or eve and bob 95 | #votes={"eve":1, "bob":1} 96 | 97 | #or alice 98 | #votes={"alice":1} 99 | 100 | countVotes("physics",votes) 101 | 102 | 103 | if __name__ == "__main__": 104 | main() --------------------------------------------------------------------------------