OEIS: First commit. Closes GH-118.
parent
823f49770c
commit
4345257308
|
@ -0,0 +1 @@
|
|||
Insert a description of your plugin here, with any notes, etc. about using it.
|
|
@ -0,0 +1,69 @@
|
|||
###
|
||||
# Copyright (c) 2013, Valentin Lorentz
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
"""
|
||||
Add a description of the plugin (to be presented to the user inside the wizard)
|
||||
here. This should describe *what* the plugin does.
|
||||
"""
|
||||
|
||||
import supybot
|
||||
import supybot.world as world
|
||||
|
||||
# Use this for the version of this plugin. You may wish to put a CVS keyword
|
||||
# in here if you're keeping the plugin in CVS or some similar system.
|
||||
__version__ = ""
|
||||
|
||||
# XXX Replace this with an appropriate author or supybot.Author instance.
|
||||
__author__ = supybot.authors.unknown
|
||||
|
||||
# This is a dictionary mapping supybot.Author instances to lists of
|
||||
# contributions.
|
||||
__contributors__ = {}
|
||||
|
||||
# This is a url where the most recent plugin package can be downloaded.
|
||||
__url__ = '' # 'http://supybot.com/Members/yourname/OEIS/download'
|
||||
|
||||
from . import config
|
||||
from . import plugin
|
||||
from imp import reload
|
||||
# In case we're being reloaded.
|
||||
reload(config)
|
||||
reload(plugin)
|
||||
# Add more reloads here if you add third-party modules and want them to be
|
||||
# reloaded when this plugin is reloaded. Don't forget to import them as well!
|
||||
|
||||
if world.testing:
|
||||
from . import test
|
||||
|
||||
Class = plugin.Class
|
||||
configure = config.configure
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1,56 @@
|
|||
###
|
||||
# Copyright (c) 2013, Valentin Lorentz
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
import supybot.conf as conf
|
||||
import supybot.registry as registry
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('OEIS')
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x:x
|
||||
|
||||
def configure(advanced):
|
||||
# This will be called by supybot to configure this module. advanced is
|
||||
# a bool that specifies whether the user identified himself as an advanced
|
||||
# user or not. You should effect your configuration by manipulating the
|
||||
# registry as appropriate.
|
||||
from supybot.questions import expect, anything, something, yn
|
||||
conf.registerPlugin('OEIS', True)
|
||||
|
||||
|
||||
OEIS = conf.registerPlugin('OEIS')
|
||||
# This is where your configuration variables (if any) should go. For example:
|
||||
# conf.registerGlobalValue(OEIS, 'someConfigVariableName',
|
||||
# registry.Boolean(False, _("""Help for someConfigVariableName.""")))
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1 @@
|
|||
# Stub so local is a module, used for third-party modules
|
|
@ -0,0 +1,113 @@
|
|||
# Author: Valentin Lorentz
|
||||
# CC-0 license.
|
||||
#
|
||||
# Note that you have to follow OEIS' license.
|
||||
|
||||
import re
|
||||
import logging
|
||||
|
||||
class InvalidEntry(Exception):
|
||||
pass
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
class OEISEntry(dict):
|
||||
_assignments = {
|
||||
'A': 'author',
|
||||
'E': 'references',
|
||||
'O': 'offset',
|
||||
}
|
||||
_appendings = {
|
||||
'C': 'comments',
|
||||
'D': 'detreferences',
|
||||
'F': 'formula',
|
||||
'H': 'references',
|
||||
'e': 'examples',
|
||||
}
|
||||
_concatenations = {
|
||||
'p': 'maple',
|
||||
't': 'mathematica',
|
||||
'o': 'programming',
|
||||
}
|
||||
def __init__(self, fd, logger=None):
|
||||
self._logger = logger
|
||||
for key in ('sequence', 'signed'):
|
||||
self[key] = []
|
||||
for key in self._appendings.values():
|
||||
self[key] = []
|
||||
for key in self._concatenations.values():
|
||||
self[key] = ''
|
||||
for line in fd:
|
||||
line = line[0:-1]
|
||||
if not line:
|
||||
break
|
||||
if line.startswith('#'):
|
||||
continue
|
||||
try:
|
||||
(mode, id_, data) = line.split(' ', 2)
|
||||
except ValueError:
|
||||
(mode, id_) = line.split(' ', 1)
|
||||
data = None
|
||||
self['id'] = id_
|
||||
self._add(mode[1:], data)
|
||||
if not self['sequence']:
|
||||
raise InvalidEntry()
|
||||
for key in self._appendings.values():
|
||||
if not self[key]:
|
||||
del self[key]
|
||||
for key in self._concatenations.values():
|
||||
if not self[key]:
|
||||
del self[key]
|
||||
|
||||
def _add(self, mode, data):
|
||||
if mode in self._assignments:
|
||||
self[self._assignments[mode]] = data
|
||||
elif mode in self._appendings:
|
||||
self[self._appendings[mode]].append(data)
|
||||
elif mode in self._concatenations:
|
||||
self[self._concatenations[mode]] += data
|
||||
elif mode == 'I':
|
||||
self['ids'] = data.split(' ') if data else None
|
||||
elif mode == 'K':
|
||||
self['keywords'] = data.split(',')
|
||||
elif mode == 'N':
|
||||
assert 'name' not in self
|
||||
self['name'] = data
|
||||
elif mode in 'STU':
|
||||
self['sequence'].extend([int(x) for x in data.split(',') if x])
|
||||
elif mode in 'VWX':
|
||||
self['signed'].extend([int(x) for x in data.split(',') if x])
|
||||
elif mode == 'Y':
|
||||
self['seealso'] = (data[len('Cf. '):-1]).split(', ')
|
||||
elif self._logger:
|
||||
self._logger.info('Unknown OEIS data mode: %s: %s' % (mode, data))
|
||||
|
||||
|
||||
|
||||
_paging_regexp = re.compile('Showing ([0-9]+)-([0-9]+) of ([0-9]+)')
|
||||
|
||||
@classmethod
|
||||
def query(cls, fd, logger=None):
|
||||
"""Fetches a page from the OEIS.
|
||||
|
||||
Return format: ((from, to, total), [results])"""
|
||||
paging = None
|
||||
for line in fd:
|
||||
line = line[0:-1]
|
||||
if line.startswith('No results.'):
|
||||
return ((0, 0, 0), [])
|
||||
if line.startswith('Showing '):
|
||||
match = cls._paging_regexp.match(line)
|
||||
paging = match.groups()
|
||||
break
|
||||
if not paging:
|
||||
raise ParseError
|
||||
fd.readline()
|
||||
results = []
|
||||
try:
|
||||
while True:
|
||||
results.append(cls(fd, logger))
|
||||
except InvalidEntry:
|
||||
pass
|
||||
return (paging, results)
|
|
@ -0,0 +1,85 @@
|
|||
###
|
||||
# Copyright (c) 2013, Valentin Lorentz
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
import supybot.log as log
|
||||
import supybot.utils as utils
|
||||
from supybot.commands import *
|
||||
import supybot.plugins as plugins
|
||||
import supybot.ircutils as ircutils
|
||||
import supybot.callbacks as callbacks
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('OEIS')
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x:x
|
||||
|
||||
from .oeis import OEISEntry, ParseError
|
||||
|
||||
def query(logger, q):
|
||||
return OEISEntry.query(logger=logger,
|
||||
fd=utils.web.getUrlFd('http://oeis.org/search?fmt=text&q=%s' % q))
|
||||
|
||||
class OEIS(callbacks.Plugin):
|
||||
"""Add the help for "@plugin help OEIS" here
|
||||
This should describe *how* to use this plugin."""
|
||||
threaded = True
|
||||
|
||||
def _advsearch(self, irc, msg, args, format_, sequence):
|
||||
"""<format> <sequence>
|
||||
|
||||
Search with advanced formating options (Python dict-formating)."""
|
||||
try:
|
||||
(paging, results) = query(self.log, sequence)
|
||||
except ParseError:
|
||||
irc.error(_('Could not parse OEIS answer.'), Raise=True)
|
||||
if results:
|
||||
irc.reply(format('%L', map(lambda x:format_ % x, results)))
|
||||
else:
|
||||
irc.reply(_('No entry matches this sequence.'))
|
||||
advsearch = wrap(_advsearch, ['something', 'somethingWithoutSpaces'])
|
||||
|
||||
def _gen(format_, name, doc):
|
||||
def f(self, irc, msg, args, sequence):
|
||||
self._advsearch(irc, msg, args, format_, sequence)
|
||||
f.__doc__ = """<sequence>
|
||||
|
||||
%s""" % doc
|
||||
return wrap(f, ['somethingWithoutSpaces'], name=name)
|
||||
|
||||
names = _gen('%(name)s (%(id)s)', 'names',
|
||||
'Return names of matching entries.')
|
||||
|
||||
|
||||
Class = OEIS
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
|
@ -0,0 +1,46 @@
|
|||
###
|
||||
# Copyright (c) 2013, Valentin Lorentz
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
from supybot.test import *
|
||||
|
||||
class OEISTestCase(PluginTestCase):
|
||||
plugins = ('OEIS',)
|
||||
|
||||
if network:
|
||||
def testNames(self):
|
||||
self.assertRegexp('names 1,2,6,24,120', '(?i)factorial.*(A000142)')
|
||||
self.assertRegexp('names 15454454651,198448,228454456', 'No entry')
|
||||
|
||||
def testAdvsearch(self):
|
||||
self.assertRegexp('advsearch "%(id)s" 1,2,6,24,120', 'A000142')
|
||||
self.assertNotRegexp('advsearch "%(id)s" 1,2,6,24,120', 'factorial')
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
Loading…
Reference in New Issue