[PYTHON] How to smartly define objects that are commonly used in View

Objects that are commonly used in each view of the Pyramid app can be defined smartly by using the Root Object mechanism.

class RootResource(object):
    u"""Application Root Resource
Since Traversal is not used, it is passed to View as context as it is.
    """

    __name__ = None
    __parent__ = None
    __acl__ = []

    def __init__(self, request):
        self._repos = {
            'user': UserRepository(),
        }

    @property
    def repos(self):
        u"""Each repository is provided via a dictionary"""
        return self._repos

If you pass it to the root_factory argument in the application definition,

config = Configurator(
    settings=settings,
    root_factory="foo.resource.RootResource"
)

You can access repos from view as follows:

@view_config(route_name="user_detail")
def user_detail(context, request):
    user_repo = context.repos['user']
    user = user_repo.getById(...)
    return ...

reference

-Resources --The Pyramid Web Application Framework

Recommended Posts

How to smartly define objects that are commonly used in View
How to implement Scroll View in pythonista 1
How to view images in Django's Admin
How to scrape pages that are “Access Denied” in Selenium + Headless Chrome
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to define your own target in Sage
How to get multiple model objects randomly in Django
How to define Decorator and Decomaker in one function
plotly trace and layout templates that are likely to be used in scatter plots
How to use bootstrap in Django generic class view
How to upload files in Django generic class view
[Linux] How to monitor logs that are constantly added
How to write type hints for variables that are assigned multiple times in one line
How to test that Exception is raised in python unittest
How to define multiple variables in a python for statement
Try to extract the keywords that are popular in COTOHA
Summary of how to write .proto files used in gRPC
How to define Go variables
How to develop in Python
Regular expressions that are easy and solid to learn in Python
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to judge that the cross key is input in Python3
Upload and manage packages that are not in conda to anaconda.org
I tried to summarize the methods that are often used when implementing basic algo in Quantx Factory
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to use SQLite in Python
How to convert 0.5 to 1056964608 in one shot
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
How to get a namespaced view name from a URL (path_info) in Django
How to write a string when there are multiple lines in python
How to hide warnings that do not affect execution in Jupyter Notebook