Skip to content

Instantly share code, notes, and snippets.

@ryanermita
ryanermita / raw_sql.rb
Created August 27, 2016 00:05
Running raw SQL in Ruby on Rails
# Running raw SQL in Ruby on Rails
sql = "SELECT * FROM table_name"
result = ActiveRecord::Base.connection.execute(sql)
@ryanermita
ryanermita / innodb_lock_wait_timeout.md
Created August 27, 2016 02:11
Updating innodb_lock_wait_timeout

Very useful when you need to update database transaction locking timeout dyanamically.

SET SESSION innodb_lock_wait_timeout = 1; will set the innodb_lock_wait_timeout

SELECT @@innodb_lock_wait_timeout; will output the updated innodb_lock_wait_timeout

@ryanermita
ryanermita / doublequotestrings.md
Last active September 9, 2016 04:17
Alternative way in ruby to enclose strings in double quotes.

%Q

  • Alternative way in ruby to enclose strings in double quotes.

  • Useful when you have more quote characters in a string.You can use this instead of putting backslashes in front of them.

  • Can use in escaping HTML and multiline SQL statements.

Usage

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
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"
}
}
@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 / 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"
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")