Last active
June 4, 2023 13:59
-
-
Save barrysmyth/b28cfbbecaa137527057b5f682f5609f to your computer and use it in GitHub Desktop.
Two functions to programmatically search for matching sequences on the OEIS and to lookup a given sequence (by id) on the OEIS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from lxml import html | |
def search(seq, n=10, m=10): | |
"""Search the OEIS for sequences matching seq and return a list of results. | |
Args: | |
seq: list of integers | |
n: number of results to return. | |
m: number of terms to return per result. | |
Returns: | |
List of OEIS results (if any) with each result represented as a tuple | |
containing: sequence id, sequence URL, sequence description, sequence | |
elements from OEIS.""" | |
# Format the query from the sequence for OEIS | |
q = '%2C'.join(map(str, seq)) | |
query = 'https://oeis.org/search?q={}&sort=&language=&go=Search'.format(q) | |
# Submit the request with the query | |
page = requests.get(query) | |
tree = html.fromstring(page.content) | |
# The xpaths to the resulting sequences. | |
links_xpath =\ | |
'/html/body/center[2]/table/tr/td/table/tr/td/table/tr/td/a' | |
# The xpath to the OEIS message box in case there are no results. | |
msg_xpath =\ | |
'/html/body/center[2]/table/tr/td/table/tr[2]/td/table/tr/td//text()' | |
# Get the sequence ids of the results. | |
seq_ids = [link.attrib.get('href')[1:] for link in tree.xpath(links_xpath)[:n]] | |
# If there are results then lookthem up and return their details. | |
if len(seq_ids): | |
return list(map(lambda ids: lookup(ids, m=m), seq_ids)) | |
# Otherwise print the OEIS message. | |
else: | |
return query, ''.join(''.join(tree.xpath(msg_xpath)).split('\n')).strip() | |
def lookup(seq_id, m=10): | |
"""Lookup the OEIS entry for sequence id and return list of elements. | |
Args: | |
seq_id: valid OEIS sequence id. | |
m: number of terms to return. | |
Returns: | |
A tuple containing: (a) the sequence id; (b) sequence URL; | |
(c) the sequence description; and (d) its listed terms.""" | |
url = 'https://oeis.org/{}'.format(seq_id) | |
page = requests.get(url) | |
tree = html.fromstring(page.content) | |
# Get the actual sequence terms | |
desc_xpath =\ | |
'/html/body/center[2]/table/tr/td/center[2]/table/tr[3]/td/table/tr/td[3]/text()' | |
terms_xpath =\ | |
'/html/body/center[2]/table/tr/td/center[2]/table/tr[4]/td/table/tr/td[2]/tt/text()' | |
desc = tree.xpath(desc_xpath) | |
terms = tree.xpath(terms_xpath) | |
# If there are term then we have a valid sequence; return its id, url, | |
# description and terms. | |
if len(terms): | |
return ( | |
seq_id, # Sequence id | |
'https://oeis.org/{}'.format(seq_id), # Sequence URL | |
''.join(desc).strip(), # Description | |
list(map(int, terms[0].split(', ')[:m])) # First m terms | |
) | |
# Otherwise return an error message. | |
else: | |
return "https://oeis.org/{} could not be found. It may be allocated for review.".format(seq_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment