Skip to content

Commit 9ed0b41

Browse files
committed
provide options for the oeis
1 parent 6bc37d7 commit 9ed0b41

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

src/sage/databases/oeis.py

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@
154154
from sage.misc.verbose import verbose
155155
from sage.repl.preparse import preparse
156156
from sage.rings.integer import Integer
157+
from sage.rings.integer_ring import ZZ
157158
from sage.rings.infinity import infinity
159+
from sage.structure.global_options import GlobalOptions
158160
from sage.structure.sage_object import SageObject
159161
from sage.structure.unique_representation import UniqueRepresentation
160162

@@ -246,7 +248,7 @@ class OEIS:
246248
- a list representing a sequence of integers.
247249
- a string, representing a text search.
248250
249-
- ``max_results`` -- integer (default: 30); the maximum number of
251+
- ``max_results`` -- integer (default: 3); the maximum number of
250252
results to return, they are sorted according to their relevance. In
251253
any cases, the OEIS website will never provide more than 100 results.
252254
@@ -255,6 +257,8 @@ class OEIS:
255257
This is useful if you are looking for a sequence that may appear
256258
after the 100 first found sequences.
257259
260+
``max_results`` can also be set using :meth:`options`.
261+
258262
OUTPUT:
259263
260264
- if ``query`` is an integer or an OEIS ID (e.g. 'A000045'), returns
@@ -361,8 +365,7 @@ class OEIS:
361365
sage: oeis('A000045') # optional -- internet
362366
A000045: Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
363367
"""
364-
365-
def __call__(self, query, max_results=3, first_result=0):
368+
def __call__(self, query, max_results=None, first_result=0):
366369
r"""
367370
See the documentation of :class:`OEIS`.
368371
@@ -383,6 +386,46 @@ def __call__(self, query, max_results=3, first_result=0):
383386
elif isinstance(query, (list, tuple)):
384387
return self.find_by_subsequence(query, max_results, first_result)
385388

389+
class options(GlobalOptions):
390+
r"""
391+
Set and display the options for the OEIS.
392+
393+
If no parameters are set, then the function returns a copy of
394+
the options dictionary.
395+
396+
The ``options`` can be accessed as using
397+
:class:`oeis.options`.
398+
399+
@OPTIONS@
400+
401+
EXAMPLES::
402+
403+
sage: oeis.options
404+
Current options for OEIS
405+
- fetch_b_file: False
406+
- max_results: 3
407+
408+
sage: oeis.options.max_results = 5
409+
sage: oeis('beaver') # optional -- internet
410+
0: A...: ...eaver...
411+
1: A...: ...eaver...
412+
2: A...: ...eaver...
413+
3: A...: ...eaver...
414+
4: A...: ...eaver...
415+
416+
sage: oeis.options._reset()
417+
sage: oeis.options.max_results
418+
3
419+
"""
420+
NAME = 'OEIS'
421+
module = 'sage.databases.oeis'
422+
max_results = dict(default=3,
423+
description='the maximum number of results to return',
424+
checker=lambda x: x in ZZ and x > 0)
425+
fetch_b_file = dict(default=False,
426+
description='whether to fetch terms from the b-file by default',
427+
checker=lambda x: isinstance(x, bool))
428+
386429
def __repr__(self) -> str:
387430
r"""
388431
Return the representation of ``self``.
@@ -440,7 +483,7 @@ def find_by_entry(self, entry):
440483
sequence._raw = entry
441484
return sequence
442485

443-
def find_by_description(self, description, max_results=3, first_result=0):
486+
def find_by_description(self, description, max_results=None, first_result=0):
444487
r"""
445488
Search for OEIS sequences corresponding to the description.
446489
@@ -483,7 +526,19 @@ def find_by_description(self, description, max_results=3, first_result=0):
483526
1: A...: ...eaver...
484527
2: A...: ...eaver...
485528
3: A...: ...eaver...
529+
530+
Alternatively, we can also set the global option `max_results`::
531+
532+
sage: oeis.options.max_results = 5
533+
sage: oeis('beaver') # optional -- internet
534+
0: A...: ...eaver...
535+
1: A...: ...eaver...
536+
2: A...: ...eaver...
537+
3: A...: ...eaver...
538+
4: A...: ...eaver...
486539
"""
540+
if max_results is None:
541+
max_results = self.options['max_results']
487542
options = {'q': description,
488543
'n': str(max_results),
489544
'fmt': 'text',
@@ -493,7 +548,7 @@ def find_by_description(self, description, max_results=3, first_result=0):
493548
T = [self.find_by_entry(entry=s) for s in sequence_list]
494549
return FancyTuple([s for s in T if not s.is_dead()])
495550

496-
def find_by_subsequence(self, subsequence, max_results=3, first_result=0):
551+
def find_by_subsequence(self, subsequence, max_results=None, first_result=0):
497552
r"""
498553
Search for OEIS sequences containing the given subsequence.
499554
@@ -1244,6 +1299,14 @@ def first_terms(self, number=None):
12441299
41
12451300
sage: len(oeis(45).first_terms(oo)) # optional -- internet
12461301
2001
1302+
1303+
sage: len(oeis(1).first_terms()) # optional -- internet
1304+
94
1305+
1306+
sage: oeis.options.fetch_b_file = True
1307+
sage: len(oeis(1).first_terms()) # optional -- internet
1308+
2048
1309+
sage: oeis.options._reset()
12471310
"""
12481311
def fetch_b_file():
12491312
url = oeis_url + f"b{self.id(format='int')}.txt"
@@ -1260,8 +1323,9 @@ def fetch_b_file():
12601323
first_terms += (v,)
12611324
self._first_terms = True, first_terms
12621325

1263-
# self._first_terms is a pair (all?, first_terms)
1264-
if number is infinity:
1326+
if ((number is infinity or oeis.options['fetch_b_file'])
1327+
and self is not oeis._imaginary_sequence()): # all other sequences have a b-file
1328+
# self._first_terms is a pair (all?, first_terms)
12651329
if not hasattr(self, "_first_terms") or not self._first_terms[0]:
12661330
fetch_b_file()
12671331
return self._first_terms[1]

0 commit comments

Comments
 (0)