Skip to content

Instantly share code, notes, and snippets.

View mgomes's full-sized avatar

Mauricio Gomes mgomes

View GitHub Profile
@mgomes
mgomes / decoration.rb
Created October 20, 2023 13:31
Method declaration with decoration in Ruby
module MethodDetector
def option_stack
@option_stack ||= []
end
def method_added(method_name)
unless option_stack.empty?
option = option_stack.pop
puts "You added the option '#{option}' to the method: #{method_name}"
end
@mgomes
mgomes / bot_controller.rb
Created June 30, 2022 03:40
Support for multiple phone numbers in Stealth
class BotController < Stealth::Controller
before_action :set_outbound_phone
private def set_outbound_phone
if current_message.service == "twilio"
Stealth.config.twilio.from_phone = current_message.target_id
end
end
@mgomes
mgomes / conv_net.py
Created March 19, 2019 14:45
Convolutional Neural Net using TensorFlow on Happy/Sad image dataset
import tensorflow as tf
import os
import zipfile
DESIRED_ACCURACY = 0.999
!wget --no-check-certificate \
"https://storage.googleapis.com/laurencemoroney-blog.appspot.com/happy-or-sad.zip" \
-O "/tmp/happy-or-sad.zip"
@mgomes
mgomes / cnn.py
Created March 19, 2019 03:05
CNN With MNIST
import tensorflow as tf
print(tf.__version__)
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images/255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
@mgomes
mgomes / dnn.py
Last active March 19, 2019 03:33
Deep Neural Net
import tensorflow as tf
print(tf.__version__)
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('loss')<0.4):
print("\nReached 60% accuracy so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
@mgomes
mgomes / web_url_regex.rb
Created March 14, 2016 18:19
Detect URL within text. Adapted from Android.
# URL Matching
# Taken from Android: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/util/Patterns.java#145
GOOD_IRI_CHAR = /a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF/
GOOD_GTLD_CHAR = /a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF/
GTLD = /[#{GOOD_GTLD_CHAR}]{2,63}/
IRI = /[#{GOOD_IRI_CHAR}]([#{GOOD_IRI_CHAR}\-]{0,61}[#{GOOD_IRI_CHAR}]){0,1}/
HOST_NAME = /(#{IRI}\.)+#{GTLD}/
IP_ADDRESS = /((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9]))/
DOMAIN_NAME = /(#{HOST_NAME}|#{IP_ADDRESS})/
WEB_URL_REGEX = /((?:(http|https|Http|Https|rtsp|Rtsp):\/\/(?:(?:[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\)\,\;\?\&\=]|(?:\%[a-fA-F0-9]{2})){1,64}(?:\:(?:[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\)\,\;\?\&\=]|(?:\%[a-fA-F0-9]{2})){1,25})?\@)?)?(?:#{DOMAIN_NAME})(?:\:\d{1,5})?)(\/(?:(?:[#{GOOD_IRI_CHAR}\;\/\?\:\@\&\=\#\~\-\.\+\
<a href='http://youtube.com'>
<img src='http://mauriciogomes.com/share/kx_video.png'>
</a>
desc "Heroku Cron Task"
task :cron => :environment do
Rake::Task["accounts:bill"].invoke
Rake::Task["accounts:expire"].invoke
end
desc "Heroku Cron Task"
task :cron => :environment do
begin
Rake::Task["accounts:bill"].invoke
rescue
HoptoadNotifier.notify($!)
end
begin
Rake::Task["accounts:expire"].invoke
@mgomes
mgomes / gist:964911
Created May 10, 2011 17:10
Ruby Array XOR
class Array
def ^(ary)
return (ary | self) - (ary & self)
end
end