Skip to content

Instantly share code, notes, and snippets.

@ryanermita
ryanermita / rails_locking.md
Last active February 17, 2024 02:19
Optimistic and Pessimistic Locking in Rails

Optimistic Locking assumes that a database transaction conflict is very rare to happen. It uses a version number of the record to track the changes. It raise an error when other user tries to update the record while it is lock.

usage

Just add a lock_version column to the table you want to place the lock and Rails will automatically check this column before updating the record.

Pessimistic locking assumes that database transaction conflict is very likely to happen. It locks the record until the transaction is done. If the record is currently lock and the other user make a transaction, that second transaction will wait until the lock in first transaction is release.

usage

@ryanermita
ryanermita / storing_nested_objects_in_redis.py
Created May 17, 2018 14:36
Storing nested objects in redis sample code which you can use to play around in python and redis.
import redis # install redis via pip > `python -m pip install redis`
import json
# Instantiate redis connection with your local redis instance.
redis_connection = redis.StrictRedis(host="localhost", port=int(6379),
decode_responses=True)
"""
Caching simple object (flat and one dimentional object) as redis hash.
"""
@ryanermita
ryanermita / pymysq_query_unittest.py
Last active October 1, 2021 19:13
unit test a function with pymysql query.
from unittest import TestCase, mock
import simple
class TestSimple(TestCase):
@mock.patch('simple.pymysql', autospec=True)
def test_get-data(self, mock_pymysql):
mock_cursor = mock.MagicMock()
test_data = [{'password': 'secret', 'id': 1}]
mock_cursor.fetchall.return_value = test_data
@ryanermita
ryanermita / explore_customer_io_anonymous_email.py
Last active May 11, 2020 01:57
explore_customer_io_anonymous_email
import requests
import json
import base64
CUSTOMER_IO_SITE_ID='<SITE ID>'
CUSTOMER_IO_API_KEY='<API KEY>'
CUSTOMER_IO_CREDS = f'{CUSTOMER_IO_SITE_ID}:{CUSTOMER_IO_API_KEY}'
CUSTOMER_IO_ENCODED_CREDS = base64.b64encode(CUSTOMER_IO_CREDS.encode()).decode()
def send_verification_email():
@ryanermita
ryanermita / publisher.py
Last active July 25, 2019 22:32
sample publisher using rabbitmq pika client
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct')
channel.queue_declare(queue='direct_queue', durable=True)
channel.queue_bind(exchange='direct_exchange', queue="direct_queue", routing_key="direct.routing.key")
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange', exchange_type='direct')
channel.queue_declare(queue='direct_queue', durable=True)
channel.queue_bind(exchange='direct_exchange', queue="direct_queue", routing_key="direct.routing.key")
@ryanermita
ryanermita / test_autospec.py
Last active February 3, 2019 01:25
A simple test that demonstrate the autospec parameter in @patch decorator.
import unittest
from unittest.mock import patch
class MyDummyClass:
def test_dummy_function(self):
return "hello"
@ryanermita
ryanermita / missing_attribute.py
Last active February 3, 2019 01:11
A simple demonstration with regards to autospeccing caveat.
import unittest
from unittest.mock import patch
class MyDummyClass:
def __init__(self):
self.my_dummy_attribute = "yey!"
def test_dummy_function(self):
@ryanermita
ryanermita / magic_mock_magic_methods.py
Last active January 20, 2019 09:14
pre-created magic methods for MagicMock object.
__lt__: NotImplemented
__gt__: NotImplemented
__le__: NotImplemented
__ge__: NotImplemented
__int__: 1
__contains__: False
__len__: 0
__iter__: iter([])
__exit__: False
__complex__: 1j
dict_object = {
"name": "John Doe",
"social_media_accounts": ["facebook", "twitter", "Instagram"],
"language_proficiency": {
"Python": "9/10",
"Javascript": "7/10",
"Ruby": "8/10"
}
}