Python Forum
Recommendation for data structure - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Recommendation for data structure (/thread-5947.html)



Recommendation for data structure - muteboy - Oct-30-2017

Hi,
I'm self-teaching myself Python using examples and the excellent documentation.
My project is a program that uses a single random seed to generate a made-up "record label", with multiple "artists", each with multiple "albums", each with multiple "tracks", which are generated CSound music code files. It also names the artists, albums, tracks and members of each band, and it generates artwork and album covers.
(If anyone was interested in learning more about this project, PM me. I'd be glad of collaborators!)

I am currently storing the generated data in dictionaries, for example:
label_data = {label_name: "xxx", num_artists: 3, etc etc}
artist_data = {artist_name: "yyy", artist_id: (sequential), num_albums: 5, etc etc}
album_data = {id, name, year, num_tracks, etc etc}
track_data = {id, bpm, tune_pattern, snare_drum_pattern, etc etc}

My question is, would nesting these structures be efficient? The number of objects would vary, due to the randomness.
1 label
1-10 artists
each with 1-10 albums
each with 1-10 tracks
This would mean quite a large number of objects, with 4 levels of nesting.

Would a different data structure be better, for example SQLite? Then I could relate the various objects together using ids and queries.

Grateful for any suggestions.

Best regards,
Matthew Petty
Dubai


RE: Recommendation for data structure - Larz60+ - Oct-30-2017

I like to nest dictionaries of related material.
It's easy to do, and then I save as json.
see CreateFIPScodeDescJson.py  listing here: https://python-forum.io/Thread-Practical-use-of-pathlib


RE: Recommendation for data structure - buran - Oct-30-2017

You mix what structure will be used for in-memory manipulation of data and what to use to store data on disk.
sqlite or any other database to that matter will make difference how you retrieve information from disk storage, but you can put this information in whatever data structure you want to manipulate it in memory. e.g. in your other post you were asking about classes, but now I see you still not use classes. As larz60+ suggest you may use also json to store the info on disk, but if this is going to be a serious long-term project I would opt for sqlite. It will be relatively easy to move to different [relational] DB later if you decide so.
Nested dictionaries or not will make difference on how you will retrieve individual records. You can have nested dictionaries also with relational database as backend.


RE: Recommendation for data structure - muteboy - Oct-30-2017

(Oct-30-2017, 07:17 AM)buran Wrote: You mix what structure will be used for in-memory manipulation of data and what to use to store data on disk.
sqlite or any other database to that matter will make difference how you retrieve information from disk storage, but you can put this information in whatever data structure you want to manipulate it in memory. e.g. in your other post you were asking about classes, but now I see you still not use classes. As larz60+ suggest you may use also json to store the info on disk, but if this is going to be a serious long-term project I would opt for sqlite. It will be relatively easy to move to different [relational] DB later if you decide so.
Nested dictionaries or not will make difference on how you will retrieve individual records. You can have nested dictionaries also with relational database as backend.

Hi buran,
The data is only used once, to generate other files, although it would be good to dump the data used in a file for reference. I think I will stick with dictionaries for now.

I have not been able to figure out how to change my code to use classes. I've replied to that thread.

Thanks,
Matthew