1
00:00:00,330 --> 00:00:05,330
Now I've mentioned a lot about using code that other developers have written and

2
00:00:05,939 --> 00:00:09,960
the most common way to do this is through using packages of code.

3
00:00:10,710 --> 00:00:11,543
In this lesson,

4
00:00:11,820 --> 00:00:16,200
I'll show you how to integrate an existing package of code into our project so

5
00:00:16,200 --> 00:00:20,820
that we can practice creating objects and working with attributes and methods.

6
00:00:21,480 --> 00:00:26,480
Now we've seen modules of code where each file that we create in our project is

7
00:00:27,870 --> 00:00:29,850
essentially a module in itself.

8
00:00:30,540 --> 00:00:35,540
But a package differs from a module in the sense that it's actually a whole

9
00:00:36,120 --> 00:00:41,120
bunch of code that other people have written, lots and lots of files all packaged

10
00:00:41,610 --> 00:00:46,470
together to achieve some sort of goal or purpose. Now,

11
00:00:46,470 --> 00:00:51,450
let's say that we wanted to create a table of Pokemon and their type.

12
00:00:51,780 --> 00:00:56,780
So we wanted some way of documenting that Pikachu is an electric type and maybe

13
00:00:58,590 --> 00:01:01,950
Squirtle is a water type. Now,

14
00:01:01,980 --> 00:01:06,930
if we wanted to do that in ASCII so that we could print it in the console, well

15
00:01:06,930 --> 00:01:10,410
then we would have to go about formatting it and it'll be quite painful.

16
00:01:10,800 --> 00:01:15,800
So we might have to print some sort of pipe and then maybe we would have the

17
00:01:16,650 --> 00:01:20,580
name of the field, so Pokemon name,

18
00:01:21,180 --> 00:01:25,410
and then maybe another pipe. And then we would have the name of the next column,

19
00:01:25,740 --> 00:01:27,000
which is their type.

20
00:01:27,720 --> 00:01:32,010
And then finally we would close that off and then we would have to print some

21
00:01:32,010 --> 00:01:36,000
sort of a horizontal line

22
00:01:36,120 --> 00:01:40,890
maybe like this and so on and so forth just to create this table.

23
00:01:41,100 --> 00:01:45,660
And it's quite difficult if you're not used to manipulating ASCII and

24
00:01:45,660 --> 00:01:49,080
visualizing it. I certainly am not up to this task.

25
00:01:49,770 --> 00:01:52,500
So what could we do instead? Well,

26
00:01:52,500 --> 00:01:57,500
we could search for a package that other developers have created to achieve this

27
00:01:58,200 --> 00:02:02,880
goal. And the place that we would do that is somewhere called PyPi,

28
00:02:03,330 --> 00:02:06,030
which is the Python package index.

29
00:02:06,480 --> 00:02:11,480
And this is a bunch of software for the Python programming language that is

30
00:02:11,640 --> 00:02:16,080
developed and shared by the Python community. So we can see their source code,

31
00:02:16,350 --> 00:02:17,460
but more importantly,

32
00:02:17,490 --> 00:02:22,290
we can see how to implement the code and how to put it into our project.

33
00:02:23,190 --> 00:02:26,670
In here, I'm going to search for something called prettytable.

34
00:02:27,600 --> 00:02:32,600
And this is a very simple library that is going to help us to display tables in

35
00:02:34,410 --> 00:02:36,000
a ASCII format.

36
00:02:36,780 --> 00:02:41,130
We can click on the project links to find out more about it.

37
00:02:41,820 --> 00:02:45,150
And this will take us to the Google code archive,

38
00:02:45,420 --> 00:02:47,370
where the documentation is hosted.

39
00:02:47,940 --> 00:02:50,460
If you go to Wikis and go to tutorial,

40
00:02:50,580 --> 00:02:53,700
you'll see some documentation on how to use it.

41
00:02:54,210 --> 00:02:59,210
Now, this particular package is not as extensive as the turtle graphics package.

42
00:03:00,760 --> 00:03:03,670
So there's actually not a lot of things you can do with it,

43
00:03:04,090 --> 00:03:06,850
but its got more than we need for our use case.

