Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: ⚡️ Prevent compiling of regex for IP on every import #8

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hitcount/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def _setup(self):
self._wrapped = Settings(app_settings, django_settings)


DEPRECATED_SETTINGS = {
_DEPRECATED_DJANGO_SETTINGS = {
'USE_TZ' if django.VERSION > (4, 0) else None,
'PASSWORD_RESET_TIMEOUT_DAYS' if django.VERSION > (3, 0) else None,
'DEFAULT_CONTENT_TYPE' if django.VERSION > (2, 2) else None,
Expand All @@ -24,7 +24,7 @@ def __init__(self, *args):
setattr(self, attr, getattr(item, attr))
for item in args
for attr in dir(item)
if attr == attr.upper() and attr.upper() not in DEPRECATED_SETTINGS
if attr == attr.upper() and attr.upper() not in _DEPRECATED_DJANGO_SETTINGS
]


Expand Down
8 changes: 5 additions & 3 deletions hitcount/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import re

from django.apps import apps
try:
from django.utils.regex_helper import _lazy_re_compile
except ImportError:
from django.core.validators import _lazy_re_compile

from hitcount.conf import settings

# this is not intended to be an all-knowing IP address regex
IP_RE = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
IP_RE = _lazy_re_compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')


def get_ip(request):
Expand Down