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.
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]
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. ]
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
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:
if sys.version_info < (3,):
integer_types = (int, long,)
integer_types = (int,)
I’ve always had a hard time with regular expressions: I know that they are useful, but I use them so rarely that I cannot get a hold of all the syntax.
So, now is the time to write an article for myself to remember all the stuff. Continue reading “Regular Expressions Demystified – A Mini DSL for Regex in Python”
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 2||Python 3
|1 / 2||0||0.5
|1.0 / 2||0.5||0.5
|3 / 2||1||1.5
|3 // 2||1||1
|3.0 // 2||1.0||1.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 🙂
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
return 'Hello World!'
if __name__ == '__main__':
All this snippet does is
- Importing the Flask module,
- Creating the app,
- Defining an so called endpoint and finally
- 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”