Enrollment by Gender

Displays gender enrollment information from the database

This command has no input parameters, although does require student information to first be imported:

mbpy import students

Once complete, this command:

mbpy pipelines enrollment-breakdown

This the kind of table displayed:

The actual pipeline code that builds the above table is the following:

@click.command('enrollment-breakdown')
@pass_pipeline_context
def pipeline_enrollment_breakdown(ctx):
   """
   Table of active students by gender and grade
   """
   with ctx.invoker('extract_from_db', table_name='students', where=['archived', False]) as cmd:
      cmd('pivot', values=('id'), index=(['class_grade_number', 'class_grade', 'gender']), aggfunc='count')      cmd('rename', columns=paired_to_tuple(['id', 'count']))
      cmd('print', title='Breakdown by gender', caption='Actual values will vary')

The above code has a great deal of boilerplate that may make it difficult to read at first, however, the key lines are 7 and 8. If we format only those two lines, we see some patterns:

with ctx.invoker('extract_from_db',
                    table_name='students',
                    where=['archived', False]) as cmd:
      cmd(
         'pivot',
            values=('id'),
            index=(['class_grade_number', 'class_grade', 'gender']),
            aggfunc='count'
      )

Which basically says "extract students whose archived status is False, then execute a pivot table displaying count, broken down by grade and gender.

Building the pipeline from the command line

The above Python code can be represented directly through mbpy commands as well.

mbpy extract from-db --help

Thus, we can extract the students like this:

mbpy extract from-db --table-name students --where archived False shape

It outputs just the first item, with columns down the left side:

Let's check out the help for the pivot command:

mbpy extract from-db --table-name students --where archived False shape pivot --help

So we can see how to build the same command on the command line, formatted with \:

mbpy extract from-db \
       --table-name students \
       --where archived False \
              shape \
              pivot \
                     --values id \
                     --index class_grade_number \
                     --index class_grade \
                     --index gender \
              print

Last updated