Coder Social home page Coder Social logo

guesser's Introduction

Guesser - a smart guessing library

Build Status

In a nutshell

Guesser is a library that allows you to smartly guess values to find an unkownn boundary on a function.

For example, imagine you are given access to an API for books. Given an integer representing a book, you are able to look up the book with Book.get(7). If you try to find a book with an integer that is higher than all of the other books, it will raise a BookNotFound exception.

We want to find out how many books there are (we assume there are not 'gaps' in the ids).

The old way we would write this code:

def get_max_book_id(self):
    book_id = 1
    while True:
        try:
            Book.get(book_id)
        except BookNotFound:
            return book_id - 1
        book_id += 1

This works, but it is going to be very slow since we need to look up every single book. Here is how the code would look with Guesser:

def check_if_book_exists(book_id):
    try:
        Book.get(book_id)
    except BookNotFound:
        return False
    return True

def get_max_book_id(self):
    return guesser.guess(check_if_book_exists, start=50)

Instead of sequentially trying all of the values, Guesser will intelligently use bisection to find the maximum value.

The first argument to guesser.guess must be a function whose first parameter which is the value to be tested. This function must return True or False depending on if the condition to check is satisfied. If it returns True, guesser will guess higher numbers. If it returns False, guesser will guess lower numbers.

The smart argument is an approximate guess at what the maxium value will be. This helps to give Guesser a general idea of where it should start searching. This value is optional and can be left off though.

You can also pass additional args and kwargs to guesser.guess to be passed to the function. Say we wanted to find the max book in a given library:

def check_if_book_exists_in_library(book_id, library_id):
    try:
        Book.get(book_id)
    except BookNotFound:
        return False
    return True

def get_max_book_id(self):
    return guesser.guess(check_if_book_exists, library_id=123, start=50)

Install

$ pip install guesser

guesser's People

Contributors

spulec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.