Python Forum
How to design a save file format? - 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: How to design a save file format? (/thread-33453.html)



How to design a save file format? - philipbergwerf - Apr-26-2021

The situation is an alternative music notation app. I've created a program in which you edit the save file directly; lilypond like notation app. But now I want to create a more graphical app that does the same. Because I want it to import midi files I need some sort of save file format that contains all information like notes, staffsize, time signature, text, dynamic text, etc... I am a beginner in this and I hope there are some starting points on how to make a save file format?

I like a save format that can be opened by a text editor and something I really liked is the way Pure Data patches are written. So I am searching for something like that. A problem to overcome is for the program to be able to detect and edit a note-length for example. In my current program, the user is doing that. I do like to hear about save files. All I found were examples of really simple game save files that only contain basic game player info.

Useful links are much appreciated!

Screenshot from my current project: https://pasteboard.co/JZ8ylCR.png

A idea I came up with:
~score{
	~staff{
		~right{
			~beam{id, start, end}
			~note{id, note, start, end}
			~note{id, note, start, end}
		}
		~left{
			~beam{id, start, end}
			~note{id, note, start, end}
			~note{id, note, start, end}
		}
		~textanddynamics{
			~dynamictext{id, text, start, xoffset, yoffset, bgcolor}
			~crescendo{id, start, end}
		}
	}
}



RE: How to design a save file format? - Gribouillis - Apr-26-2021

philipbergwerf Wrote:something I really liked is the way Pure Data patches are written
My search engine found only this specification of Pd file format. Using this format would mean writing a parser and a code generator for the format, which doesn't seem to be very easy because there is apparently no formal grammar specification.

I don't know much about music or music software programming, but you could perhaps simply store the data in Python structures such as dicts lists and tuples and write the repr() of these python objects in a file. It would be easy to write and to read for a python program. It could also be opened in a text editor.

For example the following Python tuple contains the same information as the above example, but it is directly loadable by a python program.
(
    'score',
    (
        'staff',
        (
            'right',
            ('beam', id, start, end),
            ('note', id, note, start, end),
            ('note', id, note, start, end),),
        (
            'left',
            ('beam', id, start, end),
            ('note', id, note, start, end),
            ('note', id, note, start, end),),
        (
            'textanddynamics',
            ('dynamictext', id, text, start, xoffset, yoffset, bgcolor),
            ('creschendo', id, start, end)),))



RE: How to design a save file format? - philipbergwerf - Apr-26-2021

(Apr-26-2021, 04:44 PM)Gribouillis Wrote:
philipbergwerf Wrote:something I really liked is the way Pure Data patches are written
My search engine found only this specification of Pd file format. Using this format would mean writing a parser and a code generator for the format, which doesn't seem to be very easy because there is apparently no formal grammar specification.

I don't know much about music or music software programming, but you could perhaps simply store the data in Python structures such as dicts lists and tuples and write the repr() of these python objects in a file. It would be easy to write and to read for a python program. It could also be opened in a text editor.

For example the following Python tuple contains the same information as the above example, but it is directly loadable by a python program.
(
    'score',
    (
        'staff',
        (
            'right',
            ('beam', id, start, end),
            ('note', id, note, start, end),
            ('note', id, note, start, end),),
        (
            'left',
            ('beam', id, start, end),
            ('note', id, note, start, end),
            ('note', id, note, start, end),),
        (
            'textanddynamics',
            ('dynamictext', id, text, start, xoffset, yoffset, bgcolor),
            ('creschendo', id, start, end)),))
If I want to read a python list from a txt file; How? Lets say we have this file as txt:
[item1, item2, item3]
how do I read it as python list? can you make a simple example?


RE: How to design a save file format? - Gribouillis - Apr-26-2021

Here is a complete example using pprint() to write the file and ast.literal_eval() to evaluate the contents of the file. There are some limitations to this but it will work if the file contains only basic python data types and the structure is not too deeply nested. Furthermore it is safe against malicious data
import io
import ast
from pprint import pprint

mylist = [
    'score',
    (
        'staff',
        (
            'right',
            ('beam', 877, 0, 12),
            ('note', 766, 'A', 13, 34),
            ('note', 988, 'C', 22, 100),),
        (
            'left',
            ('beam', 824, 7, 22),
            ('note', 'foo', 33, 876),
            ('note', 'bar', 55, 997),),
        (
            'textanddynamics',
            ('dynamictext', 'spam', 'corge', 31, 7, 8, 'blue'),
            ('crescendo', 'grault', 15, 188)),)]

def save(afile, obj):
    pprint(obj, stream=afile, indent=4)
    
def load(afile):
    s = afile.read().strip()
    return ast.literal_eval(s)

def main():
    myfile = io.StringIO()
    save(myfile, mylist)
    # let us see how the list was saved in a file
    print('We saved:')
    print(myfile.getvalue())
    # let us now load the list from the file and see what we get
    myfile.seek(0)
    obj = load(myfile)
    print()
    print('We loaded:')
    pprint(obj)
    print('saved == loaded ?', mylist == obj)

if __name__ == '__main__':
    main()



RE: How to design a save file format? - buran - Apr-26-2021

You can look also at JSON. Python json module is part of the standard library


RE: How to design a save file format? - Gribouillis - Apr-26-2021

Also there seems to be an ecosystem of Python modules that interact with Pure Data files. I found this module on Pypi that lets you write Pure Data patches from Python. It may be the way to go.