Skip to content

Instantly share code, notes, and snippets.

View hynekcer's full-sized avatar

Hynek Cernoch hynekcer

  • Prague, Czech republic
View GitHub Profile
@hynekcer
hynekcer / html_like_layouting.py
Created January 14, 2013 08:47
HTML like layouting (StackOverflow question 14264427) http://stackoverflow.com/questions/14264427/html-like-layouting The original source by Niklas R from the question
# coding: utf-8
import copy
import pygame
import argparse
LEFT = 1
def rint(x):
if x % 1 >= 0.5:
@hynekcer
hynekcer / post-checkout.py
Last active November 27, 2019 20:14
Git post-checkout hook for Python to remove orphan *.pyc files and empty dir
#!/bin/env python
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
"""
A hook to git that removes orphan files "*.pyc" and "*.pyo" for "*.py"
beeing deleted or renamed by git checkout. It also removes their empty parent
directories.
Place it to "my_local_repository/.git/hooks/post-checkout" and make it executable.
Nothing is cleaned for .py files deleted manually or by "git rm" etc.
Related to http://stackoverflow.com/q/1504724/448474
@hynekcer
hynekcer / django_dot_filter.py
Last active November 4, 2020 14:58
Django filters with relational operators and dot to related fields instead of double underscores
from django.db.models import Q
from django.utils import six
class MetaV(type):
# metaclass to automatically create an instance by . dot
def __getattr__(self, name):
if name.startswith('_'):
return super(self, MetaV).__getattr__(name)
else:
@hynekcer
hynekcer / case_safe_id.py
Last active November 24, 2020 22:28
Convert 15 chars Salesforce ID to 18 chars case safe ID
# free MIT license
def case_safe_id(id_15):
"""
Convert a 15 char case-sensitive Id to 18 char case-insensitive Salesforce Id.
Long 18 char Id are from SFDC API and from Apex. They are recommended by SF.
Short 15 char Id are from SFDC formulas if omitted to use func CASESAFEID(),
from reports or from parsed URLs in HTML.
The long and short form are interchangable as the input to Salesforce API or
@hynekcer
hynekcer / models_and_tests.py
Created August 4, 2017 20:46
test for django-salesforce issue 191
# == models.py ==
from salesforce import models
class RecordType(models.SalesforceModel):
sobject_type = models.CharField(max_length=40, sf_read_only=models.NOT_UPDATEABLE)
class Contact(models.Model):
last_name = models.CharField(max_length=80)
record_type = models.ForeignKey(RecordType, models.DO_NOTHING, blank=True, null=True)
@hynekcer
hynekcer / run_sf_report.py
Last active December 21, 2023 12:53
Parse Salesforce report data in Python
"""Parse Salesforce report data in Python
details in my answer https://stackoverflow.com/a/45645135/448474
"""
from collections import OrderedDict
from simple_salesforce import Salesforce
import pandas as pd
import json
@hynekcer
hynekcer / results_in_teardown.py
Last active July 1, 2024 07:55
Getting Python's unittest results in a tearDown() method (my SO answer, question 4414234)
"""Getting Python's unittest results in a tearDown() method
https://stackoverflow.com/questions/4414234/getting-pythons-unittest-results-in-a-teardown-method#39606065
"""
import unittest
class MyTest(unittest.TestCase):
def tearDown(self):
if hasattr(self._outcome, 'errors'):
# Python 3.4 - 3.10 (These two methods have no side effects)
/* source from
http://stackoverflow.com/questions/13560037/effcient-way-to-find-longest-duplicate-string-for-python-from-programming-pearl
http://www.cs.bell-labs.com/cm/cs/pearls/longdup.c
/* Copyright (C) 1999 Lucent Technologies */
/* From 'Programming Pearls' by Jon Bentley */
/* longdup.c -- Print longest string duplicated M times */
#include <stdlib.h>
#include <string.h>
@hynekcer
hynekcer / maxsubstring.py
Created January 12, 2018 02:30
fast longest common substring - by suffix array
#!/usr/bin/env python
"""Find the longest repeated substring.
"Efficient way to find longest duplicate string for Python (From Programming Pearls)"
http://stackoverflow.com/questions/13560037/
The algorithm is based on "Prefix doubling".
The worst time complexity is O(n (log n)^2). Memory requirements are linear.
"""
import time
@hynekcer
hynekcer / rubiks_cube.py
Created August 18, 2018 14:07
Max estimated number of states of Rubik's Cube after N quarter-turn moves (Czech)
"""
Horní odhad počtu stavů Rubikovy kostky po N tazích typu čtvrt-obrátka
# https://sciencemag.cz/umela-inteligence-se-sama-naucila-slozit-rubikovu-kostku/#post-3981572954
Počty stavů po daném počtu tahů v jedné ze tří rovin
O: - 0
1: L l R r 4
2: LL LR Lr lR lr RR 6
3: LLR LLr LRR lRR 4