 All righty, we're gonna dive into a new project here this one. It's gonna be a Christmas exchange Generator and emailer Family and I like to exchange gifts via kind of a secret Santa Method essentially but there's We have spouses that we always want to give gifts to but the extended family Just want to give One random gift to a big group of us. So we make a secret Santa through everybody And with that we have some restrictions of not wanting to pick your spouse but having the spouse included That so with that we're gonna create a little Python program to generate all of our Exchange recipients and then put some restrictions on to who can be a recipient for each person and Thirdly we wanted to send out the email so that We don't have to manually do that as well Rather than using a notebook, we're going to use Microsoft's Visual Studio Code works pretty well with Python. It's free and we can do a lot more Stuff than was a notebook especially with editing and modifying classes rather than a little more difficult with a notebook So we're gonna start off make this a class Exchange Welcome to call that whatever you want Then we need to have our In it generator We're gonna Have this be generated two different ways one possibly with out a file and one with a file So we're gonna throw this name in here. This file name is gonna be a string course But we're gonna have it Default to none, so it's not required and this generator doesn't return anything special so We're gonna first just lay out everything that we want to have this class do Kind of it's a good way to mentally break it all up So that you know how to break it down into pieces and tackle them one at a time So what do we need to do? And create the class we need to be able to read all the part Pants as one method That we want to have done through a file Probably a CSV We're gonna store it all internally into the class so we don't need to return anything just put pass for the time being Next we will do That's one method of getting them the other method is we're gonna ask The command prompt to tell us ask for Participants self that one You don't need any Functional internal so we'll just leave that as self and pass We got away. We got two ways to Read from a file the participants and one way to ask for the participants We need to correct that type over there participants Next is we want to be able to select Names I'll return them Most of these are just gonna keep all the data internal to the class We're going to store all these names in a pandas data frame. So we do need We do want this a simple way to get that data frame and we're gonna create a property And we're just gonna call that people that get the whole data frame there, so So this property will let us just type the class variable name dot people that and I'll return our Internal people to add a data frame We haven't defined that yet But that is under self dot people data. We get the participants. We've selected the names we now need to probably have a way to print the results nicely So we can verify That they are Correct. That's just printing. So that also returns none We can also throw in a Default print in place you print the method Get going print once we do get it working. We can just do send emails Sending the emails we're gonna do through Gmail so we're gonna create connect to Gmail Method do all the authentication for that or who is sending them all right and If we want to use this multiple years in a row, we're gonna want to save all this stuff So we're going to write it right results, and we'll do that to a file name As well that is a string Let's see here. We've got the reading. We got the writing We got some printing. We got some sending emails I think that's good for now. So we will start diving into each of these portions here Since we don't have a file already We're gonna just start with the ask for participants method, but to Actually have this run something when we run the file We're actually going to create our if Main sorry if name equals Main we're going to create initially all we need is Xmas equals Christmas exchange Not passing a file name because we're not doing it from the file initially, but to Do it through the ask for participants, but how does it get to the ask participants? We got it actually generate our knit knit Which will be? Something as simple as self dot That's going to be our gin our hand does But for now, it's empty next is if File name is none We want to do read self read file name It's only variable. So we're just leaving it there and then otherwise else Do self dot ask for participants. I Think that's all we need for generator right now so We're only going to start with the non-clear file name to start with so we'll just dive into the ask for participants With that we need to do some inputs we're generating this for a bunch of siblings and their spouses so We want to ask for Who the person is that's giving it and I'll create a prompt on the command line to enter in the name but we've got Tell the user what they're asking for so name of artist A little spacer so it doesn't start typing right after gives a little clue Endpoint we need to ask for partner is that We're including this so that Spouses don't have to give to their spouse in the secret Santa in addition to their own gift their own gift exchange so do input name of partner To as well as as well because we're going to assume that both The person and the partner both want to be giving gifts and not to each other So we'll put that logic in later, but this way we don't have to ask for John Who has a partner of Jane and also Jane who has a partner of John? We'll just automatically add both of them so we don't have to leave both of them but We'll also leave the option of leave blank if Done if you don't care about the partner logic then We'll just tell them to leave a blank If you happen to be doing the gift exchange that doesn't need it more People just label that function as more input add more people Make this cyclical We'll help them out. Yes or no so that's easier for us to determine what they meant and All this needs to be indented a bit to Add it inside of a while while more So while true But more isn't a boolean so we need to do another variable more people people And that should start off as More People always need more people right so More people But now we need to deal with this actual more yes or no, so We'll do this simply we don't necessarily care what they enter as long as they say no, so We'll just assume anything other than a no will be a yes so more Lower so we don't have to deal with capitalization or lowercase We'll just make the more input Of any version of no lowercase, so if no lower more dot lower equals No or Lot of times people like to only use the initials for yes and no so we'll do or more dot lower equals Just n We'll go more people Equals false no more people that'll end it after this one cycle now We got our person and our partner we now need to Add those to a list that we can add to our data frame eventually so we'll go if Partner because we did have the option of leaving partner blank. So partners blank We're gonna go people I mean all the people so make those people list And we'll add the two people the person And our We're gonna go people list and Partner no way we had both John was a partner of Jane and Jane with partner of John Don't have to ask for both the people but we're appending this so person created people list Equals an empty list. There we go. Okay, so We've now added it if they have a partner but else They don't have a partner we need to do people list dot up and person None for the partner All right so We've asked for names. We've added them to list We will now Continue to cycle through this until more is no long isn't returned with a no And then once we are done with that whole while loop we can do a self Dot people data make that Into a Actual data frame And does dot data brain So nice part about This program here Gives you nice little helpers so PD as it commonly is imported as From pandas we can quickly get a quick fix of ad import pandas as PD at the top of our list Growing up here. We'll now see it through in that pandas as PD and now our function and everything turns green because it knows it's imported correctly and So how do we want to do this? We made this as a list before so we can just throw this into data frame PD dot data frame through our listener people list All right now We know what those columns are but pandas doesn't so we will label those as with columns equals Our two column names name person that's giving and With their partner Well, there are two column names simple as that and Protesting sir sakes we're gonna go print self dot People data right at the end. All right, so that In theory should work right there or as participants. So making sure we're in the right folder down here in our terminal we're going to run it with Python 3 what we're using Just for sake of pointing out I'm on Python 3.10.7 now So Python 3 Then our file name is Xmas exchange dot pie run that Already that didn't work. So Now I need to do a little troubleshooting go back to the game See if check our main first of all actually at the end here just goes Xmas Christmas exchange. Where does that go? That goes to the init Initializer which all we're asking for is people that is none if file name is none else this We want here. So we're going to go in and go print Test We'll run this again No change. So we know else. We're not getting inside this else So we'll just drag it up a little more a little broader Make sure we update that Wait space Run it again. All right, we got test printed out here. Okay, so we get here. Let's look at this logic again So if file name is none, so we have no file name As you read it out again Sometimes just reading out loud helps we need a file name if we're gonna actually read from a file So it's not is none, but is not none There was our typo, but We'll double check throw this back in throw this back in the Correct else statement run this one more time. We did get test and hey name my participant Throw in John Name a partner to add Jane just so we make Letters similar so that we can easily see add more people. Yes They have a participant Sue and Sam More people. Hey, we know we don't need a yes or no We just need anything but a no to keep going. We'll just enter that in name a participant We got John Jane Sam Sue Tom Tammy and Paul oh, we're add more people This should work so just testing it. Yep. Go back to name a participant Paul Name a partner as well leave blank if none Paul doesn't have a partner add more people. Let's do a lower uppercase and Ends it and like we threw in there printing the data frame. We get our name partner columns John Jane Jane and John Sue Sam Sam Sue Tom Tammy Tammy Tom Paul and none no extra blank one with the none so perfect seems like our logic worked We got our whole list of participants inside of a data frame All right now that we got our command line input working we're going to actually jump over select names and Dive into writing the data and then reading the data so that We don't have to always type those in even when we're testing out things here So we can just quickly get this saved from the ask for our participants Into a file and then take that file and just read it in as new participants here. So Can't read anything we don't have a file for it yet, but we can jump to the end and do the right results Yeah, we don't have results technically, but we do have the first two columns name and partner so with that what do we need to do we need a file name and We're going to actually make that optional. So we'll leave that as none Because you can pass in a file name or we can generate it. We know roughly what we're doing here. So What do we need to do Start off with if file name is none. I'm sure it's none and Is none meaning that they haven't passed it in so what do we need to do? We need to generate what the file name is file name is What would be a logical name for this maybe Exmus results Because we also want as an input. So exmus Exchange then maybe the year How do we know what the year is though? We will throw that in above here with year equals Date time Dot today Believe is the right function here import date time not today, but Date dot today. There we go. I'll give us a full timestamp of Today now But we don't need just now we need just the year So dot year gives us a year so Now this is nice and blue Christmas underscore exchange underscore year is what the file name will be But we need an extension also CSV comma separated value good and simple We have a file name whether or not they gave it to us with the right results So we just need to turn this file. I mean turn this data frame into a file a CSV file Thankfully pandas has a wonderful method for this. All I have to do is pull up our People data data frame and go to CSV all that needs has lots of possibilities, but all it needs is The file name actually so file name Pass that in we're also going to remove the index. We don't care about the index of the names It's a small small list And we're not actually looking at indexes on the CSV so we'll do index equals false All right, so Normally you can just go Python 3 Filing, but we want to stay in Python. So we're going to go dash I then file Xbox Now you'll notice after it ran through the if name equals main List of commands it dropped us straight into a Python interpreter so we can now get to our Xmas function we can see type Xmas is Christmas exchange class And we can go Xmas dot write results We'll do Both methods test file name CSV Once without Messed up here. I forgot Year gets returned as an int not as a string. So we need to make that year Inside the file name into a string actually Quit out of this Python interpreter so we can use the updated file. Oh Also one more thing before we forget we left that test in here of the generator to believe that out Okay now we can go Xmas dot Rate results CSV tendency and then we'll go One more No file name seemed to work both cases. All right exit out of The Python interpreter and then we'll go LS test Yep, there's our CSV we'll even go cat test We get our name partner and then all of ours with our nice little comma-separated values then we can also do LS What do we call it Xmas or Xmas CSV? So cat that as well Xmas Perfect seems to work pretty well already so from there What do we need to do? We need to write the results Just like pandas was nice and inconvenient For writing to a CSV file. It's super convenient for reading. So all we need to do is self dot people data equals PD dot CSV Inside there. We need to pass on our file name. I have you not known what the file name is prior And what files person has used we're gonna not leave not auto generate any Names they can if they wanted to put that in they can code it in but we are going to just do that and With that we're going to start with this in case we start reading to that default that way it stays as Default blank just name not blank, but just name and partner no additional columns of our results for testing sake Whereas we can still keep saving To that default Xmas exchange 2023 Alrighty now that we got that backbone of the whole project done. We're going to Take a break here and in the next video. We will do the emailing portion specifically with Gmail show you how Python and Gmail can interact so that you can send these emails without actually having to write them yourself as well as Make sure that they don't go into the spam filter of other people's emails With that, let me know how you liked this longer format rather than breaking down the videos and Any other questions that you might have let me know Thanks for watching