Skip to content

Commit d906a94

Browse files
committed
adding some django core modules
1 parent dbf15a4 commit d906a94

30 files changed

+3405
-59
lines changed

content/pages/04-web-development/23-javascript.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ Allen Wirfs-Brock for more context on the language's evolution.
130130
* [Learn JS data](http://learnjsdata.com/) teaches how to manipulate data
131131
using JavaScript in a browser or on the server using Node.js.
132132

133+
* [A Beginner's Guide to JavaScript's Prototype](https://ui.dev/beginners-guide-to-javascript-prototype/)
134+
explains the fundamentals of JavaScript's object system, which is
135+
a prototype-based model and different from many other common
136+
programming languages' object models.
137+
138+
* [Understanding Data Types in JavaScript](https://www.digitalocean.com/community/tutorials/understanding-data-types-in-javascript)
139+
examines JavaScript's dynamic data type model and how it manifests
140+
in the way numbers, string, Booleans and arrays are used.
141+
133142

134143
### JavaScript learning checklist
135144
1. Create a simple HTML file with basic elements in it. Use the
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
title: django.core cache code examples
2+
category: page
3+
slug: django-core-cache-examples
4+
sortorder: 500011078
5+
toc: False
6+
sidebartitle: django.core cache
7+
meta: Python example code for the cache function from the django.core module of the Django project.
8+
9+
10+
cache is a function within the django.core module of the Django project.
11+
12+
13+
## Example 1 from django-debug-toolbar
14+
[django-debug-toolbar](https://github.com/jazzband/django-debug-toolbar)
15+
([project documentation](https://github.com/jazzband/django-debug-toolbar)
16+
and [PyPI page](https://pypi.org/project/django-debug-toolbar/))
17+
grants a developer detailed request-response cycle information while
18+
developing a [Django](/django.html) web application.
19+
The code for django-debug-toolbar is
20+
[open source](https://github.com/jazzband/django-debug-toolbar/blob/master/LICENSE)
21+
and maintained by the developer community group known as
22+
[Jazzband](https://jazzband.co/).
23+
24+
[**django-debug-toolbar / debug_toolbar / panels / cache.py**](https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/panels/cache.py)
25+
26+
```python
27+
# cache.py
28+
import inspect
29+
import sys
30+
import time
31+
from collections import OrderedDict
32+
33+
from django.conf import settings
34+
~~from django.core import cache
35+
from django.core.cache import CacheHandler, caches as original_caches
36+
from django.core.cache.backends.base import BaseCache
37+
from django.dispatch import Signal
38+
from django.middleware import cache as middleware_cache
39+
from django.utils.translation import gettext_lazy as _, ngettext as __
40+
41+
from debug_toolbar import settings as dt_settings
42+
from debug_toolbar.panels import Panel
43+
from debug_toolbar.utils import (
44+
get_stack,
45+
get_template_info,
46+
render_stacktrace,
47+
tidy_stacktrace,
48+
)
49+
50+
cache_called = Signal()
51+
52+
53+
def send_signal(method):
54+
def wrapped(self, *args, **kwargs):
55+
t = time.time()
56+
value = method(self, *args, **kwargs)
57+
t = time.time() - t
58+
59+
60+
61+
## ... source file abbreviated to get to cache examples ...
62+
63+
64+
@property
65+
def nav_subtitle(self):
66+
cache_calls = len(self.calls)
67+
return __(
68+
"%(cache_calls)d call in %(time).2fms",
69+
"%(cache_calls)d calls in %(time).2fms",
70+
cache_calls,
71+
) % {"cache_calls": cache_calls, "time": self.total_time}
72+
73+
@property
74+
def title(self):
75+
count = len(getattr(settings, "CACHES", ["default"]))
76+
return __(
77+
"Cache calls from %(count)d backend",
78+
"Cache calls from %(count)d backends",
79+
count,
80+
) % {"count": count}
81+
82+
def enable_instrumentation(self):
83+
if isinstance(middleware_cache.caches, CacheHandlerPatch):
84+
~~ cache.caches = middleware_cache.caches
85+
else:
86+
~~ cache.caches = CacheHandlerPatch()
87+
88+
def disable_instrumentation(self):
89+
~~ cache.caches = original_caches
90+
middleware_cache.caches = original_caches
91+
92+
def generate_stats(self, request, response):
93+
self.record_stats(
94+
{
95+
"total_calls": len(self.calls),
96+
"calls": self.calls,
97+
"total_time": self.total_time,
98+
"hits": self.hits,
99+
"misses": self.misses,
100+
"counts": self.counts,
101+
}
102+
)
103+
104+
def generate_server_timing(self, request, response):
105+
stats = self.get_stats()
106+
value = stats.get("total_time", 0)
107+
title = "Cache {} Calls".format(stats.get("total_calls", 0))
108+
self.record_server_timing("total_time", title, value)
109+
110+
111+
112+
## ... source file continues with no further cache examples...
113+
114+
```
115+

0 commit comments

Comments
 (0)