Skip to content

Commit 22c75d2

Browse files
committed
deprecated memoize and added lru_cache
1 parent a33902f commit 22c75d2

2 files changed

Lines changed: 7 additions & 26 deletions

File tree

synapseclient/client.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
id_of,
119119
get_properties,
120120
MB,
121-
memoize,
122121
is_json,
123122
extract_synapse_id_from_query,
124123
find_data_file_handle,
@@ -525,7 +524,7 @@ def login(
525524
cached_sessions.set_most_recent_user(self.credentials.username)
526525

527526
if not silent:
528-
profile = self.getUserProfile(refresh=True)
527+
profile = self.getUserProfile()
529528
# TODO-PY3: in Python2, do we need to ensure that this is encoded in utf-8
530529
self.logger.info(
531530
"Welcome, %s!\n"
@@ -639,21 +638,19 @@ def invalidateAPIKey(self):
639638
if self._is_logged_in():
640639
self.restDELETE("/secretKey", endpoint=self.authEndpoint)
641640

642-
@memoize
643-
def getUserProfile(self, id=None, sessionToken=None, refresh=False):
641+
@functools.lru_cache()
642+
def getUserProfile(self, id=None, sessionToken=None):
644643
"""
645644
Get the details about a Synapse user.
646645
Retrieves information on the current user if 'id' is omitted.
647646
:param id: The 'userId' (aka 'ownerId') of a user or the userName
648647
:param sessionToken: The session token to use to find the user profile
649-
:param refresh: If set to True will always fetch the data from Synape otherwise will use cached information
650648
:returns: The user profile for the user of interest.
651649
652650
Example::
653651
my_profile = syn.getUserProfile()
654652
freds_profile = syn.getUserProfile('fredcommo')
655653
"""
656-
657654
try:
658655
# if id is unset or a userID, this will succeed
659656
id = "" if id is None else int(id)
@@ -674,7 +671,6 @@ def getUserProfile(self, id=None, sessionToken=None, refresh=False):
674671
else: # no break
675672
raise ValueError('Can\'t find user "%s": ' % id)
676673
uri = "/userProfile/%s" % id
677-
678674
return UserProfile(
679675
**self.restGET(
680676
uri, headers={"sessionToken": sessionToken} if sessionToken else None
@@ -2589,7 +2585,7 @@ def _download_from_URL(
25892585
else:
25902586
mode = "wb"
25912587
previouslyTransferred = 0
2592-
sig = hashlib.md5()
2588+
sig = hashlib.md5(usedforsecurity=False)
25932589

25942590
try:
25952591
with open(temp_destination, mode) as fd:
@@ -4681,7 +4677,6 @@ def _generate_headers(self, headers=None):
46814677

46824678
if headers is None:
46834679
headers = dict(self.default_headers)
4684-
46854680
headers.update(synapseclient.USER_AGENT)
46864681

46874682
return headers
@@ -4717,6 +4712,7 @@ def _rest_call(
47174712
uri, headers = self._build_uri_and_headers(
47184713
uri, endpoint=endpoint, headers=headers
47194714
)
4715+
47204716
retryPolicy = self._build_retry_policy(retryPolicy)
47214717
requests_session = requests_session or self._requests_session
47224718

@@ -4733,6 +4729,7 @@ def _rest_call(
47334729
verbose=self.debug,
47344730
**retryPolicy,
47354731
)
4732+
47364733
self._handle_synapse_http_error(response)
47374734
return response
47384735

synapseclient/core/utils.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import collections.abc
1212
import datetime
1313
import errno
14-
import functools
1514
import hashlib
1615
import importlib
1716
import inspect
@@ -51,7 +50,7 @@ def md5_for_file(filename, block_size=2 * MB, callback=None):
5150
:returns: The MD5
5251
"""
5352

54-
md5 = hashlib.md5()
53+
md5 = hashlib.md5(usedforsecurity=False)
5554
with open(filename, "rb") as f:
5655
while True:
5756
if callback:
@@ -644,21 +643,6 @@ def extract_synapse_id_from_query(query):
644643
raise ValueError('Couldn\'t extract synapse ID from query: "%s"' % query)
645644

646645

647-
# Derived from https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
648-
def memoize(obj):
649-
cache = obj._memoize_cache = {}
650-
651-
@functools.wraps(obj)
652-
def memoizer(*args, **kwargs):
653-
refresh = kwargs.pop("refresh", False)
654-
key = str(args) + str(kwargs)
655-
if refresh or key not in cache:
656-
cache[key] = obj(*args, **kwargs)
657-
return cache[key]
658-
659-
return memoizer
660-
661-
662646
def printTransferProgress(
663647
transferred,
664648
toBeTransferred,

0 commit comments

Comments
 (0)