0% found this document useful (0 votes)
17 views30 pages

Python Persistent Classes Explained

1) The document discusses persistent classes in Python, which allow for the permanent storage of object attributes for later retrieval. 2) It explains that persistence is the ability to save information for reuse later, whether in external files, databases, or other mediums. 3) Basic concepts of Python such as objects, classes, attributes, and methods are described, and the advantages of using Python as a programming language are explained.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views30 pages

Python Persistent Classes Explained

1) The document discusses persistent classes in Python, which allow for the permanent storage of object attributes for later retrieval. 2) It explains that persistence is the ability to save information for reuse later, whether in external files, databases, or other mediums. 3) Basic concepts of Python such as objects, classes, attributes, and methods are described, and the advantages of using Python as a programming language are explained.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Persistent classes in Python

Profesor:

Carlos Efrain Palomino Vidal

Course:

Object-Oriented Programming

Student:

Cochachin Polich, Kevin (U20217096)

Index
2

1 Introduction. 3

2 Concepts of Python. 4

2.1 Definition. 4

2.2 Advantages of using Python. 4

2.3 Objects, classes, attributes, and methods in Python..............................................................5

3 Persistence in Python. 6

3.1 Object persistence. 6

3.2 Persistent classes.......................................................................................................... 7

4 Exercises Proposed. 8

4.1 Exercise with external files..8

4.2 Exercise with the Pickle library....12

5 Bases data with Python. 14

5.1 Definition of databases.........................................................................................14

5.1.1 Characteristics. 15

6 MySQL. 15

6.1 PhpMyAdmin. 15

6.1.1 Exercise with PhpMyAdmin............................................16

7 SQLite. 24

7.1 SQLite3 Module. 24

7.1.1 Exercise with the sqlite3 module.................................................................................24

8 Conclusions. 30

9 Bibliographies. 30
3

Persistent Classes in Python

1 Introduction

To begin with, we must keep in mind that persistent classes serve as a

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.

On the other hand, data persistence is the ability to store information.

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

data from the serialized information.

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

to provide a better understanding of our topic. Likewise, to do some exercises on this

program using persistent classes, which will help us with the explanation of

our research work.

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

widely used by large companies. Likewise, we will explain its model of

implementation.
4

2.1 Definition

Python is a general-purpose, cross-platform programming language.

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

schools and universities around the world.

Python is ideal for working with large amounts of data because it supports

extraction and processing capabilities as a cross-platform, which is why the

Big Data companies choose it. Scientifically, it has a large library of resources with a

special focus on mathematics for novice programmers in specialized fields.

2.2 Advantages of using Python

The main advantages of using Python are:

It facilitates the programmer with its readability and easy interpretation.

Supports complex objects and sets.

It's easy to learn, it's very easy to start programming and encourages the

productivity.

It is easily portable as it can be used on almost any system.

of the present.

Presents organized modules.

2.3 Objects, classes, attributes, and methods in Python

Classes and objects are used to create user-defined data types.

class is a user-defined data type, and by creating instances of a class it relates


5

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.

Below, we present an example, where we observe that in figure 1 we have the

class Car that has its attributes and methods. Then the object 'myCar' is instantiated.

where it prints the displayed values.

Figure 1

Example of the use of a class and its object.


6

Note. Made by myself.

3 Persistence in Python

As we mentioned before, persistence refers to the action of

to conserve the information of an object permanently, and to retrieve it. Next,

we will explain the types of persistence that Python handles.

3.1 Object Persistence

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

interact with these while the program is active in memory.

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

will be used for a later recovery.

3.2 Persistent classes

There are two aspects in the use of persistent classes in Python, converting data between a

object in memory and a storage format, and manage data storage

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

and object serialization.

There is a way for an object to persist. This can be done in Python.

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

of the function 'open'.

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

libraries, among the most notable is Pickle.

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

persistent classes already explained earlier.

4.1 Exercise with external files

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

Creation of the external file.


9

Note. Own elaboration.

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

we will save the information to be entered.

Figure 3

Creation of the interface.


10

Note. Own elaboration.

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

It will store in an external file making the information saved.

Figure 4

Sample of the finished interface.


11

Note. Own elaboration.

4.2 Exercise with the Pickle library

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

Applying the Pickle library.


12

Note. Own preparation.

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

stored our catalogs, which is encoded in binary code.

Figure 6

Creation of catalogs.
13

Note. Own elaboration.

5 Databases with Python

When we have to store a large amount of data and its subsequent processing

It is very common to use a database manager.

