Skip to content

Instantly share code, notes, and snippets.

Sergey Prokhorov seriyps

View GitHub Profile
View least_busy.erl
%%% @author sergey <me@seriyps.ru>
%%% @copyright (C) 2018, sergey
%%% @doc
%%% Given a liast of resources M and list of users N, N > M;
%%% Any resourse might be acquired by one or more users (by request from user).
%%% There is no bound on how many users can acquire single resource.
%%% User may release resource at any time.
%%% User must get resource that is used by least amount of other users.
%%% @end
%%% Created : 8 Oct 2018 by sergey <me@seriyps.ru>
@seriyps
seriyps / rf.erl
Last active Feb 1, 2018
Print erlang records as records, not tuples; print function source code
View rf.erl
%% Prints source code of a function.
%%
%% Requires debug_info
%% Will not work for modules mocked by meck
%%
%% > io:format("~s~n", [rf:print_function(dict, new, 0)]).
%% new() ->
%% Empty = mk_seg(16),
%% #dict{empty = Empty, segs = {Empty}}.
@seriyps
seriyps / exported.erl
Created Dec 5, 2017
Erlang is function exported with loading
View exported.erl
is_exported(M, F, A) ->
%% make a cheap test first to avoid unnecessary calls to code server
case erlang:module_loaded(M) of
false -> code:ensure_loaded(M);
true -> ok
end,
erlang:function_exported(M, F, A).
@seriyps
seriyps / uptime.erl
Created Jun 24, 2017
Uptime of Erlang node
View uptime.erl
-export([uptime/0, uptime/1, uptime_string/0]).
%% @doc uptime in native time units
uptime() ->
Now = erlang:system_time(),
StartTime = erlang:time_offset() + erlang:system_info(start_time),
Now - StartTime.
%% @doc uptime in specified time units
uptime(Unit) ->
@seriyps
seriyps / on_same_timezone.erl
Created Aug 21, 2015
Check if epgsql database connections are on the same timezone
View on_same_timezone.erl
% If we have some amount of PostgreSQL instances and want to transfer data between them,
% in case when model has 'timeatamp' fields, we should check that both instances configured with the
% same timezone
on_same_timezone(Connections) ->
Tests =
[%% Check using TimeZone connection parameter
{fun(Conn) ->
{ok, TZ} = pgsql:get_parameter(Conn, <<"TimeZone">>),
TZ
@seriyps
seriyps / tcp_server.rs
Last active Sep 22, 2018
Simple concurrent TCP echo server (rust 0.11)
View tcp_server.rs
#![feature(phase)]
#[phase(plugin, link)] extern crate log;
extern crate green;
extern crate rustuv;
use std::io;
use std::os;
use std::io::{Listener,Acceptor,TcpStream};
#[start]
@seriyps
seriyps / erlydtl_xgettext.erl
Last active Aug 29, 2015
.po file generator from erlydtl templates
View erlydtl_xgettext.erl
%% Usage:
%% TemplatesDir = "templates",
%% TemplatesNames = ["index.dtl", "_layout.dtl"], % filelib:wildcard("*.dtl", TemplatesDir)
%% LocalesDir = "locales",
%% Domain = "my_project", % phrases will be written to "locales/my_project.pot"
%% Messages = extract_messages(TemplatesDir, TemplatesNames),
%% write_pot(Messages, LocalesDir, Domain).
-module(erlydtl_xgettext).
@seriyps
seriyps / microlisp.py
Created Apr 11, 2014
Lisp subset interpreter
View microlisp.py
# -*- coding: utf-8 -*-
'''
Created on 2014-04-05
@author: Sergey Prokhorov <me@seriyps.ru>
'''
import re
import operator
import decimal
@seriyps
seriyps / mo_parser.erl
Created Sep 2, 2013
Gettext .mo format parser for Erlang
View mo_parser.erl
%%% @author Sergey Prokhorov <me@seriyps.ru>
%%% @copyright (C) 2013, Sergey Prokhorov
%%% @doc
%%% Simple gettext .mo file format parser for Erlang.
%%%
%%% Produce [{KeyPlurals::[binary()], TransPlurals::[binary()]}] orddict as
%%% output.
%%% Eg, for .po file (converted to .mo)
%%% <pre>
%%% msgid "Download"
@seriyps
seriyps / email.erl
Last active Jan 11, 2019
Send emails using Erlang gen_smtp shortcut.
View email.erl
% Send plaintext email using gen_smtp https://github.com/Vagabond/gen_smtp
% This function sends email directly to receiver's SMTP server and don't use MTA relays.
%
% Example plaintext email:
% Mail = mail_plain(<<"Bob <sender@example.com>">>, <<"Alice <receiver@example.com>">>, <<"The mail subject">>, <<"The mail body">>),
% send_email(Mail).
%
% Example email with image attachment:
% ImgName = "image.jpg",
% {ok, ImgBin} = file:read_file(ImgName),
You can’t perform that action at this time.