19

I'm trying to run a Python script using cron in Linux, which should construct a dictionary of data. I'm attempting to use datetime().now().time()as keys in the dictionary, but it seems to raise an error.

Can't the datetime type be used as a dictionary key in Python? If that is the case, what are my alternatives?

Code:

time_now = dt.datetime.now().time()
date_today = dt.datetime.now().date()
usage_dict_hourly = {}
date_wise_dict = {}

def constructing_dict(data_int):
    date_wise_dict[usage_dict_hourly[time_now]] = data_int
    print date_wise_dict

Error:

<ipython-input-9-ef6a500cc71b> in constructing_dict(data_int)
     36 
     37 def constructing_dict(data_int):
---> 38     date_wise_dict[usage_dict_hourly[time_now]] = data_int
     39     print date_wise_dict
     40 

KeyError: datetime.time(22, 40, 33, 746509)
emartinelli
  • 1,019
  • 15
  • 28
user3591675
  • 399
  • 1
  • 4
  • 15
  • 3
    You didn't define `usage_dict_hourly` in your example, and if you meant `usage_hourly_dict`, you didn't ever put anything in it. No wonder it can't find the key if there aren't any keys at all. – zondo Mar 11 '16 at 17:33
  • 1
    It might be worth adding a few comments to your code. Apart from being good practice, this will be useful for us now and you/whoever else looks at this in the future. It's not clear what `date_wise_dict` and `useage_dict_hourly` are supposed to contain. – DaveBensonPhillips Mar 11 '16 at 17:42
  • You are correct. That's a typo I committed that while I was typing here. But it doesn't solve the error. – user3591675 Mar 11 '16 at 18:02
  • Despite the typo, as @zondo stated your error was caused because `usage_dict_hourly` is empty, so there is nothing to be found there. It was not caused by the key type as datetime. >> *To be used as a dictionary key, an object must support the hash function (e.g. through \_\_hash\_\_), equality comparison (e.g. through \_\_eq\_\_ or \_\_cmp\_\_)* (Source: [DictionaryKeys](https://wiki.python.org/moin/DictionaryKeys)) – emartinelli Mar 11 '16 at 18:06

2 Answers2

11

Answering your question about datetime as a dictionary key:

Yes, time object of datetime can be used as dictionary key.

Conditions to use an object as a dictionary key:

To be used as a dictionary key, an object must support the hash function (e.g. through __hash__), equality comparison (e.g. through __eq__ or __cmp__)(...)

(Source: DictionaryKeys)

datetime support for cited conditions:

Supported operations:

...

  • hash, use as dict key

(Source: datetime - time Objects)

However, you are getting this exception because dict usage_dict_hourly is empty.

usage_dict_hourly is initiated with {}. So, when you try to look for the element with key time_now in your function constructing_dict, it raises KeyError exception, because that key has not been found.

Community
  • 1
  • 1
emartinelli
  • 1,019
  • 15
  • 28
1

You aren't actually setting useage_dict_hourly to any value that way, that's your error. You should ideally do something like:

useage_dict_hourly[time_now] = None
date_wise_dict[useage_dict_hourly[time_now]] = data_int

Or, better yet, just use datetime.datetime.now() as the key:

datetime_wise_date[datetime.datetime.now()] = data_int.

This way, there is no possibility of clashes in the value (which is possible - though unlikely - for useage_dict_hourly)

Abhishek Divekar
  • 1,131
  • 2
  • 15
  • 31