1

I'm trying to figure out how to use the data a user enters as input to get information from a TinyDB DB.

My DB looks something like this:

{"_default": {"1": {"switch": "n9k-c9372px", "names": ["nexus 9372px", "nexus 9372-px", "nexus9372px", "n9372px", "n9k-c9372px"], "fex_comp": ["2224tp", "2232pp"]}, "2": {"switch": "n9k-c9396px", "names": ["nexus 9396px", "nexus 9396-px", "nexus9396px", "n9396px", "n9k-c9396px"], "fex_comp": ["2232tm-e", "2248tp"]}}}

Basically, the DB is the result of two dictionaries with lists, like these:

{"switch": "switch1", "names": ["name1", "name2", "name3"], "fex_comp":["fex1", "fex2", "fex3"]

My idea is the following:

  1. To have a prompt asking for a switch model (q= input("Tell me the model")).
  2. Take the input (q) from the user, and check if it matches any of the "names" in the database.
  3. If it does, then print the fex_comp list, the whole list. Otherwise, print a different message.

I understand how to form the if, else, statements and also how to use for loops, but I haven't managed to figure out how to do what I describe above.

Any help is much appreciated!

Edvard

Edvard Haugland
  • 125
  • 1
  • 9

1 Answers1

0

Like so then?

   from tinydb import TinyDB, Query

    ql = ['nexus9372px','nexus9396px', 'not_there']

    def mkdb():
        db = TinyDB('db.json')
        db.purge()
        db.insert({'switch': 'n9k-c9372px',
                   'names': ['nexus 9372px',
                             'nexus 9372-px',
                             'nexus9372px', 'n9372px'],
                   'fex_comp': ['2224tp', '2232pp',
                                '2232tm', '2232tm-e']})
        db.insert({"switch": "n9k-c9396px",
                   "names": ["nexus 9396px", "nexus 9396-px",
                             "nexus9396px", "n9396px",
                             "n9k-c9396px"],
                   "fex_comp": ["2232tm-e", "2248tp"]})
        return(db)

    def get_name():
        return(input('Name? '))

    def search(name, db):
        Name = Query()
        res = db.search(Name.names.any(name))
        if res:
            #print(res)
            print('fex_comp for {}: {}'.format(name, res[0]['fex_comp']))
        else:
            print('{} not found'.format(name))

    db = mkdb()
    name = get_name()
    search(name, db)
hvwaldow
  • 1,296
  • 1
  • 11
  • 13
  • Hi, thanks for answering. It actually doesn't work for me. It seems to me your script is not actually fetching data from TinyDB, since TinyDB uses search.db methods to get data. I'm also under the impression that you have to declare variable = Query() somewhere for it to work, though I can be wrong about that. Could you please show how I could implement your suggestion including an input prompt and fetching data from a TinyDB data base? – Edvard Haugland Jan 21 '17 at 16:08
  • I see, that was a misunderstanding. Can you provide the code that created the database? – hvwaldow Jan 21 '17 at 17:32
  • Sure, this twice, for 2 switch models: # db.insert({'switch': 'n9k-c9372px', 'names': ['nexus 9372px', 'nexus 9372-px', 'nexus9372px', 'n9372px'], 'fex_comp': ['2224tp', '2232pp', '2232tm', '2232tm-e'}) – Edvard Haugland Jan 21 '17 at 17:34
  • 1
    Note that this is very pedestrian. For example in case the queried for 'name' is in multiple records only one will be returned. Honestly it looks to me as if using TinyDB is not much better than pickling you data to disk, or use json.dumps(). Depending on what you are doing, you might want to switch to pandas (http://pandas.pydata.org/) or SQLite (https://docs.python.org/3.6/library/sqlite3.html) – hvwaldow Jan 21 '17 at 18:40
  • I think you are right. Now that you mention it, it maybe that using TinyDB is not the right tool for the job in this case. I think I'll give SQLite a try. Thanks for your help! – Edvard Haugland Jan 21 '17 at 18:56
  • BTW, your new code is very close to what I'd like to do, but it returns the whole dictionary, instead of just returning the contents of fex_comp. I'll try with SQLite though, thanks for your time! – Edvard Haugland Jan 21 '17 at 19:02
  • It doesn't return the whole DB but the complete top-level record that contains the match. Yeah, still, that kind of defeats the purpose of having a DB. – hvwaldow Jan 21 '17 at 19:03