Skip to content

Commit 40ad287

Browse files
committed
make use of the b-file
1 parent 4e2319b commit 40ad287

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

src/sage/databases/oeis.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@
147147

148148
from sage.version import version
149149
from sage.cpython.string import bytes_to_str
150-
from sage.misc.cachefunc import cached_method
151150
from sage.misc.flatten import flatten
152151
from sage.misc.html import HtmlFragment
153152
from sage.misc.temporary_file import tmp_filename
@@ -689,7 +688,7 @@ def __init__(self, ident):
689688

690689
def online_update(self):
691690
r"""
692-
Fetch the online OEIS to update the informations about this sequence.
691+
Fetch the sequence from the OEIS.
693692
694693
TESTS::
695694
@@ -1205,15 +1204,16 @@ def is_full(self):
12051204
else:
12061205
return Unknown
12071206

1208-
@cached_method
12091207
def first_terms(self, number=None):
12101208
r"""
1209+
Return the first few terms of the sequence.
12111210
12121211
INPUT:
12131212
1214-
- ``number`` -- integer or ``None`` (default); the number of
1215-
terms returned (if less than the number of available terms). When set
1216-
to ``None``, returns all the known terms.
1213+
- ``number`` -- integer, ``True`` or ``None`` (default); the
1214+
number of terms returned (if less than the number of
1215+
available terms). When set to ``True``, fetches the
1216+
b-file. When set to ``None``, returns all the known terms.
12171217
12181218
OUTPUT: tuple of integers
12191219
@@ -1237,9 +1237,41 @@ def first_terms(self, number=None):
12371237
(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
12381238
sage: s.first_terms(5)
12391239
(1, 1, 1, 1, 2)
1240+
12401241
"""
1241-
fields = ['S', 'T', 'U']
1242-
return to_tuple(" ".join(flatten([self._field(a) for a in fields])))[:number]
1242+
def fetch_b_file():
1243+
url = oeis_url + f"b{self.id(format='int')}.txt"
1244+
terms = _fetch(url)
1245+
first_terms = tuple()
1246+
check = None
1247+
for term in terms.split('\n'):
1248+
if not term or term[0] == '#':
1249+
continue
1250+
k, v = [Integer(e) for e in term.strip().split()]
1251+
if check is not None and k != check + 1:
1252+
raise ValueError(f"malformed b-file {url}: key {check} followed by {k}")
1253+
check = k
1254+
first_terms += (v,)
1255+
self._first_terms = True, first_terms
1256+
1257+
# self._first_terms is a pair (all?, first_terms)
1258+
if number is True:
1259+
if not hasattr(self, "_first_terms") or not self._first_terms[0]:
1260+
fetch_b_file()
1261+
return self._first_terms[1]
1262+
1263+
if not hasattr(self, "_first_terms"):
1264+
fields = ['S', 'T', 'U']
1265+
first_terms = to_tuple(" ".join(flatten([self._field(a) for a in fields])))
1266+
self._first_terms = (False, first_terms)
1267+
1268+
if number is None:
1269+
return self._first_terms[1]
1270+
1271+
if number > len(self._first_terms[1]) and not self._first_terms[0]:
1272+
fetch_b_file()
1273+
1274+
return self._first_terms[1][:number]
12431275

12441276
def _repr_(self):
12451277
r"""
@@ -1755,8 +1787,7 @@ def show(self):
17551787
https://oeis.org/A012345
17561788
<BLANKLINE>
17571789
AUTHOR
1758-
Patrick Demichel (patrick.demichel(AT)hp.com)
1759-
<BLANKLINE>
1790+
...
17601791
17611792
TESTS::
17621793

0 commit comments

Comments
 (0)