98-381 Microsoft Introduction to Python – Advanced Concepts in Python

  1. Sending Emails to multiple receivers with Python

Hey guys, welcome back. Now in this lesson, we are going to create a process that will allow us to send an email to any client that we might want. So first of all, let’s start and create right here. Import the SMTP Library So the SMTP library, as you can see in the description, allows us to follow SMTP protocols so that we can send a sample email.

So, as you can see here, it’s also an example that will help you create your own code in order to send email. So you can use it, but let’s try it together. So first of all, I have to import the SMTP library and then send an email. I’m writing this comment right here so that we’re going to have something concrete right here. Sending a mail test example So first of all, I’m going to create my methods. So I’ll use three brackets, and from “I’d like” to “Christos Mayarak,” I’ll use a testimonial account. So I will use testimonials.com right here. I’m going to send it to my Gmail account. So I will use Myrack@gmail.com, and then my subject will be “SMTP email tests.” So SMTP is actually the protocol that email uses in general when it is trying to send an email. So usually, the protocols are SMTP for sending and POP for incoming messages.

So we’re going to send an email. So we are writing an SMTP test right here, and then the body will say, “This is my first test email.” So in the end, I have to close these three brackets, and then I am going to initialize my server so that it will use Smtplib SMTP. And then you have to define where your server is located. So actually, my domain palace.com hosts this SMTP library , my domain pilaf you don’t know who your hosting provider is, your Gmail account, your Hotmail account, or what it actually uses, you can google and see what the SMTP settings are for Hotmail, Gmail, or anything else. So I googled right here, “SMTP settings for hotmail,” and you can see which port is used by which server, imap.com. So this is what you are going to use: SMTP settings for Gmail. You can use exactly Smtp@gmail.com. So you’re going to type SMTP right here at @gmail.com. But I really suggest you use my test email account for your first example. So please accept my credentials. And this is a test email account that you can use to send your test emails. So as you can see, I will write right here “SMTP lib SMTP.” This is my server, and this is my port.

So then I’m going to use connectserver to connect exactly to this server. I’m writing this again; I will copy and paste it, and then I will start with to this sirs this is actually what we are going to do: create a new string object from this given object. so that we’re going to initialize our server. The server will then initiate TLS. This is actually TLS, a secure protocol that will allow us to use TLS so that we can encrypt the rest of the SMTP session. As you can see, it puts the connection to the SMTP server into TLS mode. So if there has been no previous “hello” command, the session tries first. Hello. So actually, we could omit the previous command, but it’s something that doesn’t bother if you can use it. So we can definitely use the hello command.

And then we will start sending the message with TLS methods. So I will log in now to my server. I’m sorry. I’ll log in to the servers. Login. I will write my credentials right here at testimonial.com. Actually, if I’m going to overuse or abuse my email account, I will change the credentials. So please don’t do it. Do it only for educational testing purposes. Testemail@pilas.com is right here. And then I will send it right here, as you can see. And then I’m going to quit. So, as you can see, this is the code to send any mail test example. So I’m going to right-click and run the Python file internally. So this is my previous example. Write something here.

And as you can see, I have an error. I have an error right on file internal Unnecessary semicolon. Of course, in Python, I don’t need any semicolons. So again, run Python filing terminal nine. Again, I have an error. Login takes three positional arguments, but four were given. So it’s one, two, three. Which is the fourth one? Where is the fourth one? Actually, I need only two. Actually, this is the server to send the email. I’m sorry for this sent mail. I’m going to send an email to this account from this account and send this message. But first, I must log in to my server using this testimony@fleece.com and then enter my password. I’ll copy it and paste it right first of all, So I’m going to save it and try again to run the Python file in the terminal. So I’ll type something, and everything will be fine. Maybe I could add a message here, an email sent, or something similar. So I’m going to check my email. And here is my first test email. Send it from the testimony at Piles to my email account. And as you can see, I used  similar So this is my first test email. Everything went great. So this is actually the code that you can use to send any mail to anyone that you might want. So thank you very much, and I look forward to seeing you in the next lesson.

  1. XML parsing

Hey guys, welcome back. Now, in this lesson, we will see how we can parse XML documents. So, what is XML? XML is a portable open-source language that allows developers to create applications that can be read by other programs. As a result, XML Mail is extremely useful for tracking small, medium, or large amounts of data without requiring an SQL-based backbone or the use of a database. So there are specific XML parser architectures and APIs. And in this lesson, we will see how we can use the document object model API Dom. This is known as Dom. So it is a Worldwide Web Consortium recommendation wherein the entire file is read into memory and stored in a tree-based form to represent all the features of an XML document. So we will see an example, and we will see how we can use an XML document, how we can read it with Python, and how we can parse and store the values and use the values in our programs.

So, first of all, I will open an XML document. Here is one; as you can see right here, I have an XML document. I have an initial node called Store Items. And then I have three products. So, as you can see, each node opens and closes. So I have an opening node right here. And then I have this slice. That means that this is the closing node. And all these are the values—the values inside this node. So then I have one, two, or three products. As you can see, I have products. I have an attribute right here that tells me that this product is for the item “Test TV.” This product is for the test phone. This product is for the item test DVD. Then I have the name of the product. I used the same name. But actually, think about having a website, and this item is for your personal use. So I can have something here for you so you can identify what the product is. But here is the name—the official name that will be shown on TV and, I’m sure, on the website.

