So I'm making a project, and it will need stuff like datastores, so I'm creating a class that already have that handled for me.
Basically, I want my end result to look like this:
CoinStore = StoreService.FetchStore("CoinStore") # if coinstore exists, it will just refer from it, if not, create a datastore with the same name.
CoinStore.SaveData(50) # obviously 50 wouldn't be consistant for everyone, but just for the sake of this.
Basically my method is to create a text file, write all the data to it, and anytime it's needed, refer right back to the store. Here is how I'm doing it:
class StoreService:
def FetchStore(datastoreName):
global datastore = open(datastoreName + ".txt", "w")
def SetData(data):
datastore.write(data)
But two problems arise:
- When you run the code, it tells you that there is a syntax error on line 3.
- I can't seem to figure out why it won't let you save integers, bools, etc, it only wants to take strings.
How can I fix all of this?
Edit for mpp: Ok, I rewrote it as you said, now when I do:
hi = StoreService.FetchStore("hi")
hi.SetData("50")
It gives me the error:
Traceback (most recent call last): File "main.py", line 10, in <module> hi.SetData("50") AttributeError: 'NoneType' object has no attribute 'SetData'
And when I write it normally,
StoreService.FetchStore("hi")
StoreService.SetData("50")
Absolutely nothing happens, no errors, nothing is written.