Skip to content

Commit 1265508

Browse files
committed
Added cached_result decorator.
1 parent 350825f commit 1265508

2 files changed

Lines changed: 53 additions & 18 deletions

File tree

addons/source-python/packages/source-python/core/cache.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
# =============================================================================
1111
# >> FORWARD IMPORTS
1212
# =============================================================================
13+
# Python Imports
14+
# FuncTools
15+
from functools import wraps
16+
# Types
17+
from types import MethodType
18+
1319
# Source.Python Imports
1420
# Core
1521
from _core._cache import CachedProperty
@@ -22,4 +28,46 @@
2228
__all__ = [
2329
'CachedProperty',
2430
'cached_property'
31+
'cached_result'
2532
]
33+
34+
35+
# =============================================================================
36+
# >> FUNCTIONS
37+
# =============================================================================
38+
def cached_result(fget):
39+
"""Decorator used to register a cached method.
40+
41+
:param function fget:
42+
Method that is only called once and its result cached for subsequent
43+
calls.
44+
:rtype: CachedProperty
45+
"""
46+
# Get a dummy object as default cache, so that we can cache None
47+
NONE = object()
48+
49+
def getter(self):
50+
"""Getter function that generates the cached method."""
51+
# Set our cache to the default value
52+
cache = NONE
53+
54+
# Wrap the decorated method as the inner function
55+
@wraps(fget)
56+
def wrapper(self, *args, **kwargs):
57+
"""Calls the decorated method and cache the result."""
58+
nonlocal cache
59+
60+
# Did we cache a result already?
61+
if cache is NONE:
62+
63+
# No cache, let's call the wrapped method and cache the result
64+
cache = fget(self, *args, **kwargs)
65+
66+
# Return the cached result
67+
return cache
68+
69+
# Bind the wrapper function to the passed instance and return it
70+
return MethodType(wrapper, self)
71+
72+
# Return a cached property bound to the getter function
73+
return CachedProperty(getter, doc=fget.__doc__)

addons/source-python/packages/source-python/players/_base.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Core
1616
from core import GAME_NAME
1717
from core.cache import cached_property
18+
from core.cache import cached_result
1819
# Engines
1920
from engines.server import server
2021
from engines.server import engine_server
@@ -217,35 +218,21 @@ def is_fake_client(self):
217218
"""
218219
return self.playerinfo.is_fake_client()
219220

220-
@cached_property
221-
def _is_hltv(self):
222-
"""Return whether the player is HLTV.
223-
224-
:rtype: bool
225-
"""
226-
return self.playerinfo.is_hltv()
227-
221+
@cached_result
228222
def is_hltv(self):
229223
"""Return whether the player is HLTV.
230224
231225
:rtype: bool
232226
"""
233-
return self._is_hltv
234-
235-
@cached_property
236-
def _is_bot(self):
237-
"""Return whether the player is a bot.
238-
239-
:rtype: bool
240-
"""
241-
return self.is_fake_client() or self.steamid == 'BOT'
227+
return self.playerinfo.is_hltv()
242228

229+
@cached_result
243230
def is_bot(self):
244231
"""Return whether the player is a bot.
245232
246233
:rtype: bool
247234
"""
248-
return self._is_bot
235+
return self.is_fake_client() or self.steamid == 'BOT'
249236

250237
def is_in_a_vehicle(self):
251238
"""Return whether the player is in a vehicle.

0 commit comments

Comments
 (0)