Skip to content

Instantly share code, notes, and snippets.

View Irostovsky's full-sized avatar

Ivan Rostovsky Irostovsky

View GitHub Profile
@Irostovsky
Irostovsky / show_sql_in_shell
Created May 27, 2019 11:54
python and django tricks
import logging
log = logging.getLogger('django.db.backends')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())
@Irostovsky
Irostovsky / timer.rb
Created July 19, 2018 09:26
Bench timer
# frozen_string_literal: true
module Utils
class Timer
def self.call(name)
t = Time.zone.now
result = yield
time = ((Time.zone.now - t).to_d * 1000).to_i
p "++++++++++ #{name}: #{time}ms"
{ result: result, time: time }
class FooPresenter
include Draper::Decoratable
attr_accessor :user
def initialize(user)
@user = user
end
def name
user.name
@Irostovsky
Irostovsky / tasks_1.rb
Created July 31, 2017 09:12
Let we have Post(title, body) -> Comments(body). Write the active record query to get all posts, where "query" is into title or body or comment.
# Let we have Post(title, body) -> Comments(body).
# Write the active record query to get all posts, where "query" is into title or body or comment.
class Post < ActiveRecord::Base
#title, body
has_many :comments
scope :search, ->(text) {
wildcard_search = "%#{text}%"
joins(:comments).where("title ILIKE :search OR body ILIKE :search OR comments.body ILIKE :search", search: wildcard_search)
Dir["app/assets/images/*"].each do |image|
name = File.basename(image)
if Dir.glob("**/*").reject {|fn| File.directory?(fn) }.
map{|fn| File.read(fn).include?(name) ? fn : nil }.any?
p image
else
p "To delete: #{image}"
File.delete image
end
end
module AuthenticityTokenCatcher
extend ActiveSupport::Concern
included do
rescue_from ActionController::InvalidAuthenticityToken do |exception|
if request.referrer
flash[:error] = I18n.t :authenticit_token_error
redirect_to request.referrer
else
raise exception
end
@Irostovsky
Irostovsky / gist:895705a15e11f04f3032538a241446c3
Created February 10, 2017 12:36
Equi: Find an index in an array such that its prefix sum equals its suffix sum.
This is a demo task.
A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.
A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
For example, consider the following array A consisting of N = 8 elements:
A[0] = -1
A[1] = 3
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-redis-on-ubuntu-16-04
How To Install and Configure Redis on Ubuntu 16.04
PostedMay 11, 2016 51.6k views NOSQL REDIS UBUNTU UBUNTU 16.04
Introduction
Redis is an in-memory key-value store known for its flexibility, performance, and wide language support. In this guide, we will demonstrate how to install and configure Redis on an Ubuntu 16.04 server.
Prerequisites
To complete this guide, you will need access to an Ubuntu 16.04 server. You will need a non-root user with sudo privileges to perform the administrative functions required for this process. You can learn how to set up an account with these privileges by following our Ubuntu 16.04 initial server setup guide.
def flattify(array)
array.each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) ? flattify(element) : element)
end
end
class Developer
def initialize
@chain = []
end
def are
@chain << "I"
self
end