Using Python, we can communicate with the database administrator to

send and retrieve data. There are many database managers to use. In the

In the following lines, we will define what a database is.

5.1 Definition of databases

A database is a set of interconnected information that is stored and

systematically organized to facilitate its conservation, search, and use.

calls database. Databases have evolved as advances in

computing and electronics move from analog systems to digital ones that are characterized by

store large amounts of information that can be used quickly and

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

to provide more efficient access to stored data. A system of

files follow a decentralized model, where each department of the

empresa almacena y gestiona sus propios datos mediante una serie de

application programs specifically written for it (Mercedes, 2011, p. 10).

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

database management systems, known as DBMS by its English acronym (Database

Management System), which allows for faster and more reliable information storage.

5.1.1 Characteristics

Some main features of database systems are:

Minimal repetition.

There cannot be two tables with the same name or record.

Simultaneous access by multiple users.

Data integrity.

Access through programming languages.

6 MySQL

MySQL is the most popular database management system today.

because it is based on open source code.

MySQL, being open source code, allows companies and

developers have a reliable and standardized solution for their

applications. MySQL will help manage everything in a simple way,

reasonable and organized (Izquierdo, 2008, p. 167).

We need a GUI program that allows access to a MySQL server, to

consult or create a database and manage stored tables or records. There are

various software alternatives to manage our databases, one of them is

through a web application, such as PhpMyAdmin.

6.1 PhpMyAdmin

It is a web application used to easily manage MySQL databases.

It also has an intuitive interface. The advantage of using a web application is that it allows us to
15

connect to remote servers that cannot always be accessed via

GUI programs. With phpMyAdmin, you can perform all kinds of operations, from creating,

delete databases and even manage tables (creation, modification, deletion) and, therefore

assumption, your own data.

6.1.1 Exercise with PhpMyAdmin

Next, we will do an example with a database in Python using

PhpMyAdmin.

To start with the exercise, we first create our database.

In figure 7, the creation of our database called 'Personales' is observed.

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

Creation of the database.


16

Note. Own elaboration.

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

Note. Own Draft.

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,

that will later serve us when using our buttons.

Figure 9

Application of the Methods.


18

Note. Own elaboration.

Figure 10

Application of the Methods.


19

Note. Own Preparation.

Figure 11

Application of the Methods.


20

Note. Own preparation.

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

use of the methods created earlier to handle the CRUD.

Figure 12

Creation of the interface.


21

Note. Own elaboration.

Figure 13

Creation of the Interface.


22

Note. Own elaboration.

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

Note. Own Elaboration.

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,

simple and flexible, making it suitable for prototyping and implementation

for production for specific applications.

These are some of the main features of SQLite:

The entire database in a single file.

Written in C, it is open source.

It is an integral part of the program.

It can be executed entirely in memory, which makes it very fast.

Read and write directly to files on the hard drive.

It has libraries accessible for many programming languages.

The source code is in the public domain and is well documented.

7.1 sqlite3 Module

The sqlite3 module implements the Python interface compatible with SQLite which, as

we said earlier, it is a relational database of processes. If our application needs

to store large amounts of data, SQLite is our best option.

SQLite is open source and is installed by default in Python, as it is part of

the standard library. Thanks to that, we do not have to install the module with the command 'pip'.
24

7.1.1 Exercise with the sqlite3 module

Next, we will do an example with a database in Python using the module

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

dimensions, then we create our variables to store it in our database.

Finally, we establish the connection with the DB, and then create our tables with their

respective variables.

Figure 15

Database Creation.
25

Note. Own preparation.

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

created later, each inheriting its commands.

Figure 16

Applications of the Methods.


26

Note. Own elaboration.

Figure 17

Application of the Methods.


27

Note. Own elaboration.

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

buttons created, to thus complete our interface.

Figure 18

Creation of the interface.


28

Note. Own elaboration.

In figure 19, we can see the graphical interface we have created. This will help us to

easily register our catalog of movies that we wish to create, as well as

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

Note. Own elaboration.

8 Conclusions

In conclusion, persistent classes in Python help with privatization.

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

our stored data.

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.

data. Likewise, we learned to connect to a database, create tables, how to insert,

obtain, modify and delete records from the database.

9Bibliographies

Mercedes (2011). "Databases". Department of Engineering and Sciences

Computers. Chile, Santiago: Sapientia. [Link]

Izquierdo (2008). “Tutorial de Practicas del Gestor de Bases de Datos MySQL”.

University of Wide. Ecuador Cuenca: Bruño.

[Link]
30

You might also like