سؤال

I'm trying to write a snippet of code that adds an ID to a set so I can see if its already been used, and stores the set in a file. I've been attempting to do this by using the Shelve module but I'm running into a bit of trouble. So far I have this code

import praw
import datetime
import shelve

user_agent ='Removed'
r = praw.Reddit(user_agent=user_agent)
submission = r.get_submission(submission_id='11v36o')
r.login('Removed','Removed')
files = shelve.open("PrawTest3.dat", writeback=True)
print "Opened!"
already_done = {} 
files["already_done"] = ["a","b"]
files.close()
done = set()
print "Running"

while True:
    subreddit = r.get_subreddit('mobilebot')
    all_comments = subreddit.get_comments()
    files = shelve.open("PrawTest2.dat", writeback=True)
    already_done = files["already_done"]
    files.close()
    for comment in all_comments:
        if (comment.body == "Hello") and (comment.id not in already_done) and (comment.id not in done):

            files = shelve.open("PrawTest2.dat", writeback=True)
            comment.reply(' world!')
            already_done = files["already_done"]
            already_done.append(comment.id)

            files[already_done] = already_done
            print "Shelves working"
            a = datetime.datetime.now()
            b = "%s:%s:%s" % (a.hour,a.minute, a.second)
            print "["+b+"]"+"Comment sent!"
            files.sync()
            files.close()
هل كانت مفيدة؟

المحلول

Python's list can't be used as a key for a shelve or dic as it doesn't have a hash value. But I think your problem is just a typo in this line:

files[already_done] = already_done

I think you want it to be

files["already_done"] = already_done

نصائح أخرى

The error is in the line:

files[already_done] = already_done

list in python is mutable. Mutable types can not be used as a key in a dictionary. Convert your list already_done to a tuple before doing files[already_done] = already_done and it will work fine.

This is what I mean:

>>> a_dict = {}
>>> a_list = [1, 2, 3]
>>> a_dict[a_list] = "Hello"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'


>>> a_tuple = tuple(a_list)
>>> a_dict[a_tuple] = "Hello"
>>> a_dict
{(1, 2, 3): 'Hello'}

However if you just wanted the key to be a string "already_done" and not the list already_done, you should be doing:

files['already_done'] = already_done

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top