knowgugl.blogg.se

Mokey tutorials
Mokey tutorials





mokey tutorials
  1. #Mokey tutorials Patch
  2. #Mokey tutorials free

# retain a pointer to the actual real method: Just doing the above will alter the Structure object for the life of the process, so you'll want to use setups and teardowns in your unittests to avoid doing that, e.g.: def setUp(self):

#Mokey tutorials Patch

'''monkey patch datasource.Structure with this to simulate error'''Īnd when we test it for behavior that relies on this method raising an error, if correctly implemented, we'll get that behavior in the test results. (So using a similar method name as suggested by Daniel Roseman:) import datasource We can monkey patch the data structure to ensure this behavior. Say we need to simulate a data retrieval call to an outside data source that results in an error, because we want to ensure correct behavior in such a case. How can we use this knowledge, for example, in testing?

mokey tutorials

Since I don't recommend name-mangling, I will not demonstrate it here. If you're using name-mangling (prefixing attributes with a double-underscore, which alters the name, and which I don't recommend) you'll have to name-mangle manually if you do this. Next we simply attach that method to the class we want to use it on: pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame classĪnd then we can use the method on an instance of the class, and delete the method when we're done: df = pd.DataFrame(, columns=)

#Mokey tutorials free

Next we create a method definition, which exists unbound and free outside the scope of any class definitions (since the distinction is fairly meaningless between a function and an unbound method, Python 3 does away with the unbound method): def just_foo_cols(self): To break this down, first we import our module: import pandas as pd Pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame classĭf = pd.DataFrame(, columns=)ĭel pd.DataFrame.just_foo_cols # you can also remove the new method """Get a list of column names containing the string 'foo' There's an example of monkey-patching in the Pandas documentation: import pandas as pd

mokey tutorials

Simply put, monkey patching is making changes to a module or class while the program is running. (Why? Python just rebinds the name get_data in your class to some other function object other name bindings are not impacted at all.) If some variable or attribute exists that also points to the get_data function by the time you replace it, this alias will not change its meaning and will continue to point to the original get_data. If anything else besides your test logic calls get_data as well, it will also call your monkey-patched replacement rather than the original - which can be good or bad. However, in a unit test, you don't want to depend on the external data source - so you dynamically replace the get_data method with a stub that returns some fixed data.īecause Python classes are mutable, and methods are just attributes of the class, you can do this as much as you like - and, in fact, you can even replace classes and functions in a module in exactly the same way.īut, as a commenter pointed out, use caution when monkeypatching: This method does an external lookup (on a database or web API, for example), and various other methods in the class call it. It's simply the dynamic replacement of attributes at runtime.įor instance, consider a class that has a method get_data.







Mokey tutorials