Python Monkey Patch Static Method

MakeCode programs can be authored in Blocks, Static TypeScript or Static Python. 1つのファイル内の別のメソッドによって呼び出されるメソッドにパッチを適用することに興味があります。例 - original.pyファイルの内容 - def A():a = 10 b = 5 a. b; def B():c = A()return c. 10単位を書きたい.

Here is a classic editable list in OpenERP v6.0 :

It’s a custom view I created this month at work for one of our customer to let him select a list of products, then batch-print their labels on stickers.

The view above is produced by the following XML :

Python monkey patch static method example

If you start searching a product template with the first field, you’ll get a pop-up similar to this one:

As you can see, these kind of pop-up inherits the width of their parent field, which hide the end of all lines if they are too long. It becomes difficult to distinguish the items when all found objects have the same long prefix.

Now I want to get rid of this behavior and let the pop-up menu take all the necessary width it needs to fully display its content.

My instinct told me that this default style could easily be overridden with some static CSS directives. But digging deeper into OpenERP web client code , I realized that the width is dynamically set by the many2one widget itself.

The code responsible for this behavior is located in the addons/openerp/static/javascript/m2o.js file, in the ManyToOne.prototype.on_keydown method:

Python Static Method

My goal is now to alter this default behavior, without touching the code in m2o.js .

And thanks to Bryan Forbes’ article , I engineered a method to monkey patch the original ManyToOne.prototype.on_keydown Javascript method.

Here is the code I added in the XML view, just below the line_ids field:

The result of this is a nice looking pop-up which doesn’t break any vanilla Javascript of the OpenERP web client:

Related content

Please enable JavaScript to view the comments powered by Disqus .

From this tutorial, you will get to learn about Python Static method. It is one of the essential concepts to use while you are learning OOPs programming with Python.

Note: The syntax used in the below section is for Python 3. You may change it to use with a different version of Python.

Python Static Method

Python

Contents

  • 3 Program Examples

To Learn about OOPs Concepts – Read Python Class

Python Monkey Patch Static Method

What is a static method in Python?

It is available as a built-in function in Python and allows you to turn a regular method into the static.

In other words, you can create a callable class using the static method and use it with some restrictions.

It helps developers write code in a safe architectural way to prevent conflicts in the code.

How does the Python Static method work?

Tactical Trunk Monkey Patch

When you create a class, you usually define a few methods and properties. They may or may not have access to the class instance.

There three types of methods depending upon their access are Instance Method, Class Method, and Static Method. This tutorial will cover the application of Static Methods.

It is a method which does not have access to the class state. In other words, the method confines to itself and cannot change the properties of the class instance without any workaround.

Python Static Method Call Static Method

You can make use of “staticmethod” in your programs with the following syntax:

Alternatively, you can follow the below syntax:

Program Examples

Function returning a value as static:

Here is a simple program to demonstrate static methods.

Save the above code as “staticmethod.py” and execute. The output will come as:

A method as static:

Check another program to use the built-in staticmethod() function.

Save the above code as “builtinstaticmethod.py” and execute. The output will come as:

Python Method May Be Static

Best,

Python Static Method Call

TechBeamers