in this video, we're gonna be going over the journeymen activities, the journeyman Python activities.
We're going to go through the four Byner Challenges that we have. And then the next video, we'll be covering the actual capstone.
So we're just gonna dive right in the 1st 1 Journeyman One
is take two arguments. A list in an integer.
The list is a series of strings and one of those strings will be the file name
the others. The other strings will be the file contents.
The imager, which we're gonna call item is the location in the list of the string that we're going to use is the vile name.
So, for example, if we get a string list of, you know, just
If we get a string list that looks like a B C D E f.
And we got a manager that is
Then we're going to call a zero. Of course, be is one
his three cheese four. And if it's five,
I'll be the file name
these will all be the contents.
It's a little complicated. Thio sort out in just the text, so it'll be a little bit easier once we actually
dig in and start doing it.
first thing we've got to do, obviously, is we've actually got to
identify the string or identify the file name. So we're gonna do file name
The reason we're doing this is just in case we get something weird. And they passed this an image or something like that. We want to make sure it's a stir so that it
creates a proper file name.
So we've got a final name.
Pretty sure it 14. Easy. Then we're gonna actually create the file.
And then f open open. Takes another argument, and we're gonna open up our
by then interpreter to see what the argument is. So python
Just the file object.
Okay. It takes a few possible arguments
open name, and then it needs mode or buffering.
see file dot doc for further information.
So this doesn't really tell us that much, so let's see what filed out. Doc tells us
file dots underscore. Underscored toc Underscore. Underscore.
Okay, there's some stuff. So Moken b r w or a
um ours read w is right. A is upend.
Ah, you can add a plus to the moto last simultaneous reading and writing.
That's what we're gonna want to do
on the buffering thing we're not going to worry about.
That's just not really something necessary.
So we're gonna open this with W Plus
all that means we're going to open it for writing or reading
Then we're going to say,
We're already using that as a file name
See, It's got a bunch of different things that need to the file name, and then you have the option to give it a compression
or other arguments. But we just need
what we're actually writing.
and that's pretty much the end of it.
After that, we're going to do f dot clothes. So we close the file always, always, always remember to close your files, and then we're gonna test this.
So we're ready to test. We're gonna pop over to the journeyman activities tester dot pie. This is, of course, like how the others included
in the materials to the side.
So we're gonna open this and we're gonna run it.
It says success. So it worked.
Uh, you don't really need to see the code in this, And it's a little
crazy and nuts. Some listed orations and generations of lists within lists. But
long story short, what it's doing is it is opening the file.
It's checking East line in the file to make sure it's doing what it thinks it's doing.
We're streaking. You joining in the file,
the to make sure it pretty greatly. And then it
So we'll go ahead and I'm gonna drop a print line in here.
A print in here. You already have it in yours.
the actual line that it gets for this
information that it gets from the file to show that it is actually running everything correctly
clean it up a little bit,
so it's easier to read
dealing with the wrong direction.
There we go. So you see, he gets a C D. This is a test
That's what we wanted. And it tells us success.
Awesome. Finished the first activity. The recital.
All right, second item 1.2.
Ready Function, which takes a single imagers an argument and returns to some of every integer up to and including that number.
This is a little trickier. It's not too bad, but it's a little trickier.
we're getting in the final number in the set
we could use if we were using the built in generator.
Be relatively easy to do, actually be some range
clarification and show that that is what we would want to do. Well, actually,
because rain just on inclusive, we want to do that. But just to show that that's kind of how it works is we can go over here and we can pass
Well, we have a minus. Sign my return over here.
Fantastic recon. See tennis. 55 Forest 10. 1000 is 505 100
666 Well, that's unfortunate number.
Didn't work. Didn't expect that. I'll change out to 35 just for the more
peacefully minded in the audience anyway.
what works. And as you can see in my tester,
that's how I'm testing it to make sure you're doing it right.
But we're actually going to implement
So instead, we're going to
first, we're gonna comment this line out. Leave it is the objective.
So we're gonna do a return,
So we've got to get range that we've got to figure out how to make this happen.
as you can pretty obviously guess we're going to be using a generator.
A generator starts out as a while. Loop every generator starts is a wild loop with hope
is less than final them.
And because we actually wanted to be inclusive with me. While it's less than or equal
we're gonna say current nam
So while it's less than or equal to final them
it's going to increase.
But first, what we want to do is, they actually want to do what's called a yield
covered yield in the slides. But
just for a quick re summary yield is going to output something without actually
doing a return or an end.
We're going to yield
But here's the thing
is that you can't yield
without some sort of function. You've got to treat a generator, has to pretend to be a function.
So we're actually not going to be able to do this one
all in one function. We're gonna have to create our own,
so we're going to call it death
And this is what we call an internal function. This isn't something that's referenced overhearing our tester. It doesn't need to know about it,
but it's an internal function. It's something that is necessary for your,
public functions to use properly. There's actually an entire process to creating internal functions, but we're not really gonna worry about that right now,
So this is also going to get the final numb.
We're gonna drop our code in here.
So now what we're gonna do very easily is we're going to some
final numbers. The argument
some generator is actually going to return,
it's going to yield over time. It's going to give us each number in that list, and some is going to calculate all of those
a t end of which will be able to well range
some people. Is that
so? The end. We're just gonna return
the result of some going through this generator.
So let's have a crack at it and see if Python likes what we've done.
Door activities Tester
as you can see, really straightforward.
Uh, the trick to generators Is this right here? The yield?
All it's doing is it's returning a value without ending execution.
What were we actually just implemented
is something that I tend to write pretty often
because I am very. It's very easy for me to get that to forget, to put in this plus one,
and it's easier, and I actually find that I save time by quickly writing this function ahead of time. Just as a range plus one,
uh, is the equivalent of a range of a number of plus one, rather
so with that, you finished the seconds minor activity.
Pretty painless. Pretty quick. Pretty easy.
And we're gonna move on to the third.
Uh, this one is going to be our first adventure with networking.
Ah, you're gonna write a Python script which connects to the included server
on Port 5 50,000 and one and returns the message it receives.
So it's gonna be pretty
We have, ah, server, the journeyman string server over here,
and it's really basic service. Had a message list that it's gonna
open. It's gonna send one message each time you connect to it
and only commit through them.
So your job is to actually
do, uh, create this client that's going to go out and get that information.
First thing we've got to do if we're gonna be doing this is import Socket
socket is a really, really important module. It's the basis of
everything you ever going to do across the network, your
going to need socket for it,
and for that reason,
you're gonna want to learn about it. So I recommend you take the time. We're not gonna do it with just the help in the dirt, because there's a lot in it.
We're going to go over some of the things with
the help in the dirt, but we're going to kind of gloss over because there is a lot of information, but it is one that you should absolutely go and look up and really do. Some reading on sockets are very important.
So we do import socket,
and then we're gonna do
your sockets. We're gonna see what options are available, what functions are available.
pick one out of that. For example, we're gonna do
socket, not sucking.
That's the one that we're gonna be using.
Ah, a bunch of other methods of socket. Are you there? Socket objects?
But what socket? Not sock. It actually does. Which I think it may have said at the top, but there's a lot of operation. What? Socket down stock it actually does. Is it returned the socket object?
Um, of a type that we define.
we're gonna look at this. We're gonna say Okay,
sockets well s, which is gonna be what we call her socket.
Now we're gonna need to arguments here.
The argument that we're going to be using for the purpose of this is a fine et
And we, of course, we covered both of these. If you haven't gone, if you haven't been actually watching the slides videos, we cover both of these. We cover a lot of socket information in those, so it's worth pausing this to go check that out.
But remember, since we imported this module instead of importing everything in it,
we need to specify where these air filed, which means socket, not a finite and socket dot socks street.
So now we have a socket.
all that's really doing is it's opening up
this socket exists here is an object for it s
as is now a socket object
ivy before and T c p
Yeah, fine. It is the i p for suction misty speak.
So then, after we create a socket,
Uh, so the host is going to your local host
so we can just give it a local host and the address is going to be
five or 50,000 and one
test Arctic set for it, not accept but s dot
Receive. That's we're looking for
which in Python It's actually not the word. Receive its r e c b. Sorry. There we go.
We're gonna do received string
equals as stock receive.
They were going to us close
and we're gonna return overseas string.
So this is obviously super basic, but it connects. It creates a socket.
It goes somewhere too.
Connect to the server. In this case, we're using these quotes here represent that it's the local machine and you're using every interface you can get your hands off.
Then we receive a string
which is get whatever this under the service sending
and then we close the socket closing stock. It is important
because if you don't close it than it might send more information or line might go, You may be using resource is that need to be released?
first of all, we're going to start up the journeyman string server,
Move that over here. Just kind of hide that
We're gonna run our activities. Tester.
We're going to see if it works.
that would exploded.
Okay, so it's not gonna let us connect with quotes in this one. You could do that in a, sir, but you can't do that in your local machine.
Uh, are you can't do that when connecting out to the server,
which is fine. We can just do
the string of the local host. I P address was just 127001
So the reason, by the way to explain why you can't actually have just an m p i p field is that when you're connecting out, it's actually trying to go to the Internet.
So rather than connecting on every every resource you have available, you have to actually say who's resource is you're reaching.
we've still got our server up, as you can see. So we're gonna go ahead and run this
and it's still going to explode.
But this is a different one. So now we c. O s not received takes an argument. Okay,
important thing first of all, is that we have a new and special error. But let's see, what
are you received? Takes
If I was smart I would leave this open, but, you know,
import socket and we're going to help socket not receive E.
All right, so that was a bit of a problem. I had to look through the python, help and figure out what's going on.
So what it actually is, what we need to do is help socket, not socket,
The reason for this is within. Is that the original sock? It actually doesn't have a function called Receive.
It's the socket, not socket function, which does,
Um, that's really complicated and
there you go. So if you want to get help on the socket, receive what you actually need to do is socket dot socket not received
and that we're actually going to call that like it's a function. There you go.
receive what? You Actually, it is a buffer size eyes the argument that it needs to have, and it's going to return the data.
After a little bit of trekking, we managed to track this back down so buffer sizes, just the maximum amount of data you're willing to accept
so we'll do. It's not received, but for size of 1000.
Well, we'll do 10. 24 because this is a computer and
powers of two were easier.
Eso Yet for reasons that aren't super important, Uh, 10. 24 is a more optimized number than 1000 when you're doing programming. Always try and use powers of two when you're talking about data field data size. But that's not super important right now anyway. So let's give this yet another crack.
now it's running something
This time we're not able to get a connection. Well, let's see why
our German string server over here doesn't seem to be running anymore.
Ah, that's the problem.
We're trying to run them both.
That's not important. We're trying to run them both in the same
So what we're going to do instead is we're real quick. We're gonna do some,
we're gonna open it Instead, we're going to go to Python.
We gotta find this file, actually. So you're gonna go and you're gonna go to your documents
or wherever you're storing these files, I'm storing mine and a folder called Jeannie
And then we're just going to do python
Ah, Journeymen String Server.
as is what, as happens often when working with Python working with I d ease you run into some weird courts by the I. T. Jeannie's. One of Jeannie's weird quirks is that you can only run one Python script at a time,
so we just have to run this one manually.
We have that running.
Go over here back to our activity, sister, and we hit F five
and test was a success. Test three failed.
Socket is not connecting.
So for some reason, it failed.
So let's take a quick look at why.
Turns out, actually, uh, the reason it failed. It just paused and check. My other code is that the server was slightly Ms Written. It'll be correct when it gets to you, of course. But I had a little psycho. So let's try this again.
This time it should work just fine.
So you run our script and we hit a five
You see that returned off of Bravo Charlie Delta.
So, barring you know, human error in the
we find that networking and python is actually pretty easy to connect and get information only took four lines.
the important thing in socket workers and anything really is
knowing you know where to look for help The pie, Thunder Python help functions are very important
as well as obviously Googling on and then the python manuals.
But yeah, that's all there really is to it for networking and Python. And with that, we've completed 1.3.
So the next 11.4 is working with classes.
Classes are one of the things that make python extremely import.
Um, classes and object, as we discussed, obviously in the slides. But quick summary of class is an object,
sort of the one of the many cornerstones of python.
So what we're going to do with class and this one is we're going to create a class called person,
and that class is going to have the attributes of height, weight, hair color and eye color.
And they're going to implement it to describe yourself or some other mythical person if you don't want to describe yourself or whatever, is it?
so the first thing we've got to do obviously, is class,
and that's a python keyword. Everything in Python. All classes and python are generated from a sort of base class, which just is something which just exists. So we do a class
And then we started adding fields so self
equal zero self doubt. Wait
hair equals and empty string
I equals an empty strings,
and that's the basic class. So that's that's the contents of the class. Obviously, we're going to do a little bit more.
So the first thing you've got to do it
is initialized that class. Pardon me
and what you initialize the way you initialize rather classes and Python is with the
So the first thing we're gonna do is self. So every function in a class has self as argument, and that's just a sort of pretty fine thing, which is the, um,
the object of the class itself so that your functions, your methods,
which is what this is called, actually. So put that in there.
No, they're referring to their own function and not something wild and wacky.
And then that's the functions of colon at the end
and same for all of them.
Pretty straightforward. Pretty simple
So now when you create a class
on instant in instance of a class, you can actually
So, for example, with new class, Billy will do Billy the incense e ation of class,
in class person again. We have height, weight, hair, and I we don't actually given an argument for self, for innit? That's something that it just needs without us doing anything so high. Well, so we'll save that Billy is
Wait will say that he is £180.
And then we're going to try it out, and we're gonna just see
if that's you know, if all of this works, right,
So print billy dot height,
And so on and so forth.
So, for this one, we're actually not going to the tester. Doesn't actually give you any information to test
Um, because obviously
it would be kind of hard to test to make sure that you're putting in the exact right things. So instead it's just gonna run it. And the print statements that we put in are gonna help us make sure that we have in San created our class properly
So we're gonna hit at five, and it's going to sprint through all of our tests. Well,
one thing we are gonna want to dio and this is the easiest way to get past it is after you've completed test three just comment that out.
Um, you can just run your server over here
every time you test,
but that sort of time consuming and it slows down to use up a lot of resources. So I recommend you just comment out test three when you when you've successfully completed it,
And it says self is not defined.
Well, let's have a look.
So we see that we have all of these fields being populated in here, But the air that it gave us was that self is undefined.
The reason for this is because the first thing
accidentally moved these Sorry.
The first thing you have to d'oh
on the classes to find is initializing this has to come before anything else. Because then it is a built in
ah has certain built in qualities and python,
which will again initialize self.
So where we tried to give it default values up here for all the self fields, those don't work. We can't do that.
but say we wanted to have sort of just the native class person, just in case we don't want to actually put anything in it
that is possible to do.
And that's why giving it
basically, it's by giving it default values. So what we do is we go in here and in the actual function arguments,
we set these values.
So what this is saying is, if you don't receive anything for this value if you don't get anything,
um, for this argument,
that's fine. Just fill it with what's already here in the argument
in the function arguments.
So save that we're gonna go over to our activity sister again, we're gonna test it.
Expected an indented block,
It's all it's doing here is
creating an object based on the arguments were giving it right here,
So the initialization is done, but
do we really wanna have to do a separate print statement
for every single thing?
Wouldn't it be much easier
We, of course, after I give itself
and then we can use to print
and we can actually, you know,
percent s. And we can actually make it a little bit easier to read by putting in the actual values
and then we give it the arguments.
And instead of printing, all of these were just going to do
billy dot print info. We're going to actually just call that macro directly.
But Colin to the end of death
of the function definition there.
Right? And look at that. It printed out just fine.
Now, some of you may be slightly confused if you were paying attention
by the fact that these
or different functions
and get these arguments are these variables are the same through them.
If you think back to the activity section or to the activities the Apprentice activities section there we go,
um, in the final activity for that, you had to define something is global if you wanted to use it in different functions. And this is where one of the really useful things about classes comes in
in the class has these values as sort of being global within itself.
Any method of that class and he function within the class can use the same variables.
That's great. And it's potentially dangerous if you're not paying attention. You might assigned the same name to two variables, but in general is just really helpful. You don't have to declare global's or you don't have to use some special macro. You just say, Hey, use the height, weight, hair and I that we to find somewhere else or that we messed with somewhere else
So that's the last in the minor activities for Journeymen Python.
pretty straightforward for all of these. Of course. As always, if you have any questions or if you'd like anything cleared up or if you just like Maur challenges Aiken
usually script sum up pretty quickly.
Send me a message on the on my page on the side worry about 80 website.
And after this, you're gonna get to dig into the journeymen final activity.
So thanks for watching.