Skip to content

Instantly share code, notes, and snippets.

@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():
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 / 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")
@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 / 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 / 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
@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
dict_object = {
"name": "John Doe",
"social_media_accounts": ["facebook", "twitter", "Instagram"],
"language_proficiency": {
"Python": "9/10",
"Javascript": "7/10",
"Ruby": "8/10"
}
}
dict_object = {
"name": "John Doe",
"social_media_accounts": ["facebook", "twitter", "Instagram"],
"language_proficiency": {
"Python": "9/10",
"Javascript": "7/10",
"Ruby": "8/10"
}
}
dict_object = {"firstname": "John", "lastname": "Doe"}
type(dict_object) # dict
# cache dict_object as redis hash
app.redis_connection.hmset("your_unique_key", dict_object)
# fetch cached data (hash) from redis
cached_data = app.redis_connection.hgetall("your_unique_key")
type(cached_data) # dict