Add your feed to SetSticker.com! Promote your sites and attract more customers. It costs only 100 EUROS per YEAR.

Title


Description

TAG 15/20


Your domain [ rss | feed ]


Pleasant surprises on every page! Discover new articles, displayed randomly throughout the site. Interesting content, always a click away

Jhimy F. Villar

My life, my code and everything between it.

Copy postgres query results to file 23 Oct 2017, 12:00 am

COPY (query)
TO 'file';
COPY (select * from users)
TO '/tmp/users.csv';

https://www.postgresql.org/docs/9.2/static/sql-copy.html

Find trailing spaces in files 23 Oct 2017, 12:00 am

find ./directory -type f -exec egrep -l " +$" {} \;

Elixir Mapset 4 Sep 2017, 12:00 am

https://hexdocs.pm/elixir/MapSet.html

Calculating the difference between lists:

iex(1)> map1 = [1,2,3,4,5]
[1, 2, 3, 4, 5]
iex(2)> map2 = [1,2,3,6]
[1, 2, 3, 6]
iex(3)> map1 -- map2
[4, 5]

For bigger lists/sets (millions of items) use Mapset to make it faster:

  Mapset.difference(Mapset.new(map1), Mapset.new(map2))

Restore mongodb from compressed gz file 4 Sep 2017, 12:00 am

  gunzip -c filename.gz | psql -U user --dbname dbname

rack-www 2.3 released 24 Jul 2017, 12:00 am

The gem rack-www had been updated to 2.3, this version introduces host-regex option:

  config.middleware.use Rack::WWW, :host_regex => /example/i

This will only redirect when the host matches example, such as example.com or example1.com. It won’t redirect on localhost or any other host that does not match the regex

https://github.com/stjhimy/rack-www

https://rubygems.org/gems/rack-www

Generate maps based on ranges in Elixir 3 May 2017, 12:00 am

Enum.into(1..100, %{}, &({to_string(&1), &1}))
%{"61" => 61, "58" => 58, "49" => 49, "10" => 10, "92" => 92, "37" => 37,
  "78" => 78, "79" => 79, "93" => 93, "87" => 87, "24" => 24, "96" => 96,
  "14" => 14, "98" => 98, "75" => 75, "71" => 71, "12" => 12, "42" => 42,
  "36" => 36, "16" => 16, "4" => 4, "81" => 81, "26" => 26, "64" => 64,
  "34" => 34, "77" => 77, "32" => 32, "82" => 82, "46" => 46, "8" => 8,
  "5" => 5, "3" => 3, "19" => 19, "90" => 90, "9" => 9, "7" => 7, "50" => 50,
  "55" => 55, "13" => 13, "94" => 94, "44" => 44, "73" => 73, "52" => 52,
  "74" => 74, "68" => 68, "89" => 89, "57" => 57, "2" => 2, "45" => 45,
  "11" => 11, ...}

https://hexdocs.pm/elixir/Enum.html#into/3

Ruby vs Elixir - json benchmark 10 Mar 2017, 12:00 am

A few points before we show the numbers:

  • This is far away from being a scientific experiment
  • I used a MacBook Air (11-inch, Early 2014) 1,7 GHz Intel Core i7 with SSD
  • The json nodes included strings, numbers and dates

90k nodes, 50 MB file

ruby: 5.2 ~ 6.0 seconds

2.3.1 :001 > file = File.read("file.json"); nil
 => nil
2.3.1 :002 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007f9510463f78 @label="", @real=5.641726000001654>
2.3.1 :003 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007f954b7ecdf8 @label="", @real=5.2335819999862>
2.3.1 :004 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007f952c75ff80 @label="", @real=5.775779999996303>
2.3.1 :005 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007f9530863f40 @label="", @real=6.034717000002274>

elixir: 6.3 ~ 6.6 seconds

iex(1)> {:ok, file} = File.read("file.json"); nil
nil
iex(2)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{6467583, nil}
iex(3)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{6524216, nil}
iex(4)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{6636715, nil}
iex(5)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{6353399, nil}

450k nodes, 250 MB file

ruby: 30 ~ 43 seconds

2.3.1 :001 > file = File.read("file.json"); nil
 => nil
2.3.1 :002 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007ff382896070 @label="", @real=30.37597399999504>
2.3.1 :003 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007ff32e2db648 @label="", @real=43.56279799999902>
2.3.1 :004 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007ff35449bf70 @label="", @real=44.20812600001227>
2.3.1 :005 > Benchmark.measure{JSON.parse(file).to_json}
 => #<Benchmark::Tms:0x007ff311cc3f60 @label="", @real=43.153361000004224>

elixir: 30 ~ 35 seconds

iex(1)> {:ok, file} = File.read("file.json"); nil
nil
iex(2)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{30292016, nil}
iex(3)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{33436981, nil}
iex(4)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{35012181, nil}
iex(5)> :timer.tc(fn ->  Poison.decode(file) |> Poison.encode; nil end)
{35776466, nil}
iex(6)>

Owning discipline - 1 year coding. 1 Dec 2016, 12:00 am

Code everyday, for one year, starting December 1 2015 ending December 1 2016. Done.

Debugging cronjobs execution errors 23 Nov 2016, 12:00 am

You can output the cronjob to a file and debug it:

#0,05,10,20,30,40,45,50,54 * * * * /bin/bash -l -c 'cd /foo/bar && bundle exec rake foo:bar\
	>> /foo/bar/output.log 2>&1'

Cronjobs not finding bundle 22 Nov 2016, 12:00 am

In a fresh linux installation you may find this issue when running bundle commands inside a cronjob:

/bin/bash: bundle: command not found

You can fix it by adding your PATH to the crontab file:

Open crontabs:

crontab -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Page processed in 0.3 seconds.

Loading Offers..
Home Privacy Policy