Skip to content

Instantly share code, notes, and snippets.

View DirkHoffmann's full-sized avatar

Dirk Hoffmann DirkHoffmann

  • CNRS/IN2P3
  • Marseille, France
View GitHub Profile
@DirkHoffmann
DirkHoffmann / udp-pkt.py
Created March 16, 2020 23:52
UDP broadcast with python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © Dirk Hoffmann -CPPM-, 2020
import socket
UDP_IP = "255.255.255.255"
UDP_PORT = 60000
msg = bytearray.fromhex('7effff0111000000000000000000000000000000000000000000000000000010020d')
print("UDP target IP:", UDP_IP)
/**
* Return a string padded with leading zeroes for a given total length.
* There are other solutions around in stackoverflow etc., in particular
* org.apache.commons.lang.StringUtils.leftPad(), but this one is a concise
* and cheap solution for the particular case of 0-padding, as a reasonable
* feature for fixed-length <strong>binary</strong> is missing in the String
* and Integer classes up to now.
*
* @param s
* Input string (in particular binary form of number) to be padded.
try {
// Create temp file.
File temp = File.createTempFile("pattern", ".suffix");
// Delete temp file when program exits.
temp.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write("aString");
@DirkHoffmann
DirkHoffmann / enniceUnicodeError.py
Created April 24, 2015 15:12
Print UnicodeError nicely (and compactly)
def enniceUnicodeError(ex):
return "<{0}>: {1}".format(ex.__class__.__name__, str(ex))
if __name__ == '__main__':
/* Make dummy exception */
ux=UnicodeDecodeError('hitchhiker', b"", 42, 43, 'the universe and everything else')
/* This is how to use the method : */
print enniceUnicodeError(ux)
@DirkHoffmann
DirkHoffmann / loic_singleton.py
Last active August 29, 2015 14:19
Decorator for making a class a singleton (without becoming a method)
class Singleton(type):
"""
Comme discuté sur la liste python@services.cnrs.fr (accès restreint à la communauté ESR)
https://listes.services.cnrs.fr/wws/arc/python/2015-04/msg00006.html
- Solution pour singleton co(?)-production Loïc Gouarin, Konrad Hinsen et al.
"""
_instances = {}
def __call__(self, *args, **kwargs):
key = (str(self), args, str(kwargs))
if key not in self._instances: