Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Chatbot/namestore.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (38 sloc)
1.17 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################## | |
# Benjamin Grant | |
import json | |
import pickle | |
#Opens names.json, updates it with the new name | |
async def set_name(id, name): | |
with open("names.json") as f: | |
names = json.load(f) | |
names[str(id)] = name | |
with open("names.json", "w+") as f: | |
json.dump(names, f) | |
#Opens names.json, checks if the user has a name and returns None if not. IMPORTANT TO CHECK FOR NONE, DO NOT ASSUME NAME EXISTS | |
async def get_name(id): | |
with open("names.json") as f: | |
names = json.load(f) | |
if str(id) in names: | |
return names[str(id)].capitalize() | |
else: | |
return None | |
#Same functionality as get_name() but it uses the pickle module | |
async def get_name_pickle(id): | |
f = open("names.pck", "rb") | |
names = pickle.load(f) | |
f.close() | |
if str(id) in names: | |
return names[str(id)].capitalize() | |
else: | |
return None | |
#Same functionality as set_name() but it uses the pickle module | |
async def set_name_pickle(id, name): | |
f = open("names.pck", "rb") | |
names = pickle.load(f) | |
f.close() | |
names[str(id)] = name | |
f = open("names.pck", "wb") | |
pickle.dump(names, f) | |
f.close() | |
################################################## |