python

Iterate with a tuple and multiple defaulted previous and next elements

In general pythons iterators are a nice way to iterate fast and source-code-clean over iterable. Sadly you can always only iterate over a single element. In general they are not really flexible (for example an iterator can not go back).

Sometimes you need to iterate not only with a single element but rather with a tuple of elements; therefore multiple previous and next elements to your current. There is always the problem what to do with your previous and next element at the borders or on which place of your tuple the iterator has to start and and end. Often calculations with the index are used, but this gets fast very ugly since you have maybe multiple border conditions and not every python iterable has also an index.

Automatically fill up missing values in an python `Enum`

Python 3.4 introduce the missing Enumerations, which I already used multiple times since they have some nice practical features.

In an python Enum you have an name-value pairs. Well in most cases the your Enumeration you defined contains all possible values for your scope.

But in some cases you only define those name-values from which you know that they exists. Then if you want to get the Enum object from an value like YourEnum(3) an ValueError will be thrown if 3 is not defined.

 
 
 

User login