Skip to content

Instantly share code, notes, and snippets.

View juancarlospaco's full-sized avatar
👑
https://t.me/NimArgentina

Juan Carlos juancarlospaco

👑
https://t.me/NimArgentina
View GitHub Profile
@juancarlospaco
juancarlospaco / numbers.py
Created October 14, 2014 11:41
spoken and roman numbers for python 3
# -*- coding: utf-8 -*-
def int_to_roman(integer_argument):
"""Convert an integer to Roman numerals.
>>> int_to_roman(0)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
>>> int_to_roman(-1)
Traceback (most recent call last):
@juancarlospaco
juancarlospaco / logging_colors.py
Last active May 17, 2019 13:02
Add Colors to Logging Python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import logging as log
from copy import copy
if not sys.platform.startswith("win") and sys.stderr.isatty():
@juancarlospaco
juancarlospaco / progressbar.py
Last active April 6, 2018 19:36
Python ProgressBar OneLiner with 80Chars limit and 0-100 integer range.
progressbar = lambda i: sys.stdout.write("[{}] {}%\r".format(
'#' * (70 * i // 100) + '.' * (70 - 70 * i // 100), 100 * i // 100)[:80])
@juancarlospaco
juancarlospaco / seconds_time_to_human_string.py
Last active January 10, 2022 01:07
Seconds positive integer to friendly human string with precision from seconds to days
def seconds_time_to_human_string(self, time_on_seconds=0):
"""Calculate time, with precision from seconds to days."""
minutes, seconds = divmod(int(time_on_seconds), 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
human_time_string = ""
if days:
human_time_string += "%02d Days " % days
if hours:
human_time_string += "%02d Hours " % hours
@juancarlospaco
juancarlospaco / ram_used_by_itlsef.py
Created February 22, 2015 16:40
RAM Memory used by itlsef
# resource is Linux/OsX only
import sys
import resource
# Gives result on Integer, MegaBytes on Linux/OsX, or 0 on Windows since info is not available.
int(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * resource.getpagesize() / 1024 / 1024
if not sys.platform.startswith("win") else 0)
@juancarlospaco
juancarlospaco / future
Created March 20, 2015 15:06
How to know which __future__ to import
import __future__, sys
[name for name in __future__.all_feature_names if getattr(__future__, name).optional <= sys.version_info < getattr(__future__, name).mandatory]
@juancarlospaco
juancarlospaco / traceback_human.py
Last active May 22, 2021 23:53
Tracebacks friendly pretty printed with more info, good to copy-paste on bug reports.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, traceback
import logging as log
def log_exception():
"""Log Exceptions but pretty printing with more info, return string."""
@juancarlospaco
juancarlospaco / single_instance_no_root.py
Last active April 6, 2018 19:25
Single Instance App Singleton.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, os, socket
import logging as log
def set_single_instance(name, single_instance=True, port=8888):
"""Set process name and cpu priority, return socket.socket object or None.
@juancarlospaco
juancarlospaco / smooth_cpu_process_name.py
Last active April 6, 2018 19:37
Set Smooth CPUs Priority and set Process Name. Linux only.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import logging as log
from ctypes import byref, cdll, create_string_buffer
def set_process_name_and_cpu_priority(name):
@juancarlospaco
juancarlospaco / sound_play_crossplatform.py
Last active June 28, 2021 20:59
Cross-platform Sound Playing with Standard Libs only, No Sound file is required, No install is required, Python2 / Python3.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys
from tempfile import gettempdir
from subprocess import call
def beep(waveform=(79, 45, 32, 50, 99, 113, 126, 127)):