1

I am using tkinter to manage the GUI for a note retrieval program. I can pull my notes by typing a key word and hitting Enter in a text field but I would like to move my dictionary to a file so that my code space is not filled up with a massive dictionary.

I have been looking around but I am not sure how I would go about doing this. I have the file in my directory. I know I can use open(“filename”, “mode”) to open said file for reading but how do I call each section of the notes.

For example what I do now is just call a keyword from my dictionary and have it write the definition for that keyword to a text box in my GUI. Can I do the same from the file?

How would I go about reading from the file the keyword and returning the definition to a variable or directly to the text box? For now I just need to figure out how to read the data. I think once I know that I can figure out how to write new notes or edit existing notes.

This is how I am set up now.

To call my my function

root.bind('<Return>', kw_entry)

How I return my definition to my text box

def kw_entry(event=None):
    e1Current = keywordEntry.get().lower()
    if e1Current in notes:
        root.text.delete(1.0, END)
        root.text.insert(tkinter.END, notes[e1Current])
        root.text.see(tkinter.END)
    else:
        root.text.delete(1.0, END)
        root.text.insert(tkinter.END, "Not a Keyword")
    root.text.see(tkinter.END)
martineau
  • 119,623
  • 25
  • 170
  • 301
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • did you look into pickle? – timgeb Mar 23 '17 at 17:54
  • 1
    @timgeb I am new to programing in general and very new to python. I have little knowledge outside of python and tkinter. I will check out pickle to see if its what I am looking for. Thanks for the info. – Mike - SMT Mar 23 '17 at 17:56
  • 1
    See [**_Saving an Object (Data persistence)_**](http://stackoverflow.com/questions/4529815/saving-an-object-data-persistence) for examples of using the `pickle` module. It will work with dictionaries whose values are picklable. – martineau Mar 23 '17 at 17:59
  • JSON is great if you are only storing things like dicts, lists, numbers and strings. It also has the advantage of being readable by many other languages... and you can kinda read it yourself. Pickle stores more data types but is python specific. There are many other choices including `msgpack` which is like json but more compact on disk. That is not an issue for a triffling few megs of data. – tdelaney Mar 23 '17 at 18:29
  • Ya I just finished moving my dictionary and list to files using json. I am happy with the results. I do have a quick question. The file that is stored is storing the info all on one line. Is there a way to have the file store the information on each line per definition in the json file? – Mike - SMT Mar 23 '17 at 18:31
  • 1
    Add `indent=" "` to the `json.dump` call. – tdelaney Mar 23 '17 at 18:32
  • Also, when saving, save to a temp file and then rename it to your real file. If you end up with something in that dict that is not json serializable, json will fail... but will have already trashed your data. – tdelaney Mar 23 '17 at 18:33
  • @tdelaney Thanks for the info on indent="" that worked perfectly. Now I have a easy to manage json file that I can read easy without having to scroll so much to the side :) – Mike - SMT Mar 23 '17 at 18:41

2 Answers2

4

Sound's like you'd need to load the dictionary to memory at init time, and use it like a normal dictionary.

I am assuming your dictionary is a standard python dict of strings, so I recommend using the python json lib.

Easiest way to do this is to export the dictionary as json once to a file using something like:

with open(filename, 'w') as fp:
    json.dump(dictionary, fp)

and then change your code to load the dict at init time using:

with open(filename) as fp:
    dictionary = json.load(fp)

Alternatively, if your data is more complex than text, you can use python shelve which is a persistent, dictionary-like object to which you can pass any pickle-able object. Note that shelve has its drawbacks so read the attached doc.

theannouncer
  • 1,148
  • 16
  • 28
  • By loading the dictionary into memory like this am I able to also edit the dictionary and put it back to the file when my program closes? – Mike - SMT Mar 23 '17 at 17:58
  • 1
    @BaconTech if you save it at the end yes. Alternatively, you can use python shelve, but it also has its drawbacks – theannouncer Mar 23 '17 at 18:00
  • Thanks for the info. I do not know what python shelve is but the JSON thing looks promising. I will do some research on the json option and work with that for now :D – Mike - SMT Mar 23 '17 at 18:01
  • @BaconTech I added examples for json and a brief summary of shelve above. – theannouncer Mar 23 '17 at 18:03
  • the json option looks like what I need from your example. I am going to test it now to see how it works and then I should be all set. Thanks for the info again. – Mike - SMT Mar 23 '17 at 18:08
  • Thanks for the help on this one. The json method worked perfectly for being able to read my dictionary and list. I now have what I need to move onto writing to said dictionary and list when adding new notes. – Mike - SMT Mar 23 '17 at 18:29
0

sqlitedict is a project providing a persistent dictionary using sqlite in the background. You can use it like a normal dictionary e.g. by assigning arbitrary (picklable) objects to it.

If you access an element from the dictionary, only the value you requested is loaded from disk.

Arco Bast
  • 3,595
  • 2
  • 26
  • 53