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/8abffbe4-9f28-4f31-adcb-c8b14a748c98',
'uuid': '8abffbe4-9f28-4f31-adcb-c8b14a748c98',
'version': '0.1.36.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/453f02b3-f6a8-46bf-b3f3-bde14a47d9a4',
'uuid': '453f02b3-f6a8-46bf-b3f3-bde14a47d9a4',
'version': '0.1.36.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/555e9059-144a-41f4-9c83-ac97cccda3e5',
'uuid': '555e9059-144a-41f4-9c83-ac97cccda3e5',
'version': '0.1.36.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/083a3aaa-2496-4f98-aafb-6a12bf74987d',
'uuid': '083a3aaa-2496-4f98-aafb-6a12bf74987d',
'version': '0.1.36.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/9bc1fd36-8a81-4c01-9764-57fdd9756b83',
'uuid': '9bc1fd36-8a81-4c01-9764-57fdd9756b83',
'version': '0.1.36.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/58fb1d4c-fc30-48c6-8316-53783a40d2e7',
'uuid': '58fb1d4c-fc30-48c6-8316-53783a40d2e7',
'version': '0.1.36.dev1',
'visibility': 'public'}
[17]:
from edsl import Notebook
[18]:
n = Notebook(path = "save_load_objects_locally.ipynb")
[19]:
n.push(description = "Methods for saving and loading objects locally", visibility = "public")
[19]:
{'description': 'Methods for saving and loading objects locally',
'object_type': 'notebook',
'url': 'https://www.expectedparrot.com/content/5496e997-b04e-4cf8-bd25-25180f2768fd',
'uuid': '5496e997-b04e-4cf8-bd25-25180f2768fd',
'version': '0.1.36.dev1',
'visibility': 'public'}
Update an object at the Coop
[20]:
n = Notebook(path = "save_load_objects_locally.ipynb") # resave the object
[21]:
n.patch(uuid = "5496e997-b04e-4cf8-bd25-25180f2768fd", value = n)
[21]:
{'status': 'success'}