Python: This Book Includes: Learn Python Programming for Beginners, Python Crash Course and Python for Data Analysis. Create Your Business Projects Immediately Thanks to This Step by Step Guide by Jason Scratch

Python: This Book Includes: Learn Python Programming for Beginners, Python Crash Course and Python for Data Analysis. Create Your Business Projects Immediately Thanks to This Step by Step Guide by Jason Scratch

Author:Jason Scratch [Scratch, Jason]
Language: eng
Format: azw3, epub
Published: 2020-10-07T00:00:00+00:00


Chapter 10:

Databases

D ata management is not a scientific discipline per se. However, increasingly, it permeates the activities of basic scientific work. The increasing volume of data and increasing complexity has long exceeded manageability through simple spreadsheets.

Currently, the need to store quantitative, qualitative data and media of different formats (images, videos, sounds) is very common in an integrated platform from which they can be easily accessed for analysis, visualization or simply consultation.

The Python language has simple solutions to solve this need at its most different levels of sophistication. Following the Python included batteries, its standard library introduces us to the Pickle and cPickle module and, starting with Version 2.5, the SQLite3 relational database.

Specifying the Database

johnsmith= os.path.expanduser (' ~ /. johnsmith’ )

if not os.path.exists (at the dir):

os.mkdir (at the dir)

sqlhub.process Connection = connectionForURI (' sqlite://’+johnsmithr +’/knowdb’)

In specifying the database, we create the directory (os.mkdir) where the database will reside (if necessary) and we will natively connect to the database. We use os.path.exists to check if the directory exists. Since we want the directory in the user's folder, and we have no way of knowing beforehand what this directory is, we use os.path.expanduser to replace / home/user as it would normally on the Unix console.

On line 11 of Specifying the database, we see the command that creates the connection to be used by all objects created in this module.

Next, we specify our database table as a class, in which its attributes are the table columns.

Specifying the database ideatable.

class Idea (SQLObject): name= UnicodeCol() nlinks= IntCol()

links= Pickle Col() address = StringCol

The class that represents our table is inherited from the SQLObject class. In this class, each attribute (table column) must be assigned an object that gives the type of data to be stored. In this example, we see four distinct types, but there are several others. UnicodeCol represents texts encoded as Unicode, i.e. it can contain characters from any language. IntCol is integer numbers. PickleCol is an exciting type as it allows you to store any type of Python object.

The most interesting thing about this type of column is that it does not require the user to invoke the pickle module to store or read this type of variable. Variables are automatically converted/converted according to the operation. Finally, we have StringCol which is a simpler version of UnicodeCol, accepting only ASCII character strings. In SQL it is common to have terms that specify different types according to the length of the text you want to store in a variable. In sqlobject, there is no limit to the size of the text that can be stored in either StringCol or UnicodeCol.

The functionality of our spider has been divided into two classes: Crawler, which is the creeper itself, and the UrlFac class that builds URLs from the word you want in Wikipedia.

Each page is pulled by the urllib2 module. The urlencode function of the urllib module makes it easy to add data to our request so as not to show that it comes from a digital spider. Without this disguise, Wikipedia refuses the connection.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.