Save & load objects
This notebook demonstrates methods for saving and loading EDSL objects locally.
[1]:
from edsl import QuestionFreeText, Survey, ScenarioList, Scenario, AgentList, Agent
Example objects
You can call the example()
method on any object to see and use an example:
[2]:
my_question = QuestionFreeText.example()
my_survey = Survey.example()
my_scenariolist = ScenarioList.example()
my_scenario = Scenario.example()
my_agentlist = AgentList.example()
my_agent = Agent.example()
Saving objects
You can call the save()
method on an object to save it as a compressed json file:
[3]:
my_question.save("saved_question")
my_survey.save("saved_survey")
my_scenariolist.save("saved_scenariolist")
my_scenario.save("saved_scenario")
my_agentlist.save("saved_agentlist")
my_agent.save("saved_agent")
Saved to saved_question.json.gz
Saved to saved_survey.json.gz
Saved to saved_scenariolist.json.gz
Saved to saved_scenario.json.gz
Saved to saved_agentlist.json.gz
Saved to saved_agent.json.gz
Loading objects
You can call the load()
method on an object class to create an object from a json file:
[4]:
my_saved_question = QuestionFreeText.load("saved_question.json.gz")
my_saved_survey = Survey.load("saved_survey.json.gz")
my_saved_scenariolist = ScenarioList.load("saved_scenariolist.json.gz")
my_saved_scenario = Scenario.load("saved_scenario.json.gz")
my_saved_agentlist = AgentList.load("saved_agentlist.json.gz")
my_saved_agent = Agent.load("saved_agent.json.gz")
Compare
Here we verify the original and saved/loaded objects are identical:
[5]:
my_question == my_saved_question
[5]:
True
[6]:
my_survey == my_saved_survey
[6]:
True
[7]:
my_scenariolist == my_saved_scenariolist
[7]:
True
[8]:
my_scenario == my_saved_scenario
[8]:
True
[9]:
my_agentlist == my_saved_agentlist
[9]:
True
[10]:
my_agent == my_saved_agent
[10]:
True
Posting to Coop
Here we post all the objects and this notebook to the Coop. Learn more about using the Coop.
[11]:
my_question.push(description = "Example free text question", visibility = "public")
[11]:
{'description': 'Example free text question',
'object_type': 'question',
'url': 'https://www.expectedparrot.com/content/56a83cf3-5f2c-48de-856f-eeb4a531c8bc',
'uuid': '56a83cf3-5f2c-48de-856f-eeb4a531c8bc',
'version': '0.1.43.dev1',
'visibility': 'public'}
[12]:
my_survey.push(description = "Example survey", visibility = "public")
[12]:
{'description': 'Example survey',
'object_type': 'survey',
'url': 'https://www.expectedparrot.com/content/de99846c-5771-40dd-9e86-dce88f3fc70f',
'uuid': 'de99846c-5771-40dd-9e86-dce88f3fc70f',
'version': '0.1.43.dev1',
'visibility': 'public'}
[13]:
my_scenariolist.push(description = "Example scenario list", visibility = "public")
[13]:
{'description': 'Example scenario list',
'object_type': 'scenario_list',
'url': 'https://www.expectedparrot.com/content/abb06194-f0ae-4585-a0e1-352fccf8c556',
'uuid': 'abb06194-f0ae-4585-a0e1-352fccf8c556',
'version': '0.1.43.dev1',
'visibility': 'public'}
[14]:
my_scenario.push(description = "Example scenario", visibility = "public")
[14]:
{'description': 'Example scenario',
'object_type': 'scenario',
'url': 'https://www.expectedparrot.com/content/7c8d1e53-8dcf-4cde-9d5a-9daf556e6ec8',
'uuid': '7c8d1e53-8dcf-4cde-9d5a-9daf556e6ec8',
'version': '0.1.43.dev1',
'visibility': 'public'}
[15]:
my_agentlist.push(description = "Example agent list", visibility = "public")
[15]:
{'description': 'Example agent list',
'object_type': 'agent_list',
'url': 'https://www.expectedparrot.com/content/b30c7a75-c1b8-4fdd-9f43-65927e3673e5',
'uuid': 'b30c7a75-c1b8-4fdd-9f43-65927e3673e5',
'version': '0.1.43.dev1',
'visibility': 'public'}
[16]:
my_agent.push(description = "Example agent", visibility = "public")
[16]:
{'description': 'Example agent',
'object_type': 'agent',
'url': 'https://www.expectedparrot.com/content/fd26b95c-cb0a-47aa-b4b8-ef25a6f73c0f',
'uuid': 'fd26b95c-cb0a-47aa-b4b8-ef25a6f73c0f',
'version': '0.1.43.dev1',
'visibility': 'public'}
Here we post this notebook:
[17]:
from edsl import Notebook
[18]:
n = Notebook(path = "save_load_objects_locally.ipynb")
[19]:
info = n.push(description = "Methods for saving and loading objects locally", visibility = "public")
info
[19]:
{'description': 'Methods for saving and loading objects locally',
'object_type': 'notebook',
'url': 'https://www.expectedparrot.com/content/fb08f43e-ee45-4331-b705-e758a7ccc636',
'uuid': 'fb08f43e-ee45-4331-b705-e758a7ccc636',
'version': '0.1.43.dev1',
'visibility': 'public'}
Update an object at the Coop
[20]:
from edsl import Notebook
n = Notebook(path = "save_load_objects_locally.ipynb") # resave the object
[21]:
n.patch(uuid = info["uuid"], value = n)
[21]:
{'status': 'success'}