Skip to content

Instantly share code, notes, and snippets.

View greggy's full-sized avatar

Grigory Fateyev (aka greg) greggy

View GitHub Profile
@greggy
greggy / postgres_add_json_sub_array_unique.sql
Created January 25, 2020 06:35 — 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
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 January 25, 2020 06:35 — 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
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 February 22, 2018 06:27
The example shows how to concatinate (join) the list of binaries with separator in Erlang
-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)).
collections.map(function(coll) {
return {
type: "checkbox"
, callback: function() { console.log('this', this.checked) }
, checked: false
, text: coll.title
}
})
@greggy
greggy / gist:1a65c83ed2828e24ac0e
Created January 31, 2015 15:04
Get timestamp with TZ
###########################################
## 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())) )
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,
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
}
}
# coding: utf8
import pymongo
connection = pymongo.MongoClient('localhost', 27017)
db = connection.photos
albums = db.albums
images = db.images
{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
%%%-------------------------------------------------------------------
%%% @author greg <>
%%% @copyright (C) 2012, greg
%%% @doc
%%%
%%% @end
%%% Created : 29 Nov 2012 by greg <>
%%%-------------------------------------------------------------------
-module(ring).