Skip to content

Instantly share code, notes, and snippets.

@florentx
florentx / util.py
Created February 6, 2016 17:57
Finalizer objects for Python 2 and Python <= 3.3
"""Backport weakref.finalize from Python 3.5, simplified."""
import atexit
import sys
import weakref
class finalize(object):
"""Class for finalization of weakrefable objects
@florentx
florentx / checksums.py
Last active August 29, 2015 14:17
Validate IBAN or Post Account
POST_TABLE = (0, 9, 4, 6, 8, 2, 7, 1, 3, 5)
IBAN_TABLE = dict([(c, str(idx))
for (idx, c) in enumerate('0123456789abcdefgh'
'ijklmnopqrstuvwxyz')])
def validate_postaccount(value):
"""Check the Post Account."""
if not value:
raise ValueError('Post Account cannot be empty')
@florentx
florentx / better_zip.py
Last active April 26, 2017 07:22
CSV import alternative - (partial implementation) - Odoo 6.1
# -*- coding: utf-8 -*-
# Example of CSV import usage
from openerp import SUPERUSER_ID
from openerp.osv.orm import Model, fields
from openerp.addons.my_base.openerp_tools import convert_csv_import
class BetterZip(Model):
"""Zip object."""
@florentx
florentx / pgutil.py
Created April 16, 2014 13:28
OpenERP - create missing indexes on Foreign Keys
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import contextlib
__all__ = ['savepoint', 'create_missing_indexes']
@contextlib.contextmanager
def savepoint(cr, name, quiet=False):
@florentx
florentx / pep8q.py
Last active August 29, 2015 13:59
Run pep8 in parallel, adapted from https://github.com/jcrocholl/pep8/pull/230 by @jdahlin
#!python
# -*- coding: utf-8 -*-
# Adapted from a contribution of @jdahlin on jcrocholl/pep8/pull/230
import collections
import multiprocessing
import pep8
import re
# Helper to check if the name is a valid identifier
_isidentifier = re.compile(r'[a-zA-Z_]\w*').match
@contextlib.contextmanager
def savepoint(cr, name, quiet=False):
assert _isidentifier(name)
cr.execute('SAVEPOINT "%s";' % name)
@florentx
florentx / mycompany.css
Created January 27, 2014 15:15
OpenERP 6.1: different layout for Production and Test environments, based on hostname
/* /web_mycompany/static/src/css/mycompany.css */
/* Header */
.openerp.production .company_logo {
background: url('/web_mycompany/static/src/img/logo.png');
/* width:180px; */
/* height:46px; */
}
/* ... and similar CSS hacks ... */
@florentx
florentx / myaddon.js
Last active December 28, 2015 10:09
Use "?managedb" in the URL to reach the "Manage Database" page (for OpenERP 6.1)
openerp.web_myaddon = function(openerp) {
var hostname = window.location.hostname,
params = jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring())
if (!openerp.web_myaddon) {
/** @namespace */
openerp.web_myaddon = {};
}
class MagicList(list):
def __getitem__(self, key):
if not isinstance(key, tuple):
return list.__getitem__(self, key)
rv = []
for element in key:
value = list.__getitem__(self, element)
if isinstance(element, slice):
rv.extend(value)
@florentx
florentx / localized_strptime.py
Created October 10, 2013 11:19
localized date parsing
import dateutil.parser
from babel.core import Locale
class LocalizedParserinfo(dateutil.parser.parserinfo):
def __init__(self, locale):
locale = Locale.parse(locale)
# Build localized list of words, remove dots
self.WEEKDAYS = [(locale.days['format']['wide'][i],
locale.days['format']['abbreviated'][i].rstrip('.'))