Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you are working with #Cement, #Flask or other python frameworks that do not give ODM (Object Document Mapper) out of the box, you can use MongoEngine for MongoDB.
It has all the capabilities that you can almost imagine for an ODM to define schema and apply restrictions to it. You'll hear more about it later.

https://github.com/MongoEngine/mongoengine

#mongodb #mongoengine #ODM #python #ORM
Flask make_response():

Sometimes it is necessary to set additional headers in a view. Because
views do not have to return response objects but can return a value that
is converted into a response object by Flask itself, it becomes tricky to
add headers to it. This function can be called instead of using a return
and you will get a response object which you can use to attach headers.

If view looked like this and you want to add a new header::

def index():
return render_template('index.html', foo=42)

You can now do something like this::

def index():
response = make_response(render_template('index.html', foo=42))
response.headers['X-Parachutes'] = 'parachutes are cool'
return response

This function accepts the very same arguments you can return from a
view function. This for example creates a response with a 404 error
code::

response = make_response(render_template('not_found.html'), 404)

The other use case of this function is to force the return value of a
view function into a response which is helpful with view
decorators::

response = make_response(view_function())
response.headers['X-Parachutes'] = 'parachutes are cool'

Internally this function does the following things:

- if no arguments are passed, it creates a new response argument
- if one argument is passed, :meth:`flask.Flask.make_response`
is invoked with it.
- if more than one argument is passed, the arguments are passed
to the :meth:`flask.Flask.make_response` function as tuple.

#flask #make_response #helper #header #response
UWSGI - Web server Gateway Interface
-----
uwsgi is a big C application which is used to deploy python applications on server. There is a full documentation of UWSGI in readthedocs, follow it and master it to handle loads of requests concurrently and use graceful reloading of the app. It is usually put behind a full web server like nginX by proxying.

Reference: http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

#python #uwsgi #reference #flask #readthedocs
If you work with Flask and have an under heavy load API, you can setup a cache for your endpoints. One of the tools is Flask- Cache that can work with different cache backends like Memcached, Redis, Simple, etc.

To install it:
pip install Flask-Cache

To setup up:
from flask import Flask
from flask.ext.cache import Cache

app = Flask(__name__)
# Check Configuring Flask-Cache section for more details
cache_config = {
"CACHE_TYPE": "redis",
"CACHE_REDIS_HOST": "127.0.0.1",
"CACHE_REDIS_PORT": 6379,
"CACHE_REDIS_DB": 3
}
cache = Cache(app,config=cache_config})

Another redis implementation:
#: the_app/custom.py
class RedisCache(BaseCache):
def __init__(self, servers, default_timeout=500):
pass

def redis(app, config, args, kwargs):
args.append(app.config['REDIS_SERVERS'])
return RedisCache(*args, **kwargs)

Now to use the cache on a method use its decorator:
@cache.memoize(timeout=50)
def big_foo(a, b):
return a + b + random.randrange(0, 1000)

#python #flask #cache #redis #memcached
9781785881114.epub
4 MB
Python Microservices Developement

#python #ebook #epub #pdf #microservice #flask