Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
The four basic functions of a database are create, retrieve, update and delete, often pronounced as CRUD. This is an example of an application that does those basic four functions. Of course there is a lot of functions that people perform with database, there is a lot of things that they are used for, and as you are writing code over the years you will be writing databases for a lot different purposes. But they all basically break down to those four functions. So those are the things that if you pay attention to those things, then building the more complex applications becomes a little bit easier.
So this is an application that just does that. It manages as a very simple database and it simply allows you to add records, edit records, and delete records, and view records. So those are the basic four functions create, retrieve, update and delete. What this application does as it manages a Testimonials database that I use in my web site and you will see if you look at my web site, you will see little boxes like this one over here that have testimonials. For our purposes I have replaced the testimonials with some witty little quotes that I have collected over the years and here is the database itself.
This has got 14 records in the database and if you wanted to add something and print a byline and click Add, you will see that it adds it to the database right here, and then I can edit it, if I want to. Update it and there it is updated in the database. And if I want to delete it, I can delete it.
It's all very simple to operate and very clear, and this comes from it being a very simple application and also from it being designed clearly. So, here in the Example files, under Projects and under testimonials, you will find db.py and that is that script right there. See down here,it says db.py version 1.13. So that is this script right here. So we are going to take a little tour through this script and see how it works.
At the top here you see that I imported a few libraries that start with bw. These libraries are available in the lib folder under Projects and these are libraries that I have built over the years, and there is this global namespace container. I tend to do this in all of my web scripts because it helps to keep state in otherwise stateless environment, and it helps me to keep my global name space cleaned up.
So I use a dictionary for that in Python, and I have here a call to init, which initializes these variables, again helping to keep state and to set up my various objects and to read my configuration file and things like that, and then I look for a variable a in the CGI variables. If I find it, I run something called dispatch. Otherwise I load the first main page. So what dispatch does is it simply looks for what is the action, a stands for action, what is the action that's been taken and it will dispatch the proper function for that.
If these get any bigger I actually have a library that runs a jump table that will do something like this. So I tend to do a lot of work on the web. I tend to use CGI a lot. CGI being stateless the way that it is, the web and HTTP being stateless, that helps to have things like this to manage a little bit of a state machine. The main part of the application lists these records. So this is the retrieve part of the four functions of the database.
So the first thing this does is it grabs a count of the records in the database using the countrecs method in the database library and that very quickly and efficiently returns a count all of the records and that allows us to do some math here and figure out how many we are going to display on the page and how many pages there is going to be and then we set up this little menu here which is this part here that tells us, we can jump to a specific page or we can go back and forward. So that's very useful.
Then we have our little bit of SQL and this actually does the retrieval. SELECT From ORDER BY byline and LIMIT and OFFSET. And in SQLite, limit and offset allow us to only get the first so many records and if it's not on the first page, then to skip forward to an offset first and then get that many records, and it will do this on any query. Most modern database engines have some way of doing this.
It's not a part of the SQL standard so they all tend to be a little bit different. This one, SQLite actually, uses a very similar syntax, I don't think it's exact, but a very similar syntax to how it's done in MySQL and those are the two really popular ones for web applications these days. So that's how we do the paging and we make that list at the bottom of the page. The pagebar itself with the various links is there and displaying pages is here and then we get down to the actual actions.
Adding a record to the database, it simply builds this dictionary object and passes it off to the insert CRUD function in my database library, which I explain in another movie in this chapter. Likewise, the delete function calls the delete method in the database library and the update function calls the update method in the database library. Using these CRUD methods in my normalized database library allows me to keep this code very small.
Each of these function uses exactly the same data structure. It uses this dictionary. It uses an id field that's called id, and this allows the methods in the database library to do there job. So, let's take a quick look at those methods and this is the database library, bwDB. So the insert method here, it takes the dictionary object and it actually goes through the dictionary object.
It grabs all of its keys and then uses this generator expression to create a list of all the values. And so it has got a list of the keys and it's got a list of the values. And then it uses those two lists to build the SQL using the strings format method and then it passes that SQL query off to the database. So this allows us to use a dictionary object and to build our query based on the names of the keys in that dictionary object and to use that so that we can use the same code for different applications that have different database schemas.
So, the same technique is also used for update. Where again, we build a query based on a list of keys and values. Keys and values. The other methods like delete(), they only need the id. So that makes that a lot easier. So this is a very useful technique where you can use normalized code for a number of different applications with different database schemas. And by using that normalized code and some simple coding conventions, you can keep an application like this, down to about 250 lines and still have it look and operated like something a lot bigger and a lot more complex.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.