Welcome to pyoeis’s documentation!¶
Contents:
Introduction¶
The Online Encyclopedia of Integer Sequences (OEIS) is a database of integer sequences. It contains many well-known sequences, such as the primes and Fibonacci numbers as well as many more obscure sequences.
PyOEIS allows for the searching of the OEIS from within Python, as well as
allowing you to access sequence information through
Sequence
objects.
OEIS Search Syntax and Other Useful Links¶
While PyOEIS provides many porcelain methods which allow you to search by sequence ID, author etc., it is also possible to query the OEIS using its usual search syntax (as you would on the website). It may also be useful to understand how the OEIS operates for debugging purposes. The following links may therefore be of use:
API¶
OEISClient
objects¶
-
class
client.
OEISClient
¶ Maintains a
Session
and contains all methods for querying the OEIS.-
get_by_id
(id)¶ Returns a
Sequence
for the sequence with the ID id, or else raisesNoResultsError
.Note
On the OEIS website, IDs are displayed with an uppercase letter and 6 (for A IDs) or 4 (for M and N IDs) digits. However, this method does not require an uppercase letter or leading zeros to be used.
-
lookup_by_name
(name, max_seqs=10)¶ Returns a list of at most max_seqs
Sequence
objects whose names contain name.Note
Sequences are retrieved in sets of 10 and sequences are then removed if necessary. So, there is no speed improvement between, for example, a max_seqs of 10 and one of 15. This applies to all methods with a max_seqs argument.
Returns a list of at most max_seqs
Sequence
objects whose authors contain author.
-
lookup_by
(prefix, query, max_seqs=10, list_func=False)¶ If prefix is “”, search OEIS with string query, otherwise use string ‘prefix:query‘.
If list_func is true, return a list of at most max_seqs
Sequence
objects or else an empty list if there are no results. If list_func is false, return the first Sequence found, or else raise aNoResultsError
.
-
Sequence
objects¶
-
class
sequence.
Sequence
(sequence_entry)¶ Takes internal format sequence entry as constructor argument.
Has attributes to contain information for each field of a sequence entry in the OEIS and methods fot retrieving a certain number of the sequence’s signed or unsigned terms.
-
generate
(n)¶ If a parsable formula exists, returns the nth term of the sequence, else raises a
NoFunctionError
.
-
signed
(n)¶ Returns the first n signed integers in the sequence.
-
unsigned
(n)¶ Returns the first n unsigned integers in the sequence.
-
Attributes¶
id | The sequence’s unique ID in the OEIS, as a string. Begins ‘A’. |
alt_ids | Other IDs, as a list of strings beginning ‘M’ and ‘N’ which the sequence carried in the “The Encyclopedia of Integer Sequences”, 1995 or the “Handbook of Integer Sequences”, 1973, respectively. |
unsigned_list | A list of terms in the sequence without any minus signs. |
signed_list | A list of terms in the sequence including any minus signs. |
name | The name of the sequence, as a string. |
references | A list of references to the sequence. |
links | A list of links about the sequence. |
formulae | Formulae for generating the sequence, as a list of strings. |
cross_references | Cross-references to the sequence from elsewhere in the OEIS, as a list of strings. |
author | The author of the sequence entry, as a string. |
offset | The subscript of the first term and the position of the first term whose modulus exceeds 1, as a tuple of two numbers. |
errors | Errors in the sequence entry, as a list of strings. |
examples | Examples to illustrate the sequence, as a list of strings. |
maple | Maple code to generate the sequence, as a string. |
mathematica | Mathematica code to generate the sequence, as a string. |
other_programs | Code to generate the sequence in other programs/languages, as a list of strings. |
keywords | The sequence’s keywords, as a list of strings. |
comments | Comments on the sequence entry, as a list of strings. |
More information about the fields in a sequence entry can be found here.
Errors
¶
-
exception
errors.
InvalidQueryError
(response)¶ Raised when a search is invalid according to the OEIS search syntax.
-
exception
errors.
MalformedSequenceError
(malformed_line)¶ Raised when a sequence entry does not contain the required information in a parsable format.
-
exception
errors.
NoFunctionError
(seq)¶ Raised when a sequence has no formula.
-
exception
errors.
NoResultsError
(query)¶ Raised when a search gives no results and it is unacceptable to return an empty list.
-
exception
errors.
OEISException
¶ Base class for PyOEIS exceptions.
-
exception
errors.
TooManyResultsError
(query)¶ Raised when too many results are found for a search for them to be properly parsed.
Usage¶
The first thing you must do when using pyoeis is to initialise an
OEISClient
:
>>> import pyoeis
>>> c = pyoeis.OEISClient()
This handles all queries to the OEIS and all methods for querying are accessed from it. For example, say we want to access the entry for sequence A000040, the primes:
>>> primes = c.get_by_id('a40')
>>> primes.name
u'The prime numbers'