Python Persistent Classes Explained
Python Persistent Classes Explained
Profesor:
Course:
Object-Oriented Programming
Student:
Index
2
1 Introduction. 3
2 Concepts of Python. 4
2.1 Definition. 4
3 Persistence in Python. 6
4 Exercises Proposed. 8
5.1.1 Characteristics. 15
6 MySQL. 15
6.1 PhpMyAdmin. 15
7 SQLite. 24
8 Conclusions. 30
9 Bibliographies. 30
3
1 Introduction
storage of the attributes that we wish to declare. Also, they can be saved to
estilo como de una base de datos mediante hibernación y leerse. Las clases persistentes, no es
precisely a class, but the objects are instantiated in such a way that the information
persist. Persistence is the property that an object has to remain over time, it is
to say that the object's information is stored permanently to then be retrieved and
reuse.
a program to reuse it at another time; in programming, it means to take care of the sequence of
data in a file, database, or other similar medium, and the reverse process of retrieving
Now, in order to focus more on the topic, research was conducted on the classes.
persistent in Python, of which in this work before delving into the topic,
we will mention some definitions, aspects, usage models, and advantages of Python, in order to
program using persistent classes, which will help us with the explanation of
2 Concepts of Python
In this chapter, we will discuss some basic concepts of Python to understand the
because it is one of the most widely used programming languages in the world and also being
implementation.
4
2.1 Definition
multiparadigm known for its clean and readable code. One of the reasons for its success is that
has an open source license that allows its use in any situation. This makes it
convert into one of the entry-level languages that many programmers study in
Python is ideal for working with large amounts of data because it supports
Big Data companies choose it. Scientifically, it has a large library of resources with a
It's easy to learn, it's very easy to start programming and encourages the
productivity.
of the present.
to the creation of objects of that type, which can have several attributes that it
They describe. Likewise, a method is a function that belongs to an object. The classes and the
Objects are considered the main building blocks for development in Python.
class Car that has its attributes and methods. Then the object 'myCar' is instantiated.
Figure 1
3 Persistence in Python
The persistence of objects can be classified into transient objects and objects
persistent.
Transient objects are those data or objects that have the ability to
be stored in memory and continue to exist after several operations; however, when one
Closing the program, this information stored in memory is lost. Usually, those
cases are those defined in arrays or vectors, in which data will be stored and it will be possible to
Persistent objects are data or objects that are stored in secondary media.
for later use, and whose life is independent of the process of their creation. These things
they are generally stored in the database through the value of the primary key that is
There are two aspects in the use of persistent classes in Python, converting data between a
converted. When we want to perform the task of storing information and then retrieving it
for later use, we use the persistence feature whose function is to preserve
7
permanently the information of the object. For this we can work with external files
working with external files, for this we will use the standard Python module 'io', the
which aims for data persistence, that is, when it comes to storing data
we ensure that these do not get lost, by using the external file. For that, we will make use of
The 'open' function allows you to open and access the file, which is also important.
return it and close it, since the operating system has a maximum standard of files that
they can remain open, moreover it wouldn't make much sense to keep a file open if
is not used.
On the other hand, object serialization can also be used, which involves
the creation of a byte array for storage. This can be done with several
The Pickle Library is the most common when wanting to use persistence, as it is
integrated with some standard library modules that store serialized data. A
simple flat file with serialized objects written one after another works for data
that do not need to be indexed. Likewise, this library allows us to easily store
collections and objects in binary files abstracting all the writing and reading part
binary. On the other hand, it also has a large number of series of methods. The most methods
highlights are the dump method, which performs a data dump to the external binary file, and the
load method, which loads data from the external binary file.
8
4 Proposed Exercises
Next, we will carry out some exercises in this program using the
In figure 2 we see that we import the "open" module from "io", then we create a class.
that will print the data to be put. Then, we create the file with the open module, which
we pass the parameters of the name with which we want our file to be saved and the
iterator to use. Finally, we print the values and close the file.
Figure 2
In figure 3 we see that to create the interface we first need to import the module
Tkinter. Then we create the interface with its respective texts. Finally, we instantiate the
data from our class with the texts to put on the interface, and so with the register button
Figure 3
In figure 4 it is observed that, when creating the interface, we see that we can write texts,
en lo cual una vez completando estos espacios podemos al darle al botón registrar y esta se
Figure 4
In figure 5, we observe that we import the Pickle library, then we create the
classes with their constructors. Then we use the "open" function and save the classes
making a dump.
Figure 5
In figure 6, we can see that we will create a movie catalog, then we add the
movies that we want and we show it. Finally, when compiling it we can see all the
catalogs created even after having deleted them with "del", and a file was created in which this is
Figure 6
Creation of catalogs.
13
When we have to store a large amount of data and its subsequent processing
send and retrieve data. There are many database managers to use. In the
computing and electronics move from analog systems to digital ones that are characterized by
simple.
Antes de existir las bases de datos se trabajaba con ficheros. Los sistemas de
files emerged when computerizing the handling of manual file cabinets for
The objective of databases is to facilitate the use and access to information, so that
they are widely used in the commercial, public, and scientific fields, as well as in libraries
and other fields. Likewise, in order to improve its functionality, they have been created
14
Management System), which allows for faster and more reliable information storage.
5.1.1 Characteristics
Minimal repetition.
Data integrity.
6 MySQL
consult or create a database and manage stored tables or records. There are
6.1 PhpMyAdmin
It also has an intuitive interface. The advantage of using a web application is that it allows us to
15
GUI programs. With phpMyAdmin, you can perform all kinds of operations, from creating,
delete databases and even manage tables (creation, modification, deletion) and, therefore
PhpMyAdmin.
which contains a records table. In this table we can see that it has code, names,
surnames, address and phone number, which will help us when grouping the staff.
Figure 7
In figure 8, we see that we first have to install the library that will allow us to
to connect correctly with the database, for this we will use PyMySQL. Then
we establish the connection with our created database, which is called 'personales'.
Figure 8
Import of PyMySQL.
17
In figures 9, 10, and 11 we apply the methods read, add, reset, delete, select,
search and update. Each of these methods had their functions and restrictions added to them,
Figure 9
Figure 10
Figure 11
In figures 12 and 13 we create the graphical interface, which will have labels, text entries.
and buttons. Each of these with their respective variables and commands, where we will do the
Figure 12
Figure 13
In figure 14, we can see how the graphical interface turned out. In this interface, we can delete,
edit, search, restart and above all correctly register our personnel, which, in the
moment of adding it, this will be automatically saved in our created database.
Figure 14
Interface sample.
23
7 SQLite
SQLite is designed to be integrated into applications, rather than using standalone software.
database server such as MySQL, PostgreSQL, Oracle among others. It is fast, rigorous,
The sqlite3 module implements the Python interface compatible with SQLite which, as
the standard library. Thanks to that, we do not have to install the module with the command 'pip'.
24
sqlite3.
In figure 15, you can see that the tkinter library was imported first along with
sqlite3, which will help us for development. Then, we create our interface by giving it its
Finally, we establish the connection with the DB, and then create our tables with their
respective variables.
Figure 15
Database Creation.
25
In figures 16 and 17, the methods were established with their respective functions and
restrictions for manipulating the CRUD. Which we will do through the buttons that will be
Figure 16
Figure 17
In figure 18, styles were added to our table, we created the labels, the text boxes and
the buttons. Finally, we instantiate the methods of our class with the text boxes and the
Figure 18
In figure 19, we can see the graphical interface we have created. This will help us to
edit, show, update, and delete. Furthermore, we will not have to worry about it
they will lose our catalogs, since this will persist in our database.
Figure 19
Interface sample.
29
8 Conclusions
Of the attributes, the declaration of them and their storage, making them persist beyond time.
of the life of the one that this program instantiates. On the other hand, we were able to observe in the examples
shown that there are several ways of using data persistence. The first is
using an external file that allowed us to see the data stored in a file and
the other way is by serializing it with the Pickle library, encoding it in binary code to
Also, in this lesson we learned everything related to MySQL and SQLite. Which
it is the most appropriate, simple, and easy way to ensure that our persist.
9Bibliographies
[Link]
30