This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {# load custom tags #} | |
| {% load custom_tags %} | |
| ... | |
| {# use our custom tags #} | |
| <p>{{ order.total_cost|money_display }}</p> | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.dispatch import receiver, Signal | |
| from tasks import notify_advisor | |
| from order.models import Order | |
| # create custom signal by property field | |
| # instance field is reserved by signal | |
| property_changed = Signal(providing_args=["instance", "property"]) | |
| @receiver(property_changed, sender=Order) | |
| def notify_advisor(sender, instance, property, **kwargs): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from celery import Celery | |
| celery_app = Celery() | |
| @celery_app.task | |
| def daily_notificator(): | |
| # notifications logic here :) | |
| # schedule task |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from views import SomeView | |
| view = SomeView() | |
| view.get_order() | |
| # cache_property save our function call to magic object dict attribute | |
| # location of value view.__dict__['get_order'] | |
| # we can get access to this cache, wtih magic method __dict__ | |
| view.__dict__['get_order'] | |
| # >>> our order |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from cached_property import cached_property | |
| class SomeView: | |
| ... | |
| @cached_property | |
| def get_order(): | |
| order = get_object_or_404(Order, pk=self.self.kwargs['order_id']) | |
| return order |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from decorators import count_calls | |
| @count_calls | |
| def say_whee(): | |
| print("Whee!") | |
| # >>> say_whee() | |
| # Call 1 of 'say_whee' | |
| # Whee! | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import functools | |
| def count_calls(func): | |
| @functools.wraps(func) | |
| def wrapper_count_calls(*args, **kwargs): | |
| wrapper_count_calls.num_calls += 1 | |
| print(f"Call {wrapper_count_calls.num_calls} of {func.__name__!r}") | |
| return func(*args, **kwargs) | |
| wrapper_count_calls.num_calls = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from decorators import timer | |
| @timer | |
| def waste_some_time(num_times): | |
| for _ in range(num_times): | |
| sum([i**2 for i in range(10000)]) | |
| # >>> waste_some_time(1) | |
| # Finished 'waste_some_time' in 0.0010 secs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import functools | |
| import time | |
| def timer(func): | |
| """Print the runtime of the decorated function""" | |
| @functools.wraps(func) | |
| def wrapper_timer(*args, **kwargs): | |
| start_time = time.perf_counter() # before main function call | |
| value = func(*args, **kwargs) | |
| end_time = time.perf_counter() # after main function call |
NewerOlder