开发者

How to extend/patch an existing module or package?

开发者 https://www.devze.com 2022-12-25 11:11 出处:网络
I want to extend some locale-specific features of a python application named OpenERP. All I need is implementing a third party module.function that would be called every tim开发者_JAVA百科e OpenERP ca

I want to extend some locale-specific features of a python application named OpenERP. All I need is implementing a third party module.function that would be called every tim开发者_JAVA百科e OpenERP calls locale.setlocale() function without changing neither OpenERP nor locale module source code.

The only way I can imagine is provide a module named locale.py inside main application package dir, but It seems that is an unpythonic workaround.


Look up Monkey Patching. It's not most elegant technique, but sometimes it's the only option.

In your case you can substitute your own function for locale.setlocale() which will do whatever you want. It would look something like that:

import locale

original_setlocale = locale.setlocale

def my_setlocale(category, locale=None):
    # Do anything you want
    # optionally call original function
    original_setlocale(category, locale)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号