Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Wayne Witzel III who resides in Florida and is currently working for Slashdot Media as a Senior Software Engineer building backend systems using Python, Pylons, mongodb, and solr. He can be reached at @wwitzel3.

Pyramid is a Python web framework that ships with a fantastic set of interactive command-line tools. These tools are great for exploring your application and taking an interactive tour of your applications, much in the same way you use the Python interactive shell. In a previous post I covered how to build Pyramid apps, and I then went on to post about behave, a behavior-driven development library for Python. Now, in this post I want to show you some of the Pyramid tools I find useful, and some scenarios where I like to use them.

Interactive Shell

This is probably one of the tools that I use the most with Pyramid. You start this tool with the pshell command and pass in the .ini file you want to use for the session:

Now you will be given a Python prompt that is a complete Python environment, exactly as your application would be setup if you were running it. Remember, this environment is exactly as it would be if you were running your application! Why is this important? Because if you pass the pshell say, production.ini, you will be running against production. So exercise caution when using this tool.

As you can see, we can have some things defined for us. From here we can import models and views, try out expressions, craft database queries, and perform any other interaction exploration we want. I frequently use the shell to explore legacy database tables and interactively build queries that result in the desired output. Below I show printing out a JSON representation of a model that has defined the __json__ method:

Routes, Show Them To Me

The proutes tool is a close second to the interactive shell. This will show you a summary of all of the routes for your application. Like pshell, proutes takes the ini file you wish to use:

Running this for your application will output a table that includes the name of your route, the pattern that is is matching on, and the view that it is assigned to call for that match. It looks like the following. Here is the output when run against a simple Todo application:

Making Requests

The final tool that I find really useful with Pyramid, is actually two that I frequently use together. The first is pviews. Like all of the other commands we have used thus far, it accepts the configuration file you wish to run against. pviews will display you matching routes and views for a given URL. Where proutes shows you the name, pattern, and view; pviews will actually allow you to see if a URL matches.

Now you have confirmed that the URL has a matching route and view. You can then use prequest to actually call that view and get the result. prequest does not require you to startup a server, and like all of the commands, the first argument is your configuration file.

By default, prequest sends a GET request. You can also send POST (or HEAD, PUT, or DELETE) requests using prequest with the -m switch.

As you can see, you get back the raw HTML, just as if the application was running. This is very useful for quick debugging and validation.

But Wait, There’s More!

I have shown you some tools to use with Pyramid that may be new to you. I encourage you to experiment with using them on your own projects. You can find even more useful tools and examples by checking out the Command-Line Pyramid documentation.

For more details about Python, see the Safari Books Online resources referenced below.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Learning Python, 5th Edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
Hello! Python fully covers the building blocks of Python programming and gives you a gentle introduction to more advanced topics such as object-oriented programming, functional programming, network programming, and program design. New (or nearly new) programmers will learn most of what they need to know to start using Python immediately.
Violent Python shows you how to move from a theoretical understanding of offensive computing concepts to a practical implementation. This book demonstrates how to write Python scripts to automate large-scale network attacks, extract metadata, and investigate forensic artifacts. It also shows how to write code to intercept and analyze network traffic using Python, craft and spoof wireless frames to attack wireless and Bluetooth devices, and how to data-mine popular social media websites and evade modern anti-virus.

Tags: development.ini, JSON, prequest, pshell, pviews, Pyramid, Python, Requests, tools,

Comments are closed.