Question

In: Computer Science

In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps...

In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps the object (an instance of your user-defined class) that contains and is able to manipulate these data in a data file through maybe the pickle module? Without importing your own class definition, will the data load from such a data file be usable or interpretable? Explain and write some codes to support your ideas if possible.

Solutions

Expert Solution

By storing the object intance, you are able to store the complete current state of the object along with the data. So when you need to re-create the object, you can easily do without manual data parsing.

You need to import the class definition, in order to manipulate data. Otherwise, data manipulation on the object will not work, because pickle only stores the state of the object, not the class definition.

The two python files demo this process. On file has the class definition saves an object to a file. The other file mporta the class defintion and it reads the data from the file, and invokes a function of the object.

Program Screenshots for Indentation Reference:

Sample Output:

Program code to copy:

picklerDemo.py:

from pickle import Pickler


# custom class for demo
class Person:
    def __init__(self, name):
        self.name = name
  
    def printName(self):
        """ Function to print the name"""
        print(self.name)


#create an object and save object to file
obj = Person("John Doe")

with open("john.pickle", "wb") as f:
    Pickler(f).dump(obj)

unpickleDemo.py:

from pickleSaver import Person
from pickle import Unpickler


with open("john.pickle", "rb") as f:
    obj = Unpickler(f).load()

    # invoke method of the object
    obj.printName()


Related Solutions

Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list...
Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list the winner of each pair, giving the number of seconds the winner won by. At the end declare which team won based on which team had the most wins. Lab Steps There are eight cars in each team called Chevy and Ford. One car from each team races its opponent on the drag strip. Read in the racing times for the eight Chevy cars...
     program will enter data into two single dimension arrays (do not store duplicate values in arrays)...
     program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
Material covered: Loops Functions Data Structures Randomness Numpy arrays (python language) Problem statement A single amoeba...
Material covered: Loops Functions Data Structures Randomness Numpy arrays (python language) Problem statement A single amoeba sits in a pitri dish. Every 5 minutes one of two things will happen to the amoeba: 1.    There is a chance that the amoeba dies - producing no offspring. 2.    If it does not die the amoeba splits into two amoebas. make a function that simulates a single trial to calculate the lifespan of a colony of amoebas, given their chance of survival. Then, run...
Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE)...
Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE) pair. 2.1) Create an empty dictionary called 'contacts'. The KEY in this dictionary will be the name of a contact (stored as a string). The VALUE in this dictionary will be a list with three elements: the first element will be a phone number (stored as a string), the second an email address (stored as a string) and the third their age (stored as...
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data...
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data from a CSV file, which has no names, just numbers. All I did was to read a .csv file. How do I pull data from three columns which contains about 1500 rows with just numbers and make a scatter plot with two in the x-axis and the third in the y-axis?
why is it important to have software vendors (such as SAP, Oracle,etc.) use the audit data...
why is it important to have software vendors (such as SAP, Oracle,etc.) use the audit data standards?
Dittman's Variety Store is completing the accounting process for the current year just ended, December 31. The transactions during year have been journalized and posted. The following data with respect to adjusting entries are available:
Dittman's Variety Store is completing the accounting process for the current year just ended, December 31. The transactions during year have been journalized and posted. The following data with respect to adjusting entries are available:Wages earned by employees during December, unpaid and unrecorded at December 31, amounted to $2,700. The last payroll was December 28; the next payroll will be January 6.Office supplies on hand at January 1 of the current year totaled totaled $450. Office supplies purchased and debited...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT