Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
make cache for statistics global
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Jan 14, 2020
1 parent d1b2f29 commit 696e79d
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/sage/databases/findstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def increasing_tree_shape(sigma):
######################################################################
# the FindStat URLs
FINDSTAT_URL = 'http://www.findstat.org/'
FINDSTAT_URL = 'http://localhost:8090/'
FINDSTAT_API = FINDSTAT_URL + "api/"
FINDSTAT_API_COLLECTIONS = FINDSTAT_API + 'CollectionsDatabase/'
FINDSTAT_API_STATISTICS = FINDSTAT_API + 'StatisticsDatabase/'
Expand Down Expand Up @@ -1812,6 +1813,8 @@ def submit(self):
# editing and submitting is really the same thing
edit = submit

_all_statistics = {}

class FindStatStatistics(UniqueRepresentation, Parent):
r"""
The class of FindStat statistics.
Expand Down Expand Up @@ -1843,13 +1846,9 @@ def __init__(self, domain=None):
"""
if domain is None:
self._domain = None
url = FINDSTAT_API_STATISTICS
else:
self._domain = FindStatCollection(domain)
url = FINDSTAT_API_STATISTICS + "?Domain=%s" % self._domain.id_str()

# TODO: it may be better to make ths lazy
self._all_statistics = {st: None for st in json.load(urlopen(url))["data"]}
self._identifiers = None
Parent.__init__(self, category=Sets())

def _element_constructor_(self, id):
Expand Down Expand Up @@ -1881,10 +1880,10 @@ def _element_constructor_(self, id):
return FindStatCompoundStatistic(id)
if not re.match(FINDSTAT_STATISTIC_REGEXP, id) or id == FINDSTAT_STATISTIC_PADDED_IDENTIFIER % 0:
raise ValueError("The value %s is not a valid FindStat statistic identifier." % id)
if id not in self._all_statistics or self._all_statistics[id] is None:
self._all_statistics[id] = self.element_class(self, id)
if id not in _all_statistics or _all_statistics[id] is None:
_all_statistics[id] = self.element_class(self, id)

return self._all_statistics[id]
return _all_statistics[id]

def _repr_(self):
"""
Expand All @@ -1900,7 +1899,7 @@ def _repr_(self):
return "Set of combinatorial statistics used by FindStat"
return "Set of combinatorial statistics with domain %s used by FindStat" % self._domain

def __getitem__(self, id):
def __iter__(self):
"""
Return an iterator over all FindStat statistics.
Expand All @@ -1911,7 +1910,16 @@ def __getitem__(self, id):
St000062: The length of the longest increasing subsequence of the permutation.
"""
return FindStatStatistic(id+1)
if self._identifiers is None:
if self._domain is None:
url = FINDSTAT_API_STATISTICS
else:
url = FINDSTAT_API_STATISTICS + "?Domain=%s" % self._domain.id_str()

self._identifiers = json.load(urlopen(url))["data"]

for st in self._identifiers:
yield FindStatStatistic(st)

Element = FindStatStatistic

Expand Down

0 comments on commit 696e79d

Please sign in to comment.