HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMajor

An OEIS Lookup tool in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lookupoeispythontool

Problem

I'm from PPCG so I was making an esolang and I decided to write it in Python. Eventually it went from an esolang to an OEIS (Online Encyclopedia of Integer Sequences) lookup tool. I'm very new to Python.

Essentially this program takes an OEIS sequence number (e.g. 55 for sequence A000055) and the nth number in the sequence to return.

The code gets the OEIS page, parses it with BeautifulSoup, and returns the result, if it doesn't exist it returns "OEIS does not have a number in the sequence at the given index".

import sys, re
from urllib2 import*
from bs4 import BeautifulSoup

# Main logic
def execute(code, nth):
# Decode input stuff
num = int(code)
try:
f = urlopen("http://oeis.org/A%06d/list" % num)
# global tree, data # Debugging
# >:D I'm sorry but I have to use RegEx to parse HTML
print {key: int(value) for key, value in
re.findall( r'(\d+) (\d+)', re.sub(r'\D+', " ", re.sub(r']+>', "",
str(BeautifulSoup(f, "lxml").find(lambda tag:
tag.name == "table" and # A table
not tag.get('cellspacing') == "0" and # A table table
len(tag.contents) > 1 # A two column table
))
)) )
}.get(nth, "OEIS does not have a number in the sequence at the given index")
except HTTPError:
print "Could not find sequence A%06d" % num
except URLError:
print "Could not connect to sources";
except:
print "Verify your numbers are correct"
raise

if __name__ == '__main__':
if len(sys.argv) > 1:
execute(sys.argv[1], sys.argv[2])
else:
print """This is the OEIS lookup tool
You haven't entered the sequence""" % (LANGNAME)


As I'm new to Python. I'm using things I only kind of understand. My main concern is how I laid out this program. Especially the HTML parsing, I really doubt I'm doing that in the best way.

Solution

import*:

Please avoid importing all modules at all costs. Import each module you use separately

Regex ಠ_ಠ

Regex is evil, the worst. seriously. stop it. right now. kill python.exe right now and change it.

You use beautifulsoup, a dom parsing library, literally a few lines later, but you choose to use regex.

You sir, are evil.

I would suggest looking further at beautifulsoup, or taking a look at Scrapy, a scraping library for Python that makes use of generators to scrape large element sets (and even has cloud support!)

Lisp

Your code reads like lisp.

))
  )) )


Seriously, you need to format this better.

Try using Python's style guide, PEP8. when running your code through the online analyser, it threw 20 errors immediately.

When coding good Python, PEP8 should be your goto style document.

Looping

You shouldn't be looping like this:

print {key: int(value) for key, value in


If you want to reduce the code indentation level, then you should use separate variables to store each step in the process, or just simply use a standard loop.

String formatting

Instead of using % (myString), you should use string.format, as it's an improvement for these reasons:

  • less independant of parameter order



  • More readable



"My name is {name}, and I work for {work_name} as a {job_name}".format(name="Batman", work_name="Gotham City", job_name="Crimestopper")

Code Snippets

print {key: int(value) for key, value in
"My name is {name}, and I work for {work_name} as a {job_name}".format(name="Batman", work_name="Gotham City", job_name="Crimestopper")

Context

StackExchange Code Review Q#120102, answer score: 27

Revisions (0)

No revisions yet.