Skip to content

Commit

Permalink
Big overhaul. Ready for V0.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlurrySquire committed Apr 3, 2024
1 parent 254c8ba commit 19f6196
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 74 deletions.
55 changes: 25 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,29 @@
This python module helps you easily create save data for your python app or game! It only requires a few function calls to work, and uses default python modules, so no need to install any other modules. There is no need to credit me but it would be appreciated.

## QuickStart Guide
1. To start you must download ``save_data.py`` from link[ here](https://github.com/BlurrySquire/Python-JSON-SaveData/blob/main/save_data.py).
2. Paste this code into the start of your main python file:
1. To start you must download ``save_data.py`` [here](https://github.com/BlurrySquire/Python-JSON-SaveData/blob/main/save_data.py).
2. This is an example of the usage:
```python
import save_data as save # import the module

# create the JSON dictionary containing the default save
default_save = {
"value": "value"
}

# set the default save file
save.set_default_save(default_save)

# create the save file
save.create('save', default_save, indent=4)

# read the save file to a variable
sd = save.read_save()

# edit the values in the save file
sd["value"] = 'edited_value'

# write the save file
save.write_save(sd, indent=4)
```
3. Mess around with the default_save dictionary, in your code, for your save file.
4. You now have a working save file!

## Notices
- There is more documentation coming soon, that will explain how to use each function.
- The code isnt perfect, if there is any errors please let me know by opening an issue.
from save_data import *

if __name__ == "__main__":
# Create a dictionary to contain the save data.
save_file_data = {
"user_info": {
"username": "guest user"
},
"wins": 0,
"loses": 0
}

# Create a savefile. If the file doesn't already exist then it creates it.
save_file = SaveFile("test.json")

save_file.write(save_file_data)
print(save_file.read())

# Modify save data to ensure it is writing the file properly
save_file_data["wins"] = 1
save_file.write(save_file_data)
print(save_file.read())
```
62 changes: 18 additions & 44 deletions save_data.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
# created by https://github.com/BlurrySquire
# github repo: https://github.com/BlurrySquire/Python-JSON-SaveData/
# please do not advertise this as your own
# Licensed under MIT License as of April 2024
# Created by https://github.com/BlurrySquire
# Repo can be found at: https://github.com/BlurrySquire/Python-JSON-SaveData

import json, os

default_save = {
"user_info": {
"username": "guest_user"
},
"wins": 0,
"loses": 0
}

save_name = 'save.json'

def set_default_save(value=default_save):
default_save = value

def create_save(name='save', value=default_save, indent=4):
global save_name
save_name = f'{name}.json'

with open(f'{name}.json', 'w') as f:
f.write(json.dumps(value, indent=indent))

def auto_create_save(name='save', value=default_save, indent=4):
if not os.path.exists(f'{name}.json'):
create_save(name, value, indent)

def reset_save():
if os.path.exists(save_name):
with open(save_name, 'w') as f:
f.write(json.dumps(default_save))
return read_save()

def read_save():
if os.path.exists(save_name):
with open(save_name, 'r') as f:
return json.loads(f.read())
else:
return default_save

def write_save(save_value=default_save, indent=4):
if os.path.exists(save_name):
with open(save_name, 'w') as f:
f.write(json.dumps(save_value, indent=indent))
class SaveFile:
def __init__(self, filename: str) -> None:
self.filename: str = filename

# If the file doesn't exist, make it.
if not os.path.exists(self.filename):
open(self.filename, 'w').close()

def read(self) -> dict:
with open(self.filename, 'r') as file:
return json.load(file)

def write(self, contents: dict) -> None:
with open(self.filename, 'w') as file:
json.dump(contents, file)
1 change: 1 addition & 0 deletions test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"user_info": {"username": "guest user"}, "wins": 1, "loses": 0}
19 changes: 19 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from save_data import *

if __name__ == "__main__":
save_file_data = {
"user_info": {
"username": "guest user"
},
"wins": 0,
"loses": 0
}

save_file = SaveFile("test.json")

save_file.write(save_file_data)
print(save_file.read())

save_file_data["wins"] = 1
save_file.write(save_file_data)
print(save_file.read())

0 comments on commit 19f6196

Please sign in to comment.