] > Blog entries april 2009 [3]
The view full_list could not be found

Blog entries april 2009 [3]

iclassmethod decorator to define both a class and an instance method in one go

2009/04/28 by Sylvain Thenault

You'll find in the logilab.common.decorators module the iclassmethod decorator which may be pretty handy in some cases as it allows methods to be both called as class methods or as instance methods. In the first case the first argument will be the class and the second case it will be the instance.

Example extracted (and adapted for simplicity) from CubicWeb:

from logilab.common.decorators import iclassmethod

class Form(object):
  _fields_ = []

  def __init__(self):
      self.fields = list(self._fields_)

  @iclassmethod
  def field_by_name(cls_or_self, name):
      """return field with the given name and role"""
      if isinstance(cls_or_self, type):
          fields = cls_or_self._fields_
      else:
          fields = cls_or_self.fields
      for field in fields:
          if field.name == name:
              return field
      raise Exception('FieldNotFound: %s' % name)

Example session:

>>> from logilab.common import attrdict
>>> f = Form()
>>> f.fields.append(attrdict({'name': 'something', 'value': 1})
>>> f.field_by_name('something')
{'name': 'something', 'value': 1}
>>> Form.field_by_name('something')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 15, in field_by_name
Exception: FieldNotFound: something

So we get a field_by_name method which will act differently (actually use different input data) when called as instance method or as class method.

Also notice the attrdict trick that can also be achieved with the Python 2.6 named tuple.


12 ans de l'April: bilan et objectifs

2009/04/05 by Nicolas Chauvat
http://www.april.org/themes/zen/zen_april/images/logo.png

L'April fête cette année ses douze ans. L'association de promotion et de défense du logiciel libre approche maintenant les 5000 membres, toutes catégories confondues: personnes physiques, entreprises commerciales, collectivités, associations. Elle vient de publier son rapport moral 2008 et sa feuille de route 2014 qui fixe des objectis pour les cinq années à venir. On notera en particulier la lutte contre les quatre dangers que sont les brevets sur les algorithmes, les dispositifs de contrôle d'usage, la vente liée et l'informatique déloyale. Consultez le site de l'April pour en savoir plus sur ses actions et n'hésitez pas à adhérer ou à donner quelques heures de votre temps.


Emacs and mercurial trick

2009/04/03 by Ludovic Aubry
http://www.logilab.org/image/8863?vid=download

While using emacs I always find the need to use grep on a terminal to search for things within a specific project. This is not ideal, even within an embedded emacs shell.

Since I recently discovered the emacs command grep-find I decided to make it work nicely with mercurial projects, and here's the result:

(defun grep-project (s)
  (interactive "sSearch project for: ")
   (grep-find (concat "files=(`hg manifest`);grep -nH -e \"" s "\" ${files[@]/#/`hg root`/}"))
)
(global-set-key (quote [f4]) (quote grep-project))