diff options
| author | Michael Merickel <github@m.merickel.org> | 2018-08-21 01:44:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-21 01:44:55 -0500 |
| commit | 820a752645b460ea8009b07a75c752ab09c53dec (patch) | |
| tree | 14616d81a7abedfe523c057e71ed7b2aca680754 /docs/designdefense.rst | |
| parent | df8598a2658632f709a64e8076cce02ca49de8e6 (diff) | |
| parent | 254710cb716dccfe536b20d077e3cb79be19c6b3 (diff) | |
| download | pyramid-820a752645b460ea8009b07a75c752ab09c53dec.tar.gz pyramid-820a752645b460ea8009b07a75c752ab09c53dec.tar.bz2 pyramid-820a752645b460ea8009b07a75c752ab09c53dec.zip | |
Merge pull request #3330 from stevepiercy/docs-code-style
Docs code style
Diffstat (limited to 'docs/designdefense.rst')
| -rw-r--r-- | docs/designdefense.rst | 122 |
1 files changed, 61 insertions, 61 deletions
diff --git a/docs/designdefense.rst b/docs/designdefense.rst index 2976dcfd5..59a064607 100644 --- a/docs/designdefense.rst +++ b/docs/designdefense.rst @@ -117,11 +117,11 @@ reading the code that performs a typical "unnamed utility" lookup using the :func:`zope.component.getUtility` global API: .. code-block:: python - :linenos: + :linenos: - from pyramid.interfaces import ISettings - from zope.component import getUtility - settings = getUtility(ISettings) + from pyramid.interfaces import ISettings + from zope.component import getUtility + settings = getUtility(ISettings) After this code runs, ``settings`` will be a Python dictionary. But it's unlikely that any civilian would know that just by reading the code. There @@ -186,28 +186,28 @@ present in the current request or ``None`` if no userid is present in the current request. The application developer calls it like so: .. code-block:: python - :linenos: + :linenos: - from pyramid.security import authenticated_userid - userid = authenticated_userid(request) + from pyramid.security import authenticated_userid + userid = authenticated_userid(request) They now have the current user id. Under its hood however, the implementation of ``authenticated_userid`` is this: .. code-block:: python - :linenos: + :linenos: - def authenticated_userid(request): - """ Return the userid of the currently authenticated user or - ``None`` if there is no authentication policy in effect or there - is no currently authenticated user. """ + def authenticated_userid(request): + """ Return the userid of the currently authenticated user or + ``None`` if there is no authentication policy in effect or there + is no currently authenticated user. """ - registry = request.registry # the ZCA component registry - policy = registry.queryUtility(IAuthenticationPolicy) - if policy is None: - return None - return policy.authenticated_userid(request) + registry = request.registry # the ZCA component registry + policy = registry.queryUtility(IAuthenticationPolicy) + if policy is None: + return None + return policy.authenticated_userid(request) Using such wrappers, we strive to always hide the ZCA API from application developers. Application developers should just never know about the ZCA API; @@ -263,21 +263,21 @@ In all core code, we've made use of ZCA global API functions, such as instead of the rule. So instead of: .. code-block:: python - :linenos: + :linenos: - from pyramid.interfaces import IAuthenticationPolicy - from zope.component import getUtility - policy = getUtility(IAuthenticationPolicy) + from pyramid.interfaces import IAuthenticationPolicy + from zope.component import getUtility + policy = getUtility(IAuthenticationPolicy) :app:`Pyramid` code will usually do: .. code-block:: python - :linenos: + :linenos: - from pyramid.interfaces import IAuthenticationPolicy - from pyramid.threadlocal import get_current_registry - registry = get_current_registry() - policy = registry.getUtility(IAuthenticationPolicy) + from pyramid.interfaces import IAuthenticationPolicy + from pyramid.threadlocal import get_current_registry + registry = get_current_registry() + policy = registry.getUtility(IAuthenticationPolicy) While the latter is more verbose, it also arguably makes it more obvious what's going on. All of the :app:`Pyramid` core code uses this pattern rather than @@ -483,20 +483,20 @@ accept positional arguments which match information in an associated "urlconf" such as ``r'^polls/(?P<poll_id>\d+)/$``: .. code-block:: python - :linenos: + :linenos: - def aview(request, poll_id): - return HttpResponse(poll_id) + def aview(request, poll_id): + return HttpResponse(poll_id) Zope likewise allows you to add arbitrary keyword and positional arguments to any method of a resource object found via traversal: .. code-block:: python - :linenos: + :linenos: - from persistent import Persistent + from persistent import Persistent - class MyZopeObject(Persistent): + class MyZopeObject(Persistent): def aview(self, a, b, c=None): return '%s %s %c' % (a, b, c) @@ -1527,22 +1527,22 @@ inlined comments take into account what we've discussed in the :ref:`microframeworks_smaller_hello_world` section. .. code-block:: python - :linenos: + :linenos: - from wsgiref.simple_server import make_server # explicitly WSGI - from pyramid.config import Configurator # to configure app registry - from pyramid.response import Response # explicit response, no threadlocal + from wsgiref.simple_server import make_server # explicitly WSGI + from pyramid.config import Configurator # to configure app registry + from pyramid.response import Response # explicit response, no threadlocal - def hello_world(request): # accept a request; no request threadlocal reqd - # explicit response object means no response threadlocal - return Response('Hello world!') + def hello_world(request): # accept a request; no request threadlocal reqd + # explicit response object means no response threadlocal + return Response('Hello world!') - if __name__ == '__main__': - with Configurator() as config: # no global application object - config.add_view(hello_world) # explicit non-decorator registration - app = config.make_wsgi_app() # explicitly WSGI - server = make_server('0.0.0.0', 8080, app) - server.serve_forever() # explicitly WSGI + if __name__ == '__main__': + with Configurator() as config: # no global application object + config.add_view(hello_world) # explicit non-decorator registration + app = config.make_wsgi_app() # explicitly WSGI + server = make_server('0.0.0.0', 8080, app) + server.serve_forever() # explicitly WSGI Pyramid doesn't offer pluggable apps @@ -1622,10 +1622,10 @@ reads something like this: .. code-block:: text - had a quick look at pyramid ... too complex to me and not really - understand for which benefits.. I feel should consider whether it's time - for me to step back to django .. I always hated zope (useless ?) - complexity and I love simple way of thinking + had a quick look at pyramid ... too complex to me and not really + understand for which benefits.. I feel should consider whether it's time + for me to step back to django .. I always hated zope (useless ?) + complexity and I love simple way of thinking (Paraphrased from a real email, actually.) @@ -1637,21 +1637,21 @@ Too Complex If you can understand this "hello world" program, you can use Pyramid: .. code-block:: python - :linenos: + :linenos: - from wsgiref.simple_server import make_server - from pyramid.config import Configurator - from pyramid.response import Response + from wsgiref.simple_server import make_server + from pyramid.config import Configurator + from pyramid.response import Response - def hello_world(request): - return Response('Hello world!') + def hello_world(request): + return Response('Hello world!') - if __name__ == '__main__': - with Configurator() as config: - config.add_view(hello_world) - app = config.make_wsgi_app() - server = make_server('0.0.0.0', 8080, app) - server.serve_forever() + if __name__ == '__main__': + with Configurator() as config: + config.add_view(hello_world) + app = config.make_wsgi_app() + server = make_server('0.0.0.0', 8080, app) + server.serve_forever() Pyramid has over 1200 pages of documentation (printed), covering topics from the very basic to the most advanced. *Nothing* is left undocumented, quite |
