Skip to content

Instantly share code, notes, and snippets.

View tantale's full-sized avatar

Laurent LAPORTE tantale

View GitHub Profile
@tantale
tantale / test_attrs_encoder.py
Created June 12, 2018 15:08
unit test for attrs_encoder
# coding: utf-8
"""
qronos_doc - test_attrs_encoder
=========================
:Created on: 2018-06-12
:Author: Laurent LAPORTE <llaporte@jouve.com>
"""
import datetime
import pprint
# coding: utf-8
"""
Qronos Attributes encoder/decoder
=================================
:Created on: 2018-06-12
:Author: Laurent LAPORTE <llaporte@jouve.com>
"""
import datetime
import enum
@tantale
tantale / py27_wget_demo.py
Created June 7, 2017 05:30
Simple Python 2.7 equivalent *wget* function with progress bar.
# coding: utf-8
import io
import urllib2
def get_file(src_url, dst_path, callback=None):
with io.open(dst_path, mode="wb") as dst_file:
get_stream(src_url, dst_file, callback)
@tantale
tantale / normalize_string.py
Last active May 17, 2018 11:57
Normalize a string to ASCII: convert accents to non accented characters.
import unicodedata
def normalize(string, encoding="utf-8"):
u"""
Normalize a string to ASCII: convert accents to non accented characters.
>>> normalize(u"Dès Noël où un zéphyr haï me vêt de glaçons würmiens je dîne d'exquis rôtis de bœuf au kir à l'aÿ d'âge mûr & cætera")
"Des Noel ou un zephyr hai me vet de glacons wurmiens je dine d'exquis rotis de boeuf au kir a l'ay d'age mur & caetera"
@tantale
tantale / rec_rmdir.py
Last active August 26, 2016 08:48
Reccursive rmdir with callback
# -*- coding: utf-8 -*-
import os
import stat
def prune_empty_dirs(root, callback=None, preserve=True):
"""
Prune empty directories in *root* directory, keeping it if *preserve* is ``True``.
Silently ignore errors.
@tantale
tantale / exclusive_options.py
Created April 26, 2016 19:06
Using mutually exclusive options with argparse
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
parser = argparse.ArgumentParser("my_app")
ini_level_group = parser.add_mutually_exclusive_group()
ini_level_group.add_argument("--system", dest="ini_level", action="store_const", const="system",
@tantale
tantale / detect_xml_encoding.py
Created January 31, 2016 20:28
Detect the character encoding of the XML file
import re
XML_DECL_REGEX = r"""
^<\?xml # w/o BOM, XML declaration starts with <?xml at the first byte
.+? # some chars (version info), matched minimal
encoding= # encoding attribute begins
["'] # attribute start delimiter
(?P<encstr> # what's matched in the brackets will be named encstr
[^"']+ # every character not delimiter (not overly exact!)
) # closes the brackets pair for the named group