Skip to content

Instantly share code, notes, and snippets.

View tiagocoutinho's full-sized avatar

Jose Tiago Macara Coutinho tiagocoutinho

View GitHub Profile
@tiagocoutinho
tiagocoutinho / random_name.py
Last active April 5, 2024 04:45
Generate random name in python
import random
import string
ascii_alphanumeric = string.ascii_letters + string.digits
def random_name(min_length=32, max_length=32):
if not (k := random.randint(min_length, max_length)):
return ""
first = random.choice(string.ascii_letters)
return first + "".join(random.choices(ascii_alphanumeric, k=k-1))
@tiagocoutinho
tiagocoutinho / test_fixture_run_once_xdist.py
Created May 18, 2023 12:59
Ensure a fixture runs once when using pytest-xdist and other workers wait for it to finish
"""
$ pip install posix_ipc pytest pytest-xdist
$ pytest -s -v -n 4 test_fixture_run_once_xdist.py
"""
import inspect
import logging
import time
@tiagocoutinho
tiagocoutinho / parallel.sh
Created October 17, 2022 08:14
Concurrent bash tasks
#!/bin/bash
BASE_URL=git@github.com:tiagocoutinho
REPOS=( "gepace" "cryocon" "vacuubrand" "xia-pfcu" "mythen-dcs" )
CLONE="git clone --depth=1 --no-single-branch"
function fetch {
for repo in "${REPOS[@]}"
do
@tiagocoutinho
tiagocoutinho / usocat.py
Created June 2, 2022 10:45
micro socat port forward in python
"""
python usocat.py <listening port> <downstream address>
Example:
python usocat.py 0:10001 192.168.1.5:8000
"""
import logging
import select
@tiagocoutinho
tiagocoutinho / multi_server.py
Created April 25, 2022 17:16
socket server multiprocess
import os
import socket
import logging
def cb(sock):
reader = sock.makefile('rb')
for line in reader:
sock.sendall(line[::-1])
@tiagocoutinho
tiagocoutinho / balanced.py
Created April 25, 2022 16:13
Balanced parenthesis in python
opening = "([{"
closing = ")]}"
def balanced(expr):
stack = []
for c in expr:
if c in opening:
stack.append(c)
elif c in closing:
@tiagocoutinho
tiagocoutinho / sync_to_async.py
Created March 29, 2022 07:07
Python sync to async
import asyncio
import inspect
import functools
def async_(fn):
"""
Wrap the given function into a coroutine function.
If the function returns a coroutine, that coroutine is awaited and its
@tiagocoutinho
tiagocoutinho / prime.py
Created September 6, 2020 11:01
prime generator in python
def nats(n):
while True:
yield n
n += 1
# another nats implementation
def nats2(n):
yield n
yield from nats2(n+1)
@tiagocoutinho
tiagocoutinho / sardana_change_tangodb.py
Last active August 4, 2020 08:20 — forked from reszelaz/sardana_change_tangodb.py
Script for changing Tango database in Sardana persistent information.
"""Changes the following persistent information:
Pool:
- MeasurementGroup's Configuration attribute
- MeasurementGroup's elements property
- IcePAP Motor's EncoderSource attribute
- TangoAttribute of TangoAttribute controllers
- TaurusAttribute of TaurusAttirubteCounterTimer controller
MacroServer:
- PreScanSnapshot
"""
@tiagocoutinho
tiagocoutinho / README.md
Last active June 26, 2020 09:45
sardana macro runner helper

Sardana macro runner helper

to run macros from the command line type:

$ (i)python -i -m ncd.sardana.macro

>>> from ncd.sardana.macro.my_macro import my_macro