LazyProxy in Python

Posted by Michał ‘mina86’ Nazarewicz on 8th of July 2012

Paths of destiny lead mysterious ways. Not so long ago, I was a hard-core C hacker and now, I spend a lot of the time coding in Python.

In somehow related news, I have discovered that my search-foo is not good enough, when I was unable to find a decent implementations of several design patterns in Python.

What I needed was a generic proxy that would defer initialisation of an object to the moment it is first used. Here is what I came up with:

class LazyProxy(object):
    __slots__ = '__get'

    def __init__(self, cls, *args, **kw):
        object.__setattr__(self, '_LazyProxy__get',
                           lambda: self.__set(cls(*args, **kw)))

    def __set(self, obj):
        object.__setattr__(self, '_LazyProxy__get', lambda: obj)
        return obj

    def __getattr__(self, name):
        return getattr(self.__get(), name)

    def __setattr__(self, name, value):
        return setattr(self.__get(), name, value)

    def __delattr__(self, name):
        return delattr(self.__get(), name)

Deep Dive into Contiguous Memory Allocator

Posted by Michał ‘mina86’ Nazarewicz on 10th of June 2012

This is an extended version of an LWN article on CMA. It contains more detail on how to use CMA and a lot of boring code samples.

Contiguous Memory Allocator (or CMA) has been developed to allow big physically contiguous memory allocations. By initialising early at boot time and with some fairly intrusive changes to Linux memory management, it is able to allocate big memory chunks without a need to grab memory for exclusive use.

Simple in principle, it grew to be a quite complicated system which requires cooperation between boot-time allocator, buddy system, DMA subsystem, and some architecture-specific code. Still, all that complexity is usually hidden away and normal users won’t be exposed to it. Depending on perspective, CMA appears slightly different and there are different things to be done and look for.

Null: The never-ending story

Posted by Michał ‘mina86’ Nazarewicz on 27th of March 2011

I have already mentioned some problems with the null pointer but my recent discovery knocked my socks off.

By now, it should come with no surprise to anyone that 0 in pointer context acts as a null pointer (no matter of its actual representation). Moreover, it takes only a tiny bit of experimenting to figure out that expressions like (int)0 do as well. The latter is in itself a bit of a pita but it is conforming to the C++ standard which says:

Prime numbers less than 100

Posted by Michał ‘mina86’ Nazarewicz on 12th of December 2010

Anyone working in a big corporation must have been hit by some ‘funny’ mail from a coworker that helps everyone gets through the day. No different at my office — at one point all engineers have been challenged to write the shortest code in C that prints all prime numbers (and only prime numbers) less than a hundred each on separate line.

This is an interesting brain-teaser so posting it here so others may choose to think about it while their code’s compiling.

Of course, a ‘C program’ needs not to be taken too seriously — depending on not too far fetched undefined behaviours of given implementation is all right (but please do not use system or exec family of calls; not that I can see how that would help).

By the way, if you’re interested in how this challenge looks solved in Rust, I’ve described that as well.

0 is ambiguous

Posted by Michał ‘mina86’ Nazarewicz on 24th of October 2010

It has been a long time since my last entry, so inspired by Adriaan de Groot’s entry, I decided to write something about 0, NULL and upcoming nullptr.

I will try to be informative and explain what the whole buzz is about and then give my opinion about nullptr. Let us first inspect how a null pointer can be donated in C and C++.

Python, wrażenia

Posted by Michał ‘mina86’ Nazarewicz on 31st of January 2010

Niedawno miałem okazję stworzyć trochę kodu w Pythonie. Popisałem sobie w nim kilka dni i muszę przyznać… język jest tragiczny, zastanowię się kilka razy zanim po raz kolejny go użyję.

Wpis ten dedykuję pewnej osobie, która ciągle mi przypomina, abym pisał nowe wpisy. :)


Posted by Michał ‘mina86’ Nazarewicz on 16th of January 2010

Sitting in a dark office, after swearing for hours at ATI video cards I noticed time on my computer was incorrect. ‘No problem,’ I thought as I started typing ntpdate. That’s where it struck me that our beloved IT department had blocked most of the Internet. Checking the time on a watch or a mobile phone was not an option — I had neither — nor was looking at GKrellM on another PC — that’s just lame.

‘I wish there was a NTP-over-HTTP protocol’ I sighed. And then I realised there was…

Total Control Exchange (No Caps Lock)

Posted by Michał ‘mina86’ Nazarewicz on 11th of October 2009

Have you ever wondered why Caps Lock, a key you press twice a decade (once to turn it on and then to turn it off), is in such a lucrative position on the keyboard? And how about Ctrl? Compared to Caps Lock it seems like miles away. If you think about it old unix keyboards with those keys swapped seem to got it right.

I’ve gone one step further and recommend turning Caps Lock into Ctrl key altogether. In this article I will describe how to do that in various systems and platforms. But lets start with some propaganda.

Treasure Hunt w Prologu

Posted by Michał ‘mina86’ Nazarewicz on 17th of January 2009

W poprzednim wpisie pisałem o Google Treasure Hunt, a w szczególności o zadaniu czwartym. Teraz, w ramach przygotowań do kolokwióm z języka Prolog postanowiłem napisać w nim rozwiązanie postawionego problemu:

Treasure Hunt a szybkość D

Posted by Michał ‘mina86’ Nazarewicz on 10th of January 2009

Zachęcony wpisem GiMa postanowiłem pobawić się w Google Treasure Hunt. Z początku moje wrażenia były negatywne, ale to dlatego, że zacząłem od zadania sieci, które jest co najmniej denne. Archiwum też nie napawało optymizmem, już zacząłem się zastanawiać, czy wszystkie będą tak prymitywne — na szczęście nie były.