Skip to content

Instantly share code, notes, and snippets.

View ViktorStiskala's full-sized avatar

Viktor Stískala ViktorStiskala

View GitHub Profile
def validate_account_number(value):
checksum = sum(int(value[i]) * (8-i) for i in range(7)) % 11
last_digit = int(value[-1])
if checksum in [0, 10]:
c = 1
elif checksum == 1:
c = 0
else:
c = 11 - checksum
def note_error(self, *msg):
if self.strict:
raise PermError(*msg)
# if lax mode, note error and continue
if not self.perm_error:
try:
raise PermError(*msg)
except PermError as x:
# FIXME: keep a list of errors for even friendlier diagnostics.
self.perm_error = x
@ViktorStiskala
ViktorStiskala / bash_colors.sh
Created July 20, 2015 09:07
Bash color variables
# source: https://wiki.archlinux.org/index.php/Color_Bash_Prompt
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
@ViktorStiskala
ViktorStiskala / test.sh
Created June 29, 2015 11:19
Bash catch exception
#!/bin/bash
set -e
err_report() {
ERR=$?
echo "$1 failed on line $2 ('$3') with error code $ERR" 1>&2
exit $ERR
}
trap 'err_report "$BASH_SOURCE" "$LINENO" "$BASH_COMMAND"' ERR
echo "l" | grep f
with open('even_numbers.txt', 'w') as f:
for number in (n for n in range(10**100) if n%2 == 0):
f.write('{}\n'.format(number))
#!/bin/bash
# CONFIGURATION
openssl_config_file=$(openssl ca 2>&1 | grep "Using configuration from" | sed 's/Using configuration from //g')
################################################################################
txtgrn='\e[0;32m'
txtred='\e[0;31m'
txtrst='\e[0m'
config_tempfile=$(mktemp --suffix openssl)
def split_sum_into_parts(limit=1500, min_parts=2, max_parts=5):
sample = sorted([0, limit] + random.sample(range(limit), random.randint(min_parts-1, max_parts-1)))
return [sample[i+1] - item for i, item in enumerate(sample) if i+1 < len(sample)]
def split_sum_into_parts(limit=1500, min_parts=2, max_parts=5):
sample = sorted([0, limit] + random.sample(range(limit), random.randint(min_parts-1, max_parts-1)))
return [sample[i+1] - item for i, item in enumerate(sample) if i+1 < len(sample)]
@ViktorStiskala
ViktorStiskala / random_crash.sh
Created January 22, 2014 10:47
Bash retry function with max_retries settings
#!/bin/bash
number=$RANDOM
let "number %= 10"
if [[ $number -gt 8 ]]
then
echo "Success"
exit 0
fi
@ViktorStiskala
ViktorStiskala / spayd.py
Created August 23, 2013 10:29
Convert account number to IBAN
def _convert_to_iban(self, account):
"""
Convert czech account number to IBAN
"""
acc = self.RE_ACCOUNT.match(account)
iban = 'CZ00{b}{ba:0>6}{a:0>10}'.format(
ba=acc.group('ba') or 0,
a=acc.group('a'),
b=acc.group('b'),
)