So I can test something on a 49-inch supermodel TV. Something like this This is the URL. so that someone can click on this URL and see this product. This is the category ID. So it is the same as in the databases. Imagine having a lot of categories, and each category has a specific ID. So this product belongs to the category that has the ID 77. The name of the category is this. Actually, it is in Greek. I will write something new TVs.This is the price. You can have manufacturer availability, so it is available. You can have something right here for him. But I will delete this. I will also delete this. I write here that it is available. It is available. So you can see, I actually use this seed data. This is very good for non-English languages, but you can also use it without all this. You can use this as the category ID and price for DVD players, for example. But actually, if you are going to use an XML document and not Greek, then you could use this C data. So I will save it right now, and I will start typing my program. I will start coding so that I can start parsing this data from an XML document. So what I’m going to do right here is actually start using XML document parsing.

So I will use a DOM tree. XML Dom mini-dom parts This is the syntax, actually. And here you can type the path where your document is located on your hard disk. But be careful; I will use a double slash right here. Users double-slash. HP is slashed twice, not once. This is my location. Okay, stop right here. I gave an example before this lesson. So I should tell you that first of all, you should import these two libraries. You should import XML Dom Minidom importparse first, followed by XML Dom Minidom. These are the two libraries that we will use in order to parse XML documents. This means that in these libraries, all the available commands and functions are already built-in, so we can use specific commands to parse this XML document. So I will go back to where I was, and here I will use a variable called Dom tree that will load the XML into the Dom Document object model. As you can see, in the Python implementation, it does all the work for us. And then the middom, a simple implementation of the level one dome. So then I will parse this XML document, and then I will use a collection called Dometree document elements. I’ll say it again: you can autocomplete your command with the tab. So, if the collection has the attribute self-rootis, I’ll write s%. That means that I will type a string here. So collection jet gets attribute self, not this one. This means that if I have an attribute in the root element, it will be proud. Right here. I will then get all the products in a collection. I’ll name the products in the collection, and the collection will get elements by not tagging the named product.

Then I’ll iterate this collection for each product to which it belongs. So, with this command, I actually use a new variable that will iterate over all of the products in this collection. Then I can write here if the product has an attribute, name the attribute, and print the percentage for the product and the attribute item. So I will now write this right here: statement print Oh, sorry, products. And then I will right-click “run Python filing terminal,” and I will have invalid syntax right here. So, if the collection has the attribute “self” again, I forgot about it and right-clicked to open the Python file terminal. So I type nine. As you can see, I made a simple error. This is the Domtree document element in Collection, correct? Oh, okay, I have to fix this. So I will stop it for now, and I will continue the next lesson. So the error was in the XML document; I had to save it back, and I will now run it again. Run the Python file in Terminal, so this is my previous example, and as you can see, I have these products being printed and then these products for each product that I have these products being printed.

Actually, I just did product, and then I have the title Test TV, then Test Phone, and finally Test DVD Player. So this was an example where I took the attributes from the item, and I think it was a really good example to see how we can use the XMLDom in order to parse XML documents. So remember that for each node, there is a specific value. And you can get this value by having either a reference that will scan all the nodes and get the name of the tag or attribute that you want. Or you can set the specific tree structure so that you can tell, for example, that I want the product name or price and get the value of this node. So in order to do this, you can always search on Google, and the Dom structure has all the appropriate languages and specific commands to use in order to parse the XML documents. In fact, these commands in all programming languages have nearly the same structure and use the same libraries: Python, CSARP, C++, and you can use the dot to parse XML files. So I’m looking forward to seeing you in the next lesson.

  1. Multithreading in Python

Hey guys, welcome back. Now in this lesson, we’re going to see multithreaded programming. So running several threads is actually similar to running several different programmes simultaneously. But actually, multithreaded programming has even more benefits than running several different programmes concurrently. Multiple threads within a process share the same data space with the main thread and can share information or communicate with each other more easily than if they were separate processes. The second advantage is that threads are sometimes called lightweight processes, and they do not require a lot of memory.

So we can say that they are much cheaper than processes, in terms of the resources that they need in order to be executed. So a thread has a beginning, a specific execution sequence, and a conclusion. It has an instruction pointer that keeps track of where it is in its context so that it can be interrupted. It can also be temporarily put on hold, which is known as “slipping” while other threads are awake and running. So we can have two different kinds of threads. We can have kernel threads and user threads. And kernel threads are part of the operating system, while user threads are not implemented in the kernel. As a result, there are two modules that support the use of threads. In Python 3, the first one is the thread that we will see, and the second one is threading. So we will now start a new thread. The first option will be an example. First and foremost, I’m going up and importing thread and time for my example. As a result, you should include these two libraries in your example. I import threads, and I import time. I will now return to line 231 and write I’ll define the f function for my thread and name it def print time, thread name, and comma delay, and then count from zero to five while count is less than five.

Then time slip—actually, I have to put a tab right here, “time slip” for a specific time called “delay.” Then I’ll do count equals count plus one and print two strings: the thread name and the time, which is the current time in seconds if you use the input time. So you will see what will be printed. So I have time, and then I will need these. Actually, I will use this format. So I have these parentheses and one more parenthesis right here.

So then I’m going to create two threads right here will be created. So, for the first one, I’ll try to use thread, start a new thread, and print the time right here and first thread right, and then I’ll copy and paste it, starting a thread again to print the time, but this time it’ll be a second thread, and this will be number four. So here’s where I’ll make an exception. I will try to handle it. And print. We have a problem, Houston. We were unable to start threads, so yesterday I saw the movie with the biography of Neil Armstrong. So I am actually influenced by this movie. So I will use this. We have a problem, Houston. And then, for one, I will use this infinite loop so that it will actually execute the same command again and again. So, when this code runs, I’ll right-click and launch Python filing terminal. I have a CDX error. Define print time lag Yes, of course. I always forget this, and I will check it. Also this. While the count is less than five, I think it’s okay right now. Right click. Run the Python file terminal.

img