-1

New to Python and all things database related. Wondering when I should consider using a database and why? I have what is essentially a list of objects that is around 30000 lines long. I'm developing a web app which will need to search this list for the correct object depending on user input.

For more context, the list contains all valid and unique musical chord names, each with the attributes that make that chord what it is. Eg. Notes, Interval notation. It is the name of the chord the user will type in, and it is the chord attributes they will get back.

I would like to know the quickest way of pulling an object from this list of objects by searching the objects name, and how I can implement it in python with minimal external libraries.

Currently I'm just parsing through a .txt file and converting the string line I find back to the proper object in python and returning that.

Any help is appreciated!

1
  • 1
    "I would like to know the quickest way of pulling an object from this list of objects by searching the objects name" Why? Is this actually the major time sink on the critical path of your application? Premature optimisation is the root of all evil, etc. – Philip Kendall May 14 '20 at 10:21
3

In general, since this data is fairly static, an in-memory data structure that is initialized from a file might suffice. I don't know whether a list with linear search is the best choice, but you could use it as a start and change to a more optimized data structure when necessary.

However, your case (musical chord names) seems to be of a highly systematic nature. If I were given this task I would try to implement it by parsing the input and generating the answer algorithmically, not by searching for an exact match in a list.

3

The absolute quickest way is probably to load the list into a hashtable where the keys are the chord names and the value the set of attributes. If this solves your problem, I doubt any other solution could be faster or simpler.

If the data does not change, and you can load it all into memory at the same time, you don't need a more complicated solution.

A database become appropriate if any of this is the case:

  • You have (or may get) more data than fits in memory all at once.
  • You have a data model which is more complex than what can be reasonably expressed in a single flat file.
  • You need more complicated searching or matching
  • The data can change.
1
  • 1
    @Numpy In Python a hashtable is called a dictionary or 'dict' e.g. {key: value, ...}. – JimmyJames May 14 '20 at 13:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.