Code Monkey home page Code Monkey logo

stupidparse's Introduction

StupidParse

Kind of stupid name, eh?

Tired of parsing things stupidly with long lists of if, elif and finally and else. Wish Python had a switch statement? Well, maybe not a switch statement.

Welcome to StupidParse

Here's a quick example of how easy it is:

    >>> from StupidParse import StupidParser
    >>> p = StupidParser(corpus="The Knights Who Say Ni!")
    >>> p.register_rule(key=True, trigger=lambda x: "Ni!" in x, callback="It!")
    >>> p.run()
    ... "It!"

A more realistic example would be:

    from StupidParse import StupidParser
    p = StupidParser(corpus="The Knights Who Say Ni!")
    p.register_rule(key=True, trigger=lambda x: "Ni!" in x, callback=MyCallback)
    action = p.run()

API

StupidParse has a stupid simple API and it's stupid simple to expand on.

###StupidParser

  • StupidParser accepts a corpus upon creation, though submitting on is optional. It defaults to None. It also accepts a default value to return which also defaults (defaultception) to None.
  • StupidParser.run also accepts a corpus, which is also optional.
  • StupidParser.run calls each rule registered to it (through their run method) sequentially and returns the first rule that's triggered. Otherwise it returns StupidParser.default.
  • Rules are registered with (surprise) StupidParser.register_rule. This is essentially a factory method. Key, Trigger and Callback are all required arguements. There's also the rule arguement if you want to pass a different rule type to register, but defaults to the basic StupidParse.StupidRule.

###StupidRule

  • StupidRule accepts a key, a trigger and a callback upon creation, none of these are optional.
  • StupidRule.trigger must be callable and return a value comparable to StupidRule.key. It also only accepts a corpus as it's only input (see below though)
  • StupidRule.run returns StupidRule.callback if the trigger return value matches the key. Callback can be anything. :)

###StupidParseError This is currently not implemented. But is included for expansion's sake. Feel free to raise and catch them as you please when using StupidParse.

Other Examples

###Getting StupidRule.trigger to accept multiple inputs. You though the trigger could only accept one value? That's true when you register it. But what if you want to run a regex on the corpus. Yeah, you could compile it. But if you're using it once, that's just more overhead. You could use a factory:

    >>> import re
    >>> def regex_trigger_factory(pattern):
    ...     def trigger(corpus):
    ...         return re.match(pattern, corpus)
    ...     return trigger

Or functools:

    >>> import re
    >>> from functools import partial
    >>> trigger = partial(re.match, "\bNi!\b")

Or use the __call__ method on a class:

    class MyTrigger(object):
        
        def __init__(self, regex="\bNi!\b"):
            self.regex = regex

        def __call__(self, corpus):
            return re.match(self.regex, corpus)

The possibilities are endless.

###Expanding StupidParser

Defining a custom custom parser is easy:

    class MyParser(StupidParser):
        pass

If you wanted to return all Rules that match your corpus, a simple modification to StupidParser.run is needed:

    class AllRulesParser(StupidParser):
        
        def run(corpus=None):
            corpus = self.corpus or corpus
            rules = []
            for rule in self._rules:
                if  rule.run(corpus)
                    rules.append(rule.callback)

             return rules or self.default

###Expanding StupidRule

If you want to define a custom Rule type, it's easy.

    class MyRule(StupidRule):
        pass

Let's say you wanted to autoload the corpus into the callback:

    class AutoloadCorpusRule(StupidRule):
        
        def run(corpus):
            if self.key == self.trigger(corpus):
                self.callback = callback(corpus)
                return True
            return False

You'd register it like this then:

    from StupidParse import StupidParser
    from rules import AutoloadCorpusRule
    
    def mycallback(corpus):
        print corpus

    p = StupidParser(corpus="The Knights Who Say Ni!", default=None)
    p.register_rule(key=True, trigger=lambda x: "Ni!" in corpus, callback=mycallback, rule=AutoloadCorpusRule)
    p.run()

stupidparse's People

Contributors

justanr avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.