Category Archives: Python

Methods for Both Classes and Instances in Python

0.00 avg. rating (0% score) - 0 votes

I came across a situation where it would be nice for a Python class to have a method which works for both the class and its instances, and when called in an instance context, to know on which instance it was invoked. Python does not support this directly, but as it is a very flexible language, it can be done. To make it work somewhat nicely requires just a few lines of not so obvious code.

Continue reading Methods for Both Classes and Instances in Python

Enable vi editing mode in IPython 5

0.00 avg. rating (0% score) - 0 votes

Create default profile and set editing mode to ‘vi’:

 

Flask-SQLAlchemy and PostgreSQL Unit Testing with Transaction Savepoints

5.00 avg. rating (95% score) - 1 vote

PostgreSQL provides us two very powerful features which are helpful with unit testing: transactional DDL and transaction savepoints. In this article I will show you how to use those with Flask-SQLAlchemy unit tests.

Transactional DDL means you can create tables inside a transaction, run tests against them, and roll back your changes after you are done. The database will be left in the same state as it was when you started. If you started with an empty database, nothing will be left in the database afterwards.

Savepoints allow you to roll back a part of a transaction without affecting what has happened before that savepoint was created. You can run a single test inside a savepoint and roll back just the changes that single test made without affecting the changes your set-up code has done before the savepoint.

That means you can create a large number of tables and other database objects in the beginning of your test suite and then run individual tests inside nested transaction using savepoints. There is no need to drop and re-create the whole database schema for each test. Continue reading Flask-SQLAlchemy and PostgreSQL Unit Testing with Transaction Savepoints

SQLAlchemy Declarative Class Reflector

0.00 avg. rating (0% score) - 0 votes

SQLAlchemy has a nice reflection facility which takes for example a database table name as argument and produces a Table object out of it for manipulation. However, those objects do not behave like the objects produced by declarative classes, which are easier to work with. Here’s a little class that helps to bridge that gap by reflecting proper declarative classes from database tables. It has only been tested with PostgreSQL, but it may work with other databases as well. Continue reading SQLAlchemy Declarative Class Reflector

Sonera CStream Messaging Web Service API with Python and SUDS

0.00 avg. rating (0% score) - 0 votes

In this article I will show you how to use the Sonera CStream Messaging Web Service API to send an SMS using Python, and a library called  SUDS. The CStream API is two-way service for both sending and receiving messages. You obviously need to pay for the service to get access. After you have your credentials, you can start using the service.

The SUDS is a lightweight SOAP Python client for exploring and using web services. A recent version can be installed on Debian based distros with “sudo apt-get install python-suds”, or on almost anything with “pip install suds”. Continue reading Sonera CStream Messaging Web Service API with Python and SUDS