SQL-Basics: Relations

As the Junior Data Scientist of Knight Industries we created a table to keep track of all our operatives: SQL-Basics: Create, Read, Update, Delete

Devon asks us to keep track of our operations aka missions as well. For the first implementation let us assume that a mission has one operative and an operative can participate in multiple missions. that’s what we call an 1 to many relationship.

  +------------+                 +----------+
  |            |1   active in   n|          |
  | operative  +-----------------+ missions |
  |            |                 |          |
  +------------+                 +----------+

Missions have an id, a code name like “Phantom Liberty”, an operative id and a total cost. Continue reading “SQL-Basics: Relations”

SQL-Functions

In SQL-Basics

we learned the standard SQL statements to create a table, insert data into it, retrieving data from tables and altering data. Now we want to do even

Devon is a data driven man and to evaluate our mission, he likes to know from us:

    • how many missions we finished
    • the total cost of all missions
    • the mission with the lowest cost
    • the mission with the highest cost
    • the average mission cost
    • the five most expensive missions

 

SELECT COUNT(*) FROM missions;
SELECT SUM(total_cost) FROM missions;
SELECT MIN(total_cost) FROM missions;
SELECT MAX(total_cost) FROM missions;
SELECT AVG(total_cost) FROM missions;
SELECT * FROM missions ORDER BY total_cost DESC LIMIT 5;

Distributing your own package on PyPi – Part 2

In Distributing your own package on PyPi I wrote about my first package on PyPI. Here are some refinements aka lessons learned:

Project Description on PyPI

I wondered why the project description on PyPi was empty. Solution: You need a long_description. If You already have a README.md, you can read it into a string and use this as the description.

But you have to add long_description_content_type=’text/markdown’ as well.

from setuptools import setup

# read the contents of your README file
from os import path
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='flask_url_mapping',
    version='0.6',
    packages=['flask_url_mapping'],
    url='https://github.com/jboegeholz/flaskurls',
    download_url='https://github.com/jboegeholz/flaskurls/archive/0.2.tar.gz',
    license='MIT',
    author='Joern Boegeholz',
    author_email='boegeholz.joern@gmail.com',
    description='Django-style URL handling for Flask',
    long_description=long_description,
    long_description_content_type='text/markdown',
    install_requires=["Flask", "Flask-Login"]
)

 

Dependencies of your Package

If your package relies on the usage of other python packages you should add them to your setup.py as well via install_requires.

setup(
    name='flask_url_mapping',
    version='0.6',
    packages=['flask_url_mapping'],
    url='https://github.com/jboegeholz/flaskurls',
    download_url='https://github.com/jboegeholz/flaskurls/archive/0.2.tar.gz',
    license='MIT',
    author='Joern Boegeholz',
    author_email='boegeholz.joern@gmail.com',
    description='Django-style url handling for Flask',
    install_requires=["Flask", "Flask-Login"]
)

Introduction to matplotlib – Part 2

When you finished reading part 1 of the introduction you might have wondered how to draw more than one line or curve into on plot. I will show you now.

To make it a bit more interesting we generate two functions: sine and cosine. We generate our x-values with numpy’s linspace function

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi)

sin = np.sin(x)
cos = np.cos(x)

plt.plot(x, sin, color='b')
plt.plot(x, cos, color='r')
plt.show()

You can plot two or more curves by repeatedly calling the plot method.

That’s fine as long as the individual plots share the same axis-description and values.

Subplots

fig = plt.figure()
p1 = fig.add_subplot(2, 1, 1)
p2 = fig.add_subplot(2, 1, 2)
p1.plot(x, sin, c='b')
p2.plot(x, cos, c='r'

The add_subplot method allows us to put many plots into one “parent” plot aka figure. The arguments are (number_of_rows, number_of_columns, place in the matrix) So in this example we have 2 rows in 1 column, sine is in first, cosine in second position:

when you have a 2 by 2 matrix it is counted from columns to row

fig = plt.figure()
p1 = fig.add_subplot(221)
p2 = fig.add_subplot(222)
p3 = fig.add_subplot(223)
p4 = fig.add_subplot(224)
p1.plot(x, sin, c='b')
p2.plot(x, cos, c='r')
p3.plot(x, -sin, c='g')
p4.plot(x, -cos, c='y')

The code is available as a Jupyter Notebook on my github

Checking test coverage with pytest-cov

Test coverage

I wanted to analyze my python package flaskurls for test coverage. this is how you can do it:

pipenv install pytest-cov
py.test --cov=flask_url_mapping tests/
----------- coverage: platform win32, python 3.6.5-final-0 -----------
Name                              Stmts   Miss  Cover
-----------------------------------------------------
flask_url_mapping\__init__.py         1      0   100%
flask_url_mapping\flask_urls.py      73      0   100%
-----------------------------------------------------
TOTAL                                74      0   100%


========================== 10 passed in 0.60 seconds ==========================