Skip to content

Instantly share code, notes, and snippets.

View inirudebwoy's full-sized avatar
🏗️
putting things on top of other things

Michael Klich inirudebwoy

🏗️
putting things on top of other things
View GitHub Profile
@inirudebwoy
inirudebwoy / Convert time to specified timezone using pytz.md
Created November 16, 2023 10:21
This code snippet defines a function that takes a datetime and a timezone name as input and returns the datetime converted to the specified timezone. It assumes that naive datetimes are in UTC.

Convert time to specified timezone using pytz

Preview:
def get_local_time(dt: datetime, timezone_name: str) -> datetime:
    """Convert given time to timezone. Naive datetimes are assumed to be in UTC"""
    tz = pytz.timezone(timezone_name)
    return to_zoned_utc(dt).astimezone(tz)
Associated Context
#! -*- coding: utf-8 -*-
from resolution import to_text
def test_1():
assert to_text(1) == 'jeden'
def test_12():
assert to_text(12) == 'dwanaście'
@inirudebwoy
inirudebwoy / verify_pr.sh
Created November 6, 2017 15:32
Make verifying of PRs easier
#!/bin/bash
UNITTEST_LOG=error_unittest_verify_pr.log
INTEGRATIONTEST_LOG=error_integrationtest_verify_pr.log
CURRENT_GIT_BRANCH=$(git name-rev --name-only HEAD)
# check argument
if [ -z "$1" ]; then
echo "$(tput setaf 1)Please supply name of the branch to verify"
exit 1
fi
@inirudebwoy
inirudebwoy / meta_debug.py
Created March 22, 2017 20:40
Metaclass wrapping each function call with an argument printer
from functools import wraps
def printer(f):
@wraps(f)
def wrapper(*args, **kwargs):
print(args)
return f(*args, **kwargs)
return wrapper
@inirudebwoy
inirudebwoy / django_client_testing.py
Created September 21, 2016 15:18
Setup function simplifying working with a Django client.
def setUp(self):
post = partial(
self.client.post,
path='/api/task/',
content_type='application/json')
self.post = lambda data: post(data=json.dumps(data))
alias halt_all_vagrants="vagrant global-status | awk '/running/{print $1}' | xargs -r -d '\n' -n 1 -- vagrant halt"
@inirudebwoy
inirudebwoy / 0_reuse_code.js
Created September 10, 2016 15:45
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
def kwargs_default(params):
"""Decorator for setting default keyword parameters
@kwargs_default((('number_of_chickens', 3),))
will add param 'number_of_chickens' with value of 3 into kwargs.
:param params: tuple of tuples consiting of name of parameter and value
:type params: tuple of tuples with str and any ((str, any), (str, any), ...)
"""
@inirudebwoy
inirudebwoy / retry.py
Last active July 22, 2016 14:25
Decorator retrying connection with the callback on failure
def retry(retries=3, callback_on_failure=None):
"""Retry requests method and run callback on failure
Decorator will retry the decorated function for specified number of times,
default is 3. If there is a callback passed, it will be called before
calling the decorated function again. Callback allows to perform cleanups,
clear the credentials, etc.
Decorator will only work with functions that return requests.Request
object, if decorated method returns other object result is returned with
out retrying.
@inirudebwoy
inirudebwoy / setup.py
Created February 2, 2016 15:45
Retrievinng version number in setup.py
def get_version():
local_vars = {}
try:
execfile('src/imagination/objectid_manager/__init__.py',
{},
local_vars)
except NameError:
# python3.x does not provide execfile
with open('src/imagination/objectid_manager/__init__.py') as f:
exec(f.read(), {}, local_vars)