Giter Site home page Giter Site logo

strict-python's Introduction

Strict Python

Strict python - adds checkers on input and output for python programs.

How do we do this? Idea is easy - when you write a docstring for a python function - you need to describe all incoming arguments, and if needed - describe a structure for each argument.

It's can be useful for web development. When we have incoming request - we need to make basic data validation on incoming data from user. If data doesn't much the described structure - we need to refuse access to this page and show error.

Small example

# Assume it's Django function in views.py that handles
# user request and returns response.

@strict
def show_user(request, response_format='HTML'):
  """Shows information about the given user.

  Args:
    request:
      POST:
        {
          user_id: int. User id.
          options: optional dict. Additional options.
            {
              additional_fields: optional list of integers.
            }
          welcome_phrase: optionsl str. Any string that will be printed before user name.
        }
    response_format: str, choice from ["JSON", "HTML", "XML"]. In which format
                     we need to return data.

  Returns:
    HttpResponse. It can be JSON/HTML/XML document inside the object.
  """
  user = get_user_by_id(request.POST['user_id'],
                        additional_fields=request.POST.get('additional_fields'))
  welcome_phrase = request.POST.get('welcome_phrase')
  if not welcome_phrase:
    welcome_phrase = "Hello"
  
  data = {'welcome_phrase': welcome_phrase, 'username': user.username}
  return render_response(data, response_format)

strict-python's People

Contributors

1st avatar

Stargazers

 avatar  avatar

Watchers

 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.