Giter VIP home page Giter VIP logo

hug-demo's Introduction

rest api with hug

some dependencies

preparation

  1. pyenv
  2. python3.5.2
  3. switch to python 3.5.2

how to setup

0.development

local style

  1. pyenv virtualenv 3.5.2 <project_name>
  2. pip install -r requirement
  3. cd your project
  4. cp .env.example .env
  5. run with gunicorn app:__hug_wsgi__ --reload

go with http://localhost:8000/open/hello?name=programmer

Note: maybe mysql_config not found, then fix it with sudo apt-get install libmysqlclient-dev

docker style

  1. git clone https://github.com/koolay/hug-demo.git
  2. git clone https://github.com/koolay/docker-passenger-python-hug.git
  3. follow docker-passenger-python-hug

1.production

db


insert(), update(), delete(), getOne(), getAll(), query()

insert(table, record{})

Inserts a single record into a table.

db.insert("food", {"type": "fruit", "name": "Apple", "color": "red"})
db.insert("books", {"type": "paperback", "name": "Time Machine", "price": 5.55})
update(table, row{}, condition[])

Update one more or rows based on a condition (or no condition).

# update all rows
db.update("books", {"discount": 0})

# update rows based on a simple hardcoded condition
db.update("books",
    {"discount": 10},
    ["id=1"]
)

# update rows based on a parametrized condition
db.update("books",
    {"discount": 10},
    ("id=%s AND year=%s", [id, year])
)
insertOrUpdate(table, row{}, key)

Insert a new row, or update if there is a primary key conflict.

# insert a book with id 123. if it already exists, update values
db.insert("books",
        {"id": 123, type": "paperback", "name": "Time Machine", "price": 5.55},
        "id"
)
getOne(table, fields[], condition[], order[], limit[])

getAll(table, fields[], condition[], order[], limit[])

Get a single record or multiple records from a table given a condition (or no condition). The resultant rows are returned as namedtuples. getOne() returns a single namedtuple, and getAll() returns a list of namedtuples.

book = db.getOne("books", ["id", "name"])
# get a row based on a simple hardcoded condition
book = db.getOne("books", ["name", "year"], ("id=1"))
# get a row based on a simple hardcoded condition
book = db.getOne("books", ["name", "year"], ("id=1"))
# get multiple rows based on a parametrized condition
books = db.getAll("books",
    ["id", "name"],
    ("year > %s and price < 15", [year, 12.99])
)
# get multiple rows based on a parametrized condition with an order and limit specified
books = db.getAll("books",
    ["id", "name", "year"],
    ("year > %s and price < 15", [year, 12.99]),
    ["year", "DESC"],   # ORDER BY year DESC
    [0, 10]         # LIMIT 0, 10
)
delete(table, fields[], condition[], order[], limit[])

Delete one or more records based on a condition (or no condition)

# delete all rows
db.delete("books")

# delete rows based on a condition
db.delete("books", ("price > %s AND year < %s", [25, 1999]))
query(table)

Run a raw SQL query. The MySQLdb cursor is returned.

db.query("DELETE FROM books WHERE year > 2005")

hug-demo's People

Contributors

koolay avatar mysoft-paas avatar

Stargazers

 avatar

Watchers

James Cloos 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.