Cloning and Modifying Mutable Objects in Python

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

The nature of how Python handles mutable and immutable data can make it easy to accidentally modify a different variable when passing mutable data structures into functions or methods as arguments. Being able to use a dictionary or list a variable’s contents without changing the original can be very handy for creating a default configuration, or as a data template. In this hands-on lab, you’ll be writing a small script that utilizes a data template to help generate information about a real estate transaction while ensuring that the original data isn’t modified during the process. Completing this lab will demonstrate that you understand how data copying works within Python.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Parse the Template JSON File

There should always be a template.json file next to the script so that it can get an up-to-date picture of the values that don’t change. When the script is initially run, this should be parsed and stored as the template variable.

Prompt for the Values That Need to Be Changed

When the tool is run, it should present the user with a series of prompts like this:

$ python
Street 1: [prompt]
Street 2: [prompt]
City: [prompt]
State: [prompt]
ZIP: [prompt]

Square Footage: [prompt]
Bedrooms: [prompt]
Bathrooms: [prompt]
Ammenities (use | between items): [prompt]

Was there a referrer? (Y or n): [prompt]
Referrer: [prompt ONLY if Y to previous]
Export New Data to a New JSON File

Write the dictionary with the new values to a JSON file named after the street address of the property with the spaces converted to dashes.

Additional Resources

A friend of yours works in real estate and uses a tool that allows them to drag in a JSON file with all of the information about a new property listing. There aren't that many fields that need to be changed between each one, but they hate modifying a JSON file because they feel like they're going to mess something up and have no idea how to fix it. Looking at their situation, you've convinced them that the command line isn't as scary as they think and told them that you can write a tool that will prompt them for the information that they always change and generate a new JSON document as long as they provide an example for you.

When creating a new listing, the following pieces of data always need to be changed:

  • listing_date - This should always be tomorrow when the application is run.
  • property - This is a complex data structure with quite a few options that will all need to be modified.
  • broker.referrer - There may or may not be a referrer for this listing.

You'd like to eventually take this logic and put it behind a long-running web application so you know that it is important that the variable holding onto the template data doesn't change at any point during the process. If that happened for a web application, then each request might change the outcome for the next request unintentionally.

There are some automated tests for this process. You can run all of the unit tests with the following command:

$ python -m unittest
Ran 2 tests in 0.004s


What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?