Skip to content

Instantly share code, notes, and snippets.

Avatar

Grigory Fateyev (aka greg) greggy

View GitHub Profile
@greggy
greggy / postgres_add_json_sub_array_unique.sql
Created Jan 25, 2020 — forked from mpneuried/postgres_add_json_sub_array_unique.sql
Add and remove elements unique to a Postgres jsonb sub key: Short a Set implemetation
View postgres_add_json_sub_array_unique.sql
UPDATE public.mytable SET
jsonfieldname = jsonb_set( jsonfieldname, '{json_obj_key}', array_to_json(
ARRAY(
SELECT DISTINCT( UNNEST( ARRAY(
SELECT json_array_elements_text( COALESCE( jsonfieldname::json->'json_obj_key', '[]' ) )
) || ARRAY['Element to add'] ) )
)
)::jsonb )
WHERE id = 23
RETURNING *;
@greggy
greggy / postgres_add_json_sub_array_unique.sql
Created Jan 25, 2020 — forked from mpneuried/postgres_add_json_sub_array_unique.sql
Add and remove elements unique to a Postgres jsonb sub key: Short a Set implemetation
View postgres_add_json_sub_array_unique.sql
UPDATE public.mytable SET
jsonfieldname = jsonb_set( jsonfieldname, '{json_obj_key}', array_to_json(
ARRAY(
SELECT DISTINCT( UNNEST( ARRAY(
SELECT json_array_elements_text( COALESCE( jsonfieldname::json->'json_obj_key', '[]' ) )
) || ARRAY['Element to add'] ) )
)
)::jsonb )
WHERE id = 23
RETURNING *;
@greggy
greggy / join_binaries.erl
Last active Feb 22, 2018
The example shows how to concatinate (join) the list of binaries with separator in Erlang
View join_binaries.erl
-module(join_binries).
-export([join/2]).
join(TitleList, Sep) ->
Fun = fun(<<>>, Acc) ->
Acc;
(Word, Acc) ->
<<Acc/binary, Sep, Word/binary>>
end,
lists:foldl(Fun, hd(TitleList), tl(TitleList)).
View test1.js
collections.map(function(coll) {
return {
type: "checkbox"
, callback: function() { console.log('this', this.checked) }
, checked: false
, text: coll.title
}
})
View gist:1a65c83ed2828e24ac0e
###########################################
## Get timestamp with TZ
###########################################
from datetime import datetime
import time, pytz
mytz = pytz.timezone('Europe/Moscow')
print( int(time.mktime(mytz.localize(datetime.now()).timetuple())) )
View gist:9759344
DEBUG: Worker compilation failed: [{error,
{'EXIT',
{undef,
[{leex,file,
["src/protobuffs_scanner.xrl",
[{scannerfile,
"src/protobuffs_scanner.erl"},
{return,true}]],
[]},
{rebar_erlc_compiler,compile_xrl_yrl,
View gist:9250351
if (data.result == 'ok'){
for (var i = 0; i < self.type_results().length; i++) {
var el = self.type_results()[i];
if (el.id == id) {
self.type_results.remove(el);
el.approved = true;
self.type_results.splice(i, 0, el);
break; // we found our id so can break the loop
}
}
View final7-solution.py
# coding: utf8
import pymongo
connection = pymongo.MongoClient('localhost', 27017)
db = connection.photos
albums = db.albums
images = db.images
View gist:4953482
{ok, Viewers} = eredis:q(State#state.redis_pool, ["HGET", "channel:clients", Key]),
ViewersInt = list_to_integer(binary_to_list(Viewers)),
case ViewersInt > 0 of
true ->
eredis:q(State#state.redis_pool, ["HSET", "channel:clients", Key, 0]),
?D([Key, ViewersInt]),
net_pinger:set_billing(binary_to_list(Key), ViewersInt);
_ -> ok
end
View gist:4250155
%%%-------------------------------------------------------------------
%%% @author greg <>
%%% @copyright (C) 2012, greg
%%% @doc
%%%
%%% @end
%%% Created : 29 Nov 2012 by greg <>
%%%-------------------------------------------------------------------
-module(ring).