Releasing Quadrapassel 3.8.2

After I had some Issues releasing Quadrapassel 3.8.2 which have been resolved thanks to the quick work of Andrea Veri.

Disable preview-button in difficult block mode

Disable preview-button in difficult block mode

As of now, the most current stable version of gnome Quadrapassel is 3.8.2. Here’s what the news file says:

Colin Kinloch:

  • Fixed repeated down press bug

Mario Wenzel:

  • Hiding preview embed when preview is unavailable or disabled
  • Disable preview-button in difficult block mode
  • Fix bug where falling block shadow would get missing
  • Fix segmentation fault when down key was released after game was over
Hiding preview embed when preview is unavailable or disabled

Hiding preview embed when preview is unavailable or disabled

Michael Catanzaro:

  • help: update translate  link for new module name

Andika Triwidada:

  • Updated Indonesian translation

Yuri Myasoedov:

  • Updated Russian translation

I wish to thank all contributors, especially Michael who brought me in and vouched for me.

Quadrapassel 3.8.2 should be in your repositories soon.

GNOME, I am here

I’ve checked in with the games maintainers, […], and we’d like to extend an offer to become a maintainer for Quadrapassel.

Bildschirmfoto von »2013-07-12 10:59:41«So that’s what I am now, I guess. I will keep you posted on the new stuff I’ll help getting into the quadrapassel repository.

If you want to contact me, you can write an email to maweki at gmail dotcom or you’ll usually find me hanging out in #gnome-games on the gnome irc servers.

It will take me some time to get all the bureaucracy out of the way so bear with me a bit but I’ll be triaging bugs in notime. 🙂

Mario

p.s.: I’d like to thank Michael Catanzaro for his quick and competent help.

Solving a math puzzle with Python’s generator expressions

Recently James Grime of numberphile-fame posted a puzzle on YouTube. The video is right here but the question is (if you don’t care to watch): „How many ways are there to completely fill a Noughts and Crosses (tic-tac-toe) board, with four noughts and five crosses? Not including rotations and reflections.“

So I am not much of a mathematician and I also did not really care for fopping about much so I wrote a small program in python to find the solution. And I took the chance to make a post about generator expressions in python as well.

A generator expression can take two general forms:

def gen(count):
  c = 10
  while c > 0:
    yield c
    c = c - 1

Here the yield keyword is what it’s all about. This will generate an iterable object that returns the yielded items. The cool thing is, that computation of this method is suspended until the next item is needed.

If you already have an iterable you can create a new generator expression from it:

g = (x for x in gen(10))

Here you can filter or map the value of x as you will see. And again, this is only a generator. No computation happens unless you actually want to print any values or you manifest them into a list.

you will find the source code of my solution below the fold

Weiterlesen

Constructors in Python: overwriting __new__ and the singleton pattern

About all object-oriented languages allow you to overwrite the constructor of a particular self-defined type. In Java (and C#) this looks somewhat like that:

public class A(object) {
  constructor A() {
    // Do Something
  }
}

If you think the parameters somehow merit the abortion of the object creation, you can only throw an exception. You only can do that. An object of (in this case) type A is always returned. You can’t even return a different object or an object of a subclass of A. This convention led to the factory pattern in which a static method is called to decide which specific constructor of which specific class to run. Since the method is declared as returning the supertype, it can return any subptype as well. But the factory-pattern can sometimes get out of hand.

If you get started with python you commonly see __init__(self [, args]) as the method you need to overwrite in order to get a constructor.

class A(object):
  def __init__(self):
    pass

And it is true that this is exactly what is written above.
Note: in python, methods are objects (and could be swapped any time). Therefore the own instance cannot be bound outside the method scope and it is always passed by the calling instance as the first parameter. Therefore any non-static method of a class starts with „self“ (or „s“ or whatever you want to call it) as its first parameter.

So __init__ is, as is the constructor in C# or Java, only called when the instance has already been created. So why is it called a constructor then? Historical reasons, for one. But with statically typed languages you can overload the constructor and build your instance from different parameters. A dynamically typed language like python has no method overloading. It is still good for initializing properties of the particular instance.

But there is another cool method in python which you can overwrite: __new__(C [,args]) in which the class to create an instance of is passed in C and the rest of the arguments in args. And now you can build something like this:

class A(object):
  pass

class B(object):
  def __new__(C):
    return A()

In statically typed languages it is almost always true that A() returns an object of type A. But in python, this does not need to be true. You can put your factory right there as the constructor of the superclass and it looks really neat.

Weiterlesen

php comparison explained

php is an awful language and we all know it. It has a very silly comparison algorithm for loose comparison (==) which I am going to try to explain.

If we compare variables of different types (bool, string, int, float, array, object) with each other, php tries to cast them into a common type. But some types have no „common ground“ and can never under any value assignment, be considered equal. This Venn-Diagram shows you, which stuff can be compared with each other to, in any way, get variables that are considered equal.

Weiterlesen

Python pregenerates class properties

This article is a reference because this cost me about an hour to debug:

class test(object):
foo = []

a = test()
b = test()
a.foo.append("bar")
print b.foo
#['bar']

When you define the class (test, in this case), any class properties will be instanciated on class-definition and not on object instanciation. Therefore, when you create multiple copies of a class, they will have the same actual object as class properties. You should instanciate those attributes in the init-method.

For this reason, the following code will generate output, even though it looks like it probably shouldn’t, because not a single object was created:
def f():
print "foo"

class test(object):
foo = [f()]

This will obviously not concern you, if you are – as you should – using immutable objects.

Syntactical sugar in Python: map, filter, reduce vs. the python way

This article is going to try to shed some light on how to convert map, reduce and filter (often used with lambda expressions) in python into a syntax conforming to „the python way“. At the bottom you will find a download link to execute this stuff for yourself.

l = range(10)
print l
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This is going to be the list (l) we’re working on.

What we’ll be looking at first, is iterating through this list and applying a function. And what simpler function could there be, than the identity?
a = map(lambda x: x, l)
b = list(x for x in l)
print a, b
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If you don’t really know what the we did in (a) you may want to read up on lambda expressions, a topic I am not covering now. We need the list-method in (b) because map() always returns a list and the for-expression coughs up a generator object which we have to fully evaluate and convert to a list, to get the equivalent. Generator objects I will cover in the future. For now, suffice to say, it has a reason we are applying the list-function.

Weiterlesen

The php comparison game

Recently, I had a discussion with EnnoBigBenno, one of the friends over at senaeh.de about the merits of php (or lack thereof). I believe that a good and simple programming language can easily be parsed and understood by a human. This brought me to the idea, to create a small game of „True or False“ where you have to decide, what the php-interpreter thinks, is true or false. Simple as that. Count your correct answers and check you php-prowess.

Warning: This game is not for the faint of heart. And feel free to share your score in the comments.
Weiterlesen