44
00:03:07,840 --> 00:03:12,640
The first thing we have to do is to install this package into our project.

45
00:03:13,120 --> 00:03:15,220
Now, unlike the turtle package

46
00:03:15,220 --> 00:03:19,780
which is already preloaded with every copy of Python, in order to use the

47
00:03:19,780 --> 00:03:23,110
packages that you find in the Python package index,

48
00:03:23,620 --> 00:03:25,660
you actually have to install it.

49
00:03:25,750 --> 00:03:30,550
So here's how we do it in PyCharm. First, go to the preferences,

50
00:03:30,730 --> 00:03:35,590
so in Windows, I think it's under file and then preferences. On Mac is under 

51
00:03:35,590 --> 00:03:37,030
PyCharm and then preferences.

52
00:03:37,600 --> 00:03:40,540
And then here you'll see a whole bunch of things you can change

53
00:03:40,570 --> 00:03:44,950
including the editor or appearance which we've already messed around with.

54
00:03:45,400 --> 00:03:47,770
But I want you to click on your project.

55
00:03:47,950 --> 00:03:52,660
So my project is called day-16-start and then we're going to go into the project

56
00:03:52,660 --> 00:03:57,660
interpreter. And here we can click on the plus button to install any package that

57
00:04:00,310 --> 00:04:04,930
you find in the Python package index. Make a note of what it's called.

58
00:04:05,020 --> 00:04:09,670
So ours is called PrettyTable and then we're going to search for it inside the

59
00:04:09,670 --> 00:04:12,310
available packages. And once we found it,

60
00:04:12,340 --> 00:04:14,650
we're going to select it and then click install.

61
00:04:15,610 --> 00:04:16,990
If you are working with Repl.it,

62
00:04:17,019 --> 00:04:21,010
you can also install packages just by going to the packages tab

63
00:04:21,310 --> 00:04:26,050
and then again, searching for PrettyTable. Once you click on the package 

64
00:04:26,050 --> 00:04:29,920
that you want, click on the plus button to add it to your project.

65
00:04:30,640 --> 00:04:34,120
Now Repl.it is a little bit weird because it says installing

66
00:04:34,120 --> 00:04:38,320
but it never actually confirms the success. But once you click run,

67
00:04:38,440 --> 00:04:41,170
everything will go through and it will work perfectly

68
00:04:41,440 --> 00:04:45,130
as long as you see it in your list of packages here. Now,

69
00:04:45,130 --> 00:04:48,640
if you want to remove the package, you can simply click on the minus button,

70
00:04:49,000 --> 00:04:50,650
but now we're going to work with it.

71
00:04:52,450 --> 00:04:54,790
So now it's installed successfully,

72
00:04:55,030 --> 00:04:57,790
we can go ahead and close this down and click okay.

73
00:04:58,360 --> 00:05:02,650
And we can now access this package called PrettyTable in our code.

74
00:05:03,220 --> 00:05:03,640
Again,

75
00:05:03,640 --> 00:05:08,440
I'm going to import it so I can say import prettytable.

76
00:05:08,560 --> 00:05:13,120
And now I can use everything that's inside this package. Now,

77
00:05:13,150 --> 00:05:14,650
if you want to see the source code,

78
00:05:14,830 --> 00:05:19,720
you can actually right-click on it and select, go to, implementation.

79
00:05:19,780 --> 00:05:24,250
And this will take you to the Python file where this entire prettytable is

80
00:05:24,250 --> 00:05:27,250
implemented, so all of the code that they've written.

81
00:05:28,120 --> 00:05:31,330
It's a little bit daunting and it's got a lot of code in it,

82
00:05:31,630 --> 00:05:35,620
but we don't have to worry about how the code is created and how it works.

83
00:05:36,040 --> 00:05:41,040
All we need to do is look at the documentation for this package and see how we

84
00:05:41,290 --> 00:05:45,190
can implement it into our project. Now in the next lesson,

85
00:05:45,460 --> 00:05:46,540
we're going to get some practice

86
00:05:46,540 --> 00:05:51,540
constructing a prettytable object and using the documentation to explore this

87
00:05:51,700 --> 00:05:55,630
object's attributes and methods. For all of that and more, I'll see you there.

