Recap of the April Pinax Hangout
The purpose of the Pinax Hangouts is to engage more with the Pinax community. We want to let you know what we are working on, answer your questions, and present demos of old and new apps and starter projects. Our host this month was Patrick Altman. Patrick talked about pinax-types.
pinax-types
is a collection of utilities. In our Hangout, Patrick covered periods and other utilities for working with ranges of quarters, weeks, months, years, etc., and showed how pinax-types
can be helpful for quickly finding range updates, for example.
Code + Demo
Patrick built a demo app called mytweets
to show how pinax-types
can be used to manage period utilities. The base for Patrick’s app is Pinax Project Zero. The sample data we are using is imported tweets from Patrick’s Twitter account.
Instead of doing the date math yourself, you can use pinax-types
when you have time series data bounds and want to query something other than specific dates you know. Let’s take a look at Q3 of 2014 for example. In a limited queryset for the third quarter of 2014 we would have to do a lot of date math and find the beginning and end dates. This is not necessarily difficult but you would still have to write a few lines of code to do so. With the utilities and period types that pinax-types
provides it’s as simple as passing in a string like Q-2014-3
into a function and you get a start and end date.
The format is very specific, intentional, and lexicographically sorts strings if you are storing periods in the database.
If you look at the views.py file of the mytweets
app, you can see on line 3 that we are importing the get_period
function and passing this period into the query string, which the Q-2014-3
string in the URL shows. Our code gets a start and end date which will return a tuple, which the range takes. This is a very simple way to quickly get ranges.
You can see in the pinax-types/pinax/types/periods/init.py file of pinax-types
that the code itself is mainly a series of functions, period types and sub periods. Starting on line 163 we can see the weekly period. This allows for us to take an object, for example, and ask a quarter period. We can also ask if that week is in the quarter, etc.
Starting on line 354 you can see the different prefixes. Within a certain period type these will all lexicographically sort so you can order them according to what works best for storing them in the database.
The parse
function on line 381 is a wrapper around some logic to parse out various formats to get to what the rest of the code uses.
pinax-types
also provides a field for storing period types in the database. If you are building something that is taking quarterly status reports for example and you want to store which quarter it is on, you can use this field and it will store and translate it.
You can also take values like 2015Q1
and it will store and translate this value in the database to 2015-Q-1
. When it hydrates in the model you won’t have a string, instead you will have a quarterly period object which has all the methods in it to give you ranges to check if weeks are inside it, etc.
It’s our goal to keep our Hangouts quick and focussed from now on.
If you have questions about this Hangout, questions about pinax-types
or Pinax in general, please join or Pinax Slack team and ping us there. We look forward to chatting with you
If you missed our April Pinax Hangout, you can watch the video here
Our next Pinax Hangout will take place on Thursday, May 19th, at noon Eastern time. The topic will be announced soon. We hope that you will join us for our May Pinax Hangout!