Cool. Hello, I am Safond Rahman. I am actually
have come from Bangladesh. My topic is actually very interesting one and it is not very
like the thing that people try to do and everything. So, it is about like monkey
patching. So, monkey patching is something that you change the code at the runtime. It is
very bad practice, but at least sometimes you need to do that. So, use that in your own
risk. So, the thing is that do not try this at home. Sorry, do not try this at production.
You can try this at home, but you should not try this at production. So, have you ever used
that in your code rather than test? Anyone who is that? Oh, okay, a lot of people who
are with that, okay. Have you pushed monkey patch to do the reproduction? Okay, not
wrong, not Raj. Okay, so, okay, sure.
So, the thing is that, my topic is actually how we can actually use the monkey patching,
the function, this powerful functionality more beneficial for our debugging our code and everything.
Okay, because maybe you should not push this code to the production, but you can use that
this code in your local machine to actually test things and also like debug your packages
the thing. So, while there are bug in a package that you are using is quite difficult to actually
fix that or like debug that in locally and then you can actually get a fix, right. So,
this is a short example as we will all know that how to actually do the monkey patch.
So, in the class, in the class dog there is a method in a bug, but if you actually see
is that after we actually, we are getting the animal object in there and we are actually
making the bug, we are calling the bug function is actually, is actually showing the roof
in the output, okay. But after we actually monkey patch the bug function in this new bug
with this new bug is actually returning the meow, okay. So, this is how we can actually
monkey patch a class directly in python code, okay. This one is actually important one
because like you can make you can monkey patch a class very easily, but it is quite difficult,
what not quite difficult is quite tricky to actually patch a instance or like a object in python,
okay. So, like if you want to, if you need some parameter or anything from your
base class in your function in your class method, what you need to do is that you need to
use the method type, okay. So, if you need to use the method type in that case that as
you see that I actually have patched the gate name function in here with this function
and after we actually use the method type, we could actually, we could actually
access the self-value from the, from the monkey patch function as well, okay.
So, the output will be, the first output will be type, okay, because this is the type,
but in the after monkey patching it will be my name is type, okay, because the monkey patch
function is actually getting the value from the, from the after main class main object,
okay. So, this is the main part, okay. So, how we can actually use the monkey patch to
which we debug, okay, you know, look at me, okay, because maybe we should not push this
code in the production, but we can actually use the monkey patch function to debug our third
package, that we are actually using and maybe that third-party package has bug in there,
okay. So, think about a like a third-party package, it is about like the kelp module,
okay, it has a like, it has a function, this name is kelp, kelp, let's sum, okay.
But it seems like the kelp or sum actually has a bug in there, because other than
calculating the sum, it is actually abstracting that, okay. So, if you actually face this kind
of issue, what would actually you do, okay, in this third-party package, either you can
fork that, you can install that package, vendorize that, anything, but it is quite difficult
to do that. So, in order to debug that, what you can actually do is that you can, you can
import this function as the wrong kelp has a different name, and then you can actually
write another function, okay, like a pairs kelp or sum. So, in the pairs kelp or sum, you
can actually add a break point in there. So, you can understand that what's parameter you
actually getting on in this function, and then you can return the wrong calculation sum
method in there, okay. So, after that, we are actually monkey-watching in there, like we
are term monkey-watching the kelp module, kelp module dot kelp sum, kelp for we actually
monkey-watching the kelp sum in there with our pairs, pairs function in there. So, when
when we are actually importing, this is the very important thing, you should always
monkey-watch before you actually import the actual method, okay. So, if you actually
patch after importing the method or function, you cannot actually monkey-watch that,
write this way, okay. So, if you actually import the kelp module, kelp sum from the kelp
module, and you actually call that function, but you will, you will see that you will
actually get a PDB, okay, you will actually get a debug interface in there. So, in that
debug interface, you can get all the variable and everything, and so that you can actually
debug it more precisely that what is actually happening in there.
This is the second example. So, actually debug that, okay, there the third
party library has a bug. So, how can you fix that in your local machine? So, you can actually
test that, this is the main course, okay. So, what you can do is that, you can, with
the same way, you can just other than adding the debug, like a debugger, you can
actually kind of fix the method in there, and you can actually paste, you can actually
paste that, okay. So, after patching that, if you actually import that, import the function
that module, you will actually get the actual result. So, at the first, I am actually
getting the 70, because the function has a bug, but after we actually paste that and fix
that, I can actually, I am actually getting 130, because this should be 130, okay.
So, this is the interesting one, another one, interesting one is that, sometimes you need to
know that while debugging that, from where this third party function is being called,
okay. So, you have a like, thousands of sounds of flying in a scope, you have like
a lot of packages, like 100 of packages, you need to know from which packages and from
where this function is being called, okay. So, what you can do is that, you can actually
use the Python-in-spec module, okay, in there, this building module in there. So, if you
actually paste that, paste the function, like previously, and you can actually, if you
actually use that, instant module and get the stack, you can understand that, which function
is actually calling that, okay, and from which line is this actually calling, okay.
So, in that way, you will actually get it very easily, that from where this function is
being called, so you can both there and fix that, okay, rather than finding air and
there and everywhere, okay. So, this is the thing that is about the debugging, okay.
So, after debugging, we actually have like a hot fix, okay. So, the thing is that, as I actually
say earlier, that, you should never do this in production, okay, unless you do, you know
what you are doing, okay. So, so, but as you are engineer, we need to handle the production
issue, hot fix, there is the name everything, like there are some downtime in there, and
you need to fix something, you need to make something very emergency in this case, okay.
So, there is the fastest way to do that is actual monkey patching, okay. If actually,
using third-party package, there is some bug, and you want to fix that, and you want
to fix that and make a like a hot fix in there. So, it is very, it is a best way to do
is actual monkey patching. In the later, after the service went, goes up, you can actually,
you can actually change the monkey patching to the actual fix in the library and make a
pull request or use your own version in there, and maybe find some other solution in there.
But, for emergency things, maybe you can actually push some monkey patch code in the production,
okay. So, let me show you some pod example in there.
Can you see this? Yeah, is it okay? Okay. So, this is the one example about the hot
fix, okay. So, this is one example of the hot fix, okay. So, thinking about the situation
like you actually have, I have upgraded the package, okay. So, you have added the package in
your, in the old package, you actually, there are other, you were calling a function, okay.
So, in the function, the one of the parameter was actually not required, okay. So, it was
only divided default none or something like that, okay. But, but in the, in the new module, in the
upgraded one, the function, the AG is actually required, like the variable is actually required
in there, okay. So, what you can actually do is that actually already have upgraded the package,
which the code in the production and the service went down because this is actually asking
about the required argument in there, okay. So, let us see the example.
Let us see, let us try hot fix, okay. If we run the hot fix that one pi, it will actually
give this kind of error, okay. And this kind of error is actually very common in production
system and everything, okay. In the, if you have to upgrade a package and you actually required
a required argument, this kind of error actually very popular in there, okay. So, what you can actually
do is that you can actually monkey pass it out and fix that as well, okay. So, you can actually,
you can actually write a function, okay. In the, in the function, you actually actually
import the module and after you actually import the module, you need to get the old function
old function in the variable, okay. If the every function and everything is object in Python,
you can actually, you keep that in a function in there, okay. Then you can actually write
your own function that actually takes only two argument, the first name and last name, okay.
And then you can pass the AG as none to this old function, okay. And you can actually monkey pass
this new module with gate person, with your own gate person function. So, it will only
take two argument in there, two keyword argument, not three. So, and you need to monkey
pass it before importing, okay. So, after you actually import the function and make the object
in there, this will actually work. You see, this one actually worked, but the previous
one didn't work. The same module and everything, but only as we are actually doing the monkey
patching thing in here, this one actually worked. And this is the simple fix. We, you will need
five minutes, particularly fix that, rather than like downloading the package, fixing that,
making a full request or maybe, downloading the detail of everything, okay.
So, this is another example in here, okay. So, this is another example. So, after you actually
upgrade the package, sometimes, sometimes, there are like, sometimes, there are like new,
there are, there are new fix ways, that's in, that's in, that's in, that's in the package,
that's in the low level package, but not in the high level package in here. You have not
upgraded the high level package, you have upgraded the low level package in here, okay.
So, in that case, what we're actually going to do, because you need the low level package
for other things, but you also need, you cannot actually upgrade the high level package in
there, okay. So, you can actually, monkey pass this low level package, so that the high
the high level package actually calls that, this will actually, this will not result in
error. So, if you see this, this low level package. So, in the person, we have a function
name get upper full name and get upper full name actually calls the self first name
upper and self last name upper. But, in our code, maybe there are some H cases and maybe
in the database, we have null values that, because of the, because of the reason, maybe the
null value is actually coming for first name and last name. So, this may actually give
some error in there, you see. So, this is the very common
error in the production that, non type object has no attribute upper or something like that. So,
because you are actually passing the non type, it cannot actually make it upper and give
you the actual level. So, what you can actually do is that, you can actually also
monkey page this, you can also monkey page this, you can actually, this get function like get
person function, in the get person function, you can actually see that, if it first name is
non, you can actually return a like blank value, if the last name is non, you can also
return a blank value rather than non. So, in that case, this will also also fix your production
issues, you see. So, we are actually passing only the first name, as we are passing only
the first name, it will actually only work. So, the best way to do this kind of fixes is
actually, it is not a best way, but in the monkey page, this is the best way. So, what you
can actually do is that, you can actually create a new class in it in this main class. So,
in the main class, in the inherited class, you can actually fix this issue, this is the first
name is non, you can actually make it blank, if the last name is non, you can actually make
it blank and then you can actually do that. And you can actually just in the in the
one module, from where this class is imported, you need to patch at that module, not where
the class is actually belong to, which is the file. So, you need to patch, where from
where this person, this class is being imported. So, you can actually in the old module,
you can just patch this person in here and it will actually, it will actually be the new
person in here and it will actually fix the issue. You see, it is fixed, even though
even though the main module, the old module has some issues, old module have the person
with this issue. And I am actually going to end with the one thing, that is very nasty
one. So, you should never do that at first first, but I am just showing it for research
purpose. So, this is the thing. So, you do not want your application to have any
call to Google.com. So, not your code, not third part code, not any code. So, what you
can do is that, you can actually patch the request library, you can patch the request library
to get to patch the gate. You see, if this patch is the gate and if the URL is Google.com,
you can just like raise the connection row, that it will not work. And this can be used in
a very different way and very unactical way and very ethical way as well. And you can
to route this, your traffic from one domain to another and anything. Sometimes, maybe you
need to route from one domain to another domain for the whole project. So, you do not want
to change everywhere, you just change in the in your based Python call and it will actually
work everywhere. So, maybe you can actually patch the URL leap. So, even though who
are not using the request library, can actually also benefit that. So, let us think, you see.
So, the request is actually like failing. So, its overall project is actually like, you cannot
actually make a good fall to Google in there. So, you actually make it as that. So, let us all.
Thank you. Do you have any question?
Well, thank you very nice talk. So, quick, quick fixes the get pushed into production
often stay in production forever. What would you say? How is your experience with like,
linting like an import and the middle of your file? Can you put it? Oh, yes, is that better?
With a monkey patch that gets into production is probably going to cause linting warnings.
Like, how do you handle your linting warnings in projects? Are you going to suppress just like
what that one line? This temporary fix is here forever. Shush or yeah. So, the thing is that
you can actually see that in a very different way. You can actually see that in the main code
base and maybe you can actually create another class in there. But sometimes you have no access to
that specific package. Like, you are using one package, that one is calling another package and
that one is calling another package. So, in that case, this one is the like super fasted way.
We just fix that, there is specific package that is going to happen in the bag and all the chain
is actually fixed. Do you worry about the tech? Yeah, it is a hot fix. You always need to make
a good fix after this hot fix after the system gets up. You always need to do a good fix in there.
You should not keep that in your code based on time. Okay, yeah, thank you very much.
Another question? Can you hear me? Oh, yes. So, you've talked about monkey patching, pushing
that code. Sorry, just try and get out of the way. So, you've talked about writing monkey
patches that you then push to your repository and then update your container, rebuild stuff.
Have you have any experience with literally live patching the code in running processes?
Like, in production. So, in that case, if you actually pay as the code directly into the
production, in the code based, if you're actually using dog car, if you actually get a new
build, if you actually just not exist there. Okay, so. So, so, it is possible. I'm here to plug
like a tool called pyrocyte, like pyrocyte that's about pyrocyte, so you can get like the live
python shell into your thing. It's the last thing you should do, but it is really, really helpful
if your thread is blocked and it's not getting any debug, say, that kind of thing. You can
literally change anything in the process. I'll do it. Cool.
Any other question?
Hi, yeah, it's not exactly a question. I would say, before trying all these things,
there are other options, for example, you gave the example of placing python as a built-in
library called trace inside the standard library, that helps with straightest tracing. Similarly,
for monkey patching, I would also say, if you really have to do it, then use the unit test
marked patch, you know, that's doing it yourself. So, in the unit test marked patch,
is actually based mostly on unit test use case, okay, and this is for the like basic use case in
there. Okay, and yeah, there are a lot of ways to do that, and for the python tracing,
but you cannot actually add a debugger to a specific package in the in the local machine or maybe
in the production. Okay, so it's quite difficult to actually add a specific debugger to actually
the third-party package in this specific line in there. So, you can actually debug that what
value is actually getting, and you can actually fix that in there, okay, maybe the thing is actually
not reproducible in your local machine, but it's actually happening in the production. Okay,
so this thing is actually gets complicated in there.
Another one? Nope. Okay, thank you so much. Thank you everyone.
Next speaker will be