Skip to content

Instantly share code, notes, and snippets.

View samukasmk's full-sized avatar

Samuel Sampaio samukasmk

View GitHub Profile
@samukasmk
samukasmk / Simple queues reprocessing.md
Created April 10, 2024 19:45
Example of reprocessing elements in a queue more than once

Simple queues reprocessing

Executing

python simple_queues.py

Example

image

Proof of Concept: Implementing MongoEngine lib with support for multi database connections

Reason:

Currently MongoEngine library only support a single database connection, managing collections by Document classes in global scope.

It has a support to change database connections by switch_db but it's made by a global class attribute called ._meta['db_alias']

When you define many sub ReferenceFields in a hierarchical tree, only first Document object has its ._meta['db_alias'] atttibute changed, not working to query Documents that implement other documents.

@samukasmk
samukasmk / asyncio_example3_async_for.py
Last active March 22, 2024 17:57
Async.IO Example #3: Run 10 async tasks waiting one by one
import asyncio
async def create_async_results(total_items: int):
for r in range(total_items):
await asyncio.sleep(1)
yield r
async def example_of_async_for_loop():
async for item in create_async_results(10):
print(item)
@samukasmk
samukasmk / asyncio_example2_as_completed.py
Last active March 19, 2024 14:37
Async.IO Example #2: Run 10 parallel tasks - But returning some results before all tasks to be done
import asyncio
from random import randint
async def execute_your_task(task_number: int):
""" Specific task to run in parallel """
# display begin of parallel task in console
print('-> starting parallel task:', task_number)
@samukasmk
samukasmk / asyncio_example1_gather_tasks.py
Last active March 19, 2024 14:38
Async.IO Example #1: Run 10 parallel tasks - And waiting all tasks to be done
import asyncio
from random import randint
async def execute_your_task(task_number: int):
""" Specific task to run in parallel """
# display begin of parallel task in console
print('-> starting parallel task:', task_number)
@samukasmk
samukasmk / confluent_kafka_uses.md
Last active December 27, 2023 15:10
Using Kafka library confluent-kafka-python

Installing

pip install confluent-kafka

Initialization

It's important to declare producer and consumer objects in global scope for better performance

@samukasmk
samukasmk / connect-ssh-by-tor-network.sh
Created December 7, 2023 19:45
Connecting in SSH by TOR network
ssh -o "ProxyCommand=nc -X 5 -x 127.0.0.1:9050 %h %p" root@your-domain.com
@samukasmk
samukasmk / Profiling memory usage in Python.md
Last active December 1, 2023 13:05
Profile the memory usage of your Python scripts easily

This is a easy example of how to profiling memory usage by memory_profiler library in 5 steps:

1.) Install memory_profiler libraries

pip install -U memory_profiler matplotlib

2.) Insert @profile decorator in your script

@samukasmk
samukasmk / recursion_replace_dict_sub_values.py
Last active September 1, 2023 15:01
[Recursion Algorithm In Practice] This script look for str objects in all sub childrens of a dict to replace values, using a recursion function.
def replace_dynamic_variable(element):
# If the element is a string, replace "{variable_dynamic}" with "hacked"
if isinstance(element, str):
element = element.replace("{variable_dynamic}", "hacked")
# If the element is a dictionary, iterate through each key-value pair
elif isinstance(element, dict):
for key, value in element.items():
element[key] = replace_dynamic_variable(value)
@samukasmk
samukasmk / raise_multiple_exceptions.py
Created August 30, 2023 13:45
Rasing multiple exception in Python Language
import traceback
# define some sample function with a nonexisting var
def my_function():
some_var = nonexisting_var
# create dict to store all raised exceptions
raised_exceptions = {}
# for 3 times execute a broken function (that generates execptions)