Skip to content

Instantly share code, notes, and snippets.

@gpad
gpad / read_only_user.sql
Created April 4, 2020 08:50
Script to create read_only user in PG
CREATE ROLE <user_name> WITH LOGIN PASSWORD '<password>' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
GRANT CONNECT ON DATABASE <db> TO <user_name>;
\c <db>
GRANT USAGE ON SCHEMA public TO <user_name>;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user_name>;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO <user_name>;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO <user_name>;
@gpad
gpad / notes_les.md
Created April 4, 2018 21:34
Notes on les for code beam lite milano

Embrace the Memory state

mix ecto.drop && mix ecto.setup iex -S mix phx.server

{:ok, user, pid} = Les.UserEntity.create(%{username: "u1", name: "n1"})
user
pid
@gpad
gpad / .iex.exs
Last active August 25, 2020 07:57
My .iex.extension. In your app dir you can do: `GPad.start_app :timex` and if you have :timex in your deps it will be launched.
defmodule GPad do
def get_base() do
try do
get_base(System.get_env("MIX_ENV")) |> Path.join("lib")
catch
e -> IO.puts("Error: #{inspect(e)}")
end
end
def get_base(nil) do
@gpad
gpad / noslides_service.ex
Created January 4, 2017 16:00
NoSlides Service First Version
defmodule NoSlides.Service do
def ping(v\\1) do
idx = :riak_core_util.chash_key({"noslides", "ping#{v}"})
pref_list = :riak_core_apl.get_primary_apl(idx, 1, NoSlides.Service)
[{index_node, _type}] = pref_list
:riak_core_vnode_master.sync_command(index_node, {:ping, v}, NoSlides.VNode_master)
end
@gpad
gpad / red-test.md
Created November 10, 2016 12:02
red-test.md
1) test Aggregate and publish internal temperature base on same minute on receipt (BackendOneTest)
    test/backend_one_test.exs:86
    No message matching {:test_consumer, %{"type" => "stats", "seller_id" => 42, "payload" => %{"receipt" => %{"date" => receipt_dt, "id" => 666}, "internal_avg_temperature" => 23.0, "external_avg_temperature" => 13.0, "people" => 1}}} after 5000ms.
    Process mailbox:
      {:test_consumer, %{"payload" => %{"external_avg_temperature" => 13.0, "internal_avg_temperature" => 23.0, "people" => 1, "receipt" => %{"date" => "2016-01-01T15:42:18Z", "id" => 666, "sellerId" => 42}}, "type" => "stats"}}
    stacktrace:
      test/backend_one_test.exs:111: (test)
@gpad
gpad / pe_fib.exs
Created October 13, 2015 13:40
Programming Elixir Fibonacci
#---
# Excerpted from "Programming Elixir",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/elixir for more book information.
#---
defmodule FibSolver do
@gpad
gpad / fibonacci.exs
Created October 13, 2015 13:37
Parallel Fibonacci
defmodule Fibonacci do
def fib(0), do: 0
def fib(1), do: 1
def fib(n), do: fib(n-1) + fib(n-2)
end
defmodule Fibonacci.Parallel do
def fib(n, processes) do
(1..processes) |> Enum.map(fn id ->
srv = self()
@gpad
gpad / quicksort.exs
Created October 12, 2015 22:01
Elixir meetup
defmodule QSv1 do
def sort([]), do: []
def sort([pivot | rest]) do
{smaller, bigger} = Enum.partition(rest, &(&1 < pivot))
sort(smaller) ++ [pivot] ++ sort(bigger)
end
end
defmodule QSv2 do
def sort([]), do: []
@gpad
gpad / heyweb.rb
Created August 26, 2014 22:16
Hey Web SMS example
require 'net/http'
require 'net/https'
require 'uri'
require 'base64'
class HeyWeb
BASE_URL = "https://secure.apisms.it/http/"
ID_API = 5
REPORT_TYPE = 'P'
@gpad
gpad / CircularRandom
Created June 23, 2014 21:03
CircularRandom - Usa una lista per decidere quali valori far tornare dal random
#include "StdAfx.h"
#include "GPadAssert.h"
template<typename MyRand>
class MyRandomObject
{
int _sum;
MyRand rand;
public:
MyRandomObject()