WEBVTT

1
00:00:00.290 --> 00:00:01.680
<v ->Hi guys and welcome back.</v>

2
00:00:01.680 --> 00:00:03.350
In this video, we're going to do something similar

3
00:00:03.350 --> 00:00:05.000
to what we did in the last one,

4
00:00:05.000 --> 00:00:07.890
but for cursors in our database.py.

5
00:00:07.890 --> 00:00:09.780
Because we've got a bit of duplication here.

6
00:00:09.780 --> 00:00:11.183
So let's get to it.

7
00:00:12.360 --> 00:00:15.870
Every function here has these two contextmanagers.

8
00:00:15.870 --> 00:00:19.100
We could make it into one contextmanager if we want.

9
00:00:19.100 --> 00:00:20.380
So how are we gonna do that?

10
00:00:20.380 --> 00:00:22.820
Is first of all, by creating the contextmanager

11
00:00:22.820 --> 00:00:25.470
that I'm gonna call, get cursor.

12
00:00:25.470 --> 00:00:27.700
And this one is gonna take in a connection.

13
00:00:27.700 --> 00:00:28.533
And what it's gonna do,

14
00:00:28.533 --> 00:00:30.910
is gonna do with connection

15
00:00:30.910 --> 00:00:34.420
and then with connection.cursor as cursor,

16
00:00:34.420 --> 00:00:36.780
is gonna yield the cursor.

17
00:00:36.780 --> 00:00:40.530
This is gonna get decorated with the @contextmanager

18
00:00:40.530 --> 00:00:42.380
as well, and we have to import it.

19
00:00:42.380 --> 00:00:43.360
So we're doing that.

20
00:00:43.360 --> 00:00:46.050
Notice that all that does is from contextlib

21
00:00:46.050 --> 00:00:48.920
input contextmanager at the top.

22
00:00:48.920 --> 00:00:51.720
Now, what we can do is instead of doing all of this,

23
00:00:51.720 --> 00:00:56.720
we can do with get cursor of connection as cursor,

24
00:00:57.360 --> 00:00:59.413
and that does that.

25
00:01:00.770 --> 00:01:02.860
Totally up to you whether you wanna do this or not,

26
00:01:02.860 --> 00:01:04.830
we don't need a try finally here

27
00:01:04.830 --> 00:01:06.320
because these contextmanagers

28
00:01:06.320 --> 00:01:08.260
already clean up after themselves.

29
00:01:08.260 --> 00:01:10.180
So just yielding the cursor means

30
00:01:10.180 --> 00:01:12.100
that we're gonna get to here with the cursor,

31
00:01:12.100 --> 00:01:14.200
return it here so we can run this

32
00:01:14.200 --> 00:01:15.940
and then it's gonna go back up here

33
00:01:15.940 --> 00:01:18.660
and close these contextmanagers as well.

34
00:01:18.660 --> 00:01:20.810
So if you wanna do this, for simplification,

35
00:01:20.810 --> 00:01:22.760
then by all means, go for it.

36
00:01:22.760 --> 00:01:25.943
I'm gonna apply this to all my other functions as well.

37
00:01:37.620 --> 00:01:40.300
So I've gone ahead and applied get cursor

38
00:01:40.300 --> 00:01:42.080
to all of these functions.

39
00:01:42.080 --> 00:01:43.770
One of the great things about this

40
00:01:43.770 --> 00:01:45.700
is you don't necessarily

41
00:01:45.700 --> 00:01:49.160
have to put get cursor here in database.py.

42
00:01:49.160 --> 00:01:53.980
You could put get cursor into the connection pool file here,

43
00:01:53.980 --> 00:01:55.430
instead of get connection,

44
00:01:55.430 --> 00:01:58.840
then all your functions here or your methods

45
00:01:58.840 --> 00:02:03.330
could use a cursor directly passing it into database.py.

46
00:02:03.330 --> 00:02:04.163
It's up to you

47
00:02:04.163 --> 00:02:06.610
whether you wanna be passing cursors or connections.

48
00:02:06.610 --> 00:02:08.520
If you only have one connection

49
00:02:08.520 --> 00:02:10.500
that you create at the start of the application,

50
00:02:10.500 --> 00:02:14.370
you could then simply use cursors every time,

51
00:02:14.370 --> 00:02:17.080
and in a single connection application

52
00:02:17.080 --> 00:02:18.750
that doesn't use connection pooling.

53
00:02:18.750 --> 00:02:21.020
That is probably going to be faster.

54
00:02:21.020 --> 00:02:23.530
So it's up to you whether you wanna do that,

55
00:02:23.530 --> 00:02:26.840
but here we're worrying more about future scalability

56
00:02:26.840 --> 00:02:28.880
by using connection pooling.

57
00:02:28.880 --> 00:02:31.450
If you wanted to use a single connection

58
00:02:31.450 --> 00:02:33.360
and not worry about multiple connections,

59
00:02:33.360 --> 00:02:36.240
then you could put this inside connection pool

60
00:02:36.240 --> 00:02:39.410
or rather maybe cursors or something like that,

61
00:02:39.410 --> 00:02:40.900
this file could be called,

62
00:02:40.900 --> 00:02:43.800
and be passing cursors around instead of connections.

63
00:02:43.800 --> 00:02:45.740
But the aim of these two changes

64
00:02:45.740 --> 00:02:48.690
were to teach you about simplifying your code

65
00:02:48.690 --> 00:02:51.500
using these contextmanagers and how you can do that.

66
00:02:51.500 --> 00:02:53.080
So hopefully this has been useful.

67
00:02:53.080 --> 00:02:54.190
I hope you've learned something.

68
00:02:54.190 --> 00:02:57.090
Thank you for watching and I'll see you in the next video.

