Placeholder Image

字幕表 動画を再生する

  • All right.

  • Hello, world.

  • This is CS 50 on Twitch.

  • My name is Colton Ogden, and this is the third code of you have done with David and Bryan.

  • Bryan, this is Brian.

  • Second time being on our code review.

  • Um, yeah, it was a good time.

  • Last time we covered a lot of different languages, and this time we'll be doing much of this is good.

  • It's good to be back.

  • I'm looking forward to all the submissions that everyone sent in by the Google form and we samples to look at that.

  • David even dressed up just for this occasion did a very formal code reviews.

  • I'm taking this one very seriously.

  • Thanks to a hacker Becker for following just before the stream.

  • We have a lot of people in the chat.

  • I say we just cut straight to the first demo that we're gonna look at today, which is actually a really nice one.

  • So let me go ahead and bring it over to clear strike by Sam Arthur G R s.

  • So what do we have here called s O.

  • When I was looking at this, it looked like a seal I version of a game that's very popular in South Asia called Carom or Karama.

  • Entirely sure how it's pronounced, but it's very similar, actually.

  • Have it on Wikipedia.

  • There on the second, Tabby pulled it over.

  • So there's a picture of the board and folks might build to see it underneath the chat with varying ease.

  • Sure, we enhance.

  • Yeah, that's enhance a little too much enhancement way Go eso it's It's very similar to, and I haven't played this myself look too deeply into it.

  • But it's a version of pool, essentially with what looked like checker pieces, where there's a sort of a Q piece that you get knocking at the very end of the queen piece, so to speak.

  • And the app that will be looking at is sort of a CLI version of this.

  • There's kind of an interesting implementation.

  • Nice.

  • Well, yeah, if you played this game in the real world, you feel free to chime in, especially to correct anything assumptions were making.

  • Fortunately, we'll focus on the code itself.

  • And indeed, in response to one of the questions that was just asked here by I lost track, I have the We have it up there.

  • Yeah, yeah, Can we make that a little bigger?

  • Oh, that one.

  • That one.

  • So, yes, this is the kind of Cobra view that we've been doing in the streams Would be akin to what you might do with a colleague or a manager in the real world.

  • That a company often these good reviews that would be done asynchronous late.

  • So if I get hub or some similar Web based doing things in person is not uncommon.

  • Shall we dive in?

  • Yeah.

  • Sounds good.

  • All right.

  • So the depository that some are contributed was last worked on 27 days ago, and I could see that right over here on get help.

  • Which means this is quite quite fresh off the presses, so to speak.

  • This coat and I'm seeing a whole bunch of files here.

  • Just under 10 or so files and folders.

  • How would you go about wrapping your mind around what to do?

  • I've never seen a project before, and you want to start with All right.

  • Well, the first file that started catches my eye is main.

  • Don't hi often main active entry point for a program that seems like a reasonable place to start looking.

  • Okay, trying to figure out works, all right, And how would you approach reading a file for the very first time?

  • That's not part of an assignment, but indeed is a whole project, So first thing would probably take a look at it.

  • It looks like they're a couple of functions that are defined here, so I might just glance through the file, get a sense for the functions.

  • We have a function called Load Turns here function called mate Right now, them, I'm guessing it's probably where the game is going to begin.

  • Main tends to be a common first function that gets called in the program.

  • We keep scrolling down to find out it was actually the case, this sort of cryptic syntax.

  • Never seen it before.

  • The underscore underscore name underscore.

  • UNDERSCORE equals Maine is Python's way of saying like, If I am running this pipe on file trying to execute it, this is the code that should run, and it looks like it does some initial set up and then down online 89 it actually called.

  • So this actually inside of this I think Brian has an opinion here.

  • It does seem like it's perhaps a little bit less conventional to be putting a lot of logic and underscore underscore name equals Maine and then run through a whole bunch of logic and then call the main function.

  • It seems like a lot of information about initializing the parts are checking the arguments, loading the end.

  • I do like the commenting.

  • It helps to make it clear what things going, what sections?

  • But I would say that probably all of this main function the main function doesn't seem to me that much of a reason to separate the main, Indeed.

  • And to be clear, let me go ahead and temporarily pretend to edit this file save changes since not actually ours.

  • If I go and scroll down here.

  • What Brian is alluding to is very common paradigm that you actually see in a few of CS 50 zone examples whereby you don't actually put much code it all in this block.

  • Let me just temporarily leave that you would instead just quite simply call.

  • So for those unfamiliar like, why is there this paradigm?

  • Because I dare say, in an introductory course like CS 50 this is just atrocious in tax problem.

  • Does this actually solve Yeah.

  • So one thing that distinguishes Python from a language like See is that when you execute a C program, the main function is a function that has some.

  • It's a special function that when you execute the program, the compiler knows that the main function is the first you're not file, but the violin higher.

  • It's not going to run the main function out of the box.

  • Maine is just the name of any other function that could be any other name on DSO.

  • This line down here is supposed to say, All right, when you run the function, we actually want to run.

  • The main function of the first file and the extra syntax of the underscore underscore name equals underscore.

  • Underscore Maine is to account for different ways in which we might use a file.

  • We could just try and run a file but running pipe on main, not pie.

  • In the you want to run the main function, you could imagine if they're variables air functions to find in a file that we want to use in some other.

  • We don't want to actually run the main function.

  • We probably just want to import for the interruption.

  • Looks like somebody saying the sound is low.

  • Just want to confirm that.

  • That's indeed the case.

  • Some people are saying that it got fixed.

  • Brian, would you mind saying a few words?

  • Hey, testing on David, Can you say you think testing, testing, and then 123 So let us know sound is going from loud.

  • Too low intermittently says methodology.

  • Yes.

  • Still happening.

  • It looks like Thanks.

  • The Kaiser twitch.

  • Okay, well, we have somebody is currently working on it.

  • So apologies for the sound issue.

  • Hopefully, we'll get that fixed on the adamant in saying Sounds good.

  • Right now.

  • Void T thank you very much for the follow here.

  • Um, folks, anybody who has any issues with sound different let us know so we can see if it's maybe localized or whether it's still going or what the issue might be.

  • Perhaps so far, so good.

  • All right, So just to be clear, in most cases, what we could even do if you do want to have a main function, is just literally in the last line of your code.

  • Just call main itself there by kicking off the whole process of running this file the additional presence of this condition here that Brian is describing really only solves a problem for more sophisticated programs where you indeed might want to import them if you're making a library or python package or module of your own.

  • But if it helps to keep things in mind more simply, for now, just do this at the end of the file.

  • But I definitely concurred with Brian's assessment that probably shouldn't have as much logic as was in here.

  • That's the whole point of having Main is move this into the main function if you if you brought this up already.

  • But having that if name Eagle's main great for a test cases, Do you mentioned that already?

  • No, I don't think we can.

  • Do you mean by test cases, like having If you were to have the test for a module within a model so he could run it and test its correctness, you could have all of those test cases be within main within that module and then run them that way in the file.

  • In the function within.

  • If you were to test this module and have a set of test cases that, for example, say this came with a bunch of functions.

  • You could import it in some as some library.

  • Or you want to test this module for correctness on its own.

  • You get execute this module by itself and within its main function would execute a set of test case.

  • Sure, Yeah, absolutely makes outside.

  • I think I saw in the Main Root directory the depository.

  • There is a test directory.

  • So it looks like this writer may have written some tests for their code as well.

  • I'm guessing it's probably more like a idiomatic way to do t d d with some other system that is useless.

  • So just a few quibbles, if I may, in terms of the style of this file.

  • So I see what we're using single quotes up here, which is perfectly fine and python.

  • It's slightly easier to type because you don't have to shoulder shifty.

  • But then I'm seeing double quotes everywhere else, and I'm not seeing a particularly compelling reason to switch in that way.

  • Down here, we're back to single quotes for some reason.

  • Down here, we're a double quotes, so to me, this just is a bit sloppy.

  • It's an easy thing to fix, but it also just presents yourself better, especially if someone is going to read your code or modify your code.

  • You want to be diligent about things like this even more typically, I'm seeing that this comment here is not capitalized.

  • This one is.

  • This one is not so here, too.

  • There's just sort of a sloppiness that the risk of reading into it too much just just Hey, if you're not even getting the basics right like that, maybe the code itself isn't a strong is that should be.

  • And I think you want to be putting your best foot forward here, too.

  • Oh, this looks like this just cominto and that's fine if the project is still in development.

  • But really in your master branch, the main branch of your depository, everything that's there should be there.

  • And this should have just been deleted, I think, from that branch and in terms of where else to go.

  • One of the very first lines here were these imports up here.

  • Let me go back to the previous view of the screen so that we have a sense of where we're going to go.

  • It looks like some Arthur was implementing this important some of his own file.

  • So Karen board dot pipe player dot pie and so forth.

  • That's a nice module or ization of it seem of the code.

  • It's not all just in one massive file.

  • Shall we take a look at, say, um, the main function itself?

  • Sure, All right.

  • So I'm seeing here.

  • That main takes in a few arguments, which is OK, but that's kind of violating the spirit of Maine.

  • Maine should really be taking command line arguments of anything, but it doesn't need to take that in Python because you could get access to those by assist on RV.

  • So I would keep this simple and more python IQ and just have a deaf main open print clothes print.

  • And that's it.

  • I do see that he's changed commenting styles here, but this is probably a good thing.

  • Do you mind speaking to this convention?

  • Yeah, These triple quotation marks that you might have seen before are what's called a doc string in python, and this is often conventional for documenting the behavior of a function or documenting the behavior of a class like a player or Karen border, which we might take a look at later.

  • And so conventionally underneath main, you'll have triple quotation marks and then a description of what it is that this function does.

  • And oftentimes, you'll also see this as an opportunity to talk about what the types of the arguments should be, what the return type of the function is if the function is returning something and just helps to document the behavior of the function, and then a whole bunch of tools that have the ability to actually read that Doc string and use the doc String in order to inform someone who's using the function, how it is that the function actually works.

  • Let me go ahead and go back to the editable version just so we can make some tweaks as we go.

  • The no tweaks necessary.

  • They're kind of oddly python.

  • Does its dock string sort of Underneath the function signature, we're asking a language like Java, it would be placed above the function.

  • Yeah, I can see the motivation because it means when you're parsing the function, the doctoring is actually part of the function and not just randomly a pendant above it, but it feels a little weird given these conventions to here and just be a little neater.

  • Give me some white space before your next comment so I could just understand that this is relating to the code below.

  • It not just commented out or something like that.

  • I would like a little more detail here.

  • You do say that this is initial coin details for the carom board and frankly, finding more about the game itself.

  • This might go without saying, but why nine?

  • Why one?

  • I would elaborate, at least for the reader, just so that we're not questioning the design decision down the road.

  • Here's pretty reasonable, I think initializing two players using the constructor function here, the player such and such.

  • Strictly speaking, I'm not sure you gain much by declaring player one and play.

  • You're too, unless, Yeah, I'm not even seeing those variables used elsewhere.

  • So I would honestly probably just simplify this and let me go ahead and grab this.

  • Personally, I would probably do this passing in player, one name and player to name just so as to not add variables unnecessarily.

  • It's just something else to think about it, something to accidentally misuse and because the line of code is still pretty darn short.

  • I think this is pretty clean, but I think reasonable people could disagree on the gold.

  • Probably say that's the Python equate.

  • D'oh!

  • Yeah, I mean, in that case to you might not even want these two names here separately.

  • All those did come in, I think, his argument.

  • So that's to be expected up in yet in Mainz actual signature, someone asked a man, This is separate, but someone was asking about sections for the Web and games Course.

  • I don't know if Brian Your course has online sections that we made available.

  • Not for the at X course.

  • No there.

  • Certainly, if you're looking for additional support without maybe online classes, like the Web class of the games class, if you log into the annex page on the right hand side, there are a whole bunch of CS 50 communities that you conjoined.

  • So there's a discord server.

  • There's a slack.

  • There's reddit and stack extremes that you can go on in order to ask questions and interact with other students.

  • But they're no sections percent for the next course, and I see a little earlier on Whip Street, 23 asked or noted that he's been using Python for two years and never used.

  • Name equals equals Underscore Main.

  • And that's fine because it really is a solution to a problem.

  • And you don't need to solve that problem.

  • If you've not yet encounter it as by writing your own library code.

  • That might warrant it is well, if youse python for Web like flash for jangle you encounter, it might still be true.

  • For Django flasks, recommended method is no longer to use a main function.

  • You should actually be running a program called Flask to initiate execution of the server, but you will see that in older versions of flasks.

  • And I think there was one other question you been scrolling up just a little better.

  • Yeah, can we go back in the questions?

  • Oh, and bad Ignite asks.

  • Why not a separate test file?

  • So I think Holden's comin earlier.

  • You could absolutely factor out any test to a separate file, However, if I were writing a pretty small library, for instance, see a safety zone library just keep everything together because it keeps things simple.

  • Only when it becomes unwieldy.

  • Should you actually solve the problem sharing, telling exist.

  • And then Adam Aunty was saying, What?

  • We're all on the topic?

  • Is there anywhere I can find the sample code that was showing class examples for the Web programming course that should be all be linked on the curriculum.

  • Pedro Necks.

  • Yeah, if you go to the course where Paige everything should be there in the set of blue links or red links associated with each and every lecture.

  • All right, well, let's take a closer look at some of the code here.

  • We're still in that main function, and I think it's pretty straightforward.

  • We seem to be initializing the carom board here and yes, very excitedly.

  • Having created it.

  • We're gonna go ahead and start playing here here on 9 45 We're calling a method would seem call play inside of the board and I like that kind of modular ization.

  • There's this notion of playing the board down here.

  • We're printing out the results that to make sense, to encapsulate as part of the board, if only because one the Carom board is representing a real world object.

  • The real world board.

  • Andi, it's also allows you theoretically to even have multiple boards in memory simultaneously, especially for testing purposes or the like.

  • Here I feel like there's maybe a non opportunity for improvements.

  • Do you know where my mind is wandering with comparing these literal strings here?

  • Yeah, I see.

  • At least two possible ways that you could approach may be improving.

  • The design here is so the issue that I think we're both touching on here is that we're comparing results outcome to a string where the string is one or draw.

  • And whenever you're dealing with a situation where there are only like a fixed number of possible outcomes for the game, it could be one, or it could be drawn.

  • Comparing it to a string is not necessarily the best way to go.

  • It becomes easier to potentially make mistakes of some.

  • Wherever I'm setting the result, I accidentally have a typo somewhere, for instance, and it's not one or draw.

  • Then you could run into unexpected behavior that you're not expecting and so sometimes even come up with ways of trying to work around that problem.

  • So I could imagine maybe you make outcome and in, um, for instance, where it can only be one of a fixed number of possible values, and so you can guarantee that will always be one or the other.

  • Or even it looks like the way they're doing this is they care about result that outcome.

  • And when it's one, they print out the player that won the game.

  • So I might imagine maybe just like having a variable called like who the winner is.

  • And the winner is set to either the player who want it or if there is no winner than it could be sent to none, which is just a default python value representing the absence of a value.

  • And I could imagine if results out winner is equal to Nana and that tells you that the game is a draw because there is no winner.

  • So you can imagine a number of possible ways you could go about trying to architect that.

  • Yeah, because python is loosely type to you can effectively cheater Neri states.

  • If you were to use a variable, uh called not outcome, but called one, you could say that result, you could check with the result that one equals equals true or fit equals false or if it is some other state.

  • It could be none.

  • Which gives you 1/3 option as well.

  • I think it's in the spirit of what you're saying now.

  • You mentioned him before, and I don't think we can do that in Python per se.

  • There's a you condone their classes, you can extend it.

  • I guess that's probably more complicated.

  • And in fact I don't even think this needs to be turn ery now that I look at anything interesting, binary, just true or false.

  • Well, I think, Well, I don't know enough about the game, but I imagine the game could be won a draw or still in progress, which is why they're not might not be an outcome yet.

  • But it's this is this is only happening after they print game.

  • And so I'm assuming at this point in time, I guess I don't know enough about the game.

  • You sure and you know a technique I just saw in a library have been using recently is actually to use class constants, even though they're not technically constant.

  • Can I go ahead and open up a little scratch about here?

  • Sure.

  • Um, what you could do.

  • What's wrong?

  • Page there.

  • Let me go back.

  • Uh, don't do this.

  • And windows.

  • There we go.

  • All right, So let me go into one of the other files, like the carom board, which I think is gonna be in its own file here.

  • And also thanks to vote a villain, Gylfie 90.

  • And I can't read that.

  • You may, but it looks like Korean text.

  • Thank you very much for the following.

  • Wonderful.

  • And so I think what you could do here in your classes do something that's very common in Java in other languages.

  • But actually, the fair declare class level variables and correct me from rolling on the syntax.

  • I think we could do something like, uh, let's say, How could we do this one?

  • We could have a variable called one and said it equal to any old value, whether it's one or even one, for instance.

  • And then we could have draw, equals, draw just a string representation of it.

  • And then in my other code, correct me if I'm wrong, I could down here if I scroll down to where we left off at the very bottom, I could say something like, if result thought outcome equals equals Karen Board 0.1 actually check as though it is a constant.

  • Even though Python doesn't give us the same constraints of someone would be.

  • It would be the class name, though not the object name, right?

  • I think both would work actual, um himself, probably more dimensional to use the same.

  • But I think you can still get away with Karen Board here 0.1 So it's a little sloppy, but at least in this way, you're not comparing against a hard coded string that you'll change for grammatical purposes or if you internationalize the game.

  • And therefore you have different languages speaking those strings.

  • You don't want to necessarily hard coat strings.

  • I think that some folks might knows that you know this as being a static variable or pythons implementation thereof.

  • Yeah, the closest you can something You may be a job R C sharp.

  • What's The difference, you see, is 50 on FedEx and CS 50 course website Are lectures divided into more bite?

  • Size is no.

  • The same content is there on Harvard College's website and exits just a different user interface on how long will assist with the beyond Siri's B.

  • We might offer it again next year.

  • Brian most recently put this online, but that's picks up our scent.

  • 50 leaves off and is a shorter version of what is Brian's Web programming class, which is already on weather decks.

  • All right, so where next to look?

  • I think if we look at the top of the file, perhaps there is this other function.

  • Deaf load turns.

  • But should we take a look, maybe at the class itself before moving on to another project?

  • When I was looking at this, it looks like Karen Board was the largest module in this project.

  • By far lovely.

  • So let's see, we have Okay, But the good news is, when I was reading this, it looked very well commented.

  • Oh, that's good.

  • OK, so let's let's go all the way back up to the top.

  • No more shortcuts don't work.

  • Yeah, it's where you have to use page up.

  • Paige up?

  • Yeah, and home.

  • There we go.

  • Yeah.

  • Okay.

  • So actually, here is something similar.

  • It seems to what you were describing earlier, or I was describing earlier of having sort of class level constants in a file called Config.

  • That pie, inside of which is the settings something or other.

  • It looks like a convict directory, inside of which is the setting time with likely.

  • Okay, nice.

  • All right.

  • And then we seem to be importing a obstruction of a player.

  • Implemented probably is a class that I'm inferring from capitalized p here.

  • All right, So here's the class name.

  • We have a really big dock string.

  • That's nice.

  • I think this does go in much more detail than we've seen thus far as to how this all works.

  • I'm seeing an innit method.

  • Now those of you who are unfamiliar with classes.

  • The class is just a construct and python and java and C plus plus for encapsulating related pieces of data as well as pieces of functionality.

  • So if you've only studied, see as and CS 50 you might recall Struck's and see which allow you to encapsulate or group together similar pieces of data.

  • Ah, class does that andMe or you can also have functions otherwise known, his methods built into it as well.

  • And in python, it's a bit curious because if you want to initialize a class or specifically an object of that class classes like called like a blueprint or mold out of which you can make multiple copies of it.

  • One or more.

  • Karen boards.

  • You see this function in Python?

  • Quite commonly.

  • Do you want to speak to what this is?

  • This is being used for?

  • Yeah.

  • So this is what we would call like a class constructor.

  • Basically, this is a function that will run that will create what we call an instance of a carom board.

  • So this class is defining a blueprint of what it means to create one such board, and that's gonna sign in again.

  • Wait a minute.

  • Function, accident.

  • And when we say in Maine, Dupuy, you may recall we used Carom Board and in Capital C, Capital B and then in parentheses.

  • We passed in the coins and the players that was implicitly calling this innit method to say, Let's create a new board.

  • And the nice thing about this object oriented syntax is that you can use this to create arbitrarily many boards.

  • If you wanted to have three games running simultaneously, you could create three Karen boards each time invoking this innit method, and that would create three different boards that have their own set of variables.

  • One of their own state andan functionality.

  • And Brian did an object oriented programming stream, actually, fairly recently.

  • So you're watching on CS 50?

  • Are you two?

  • Definitely.

  • Check that one out.

  • If you're on Twitter thing, the body probably still up right now.

  • All right, so we see here.

  • Ah, a few different checks here.

  • A lot of good error checking.

  • I think this looks a lot like code that I write where the very top of my function.

  • I do a lot of if else if I might interrupt over a whole bunch of variables just to make sure they're the right type.

  • Looks like we're using some art here is using value error, which is built an exception to python, which just kind notes You have done something wrong with some value.

  • If you pass in a string to it.

  • That is the exceptions message that the usable see where the programmer will see, which is nice.

  • I see we have some kind of dictionary here of coin types that we could see if we dived into that config file.

  • Most likely to see what we're validating here.

  • You look, you're looking a sconce.

  • Yeah, if coin types is actually a dictionary.

  • I don't know whether it is.

  • I'm assuming it is because we're calling dot keys.

  • And I actually think the darkies is probably not necessary here that if you want to check if coin is the key inside, the dictionary could commit the donkeys.

  • That's true because Keys is going to return a list of the keys in the dictionary, which is just a redundant operation.

  • If you can already do it by way of the end operator, that's a good point, All right, so we're checking some types just to make sure that the user is passed in the right variables, it would seem, Yeah, it's worth a wholesome and that it's a little bit more.

  • This will work.

  • You can use the type, function and fun to get the type of a particular variable on the check of players is, in fact, a list.

  • It's a little bit more platonic and more recommended in python to use actually a different function, which exactly is instance, Um, method every function and that's going toe.

  • Yeah, syntax like that will say, if players is an instance of list, which is effectively doing a very similar thing, it's more recommended for a couple of reasons, but one is to deal with the possibility of classes that are inheriting from other classes.

  • So you could imagine, for instance, if I had to find a class called book, for example, and I had to find a new class called hardcover book that he is a type of book, like a specific type of book.

  • And now I have a hardcover book, and I want to check to see if it is in fact, a book.

  • If I just checked.

  • If the type is equal to book, it's not going to match up.

  • But if I check if it's an instance of a book that I'll get a result the next, so long story short, it's usually preferable to use these instance over just checking if type is equal to something, actually, back up to the line 36 here.

  • I'm guessing if, uh, they're currently checking to see if coin is in a, you know, a sort of collection.

  • We could probably use a set here.

  • They're all the unique way.

  • Have to look at coin types and verify that, but true.

  • But if coin types, I'm guessing, is associating keys with values there are be doing necessary, Birdie said.

  • And if I may, I would push back on this.

  • And we were just coincidentally talking about this over email with some of the staff because I was trying to design something involving types just this weekend.

  • And, you know, I actually prefer this on further thought cause one.

  • It would be weird.

  • A presumption is to subclass list class, and clearly that's a primitive type built into the language.

  • You can use it as part of your own class, but it should really be tucked in Is an instance Variable, I think not just extended on dhe, too, when I'm writing code, especially for main code like this.

  • Like I don't want you to be able to subclass something necessarily like That's not a feature I want provide.

  • I don't know if that's gonna break my own code.

  • And so I wouldn't least in cases like this if I know it should be a list.

  • I would argue that it should be a list, no matter what, but I do agree that the code would break especially down here.

  • For instance, if we enhance player to have its own subtypes then I'm gonna have to go back and do this so I could be convinced that way that's here is well.

  • And Ellen one asked, Why not use a try catch block here in reference to the various raise errors.

  • You could, because the presumption is, I think, later on that it will be taken for granted that players is a list.

  • That player is a player, and just bad things will happen if you try to treat those as they in ways that they're not.

  • This is a downside.

  • I think of python in any loosely typed language and languages like Java and C and C Plus Plus.

  • You would specify the types of these parameters as the inputs to the function, so the compiler would actually statically be able to tell you yes or no.

  • The type is as you expect, and you wouldn't have to have all this annoying error checking.

  • And this is a trader.

  • I actually think this is annoying feature of a lot of languages, like Python, where you have to or should do this kind of checking if you really want a right defensive code or an argument could be made.

  • Don't do any of this error checking and just don't write incorrect code that uses incorrect types.

  • I mean, the python is kind of a mature language for mature adults, if you will, where you should just agree not to miss call things, too.

  • I think that's probably a little naive because even I miss call some of my own function.

  • Sometimes that's their defense for help for not having like or not having a idiom or paradigm for private variables.

  • Right?

  • You have it feels same idea.

  • Just don't don't do that is generally the advice.

  • So I would love to see a comment.

  • Or so here.

  • This looks like we're just tucking away some default state, which is fine.

  • But it would be nice to just define this in the constructor because of all the functions I mean to care about the constructors, kind of like a main entry point and giving them there.

  • And I think here I can echo Brian's concern earlier.

  • Don't just hard code, some arbitrary string instead treated as some kind of constant value approximation thereof.

  • We have a quite a question from a bell p 2006.

  • Please, please, please.

  • Hello, guys.

  • Nice to see you.

  • Could you help me with this simple question?

  • How can I receive the data in book as a dictionary?

  • Not as a simple array book idea gets input?

  • Um, if you maybe want to write it into a thing or what not, but so this, I presume, is from what 50 if looks like part of a Web programming problem.

  • So if this is using sequel Alchemy, which is the library via which you can execute sequel Curry's on a database Devi Execute probably returned some kind of class that specific sequel.

  • Alchemy.

  • You can probably if you're storing that in a variable called book just cast or convert book to a dictionary, and I wanna making this up just off the top of my head.

  • The stolen Well, I'm just gonna arbitrarily added Thio Sam Arts program Heather, I think you could probably do something like this to convert whatever that structure is to a book that should most experience.

  • Yeah, you shouldn't necessarily need to do that, though.

  • If you're trying to do is access the properties of the book, you can probably do something like book dot title and book dot auther allready thio get the properties of the book.

  • By the looks of that and another comment here about YouTube, I'm glad folks were able to tune in via YouTube.

  • It's also on Facebook, though.

  • You two probably tends to be the most robust internationally and by all means have a couple windows open watches there and comment here.

  • I think they have dynamic encoding to lower resolutions.

  • Twitch does not realize you can comment on YouTube and Facebook.

  • It's just none of us are looking there right now.

  • We don't have a twitch.

  • Comments up S o pull up.

  • Twist that TV Sexy sweetie TV you're watching on Facebook and YouTube and comment in there, someone mentioned typescript.

  • I'm a big fan of typescript, for if you're unfamiliar.

  • Typescript is a typed version of Java script, and it just makes it really easy to write JavaScript code that is free of potential type areas because it is a strongly typed version of drama script.

  • Heard good things.

  • You should take it All right, Well, let's let's move on shortly, I think, to another project, but overall, I think we're seeing a good amount of commenting.

  • There's still some room for improvement.

  • I think we see some pretty clean code, but some inconsistencies with commenting capitalization on with single quotes versus double quotes and then some actual design opportunities for improvement, things like state management and actually representing things andan the main function as well.

  • Yeah, the game state being sort of a string to I think it goes back to our prior Brian's prior point about using an e number, at least your point about using phony numbers using static variables.

  • I think we could have done something like not yet began in all caps, and I would've probably better design ministering to.

  • Nice aside, I'm just kind of thinking ahead here.

  • This is kind of interesting because I'm seeing a double negative, so to speak.

  • If it is not the case that input turns equals equals none.

  • I think what you could do is actually distill that a little more python ycl e as just this, because that will return true if it is anything other than none, so long as we don't need to distinguish between nine and Vera Correct.

  • Assuming that's some kind of off input turns, I don't know what data type that's gonna pay.

  • I don't either so it could just be that it was optional.

  • So if it's anything other than what is the default value?

  • Actually, looks like it's supposed to be a addict.

  • Yes, that should work.

  • Fine.

  • Okay.

  • I'll bring us over to the, um, other screen.

  • You want to say a couple hellos as we get ready for your next story about that, By the way, Uh, 23 please.

  • Look at my code.

  • We'll get to you, Nate.

  • Don't worry.

  • I don't know about today, but we have you on the list.

  • Um, defensive programming is an art to write good python code.

  • Yeah, it's a pain knows his bad ignite and bad ignite.

  • Says thank you, Brian.

  • I will keep that in mind regarding types.

  • Funny you mentioned Babbitt tonight because I think we are next up to tic toe.

  • Which air radio location Bab ignited, you see, is in the chat today.

  • So everyone get ready.

  • Thio, with your feedback.

  • Hopefully very positive as to what we're about to see.

  • All right?

  • First thing I'm noticing.

  • Actually, though I don't think this is a reading thing.

  • This is a directory inside of the CS 50 beyond repositories.

  • So maybe see a 50.

  • Beyond hasn't read me as I can see, It does.

  • Okay, so thanks for the link back to us.

  • We would have liked to see more descriptive information about the project, but that's okay.

  • Let me go back here.

  • So Brian probably knows this best, so I'm a little scared.

  • This is not the guy to ask, I think for feedback.

  • So here we go.

  • So, actually, can I comment on one thing?

  • Don't check in diversion control your pipe cache files folder.

  • So python azan interpreted language typically can do some optimization underneath the hood, and one is to pre compile your code and save the output.

  • It bite code the temporary version of your code for efficiency.

  • This should not end up in version control.

  • And so one way to avoid this if we go back to the repo is in this get ignore file.

  • Uh, it looks like pie cash.

  • Uh, issue is the spelling of get ignored.

  • Oh, wow.

  • I think that would have taken me a little while to figure out.

  • I think.

  • Yes.

  • You must name your get, ignore file, get, ignore and not get it.

  • Ignore it.

  • Ignore.

  • But this is the right line here on.

  • In fact, you don't strictly have to do the star.

  • You can actually just specify tick tack, toe pie, cash.

  • And I think if you only do underscore underscore pie cash underscore.

  • Underscore slash.

  • That'll get well that get pike ashes everywhere.

  • I can show you if you if you don't have the leading slash house popular.

  • You're getting roasted.

  • Kappa.

  • That's okay.

  • So I think we're I think we're friends by now.

  • Actually, I think we are on Facebook, so we're cool.

  • All right.

  • Should we take a look at tic tack toe?

  • Yeah.

  • Look, So do you want to remind folks with tic tack toe is all about I mean, for Can we draw on the screen?

  • We know how to draw.

  • Uh, we might have a nap.

  • Why don't we switch over to here?

  • You know, I could do it right here.

  • Oh, yeah, Yeah.

  • Draw Nazis.

  • Pity that Io.

  • And then actually, you should be able to use this pen, right?

  • Yeah.

  • So why don't we make this a little interactive?

  • Would you play it with tested to make sure?

  • So we have a draw dot CS 50 i Ethical eso here we just draw that CS etc dot io.

  • What you're welcome to use to use it in class is for drawing on touch screens and showy for those unfamiliar, especially outside the U.

  • S.

  • What is tic tac toe?

  • Yeah, tick tack toe is a game whereby we're gonna have a three by three grid that's basically just a nine squares arranged in a three by three graded their two players, a player X and a player.

  • Oh, and the alternate taking turns drawing either an X or an O, whichever one of them in one of the nine squares.

  • And the goal is to get three in a row.

  • You get three in a row in a vertical line, get three in a row in a horizontal line, or you can get three in a row along a diagonal on.

  • So the goal is to try and get three in a row, and the funny thing is with tic tac toe.

  • The game is such that if you have two players playing optimally, let's call it intelligently.

  • No one should ever win this game.

  • So with that in mind, watch me lose now, I think, even though most people start with center.

  • I think the best strategy is not to do that.

  • And you're smiling.

  • Which makes me think you're not gonna tell me.

  • I'm gonna go over here.

  • I'm gonna hope that that was for the best point.

  • A O in the middle of I'll go.

  • Yeah.

  • Here.

  • All right, all right.

  • And now, again, we don't want I don't want to let Brian get three O's in a row, and he doesn't want to get me let get three exes in a row.

  • So now I'm gonna I'm gonna go here, which I'm really liking that's gonna force his hand, Doesn't want to lose.

  • You have to play in the upper left.

  • That's the only way to block.

  • And now I'm gonna go.

  • Yeah, I didn't do this right.

  • I have to go here if I don't want to lose now.

  • Because diagonal is allowed.

  • No, I'm running into the same problem.

  • I have to play in the lower middle.

  • Otherwise he's gonna get three in a row.

  • And now I have to go over here so I don't immediately lose.

  • And it looks like this kid is probably gonna be a tie because no I could choose either one of these places, but I no longer have.

  • Ah, you could pass.

  • I couldn't go ahead and oh, my various.

  • And that way we both played optimally.

  • Kaisa Twitch, saying You started with the worst possible move.

  • I don't think so.

  • Case of wine.

  • Are you supposed to start with the corner, though?

  • I want to start with one of the corners, I think.

  • First move.

  • I always start with the center.

  • I know you can.

  • You can avoid losing if you start with the center.

  • It's the second move where the first person place in the center and the second person place on the edge.

  • Then you can lose.

  • You might not have played often.

  • I might not have played off.

  • It's Paul Ryan.

  • Let me win.

  • I think you should take a sandbag in, Let me in and not lose.

  • Yeah, if you're actually interested in sort of the game.

  • Theory of tic tac toe is an example of what's called an en en que game, where you're playing on an N by N board until someone gets K in a row.

  • In the example of Tekken, Octo mn and they all happen to be three.

  • But if you vary what n n n k or equal to you can get different games that have different strategies.

  • So if this is something of interest or if you'd like to take all the fun out first.

  • All right.

  • So that's what Tic Tac toe is.

  • Now let's go ahead and take a look at bat McKnight's implementation thereof and maybe even some of this cash by code here.

  • All right, So where do you want?

  • What you want to start?

  • All right.

  • So I happen to know that tic tac toe is a flask based games with 50 beyond.

  • But even if I didn't, there's a file called application dot pie, which seems like a reasonable place to start, not a requirement.

  • That flask is used in this filing.

  • Like their documentation is just the default you might see server dot pie or AP top I.

  • Other possible.

  • These exist too.

  • All right.

  • Got a single function or a comment block up top.

  • Yeah, and I'm seeing this shebang up here.

  • This hash bang, which allows us to run this code through python, and if I scroll down to the bottom?

  • Yeah, this came up before.

  • This is actually the older way of doing this with flask, and I think it's not recommended anymore.

  • So first and foremost, I would give it to the first line.

  • The shebang get rid of this line because all of your application dot pie should be run as a command line argument.

  • These days toe a program called Flask.

  • This will still work, but there is some reason that I think is documented as to why they now recommend the other approach.

  • All right, let's go back up to the top here.

  • How would we go about wrapping our minds around this file?

  • All right, well, let's start by looking at some of the things that were imported.

  • We're importing a whole bunch of things from flask itself and things to help this Web application run online.

  • 14 were importing from flask session.

  • It looks like we're gonna be using sessions in this application, probably so that we can maintain the state of the game board inside of the application session.

  • And the session technically needs to be stored somewhere.

  • And so here we're importing a function called Make D temper.

  • Make a temporary directory, which is where we're gonna store that session information, and then there's some configuration.

  • And this is actually problematic in some context because if you run flask debugging mode and therefore it restarts after every request, I believe you will get a different temporary directory.

  • We've run into this problem before, remember before.

  • So I think this is actually fine in production code where the servers running for a long time.

  • But if you're using flasks debugging mode where it's supposed to restart itself after every request just to give you a clean slate, I believe you will end up seeing your session files end up indifference folders on the file system, and therefore you might be losing some of your state.

  • So just f.

  • Y I you can probably omit this, and what we have started to do for class purposes is let flask use the default, which is core called flask underscore session.

  • Then add that to your get ignore file to make sure you don't check it in unnecessarily as well.

  • All right, so here we have a route in flask.

  • If unfamiliar, this allows you to specify hey flask, call the following function anytime the user visits this route and slashes the default route presumably.

  • And here we see an index function.

  • That's for some reason takes an argument.

  • Can you help me figure this out?

  • Yeah, I'm a little bit uncertain about why that's there.

  • I mean, it probably could be a very bold by wonder if anywhere index is called.

  • That's a different that's a different in Vegas.

  • Yeah, I'm not sure we're used.

  • Not sure, either.

  • So unless there's some way, just not in this file to get to index with a different value of them, which is it possible there could be a u R L four.

  • Let's see Your girl for index doesn't change, but you could pass in a different value for end.

  • You could, although generally when you do t

All right.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

より多くのソースコードを!- CS50 on Twitch, EP.37 (MORE OF YOUR SOURCE CODE! - CS50 on Twitch, EP. 37)

  • 0 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語