0

I am trying to build a dictionary for a discord bot. The data structure will look like this:

poll = {
  'LINK' : {'MoonRaccoon' : 1, 'TheDirtyTree' : 1},
  'ZRX' : {'MoonRaccoon' : 1, 'Dontcallmeskaface' : 1, 'TheDirtyTree' : 1},  
  'XRP' : {'Dontcallmeskaface' : 1},
  'XLM' : {'aeon' : 1, 'Bob' : 1} 
}

this will then be saved to db["poll"] in the Repl.it database. https://docs.repl.it/misc/database which i guess is just a persistent dictionary? (I don't think this is relevant to the issue I'm having though)

users will enter their vote like

!v ZRX

The bot then needs to add that "vote" to the dictionary.

The code I am trying to get to do that goes like so:

update:

def update_poll(ticker,requestor):
  vote = {ticker : {requestor : 1}}

for (key, value) in vote.items():
    if 'poll' not in db.keys():
        db['poll'] = []
    if key in db['poll'].keys():
        db['poll'][key].append(value)
    else:
        db['poll'][key] = value

it's not going well though.. Some have previously suggested using defaultdict but i honestly have not been able to wrap my brain around it yet. python novice here..

any suggestions would be most appreciated.

  • So you want a persistant dictionnary with? – Synthase Jan 13 '21 at 04:41
  • Repl.it offers the persistent dictionary with the `db` variable.. i just have to figure out how to work with it. seems to work fine when i load it with the test set of data above.. not so much when i try to add items one by one. – Robin Goodheart Jan 13 '21 at 04:44
  • Just one question. Why don't you just pickle a normal dict in a file to preserve it? – Synthase Jan 13 '21 at 04:49
  • btw, once i figure out how to add items one by one I will also want to be able to take them away one by one. basically remove usernames from the ticker symbol dictionary until there are 0 items in it in which case the ticker symbol key will be deleted. – Robin Goodheart Jan 13 '21 at 04:50
  • This is my first time taking a crack at building a discord bot and i honestly don't know enough about it.. found a tutorial on building a bot on Repl,it so trying to work with the tools they offer first. If I end up running the bot somewhere else i guess i will need another solution. it works fine with the above test data structure so it seems my issue is with manipulating that data structure. – Robin Goodheart Jan 13 '21 at 04:52
  • Ok, just edit your post I would like to see what print(db) yields. I have no knowledge of repl stuff. – Synthase Jan 13 '21 at 04:54
  • i don't think this issue is with repl stuff.. it's a library. i have one of these at the top.. `from replit import db`. but i'm not building the data structure properly with the user input. everything is hunky dory if i just load that above data structure into db["poll"]. and now i can sort it. i can restart the bot and it perists. i can delete the whole thing and reload it. but i still can't build and manipulate the data structure one by one. If I can build it I can save it to db["poll"] and that persists. – Robin Goodheart Jan 13 '21 at 05:22
  • I did a print of `db` and it just gave me what looked like an URL to an API with a really long token attached to it. it didn't even show me db["poll"] although i can print it separately so i know it's there somehow.. through the API library i guess. – Robin Goodheart Jan 13 '21 at 05:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227248/discussion-between-synthase-and-robin-goodheart). – Synthase Jan 13 '21 at 05:28

1 Answers1

1

Try this:

ticker = 'a'; requestor = 'b'
vote = {ticker : {requestor : 1}}
db = {}
for (key, value) in vote.items():
    if 'poll' not in db.keys():
        db['poll'] = {}
    if key in db['poll'].keys():
        db['poll'][key].append(value)
    else:
        db['poll'][key] = value
Synthase
  • 5,849
  • 2
  • 12
  • 34