SQLite3: Python and SQL

Everything we did in last articles was a dry run because we just used SQLFiddle. So let’s start with a real database like SQLite.

SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say:

“SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). [..] Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.”

Because Knight Industries is not Google, Amazon nor Facebook we can definitely use SQLite.

Creating and connecting to a database

In Python it is pretty easy to connect to a SQLite database:

from sqlite3 import connect
db_connection = connect('knight_industries.db')

If the file knight_industries.db does not exist, it will be created automagically. A nice little feature of the sqlite3 library.

But be careful: If You already have a database file and you mess up the path in the connect statement you will wonder why you cannot access your data, because a new file is created silently.

cursor = db_connection.cursor()
cursor.execute('''CREATE TABLE operatives (id INTEGER, name TEXT, birthday DATE)''')
cursor.execute('''INSERT INTO operatives (id, name, birthday) \
                  VALUES (1, "Michael Arthur Long", "1949-01-09")''')

db_connection.commit()
cursor.execute('''SELECT * FROM operatives''')
print cursor.fetchone()
db_connection.close()

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 ==========================

pip optional dependencies

Sometimes you want to make your python package usable for different situations, e.g. flask or bottle or django.

If You want to minimize dependencies You can use an optional dependency in setup.py:

extras_require={
 'flask': ['Flask>=0.8', 'blinker>=1.1']
}

Now you can install the library with:

pip install raven[flask]

 

My personal road map for learning data science in 2018

I got confused by all the buzzwords: data science, machine learning, deep learning, neural nets, artificial intelligence, big data, and so on and so on.

As an engineer I like to put some structure to the chaos. Inspired by Roadmap: How to Learn Machine Learning in 6 Months and Tetiana Ivanova – How to become a Data Scientist in 6 months a hacker’s approach to career planning I build my own learning road map for this year: Continue reading “My personal road map for learning data science in 2018”

Code Kata: Roman Numeral – Part 1

Christmas is over, so we get rid of the Christmas Tree.
Today I want to show You another Code Kata: Roman Numerals. The task seems to be quite easy. Write a program which converts a decimal number into a string which contains the equivalent as a roman literal. E.g. convert 1984 into MCMLXXXIV.

The requirements

We just use the characters from I to M.

Symbol I V X L C D M
Value 1 5 10 50 100 500 1,000

We first write a simple conversion function which ignores the abbreviation syntax, so instead of IX for 9 we write VIIII. To do so we use integer division and modulo operation.  We start with the highest number and work our way down. Have a look: Continue reading “Code Kata: Roman Numeral – Part 1”

Code Kata: Christmas Tree

Inspired by a blog post of Dave Thomas I started to implement my technology learning roadmap by writing little code katas.

To kill two birds with one stone I first solve a programming puzzle with my lingua franca Python. That helped me to concentrate on solving the algorithmic part of the puzzle because I don’t have to constantly worry about syntax and semantics.

Then I did the whole thing again this time in the language I want to learn: Rust

To give You an example how that works I will elaborate on the Christmas tree kata. Continue reading “Code Kata: Christmas Tree”