Distributing your own package on PyPi

In Regular Expressions Demystified I developed a little python package and distributed it via PyPi.

I wanted to publish my second self-written package as well, but coming back after almost a year, some things have changed in the world of PyPi, i.e. the old tutorials aren’t working anymore.

What You have to do is the following thing in a nutshell:

python setup.py sdist
twine upload dist/*

The complete guide can be found here.

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:

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

Now you can install the library with:

pip install raven[flask]


Numpy linspace function

To create e.g. x-axis indices you can use the linspace function from numpy.

You give it a range (e.g. 0 to 10) and the number of divisions and it will distribute the values evenly across that range. The stop values is included in the resulting value array by default.


>>import numpy as np
>>np.linspace(0, 10, num=9)
>>array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. ]

Removing pyc files on server

Sometimes Python gives You a hard time when You deploy code to a server after you changed directory structures or simply moved files.
With the following command You can remove the pyc files in the working directory and subdirectories:

find . -name \*.pyc -delete

Python 3 – there shall be just int

Trying to contribute to the Flask plugin flask-login I just added these lines:

if isinstance(duration, (int, long)):
    duration = timedelta(seconds=duration)

Looking quite plausible, isn’t it? But lo and behold: it doesn’t work under Python 3.x. Dang!

The reason: Python 2 has two integer types: int and long. In Python 3 there is only int, which makes it necessary to distinguish between these two major versions. I’ve found a nice page which deals with this issue. Here is what You must do to make it work in both Python 2 and 3:

import sys
if sys.version_info < (3,):
    integer_types = (int, long,)
    integer_types = (int,)

isinstance(1, integer_types)


Division in Python 2 vs 3

One major change in Python 3 is the implementation of the division operator /.

In Python 2 the division yielded a floor rounded integer when dividing two integers but a float when using a float as divider or divisor. Due to Python’s weakly typed nature this behavior could lead to some issues. So PEP-238 changed the behavior of the operator in Python 3 and introduced a new operator // in Python 2.2. Let’s have a look at the different behavior

 Python 2Python 3
1 / 200.5
1.0 / 20.50.5
3 / 211.5
3 // 211
3.0 // 21.01.0

If You want to use the new behavior of operator / from Python 3 in Python 2 you can do:

from __future__ import division

So have fun when You divide and conquer 🙂

Bringing AJAX to Flask – Part 1

Flask is a micro web framework which is really fun to use. With the following snippet You have a complete web app working within seconds.

from flask import Flask # 1
app = Flask(__name__)   # 2

@app.route('/')         # 3
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()           #4

All this snippet does is

  1. Importing the Flask module,
  2. Creating the app,
  3. Defining an so called endpoint and finally
  4. Running the web app in a container.

Flask brings its own WSGI server called “werkzeug”. Please use it just for development purposes. It is not suitable for live applications. Continue reading “Bringing AJAX to Flask – Part 1”