-Поиск по дневнику

Поиск сообщений в rss_planet_mozilla

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 19.06.2007
Записей:
Комментариев:
Написано: 7


Will Kahn-Greene: Anatomy of a class/function decorator and context manager

Понедельник, 16 Мая 2016 г. 19:00 + в цитатник

Over the weekend, I wanted to implement something that acted as both a class and function decorator, but could also be used as a context manager. I needed this flexibility for overriding configuration values making it easier to write tests. I wanted to use it in the following ways:

  1. as a function decorator:

    @config_override(DEBUG='False')
    def test_something():
        ...
    
  2. as a class decorator that would decorate all methods that start with test_:

    @config_override(DEBUG='False')
    class TestSomething:
        def test_something(self):
            ...
    
  3. as a context manager that allowed for multiple layer of overriding:

    def test_something():
        with config_override(DEBUG='False'):
            with config_override(SOMETHING_ELSE='ou812'):
                ...
    

This kind of need comes up periodically, but infrequently enough that I forget how I wrote it the last time around.

This post walks through how I structured it.

Read more… (3 mins to read)

http://bluesock.org/%7Ewillkg/blog/dev/python/anatomy-class-fun-deco.html


 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку