Skip to content

Instantly share code, notes, and snippets.

View josepsmartinez's full-sized avatar

José Pedro Silveira Martinez josepsmartinez

View GitHub Profile
@josepsmartinez
josepsmartinez / install_python_src.sh
Last active June 7, 2022 17:23
Install Python from source (Debian systems)
RELEASE_URL=https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz
wget ${RELEASE_URL} -O Python-src.tgz && tar xzf Python-src.tgz &&\
cd Python-src &&\
./configure --enable-optimizations --with-openssl=$(which openssl) &&\
sudo make altinstall &&\
rm -rf Python-src Python-src.tgz
@josepsmartinez
josepsmartinez / timestamp.py
Last active May 10, 2022 17:41 — forked from wzpan/timestamp.py
Python datetime timestamps
import datetime
# ISO formatting
datetime.datetime.utcnow() # ('2022-05-10 04:18:09.297644')
datetime.datetime.now() # ('2022-05-10 01:18:09.297743')
# Custom formatting
## Pretty ('Saturday, 15. December 2012 11:19AM')
datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
## Calendar ('2012-12-15')
@josepsmartinez
josepsmartinez / gcloud-zone_of_instance.sh
Created April 27, 2022 00:29
gcloud zone of instance
# https://techoverflow.net/2019/04/01/how-to-find-zone-of-google-cloud-vm-instance-on-command-line/
gcloud compute instances list --filter='name="${INSTANCE_NAME}"' --format 'get(zone)' | rev | cut -d/ -f1 | rev
@josepsmartinez
josepsmartinez / label2attr.py
Created February 15, 2022 17:43
Converts CVAT annotation samples from *checkbox tags* to *object tag attributes*
import bs4
source_path = "annotations_source.xml"
output_xml_path = "annotations_out.xml"
LABELS = [
"no-category", "valid", "non-centered", "occluded", "spoof",
"rotated", "blurred"]
@josepsmartinez
josepsmartinez / exit_codes.sh
Last active February 14, 2022 21:42
Bash practices for handling exit codes (Reference: https://stackoverflow.com/a/45817972/4449273)
#!/bin/bash
# In case of simple logging
complex_command && echo "Success" >> log.txt || echo "Failure" >> log.txt
# In case of complex command chaining
complex_command
if [ $? -ne 0]; then
success_complex_command # this command could fail as well
# its exit code can be treated in a nested manner by testing $? again
@josepsmartinez
josepsmartinez / sudo-func-call.sh
Created April 16, 2021 15:43
Calling Bash functions with sudo
#!/bin/bash
# Adapted from (https://unix.stackexchange.com/a/269080/388950)
no_sudo_func () {
apt update
}
F=$(declare -f no_sudo_func)
sudo bash -c "$F; no_sudo_func"
@josepsmartinez
josepsmartinez / gcloudt
Created April 14, 2021 15:36
Alias for `gcloud $@ --tunnel-through-iap`. Remember do `chmod a+x` the script.
#!/bin/bash
PATH_TO_GCLOUD=/snap/bin/google-cloud-sdk.gcloud
${PATH_TO_GCLOUD} $@ --tunnel-through-iap
@josepsmartinez
josepsmartinez / online_averaging.py
Last active March 27, 2021 01:24
Python class for online computing of `np.mean`
import numpy as np
class OnlineStatsCompute():
def __init__(self, *l):
if len(l) > 0:
self.add_l(*l)
def add_e(self, e):
"""Includes a single element `e` in the OnlineStatsCompute.
Updates attributes `N` and `mean`.
@josepsmartinez
josepsmartinez / docstring.py
Created March 12, 2021 13:58
Python docstring template (like gensim and sklearn)
def foo():
"""<FUNCTION DESCRIPTION>
<BLANK LINE>
Parameters
----------
<VARIABLE> : <TYPE>
<DESCRIPTION>
...
<BLANK LINE>
Returns
@josepsmartinez
josepsmartinez / ssh-set-permissions.sh
Last active October 3, 2021 15:49
ssh permissions
#!/bin/bash
# Configs
chmod 700 $HOME/.ssh
chmod 644 $HOME/.ssh/authorized_keys
chmod 644 $HOME/.ssh/known_hosts
chmod 644 $HOME/.ssh/config
# Keys
chmod 600 $HOME/.ssh/id_rsa