Skip to content

Instantly share code, notes, and snippets.

View samukasmk's full-sized avatar

Samuel Sampaio samukasmk

View GitHub Profile
@samukasmk
samukasmk / comparing_set_operations.py
Created November 13, 2024 20:07
Comparing set() operations in python language and showing differences in operations using [.difference() vs -] and [.intersection() vs &]
import time
#
# Creating two large sets for testing
#
a = set(range(1, 1000000)) # Set with 1 million elements
b = set(range(500000, 1500000)) # Set that partially overlaps with set 'a'
print()
@samukasmk
samukasmk / examples-colab-gspread.py
Last active October 31, 2024 20:37
Google colab - using Google spreadsheet from Google drive - gspread
# reference: https://docs.gspread.org/en/latest/user-guide.html
# Install the required libraries
!pip install gspread google-auth
# Import libraries
import gspread
from google.colab import auth
from oauth2client.client import GoogleCredentials
from google.auth import default
@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" [email protected]
@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