tag:blogger.com,1999:blog-55643994518541689402024-03-14T11:33:11.412+01:00Notes of Software EngineerRecords of adventures called programming. For the future me.Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.comBlogger54125tag:blogger.com,1999:blog-5564399451854168940.post-15715096959963936052018-09-30T00:03:00.001+02:002018-09-30T00:03:27.983+02:00A city council challangeI've decided to stand for an election for a city council. Thererefor posting a link to my local non-politician group just so we appear in Google: <a href="https://wspolnyolsztyn.pl/kandydaci/">Wspólny Olsztyn</a> and to my profile: <a href="https://www.facebook.com/Pawe%C5%82-Szczur-Kandydat-KWW-Wsp%C3%B3lny-Olsztyn-do-Rady-Miasta-180976646058826/?modal=admin_todo_tour">Paweł Szczur. Kandydat KWW Wspólny Olsztyn do Rady Miasta Olsztyna.</a><br />
<br />
I don't promise to build a cosmodrome or a new highway. My purpose is to make Olsztyn a smart city. Smart city as a city which makes decisions based on data, not necessarily buys a ton of new solutions and pretends it's smart.<br />
<br />
I want to introduce a data based thinking into the city board and public institutions. The investitions should have KPI and we, as citizens should be presented with results. No more wishful thinking and guessing.Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com010-315 Olsztyn, Poland53.778422 20.4801191999999953.6282875 20.157395699999991 53.9285565 20.802842699999989tag:blogger.com,1999:blog-5564399451854168940.post-44682441702749731842017-05-11T17:58:00.002+02:002017-05-11T17:58:56.769+02:00Tensorflow custom operation - problem with word2vec operationThis is just a quick note for myself. One beautiful afternoon I got a below error:<br />
<pre class="prettyprint bash"><code>
tensorflow.python.framework.errors_impl.NotFoundError:
models/tutorials/embedding/word2vec_ops.so:
undefined symbol: _ZN10tensorflow16ReadFileToStringEPNS_3EnvERKSsPSs
</code></pre>
The distributed package of Tensorflow uses gcc 4.<br />
If you compile from sources on more or less up to date Ubuntu or Debian, you probably will have gcc 5 or newer installed.<br />
gcc 5 and gcc 4 ABI are not compatible, thus your Tensorflow and operations <b>MUST</b> be compiled with same ABI.<br />
You have two choices either compile the Tensorflow with old ABI support by providing <br />
<pre><code>-cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"</code></pre>
flag to <b>bazel</b> build or
ensure your operations are compiled with new ABI.<br />
You can also force gcc5 to use old ABI (<a href="https://github.com/tensorflow/models/tree/master/tutorials/embedding">source</a>):
<br />
<pre><code>
TF_INC=$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')
g++ -std=c++11 -shared word2vec_ops.cc word2vec_kernels.cc \
-o word2vec_ops.so -fPIC -I $TF_INC -O2 <b>-D_GLIBCXX_USE_CXX11_ABI=0</b>
</code></pre>
<p>More could be found in Tensorflow doc: <a href="https://www.tensorflow.org/install/install_sources#build_the_pip_package">https://www.tensorflow.org/install/install_sources#build_the_pip_package</a>
</p><p>References:
<ul><li><a href="https://github.com/tensorflow/tensorflow/issues/6473">github.com/tensorflow/tensorflow/issues/6473</a></li>
<li><a href="https://github.com/tensorflow/tensorflow/issues/6473">github.com/tensorflow/tensorflow/pull/6997</a></li></ul>
</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-29752088279199270432017-01-27T12:20:00.001+01:002017-01-27T12:20:38.685+01:00To shadow or to not?Long story short: it's easy to accidentally shadow a variable by using <code>:=</code> operator in <code>if</code> statement. Be aware:
<pre><code>
package main
import (
"fmt"
)
func f() (int, int) { return 2, 2 }
func main() {
var x = 1
if x, y := f(); x == y {
fmt.Printf("%d %d\n", x, y)
}
fmt.Printf("%d\n", x)
x, y := f()
fmt.Printf("%d %d\n", x, y)
}
</code></pre>
The output is:
<pre><code>
2 2
1
2 2
</code></pre>
Run in Golang Play: <a href="https://play.golang.org/p/lymn2gl6Wi">https://play.golang.org/p/lymn2gl6Wi</a>.Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-45160408607483380012016-07-09T09:40:00.000+02:002016-07-10T12:26:14.636+02:00JavaScript not evaluating all functions in condition<p>I’ve just been struck by a simple though nuance code optimization. I’ve met it probably thousands times. In fact, some style guides I’ve followed forbids to do what I’ve just done ;-)</p>
<p>First let me explain what we want to achive. We want to call 3 functions and early return if all 3 returned <code>false</code>. Does the below code look good for you?</p>
<pre class="prettyprint"><code class="prettyprint">if (!func0() && !func1() && !func2()) {
return;
}
</code></pre>
<p>Unfortunately it’s wrong. The JavaScript execution engine will optimize it and if the <code>func0</code> call returns <code>true</code> (negated by <code>!</code>) then the <code>func1</code> and <code>func2</code> won’t be called at all.</p>
<p>This phenomenon has a name and it’s called <a href="1">short-circut evaluation</a>, quoting Wikipedia:</p>
<blockquote>
<p>the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression</p>
</blockquote>
<p>This applies to third and further arguments. Thus the lesson is:
<strong>never put function calls in logical statements</strong>, at least not functions you want guarantee to be executed.</p>
<p>Changed code:</p>
<pre class="prettyprint"><code>var skip = !func0();
skip &= !func1();
skip &= !func2();
if (skip) {
return;
}
</code></pre>
<p>I’m sure the world is full of this kind of bugs.</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-27301494050767829082016-05-10T14:00:00.001+02:002016-05-10T14:07:42.165+02:00Postmortem culture<h1><a id="Culture_0"></a>Culture</h1>
<p>It happens, something goes wrong, the system goes down, it stops proceeding orders or serve ads or … situation becomes nervous.</p>
<p>I would say one of the main differences how organisation handles those critical situations is a pretty good indicator of it shape. I’ve seen a companies putting the employees under huge pressure during incidents and blaming after.</p>
<p>Then, I’ve worked for Google and… When you make mistake, the impact factor happens to be thousands QPS. You look at graph and see how some stats goes down or crazily spike. You rollback or do emergency release and it starts (ok, it’s more complex, but hey). You (in most cases more than you) are responsible for writing a <strong>postmortem</strong>.</p>
<h1><a id="What_is_postmortem_8"></a>What is a postmortem?</h1>
<p>In short, it’s a document describing what happened. I know in many companies it looks different, many adopted it after some ex-Googler has joined them (can we call it Googleism? or Googleisation?). What is important in Google’ postmortems is it’s purpose and main pur aim of postmortem is: <strong>to learn and never repeat old mistakes</strong>.</p>
<p>That changes the perspective dramatically. Writing about your own fuckup is not trivial and writing in no-blame way is even harder. It’s not easy even for local stars (local genius theory). How to write postmortem? What to put in it? How it should look? Check out the links</p>
<ul>
<li><a href="https://codeascraft.com/2012/05/22/blameless-postmortems/">Blameless postmortems</a> - how and what? (<a href="https://web.archive.org/web/20160505044123/https://codeascraft.com/2012/05/22/blameless-postmortems/">archive</a>)</li>
<li>Google Chrome <a href="https://docs.google.com/document/d/1oBYQmpBthPfxMvW0XgHn7Bu918n6eFLlQM7nVhEdF_0/edit">postmortem template</a>.</li>
<li>How to write Incident Report / Postmortem - <a href="https://sysadmincasts.com/episodes/20-how-to-write-an-incident-report-postmortem">video</a></li>
</ul>
<h1><a id="Whats_next_22"></a>What’s next?</h1>
<p>The document usually should be created in next 24-48h after, unless the incident is very complex or not understood. Usually, the PM are open for comments, so everyone may ask a question for some details. When ready and the document went through some ‘peer-review’, it’s should be sent to all interested parties (mailing group, #slack, whatever) and it should be discoverable. Means, it should end up in bug tracker under the issue (because, you have an open issue about the outage, right?). It should be kept in some postmortems’ database. Thus, even if it’s not you who pushed a binary or write the code, you will learn. And in the future, you can always look for similar cases.</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-30928729145677986392016-05-04T15:20:00.000+02:002016-05-10T14:11:53.312+02:00Read from file: Length-prefixed protocol buffers<h1><a id="File_format_0"></a>File format</h1>
<p>In short it’s binary encoded protobuf message prefixed with the size. For longer description please check <a href="http://eli.thegreenplace.net/2011/08/02/length-prefix-framing-for-protocol-buffers">Length-prefix framing for protocol buffers</a>.</p>
<p>In most languages the file is read as stream. If you need to open and parse a file which holds lenght-prefixed protobuf messages in Python, you read internet and you find a code which read the whole file. It’s huge bottleneck. The hit in a performance between reading whole file then parsing byte after byte and using BufferedReader was in 1000x. Thus enjoy my little piece of code:</p>
<pre><code class="prettyprint python">def ReadItm(fname, <span class="hljs-function"><span class="hljs-keyword">constructor</span>, <span class="hljs-title">size_limit</span> = 0):</span>
<span class="hljs-string">''' Reads and parses a length prefixed protobuf messages from file.
The file MUST not be corrupted. The parsing is equivalent to parseDelimitedFrom.
'''</span>
f = None
<span class="hljs-keyword">if</span> fname.endswith(<span class="hljs-string">'.gzip'</span>):
f = gzip.open(fname, <span class="hljs-string">'rb'</span>)
<span class="hljs-keyword">else</span>:
f = open(fname, <span class="hljs-string">'rb'</span>)
reader = BufferedReader(f)
bytes_read = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> size_limit<=<span class="hljs-number">0</span> <span class="hljs-keyword">or</span> bytes_read<size_limit:
buffer = reader.peek(<span class="hljs-number">10</span>)
<span class="hljs-keyword">if</span> len(buffer) == <span class="hljs-number">0</span>:
<span class="hljs-keyword">break</span>
(size, position) = decoder._DecodeVarint(buffer, <span class="hljs-number">0</span>)
reader.<span class="hljs-keyword">read</span>(position)
itm = <span class="hljs-function"><span class="hljs-keyword">constructor</span><span class="hljs-params">()</span>
<span class="hljs-title">itm</span>.<span class="hljs-title">ParseFromString</span><span class="hljs-params">(reader.<span class="hljs-keyword">read</span>(size)</span>)
<span class="hljs-title">bytes_read</span> = <span class="hljs-title">bytes_read</span> + <span class="hljs-title">position</span> + <span class="hljs-title">size</span>
<span class="hljs-title">yield</span> <span class="hljs-title">itm</span>
<span class="hljs-title">f</span>.<span class="hljs-title">close</span><span class="hljs-params">()</span>
</span></code></pre>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Śródmieście, Olsztyn, Polska53.776362661015391 20.47632694244384853.774016661015388 20.471284442443849 53.778708661015393 20.481369442443846tag:blogger.com,1999:blog-5564399451854168940.post-63376506874250226622016-04-07T16:33:00.000+02:002016-04-07T16:34:38.311+02:00Go: imagick image croping after rotation problemI need to load image rotate it by some angle, then crop some part of it. Seems trivial, isn't?<br />
<div>
<br /></div>
<div>
I use: <span style="font-family: "courier new" , "courier" , monospace;">gopkg.in/gographics/imagick.v2/imagick</span><span style="font-family: inherit;"> package.</span><br />
<span style="font-family: inherit;"><br /></span>
<b>Load image</b><br />
<pre class="prettyprint go"><code>iwand := imagick.NewMagickWand()
defer iwand.Destroy()
if err := iwand.ReadImage(s); err != nil {
log.Panicf("cannot open image %s", err)
}</code></pre>
<b>Rotate it, fill new area with yellow</b><br />
<pre class="prettyprint go"><code>w := iwand.GetImageWidth()
h := iwand.GetImageHeight()
log.Printf("old size: %d,%d", w, h)
pwand := imagick.NewPixelWand()
pwand.SetColor("yellow")
if err := iwand.RotateImage(pwand, 45); err != nil {
log.Panicf("problem with rotation: %s", err)
}
</code></pre>
<b>Calculate the position and size of the rectangle to cut</b><br />
<pre class="prettyprint go"><code>newW := calcSize(w, h)
newH := newW
w = iwand.GetImageWidth()
h = iwand.GetImageHeight()
log.Printf("new size: %d,%d", w, h)
x := int((w - newW) / 2)
y := int((h - newH) / 2)</code></pre>
<b>Make the area red (with transparency), just for learning</b><br />
<pre class="prettyprint go"><code>dwand := imagick.NewDrawingWand()
pwand.SetColor("red")
pwand.SetOpacity(0.5)
dwand.SetFillColor(pwand)
dwand.Rectangle(float64(x), float64(y), float64(newW+uint(x)), float64(newH+uint(y)))
iwand.DrawImage(dwand)</code></pre>
<b>Crop and save to file</b><br />
<pre class="prettyprint go"><code>log.Printf("w,h,x,y: %d,%d,%d,%d", newW, newH, x, y)
if err := iwand.CropImage(newW, newH, x, y); err != nil {
log.Fatalf("problem with crop: %s", err)
}
iwand.WriteImage(d)</code></pre>
</div>
<div>
<br />
<b>Input file</b>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqnig7xmrf9RD2eCQNCm_Ojo7jiWUHtiaqRzMW3y8G_0LCg-SZSxNbP3zpFZaWbzmNdHvMfJUOQFBvF6bHHPaJK7Bt6QN2FEiFix9bqQQnBFV8XkNGRKzJRba4JeEAQQkpaZe7K4bLcLU/s1600/example.jpeg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="184" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqnig7xmrf9RD2eCQNCm_Ojo7jiWUHtiaqRzMW3y8G_0LCg-SZSxNbP3zpFZaWbzmNdHvMfJUOQFBvF6bHHPaJK7Bt6QN2FEiFix9bqQQnBFV8XkNGRKzJRba4JeEAQQkpaZe7K4bLcLU/s320/example.jpeg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Original image (from: https://en.wikipedia.org/wiki/Linux)</td></tr>
</tbody></table>
<br />
<b>Output file</b>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN4I6ps4D8QmEaBaI25op9yabdP0trzUSk5OjeSvXBcKNpTU7zjMyWWT21ynJ3dS93PBxTCN4FZ7L7Fdx1xdsq8lTLW8nGRYVgFDC7Uiisx4yigJS-xB1ixsO-tWAoGNSPgPG3-Rb6nYM/s1600/example.cut.jpeg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN4I6ps4D8QmEaBaI25op9yabdP0trzUSk5OjeSvXBcKNpTU7zjMyWWT21ynJ3dS93PBxTCN4FZ7L7Fdx1xdsq8lTLW8nGRYVgFDC7Uiisx4yigJS-xB1ixsO-tWAoGNSPgPG3-Rb6nYM/s320/example.cut.jpeg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Image rotated and cropped</td></tr>
</tbody></table>
<br />
<b>Need to repage!</b><br />
<pre class="prettyprint go"><code>log.Printf("w,h,x,y: %d,%d,%d,%d", newW, newH, x, y)
iwand.ResetImagePage("")
if err := iwand.CropImage(newW, newH, x, y); err != nil {
log.Fatalf("problem with crop: %s", err)
}
iwand.WriteImage(d)
</code></pre>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGUtFh2241kiTFSDGmJzr-IGOpghbZB2THiTnNkvztE4kqZEOWPi_XzMZLHai9WzNWL8-JmQgyIx0LJEfagYymDUqnLZ5EbuWnv8g72MN_wMMPfdz3JQU8XelPdee2WburYBYJ6T5-Kvw/s1600/example.cut.jpeg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGUtFh2241kiTFSDGmJzr-IGOpghbZB2THiTnNkvztE4kqZEOWPi_XzMZLHai9WzNWL8-JmQgyIx0LJEfagYymDUqnLZ5EbuWnv8g72MN_wMMPfdz3JQU8XelPdee2WburYBYJ6T5-Kvw/s320/example.cut.jpeg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Rotated and cropped - fixed</td></tr>
</tbody></table>
</div>
Full source: <a href="https://gist.github.com/orian/36dfc5bd622102b87f6e140d4e65379c">gist</a>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-11132334290210233272016-03-11T12:30:00.003+01:002016-03-14T16:16:29.761+01:00Android developer roadSo, I'm going through a quick Android developer course. The main target is to learn how to provide the best Android apps with minimal effort. Assumption is we all know Java ;-)<br />
<h2>
The start</h2>
A good starting point is:<br />
<a href="http://developer.android.com/training/index.html">http://developer.android.com/training/index.html</a><br />
<br />
I don't recommend to read it all at the beginning. I would suggest:<br />
<ul>
<li>Getting started (<a href="http://developer.android.com/training/index.html">http://developer.android.com/training/index.html</a>)</li>
<li>Building apps with content sharing (<a href="http://developer.android.com/training/building-content-sharing.html">http://developer.android.com/training/building-content-sharing.html</a>)</li>
</ul>
<div>
Others as needed, Best Practices for Interaction and Engagement (<a href="http://developer.android.com/training/best-ux.html">http://developer.android.com/training/best-ux.html</a>) also helps.</div>
<div>
<br /></div>
<div>
The above gives you a rough overview. The real stuff comes in API Guides (<a href="http://developer.android.com/guide/components/index.html">http://developer.android.com/guide/components/index.html</a>). I do recommend to read the first three sections:<br />
<ul>
<li>Introduction (<a href="http://developer.android.com/guide/index.html">http://developer.android.com/guide/index.html</a>)</li>
<li>App Components (<a href="http://developer.android.com/guide/components/index.html">http://developer.android.com/guide/components/index.html</a>)</li>
<li>App Resources (<a href="http://developer.android.com/guide/topics/resources/index.html">http://developer.android.com/guide/topics/resources/index.html</a>)</li>
</ul>
<div>
This gives you a high level overview of how the Android works.</div>
</div>
<div>
<br /></div>
<h2>
The lifecycle</h2>
<div>
You basically should have it at your desk. You will use it often. It's been reported to have some inaccuracies but it's still better than the Google provided simplified version.</div>
<div>
<br /></div>
<div>
(check out: <a href="https://github.com/xxv/android-lifecycle">https://github.com/xxv/android-lifecycle</a>)</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt6pp4TdopClxDZhn-myfuPfx-d8Id1WePhEGbkMdSnvzTmc1XhIz6hl30ErBhmMl0wDcvhprkbzLv1ZzHRuLHmDkCqqFBwoF4RQRMT-QpOQ7_7joAOc0bpJDVLjwPdWZeavgDjTRGVDU/s1600/complete_android_fragment_lifecycle.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt6pp4TdopClxDZhn-myfuPfx-d8Id1WePhEGbkMdSnvzTmc1XhIz6hl30ErBhmMl0wDcvhprkbzLv1ZzHRuLHmDkCqqFBwoF4RQRMT-QpOQ7_7joAOc0bpJDVLjwPdWZeavgDjTRGVDU/s640/complete_android_fragment_lifecycle.png" width="300" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<h2>
Codelabs</h2>
<div>
Google provides two interesting codelabs:</div>
<div>
<ul>
<li>Build a Material Design App with the Android Design Support Library (<a href="https://codelabs.developers.google.com/codelabs/material-design-style/index.html">https://codelabs.developers.google.com/codelabs/material-design-style/index.html</a>)</li>
<li>Android Testing Codelab (<a href="https://codelabs.developers.google.com/codelabs/android-testing/">https://codelabs.developers.google.com/codelabs/android-testing/</a>)</li>
</ul>
<div>
If you have to provide some early results quickly, skip above readings and just do the codelab.</div>
</div>
<h2>
Other must-to-know</h2>
<div>
<b>Butterknife</b> - <a href="http://jakewharton.github.io/butterknife/">http://jakewharton.github.io/butterknife/</a> - saves you lot of boilerplate with binding view elements to variables.</div>
<div>
<b>Android ContentProvider Generator</b> - it will save you a lot of time on boilerplate code - <a href="https://github.com/BoD/android-contentprovider-generator">https://github.com/BoD/android-contentprovider-generator</a><br />
<b>Mosby</b> - <a href="http://hannesdorfmann.com/mosby">http://hannesdorfmann.com/mosby</a> - if you go with MVP this is must to use.<br />
<b>Dagger</b> - dependency injection framework - <a href="http://google.github.io/dagger/users-guide">http://google.github.io/dagger/users-guide</a><br />
<br />
And when you consider which Android version to support, check:<br />
<b>Android stats Dashboard</b> - gives an overview of the Android version distribution - <a href="http://developer.android.com/about/dashboards/index.html">http://developer.android.com/about/dashboards/index.html</a><br />
<h2>
Learning</h2>
</div>
<div>
<h4>
Code Examples</h4>
<a href="https://github.com/mgrzechocinski/dagger2-example">https://github.com/mgrzechocinski/dagger2-example</a><br />
<a href="https://github.com/cgruber/u2020/tree/dagger2">https://github.com/cgruber/u2020/tree/dagger2</a><br />
<a href="https://github.com/frogermcs/DaggerExample">https://github.com/frogermcs/DaggerExample</a><br />
<h4>
Blog Posts</h4>
<a href="http://www.future-processing.pl/blog/dependency-injection-with-dagger-2/">http://www.future-processing.pl/blog/dependency-injection-with-dagger-2/</a><br />
<a href="http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android">http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android</a><br />
<a href="http://konmik.github.io/snorkeling-with-dagger-2.html">http://konmik.github.io/snorkeling-with-dagger-2.html</a><br />
<a href="http://siphon9.net/loune/2015/04/dagger-2-0-android-migration-tips/">http://siphon9.net/loune/2015/04/dagger-2-0-android-migration-tips/</a><br />
<a href="http://blog.gouline.net/2015/05/04/dagger-2-even-sharper-less-square">http://blog.gouline.net/2015/05/04/dagger-2-even-sharper-less-square</a></div>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-44839800183346561812016-02-08T20:25:00.000+01:002016-02-08T20:25:08.532+01:00No, because noI need Math.Round from Go's standard library, poor me:<br />
<br />
<ul>
<li><a href="https://github.com/golang/go/issues/4594">https://github.com/golang/go/issues/4594</a></li>
<li><a href="https://groups.google.com/forum/#!searchin/golang-nuts/math.Round/golang-nuts/ITZV08gAugI/pNsL5FF4pLUJ">https://groups.google.com/forum/#!searchin/golang-nuts/math.Round/golang-nuts/ITZV08gAugI/pNsL5FF4pLUJ</a></li>
</ul>
<br />
I know it's hard. But hey, most languages provide it.<br />
<br />
Rob Pike:<br />
<blockquote class="tr_bq">
Floating point numbers don't have digits.<br />The bar for being useful needs to be pretty high to be in the Go math package. We accepted most of what's in the C library but we're not going to take all the routines from other libraries. The Javascript round is a 1-liner: int(f+0.5). </blockquote>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-48246483886091388232016-01-27T10:28:00.003+01:002016-01-27T10:28:46.188+01:00Angular-2.0.0.beta1<h2>
<a href="https://draft.blogger.com/null" id="Http_problem_2"></a>Http problem</h2>
I was getting an error:<br />
<pre class="prettyprint"><code><span class="hljs-title">error</span> TS2305: Module <span class="hljs-string">'"angular2/http"'</span> has <span class="hljs-built_in">no</span> exported member <span class="hljs-string">'HTTP_PROVIDERS'</span>
</code></pre>
may be caused by simply outdated angular2.d.ts installed through <code>tsd.</code><br />
<blockquote>
<a href="http://definitelytyped.org/">DefinitelyTyped.org</a> typings for Angular2 are outdated, tsd install angular2 gives old .d.ts which makes typescript unhappy.
What’s the new approach or which angular.d.ts is appropriate?</blockquote>
When appropriate angular2 node package is installed everything works fine.<br />
<h2>
<a href="https://draft.blogger.com/null" id="Duplicate_properties_14"></a>Duplicate properties</h2>
The other error I’ve encounter (Angular-2.0.0.beta.1):<br />
<pre class="prettyprint"><code>ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">6</span>,<span class="hljs-number">14</span>): error TS2300: Duplicate identifier <span class="hljs-string">'PropertyKey'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">6</span>,<span class="hljs-number">14</span>): error TS2300: Duplicate identifier <span class="hljs-string">'PropertyKey'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">9</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'done'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">10</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'value'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">248</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'EPSILON'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">283</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'MAX_SAFE_INTEGER'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">290</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'MIN_SAFE_INTEGER'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">346</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'flags'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">498</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">561</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'size'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">570</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">581</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'size'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">590</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">605</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/node_modules/angular2/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">619</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">6</span>,<span class="hljs-number">14</span>): error TS2300: Duplicate identifier <span class="hljs-string">'PropertyKey'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">9</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'done'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">10</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'value'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">248</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'EPSILON'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">283</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'MAX_SAFE_INTEGER'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">290</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'MIN_SAFE_INTEGER'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">346</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'flags'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">498</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">561</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'size'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">570</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">581</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'size'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">590</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">605</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
ts/typings/es6-shim/es6-shim<span class="hljs-class">.d</span><span class="hljs-class">.ts</span>(<span class="hljs-number">619</span>,<span class="hljs-number">5</span>): error TS2300: Duplicate identifier <span class="hljs-string">'prototype'</span>.
</code></pre>
<p>I simply solved it by specifying the files in tsconfig.json explicitly through files. A better solution is to remove <code>typings/es6-shim/</code>.</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-80684213658871499252015-12-17T19:11:00.001+01:002016-01-17T18:53:23.995+01:00Google Identity Toolkit for Android - weird compilation error I’m hacking tiny Android app, one of the libraries I use is Google Identity Toolkit (it’s super easy to integrate).
A <a href="https://developers.google.com/identity/toolkit/android/">documentation</a> states that a Play Services should be added as dependency:<br />
<pre class="prettyprint"><code><span class="hljs-keyword">compile</span> <span class="hljs-string">'com.google.android.gms:play-services:8.3.0'</span>
</code></pre>
For any non trivial application it’s going to be a problem resulting in below error (or similar):<br />
<pre class="prettyprint"><code>Error:Execution failed <span class="hljs-keyword">for</span> task <span class="hljs-string">':app:transformClassesWithDexForDebug'</span>.
> com<span class="hljs-class">.android</span><span class="hljs-class">.build</span><span class="hljs-class">.api</span><span class="hljs-class">.transform</span><span class="hljs-class">.TransformException</span>: com<span class="hljs-class">.android</span><span class="hljs-class">.ide</span><span class="hljs-class">.common</span><span class="hljs-class">.process</span><span class="hljs-class">.ProcessException</span>: org<span class="hljs-class">.gradle</span><span class="hljs-class">.process</span><span class="hljs-class">.internal</span><span class="hljs-class">.ExecException</span>: Process <span class="hljs-string">'command '</span>/usr/lib/jvm/java-<span class="hljs-number">8</span>-oracle/bin/java<span class="hljs-string">''</span> finished with non-zero exit value <span class="hljs-number">2</span>
</code></pre>
Search on Internet points to StackOverflow question: <a href="http://stackoverflow.com/questions/32807587/com-android-build-transform-api-transformexception/32826010">com.android.build.transform.api.TransformException</a>. Apparently the bugs are related. The exit return code 2 is meaningless unless one has any clues. Anyway, the problem can be solved by replacing the dependency by:<br />
<pre class="prettyprint"><code><span class="hljs-keyword">compile</span> <span class="hljs-string">'com.google.android.gms:play-services-plus:8.3.0'</span>
<span class="hljs-keyword">compile</span> <span class="hljs-string">'com.google.android.gms:play-services-auth:8.3.0'</span>
</code></pre>
I’ve opened an issue on Google Identity Toolkit - <a href="https://github.com/googlesamples/identity-toolkit-android/issues/18">Android sample project</a> asking for more documentation.<br />
The error is related to a constraint of Android Dalvik runtime of to <a href="https://developer.android.com/sdk/installing/studio-build.html">65K Reference Limit</a>. There are two solutions for the problem: decreasing the references (usually by chosing smaller dependencies, in our case subpackages instead of the full Play Services) and using so called <a href="https://developer.android.com/tools/building/multidex.html">multidex</a> (<a href="https://www.contentful.com/blog/2014/10/30/android-and-the-dex-64k-methods-limit/">some 3rd party blog</a>).Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-36404522997716567312015-10-19T23:51:00.000+02:002015-10-19T23:52:58.101+02:00Google Analytics doesn't work a.k.a. name collision in JavaScript<p>Today is another great day I’ve encounter a trivial bug hard to catch. It goes as follow. I went to Google Analytics to setup a new page. I’ve followed the instruction and I’ve copied into a source:</p>
<pre class="prettyprint lang-javascript"><code><script>
<span class="hljs-list">(<span class="hljs-keyword">function</span><span class="hljs-list">(<span class="hljs-keyword">i</span>,s,o,g,r,a,m)</span>{i[<span class="hljs-quoted">'GoogleAnalyticsObject</span>']=r<span class="hljs-comment">;i[r]=i[r]||function(){</span>
<span class="hljs-list">(<span class="hljs-keyword">i</span>[r].q=i[r].q||[])</span>.push<span class="hljs-list">(<span class="hljs-keyword">arguments</span>)</span>},i[r].l=1*new Date<span class="hljs-list">()</span><span class="hljs-comment">;a=s.createElement(o),</span>
m=s.getElementsByTagName<span class="hljs-list">(<span class="hljs-keyword">o</span>)</span>[<span class="hljs-number">0</span>]<span class="hljs-comment">;a.async=1;a.src=g;m.parentNode.insertBefore(a,m)</span>
})</span><span class="hljs-list">(<span class="hljs-keyword">window</span>,document,<span class="hljs-quoted">'script</span>',<span class="hljs-quoted">'//www</span>.google-analytics.com/analytics.js',<span class="hljs-quoted">'ga</span>')</span><span class="hljs-comment">;</span>
ga<span class="hljs-list">(<span class="hljs-quoted">'create</span>', <span class="hljs-quoted">'UA-AAAAAAAA-X</span>', <span class="hljs-quoted">'auto</span>')</span><span class="hljs-comment">;</span>
ga<span class="hljs-list">(<span class="hljs-quoted">'send</span>', <span class="hljs-quoted">'pageview</span>')</span><span class="hljs-comment">;</span>
</script>
</code></pre>
<p>and it didn’t work ;-)</p>
<p>First I’ve thought is my mistake, i’ve put a code just before <code></html></code>, but after trying <code></head></code> it
wasn’t any better.</p>
<p>I’ve tried to copy paste the code to developer console and tadah, it works! Here I should mention that I use both Google Closure Library and Google Closure Compiler on my site. After thinking and poking the code
for a moment I’ve made an educated guess:</p>
<blockquote>
<p>Closure compiler compiled code is colliding a name with Google Analytics!</p>
</blockquote>
<p>Took me 1 minute to verify and yes, I’ve got it.</p>
<p>The solution was straight:</p>
<pre class="prettyprint lang-javascript"><code><script>
<span class="hljs-list">(<span class="hljs-keyword">function</span><span class="hljs-list">(<span class="hljs-keyword">i</span>,s,o,g,r,a,m)</span>{i[<span class="hljs-quoted">'GoogleAnalyticsObject</span>']=r<span class="hljs-comment">;i[r]=i[r]||function(){</span>
<span class="hljs-list">(<span class="hljs-keyword">i</span>[r].q=i[r].q||[])</span>.push<span class="hljs-list">(<span class="hljs-keyword">arguments</span>)</span>},i[r].l=1*new Date<span class="hljs-list">()</span><span class="hljs-comment">;a=s.createElement(o),</span>
m=s.getElementsByTagName<span class="hljs-list">(<span class="hljs-keyword">o</span>)</span>[<span class="hljs-number">0</span>]<span class="hljs-comment">;a.async=1;a.src=g;m.parentNode.insertBefore(a,m)</span>
})</span><span class="hljs-list">(<span class="hljs-keyword">window</span>,document,<span class="hljs-quoted">'script</span>',<span class="hljs-quoted">'//www</span>.google-analytics.com/analytics.js',<span class="hljs-quoted">'dzialaj</span>')</span><span class="hljs-comment">;</span>
dzialaj<span class="hljs-list">(<span class="hljs-quoted">'create</span>', <span class="hljs-quoted">'UA-AAAAAAAA-X</span>', <span class="hljs-quoted">'auto</span>')</span><span class="hljs-comment">;</span>
dzialaj<span class="hljs-list">(<span class="hljs-quoted">'send</span>', <span class="hljs-quoted">'pageview</span>')</span><span class="hljs-comment">;</span>
</script>
</code></pre>
<p>Just for quick explanation, the string I’ve changed is a parameter to function which sets up a Google Analytics
code on a website, it is a name that should be used as a Analytics main function and may be anything one wants.</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-67506404431890735472015-10-08T18:28:00.003+02:002015-11-05T13:55:43.543+01:00Save yourself and pass the context by<p>One of extending packages of Go is <a href="https://godoc.org/golang.org/x/net/context"><code>golang.org/x/net/context</code></a> package. The idea comes form task oriented programming. You get a task to execute but want to attach some data to it. It appears to be useful when programing a RPC or web server solutions. In such cases your listener creates a context object and pass it to handlers. The handlers keep it and use it when appropriate.</p>
<p>This idea guided most Google App Engine APIs for Go. Now they’ve introduced a Managed VM’s which are something between App Engine and Compute Engine. The refactored libraries can be found here: <a href="https://github.com/golang/appengine"><code>google.golang.org/appengine</code></a>.</p>
<p>The standard way of logging in App Engine was to get a context and call methods:</p>
<pre class="prettyprint lang-go"><code class="language-go"><span class="hljs-keyword">func</span> SomeHandler(w http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
ctx.Infof(<span class="hljs-string">"got request!"</span>)
}
</code></pre>
<p>Whereas the new way:</p>
<pre class="prettyprint lang-go"><code class="language-go"><span class="hljs-keyword">func</span> SomeHandler(ctx context.Context, w http.ResponseWriter, req *http.Request) {
log.Infof(ctx, <span class="hljs-string">"got request!"</span>)
}
</code></pre>
<p>The new way has one huge advantage, it allows you to pass any context which implements interface <code>context.Context</code>. It also requires to replace all <code>appengine/log</code> imports by a new package <code>google.golang.org/appengine/log</code></p>
<p>The Go’ standard <code>log</code> library doesn’t have a concept of context at all.</p>
<h1><a id="Reasoning_22"></a>Reasoning</h1>
<p>I guess I should explain the advantage of using a context based log instead of standard one. In a case of task oriented application it allows the programmer to group all log messages related to given task. It’s a common practice to group messages by the place of the log function callback (the file and line where the log message is written). More sophisticated grouping includes process id and it may be a task id (or context in our case).
Just imagine how much time it saves during debugging. It also allows much more sophisticated analysis of the server event flow without running in debugging mode.</p>
<p>I’ve created a tiny wrapper around both <a href="github.com/golang/glog"><code>github.com/golang/glog</code></a> library and <code>google.golang.org/appengine/log</code>:
<a href="https://godoc.org/github.com/orian/utils/net/log"><code>github.com/orian/utils/net/log</code></a>. It should allow you to log both at App Engine and stand alone processes. Enjoy.</p>
<p>
A real in depth explanation can be found in Go blog' article: <a href="http://blog.golang.org/context">Go Concurrency Patterns: Context</a></p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-53731957950781438312015-09-29T19:39:00.001+02:002015-09-29T19:42:09.952+02:00Upgrading an ASUS Z87 PLUS motherboard firmwareI needed to upgrade a firmware (in fact, it should have been done a long time ago). The steps goes as follow:<br />
<br />
<ol>
<li>Find a firmware: (https://www.asus.com/Motherboards/Z87PLUS/HelpDesk_Download/)<br />http://dlcdnet.asus.com/pub/ASUS/mb/LGA1150/Z87-PLUS/Z87-PLUS-ASUS-2103.zip</li>
<li>Unpack it and localize Z87-PLUS-ASUS-2103.CAP (or some other older version Z87-PLUS-ASUS-2004.CAP or Z87-PLUS-ASUS-1802.CAP)</li>
<li>Prepare a FAT flash device</li>
<li>Copy the firmware *.CAP file to flash root directory and rename to: <b>Z87PL.CAP</b></li>
<li>Plug flash into the 'flashback' USB port at the back of PC (it's next to ethernet with a green border around)</li>
<li>Press flashback button on a motherboard, it's below a power plug, labeled as "BIOS_FLBK"</li>
</ol>
<div>
The FLBK_LED should start blinking (if it stops blinking means it cannot read firmware or the file is broken). It will take few minutes. DONE.<br />
<br />
<br />
BTW. It sucks that Asus just doesn't say what should be the name of a firmware file. They could just use FIRMWARE.CAP and it would be fine. There are other ways of protecting people from themselves than encoding the model in a file name.</div>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com1Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-28960795270608712822015-09-15T19:19:00.001+02:002015-10-25T15:39:19.439+01:00Slow compilation in GolangBecause I'm lazy and Go tends to not backport it's libraries, I've decided to migrate
a project from go1.4 to go1.5. Everything would be great but the compilation process
of the new version is noticeably slower than the old one.<br />
<h2>
Compile all your dependencies</h2>
What I've noticed some time ago is that if you change the version of Go the libs stay at old version.
This causes them to be built on each <code>go run</code> you invoke. And if you use a library which
automates rebuilding the binary on change like reflex () than you may wait long.
<br />
<h3>
List all deps</h3>
To list all dependencies your application or library have you can invoke:<br />
<pre class="prettyprint"><code>go list -f '{{.Deps}}' | tr "[" " " | tr "]" " " | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' > deps</code></pre>
(borrowed from from <a href="https://groups.google.com/forum/#!topic/golang-nuts/611QEJqkDKw">go-nuts</a>)
<br />
<h3>
Rebuild all packages</h3>
After that you can call:<br />
<pre class="prettyprint"><code>go install -a $(tr -d '\n' < deps)</code></pre>
<h3>
Update all packages</h3>
Alternative to the above, updates the code:<br />
<pre class="prettyprint"><code>go install -u $(tr -d '\n' < deps)</code></pre>
<h3>
Speedup</h3>
For my code it was from 15 seconds to below 1 second.
<p>
<h3>Apply to all subpackages in a project</h3>
As <a href="https://www.blogger.com/profile/14791663389514120922">Craig Furman</a> suggested in a <a href="http://dev.pawelsz.eu/2015/09/slow-go-compilation.html?showComment=1442674650383#c5688509318725810779">comment</a> (thanks):
<pre class="prettyprint"><code>go list -f '{{.Deps}}' ./... | tr "[" " " | tr "]" " " | \
xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' | \
xargs go install -a
</code></pre>
</p>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com3Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-50326588731290044832015-09-12T23:21:00.001+02:002015-09-12T23:28:29.098+02:00MySQL under the hood hurts<p>
Recently I've been poking around MySQL, i've forgot how it's when someone makes a lot of decisions for you, e.g. that a column of type <code>TIMESTAMP</code> should have a <b>default</b>
value set to <b>CURRENT_TIMESTAMP</b> or that on each row write it should be automatically updated to CURRENT_TIMESTAMP, and btw did you know, that NOW() and CURRENT_TIMESTAMP are not equal (check <a href="http://www.hydrogen18.com/blog/time-zones-and-databases-dont-mix.html">this</a>)?
</p>
<p>
The simplest way to discover what MySQL have done for you is to either read the documentation (which is kind of long) or:
<pre class="prettyprint sql"><code>SHOW CREATE TABLE table;
</code></pre>
This will print you the full command necessary to recreate the table in the same state as it's currently:
<pre class="prettyprint sql"><code>CREATE TABLE `table` (
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(255) DEFAULT NULL,
`tags` text,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`state` int(11) DEFAULT '0',
`date_of_publication` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`auth_fb_share` tinyint(1) DEFAULT NULL,
CONSTRAINT `table_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8</code></pre>
</p>
<p>
But why is it a problem?<br/>
Because by default CURRENT_TIMESTAMP inserts the value with timezone shift, but without the information about time zone. Let's say it's 2015-09-09 21:23 +02:00. The TIMESTAMP keeps the data as UTC (+00:00). The proper value should be: 2015-09-09 19:23.<br/>But the CURRENT_TIMESTAMP will
insert the data as: 2015-09-09 21:23 skipping time zone information.<br/>
This causes a shift of X hours depending what's a server timezone is.
</p> Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-16694971338465983882015-07-30T15:50:00.000+02:002017-05-11T17:58:48.242+02:00Go: a list of structure field names<pre class="prettyprint golang"><code>package main
import (
"fmt"
"reflect"
)
type User struct {
Name string
Age int
Password string
}
func GetFieldNames(s interface{}) []string {
t := reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if kind := t.Kind(); kind != reflect.Struct {
fmt.Printf("not a struct kind: %s", kind)
return nil
}
var ret []string
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
ret = append(ret, field.Name)
}
return ret
}
func main() {
u := User{}
fmt.Println(GetFieldNames(u))
fmt.Println(GetFieldNames(&u))
}
</code></pre>
At Go Playground: <a href="http://play.golang.org/p/6LDoCIKFeH">http://play.golang.org/p/6LDoCIKFeH</a>
<br />
The function returns a slice of strings. Each is a name of a field. If a pointer is an argument the underlying type's field names are returned.
The function ended up in: <a href="https://github.com/orian/utils">github.com/orian/utils/reflect</a>. A documentation is browsable at: <a href="https://gowalker.org/github.com/orian/utils/reflect">https://gowalker.org/github.com/orian/utils/reflect</a>.
<br />
A short explanation:
<br />
<ul>
<li>First get a <code>reflect.Type</code> of a given argument <code>s</code></li>
<li>If <code>s'</code> kind is a pointer, get a descriptor of underlying type.</li>
<li>If underlying type is not a struct, return error..</li>
<li>Iterate over fields, add field names and return the result slice.</li>
</ul>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-28137950501600652452015-07-01T14:01:00.002+02:002015-07-01T14:01:34.833+02:00TIL: clicked element reference in JavaScript consoleI had a break from WebDev for around 2 years. I'm rediscovering things now. Today I've learnt: <code>$0</code>
<ul>
<li>Right click on the element of interest in your browser and select 'inspect element'. You should see the browser debugger with the element you clicked on highlighted.</li>
<li>The debugger allows you to access the currently selected element in the console as <code>$0</code> variable.</li>
</ul>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Olsztyn, Polska53.778422 20.48011930000006953.6282875 20.157395800000071 53.9285565 20.802842800000068tag:blogger.com,1999:blog-5564399451854168940.post-19322415611860988352015-06-11T00:26:00.003+02:002015-06-11T00:28:09.721+02:00TIL: never depend on the order of evaluation of sub-expressions in a streamThe code below may result in a 6 different execution orders. It will surprise and hurt you, so don't do that ;-)
<pre class="prettyprint cpp">
std::cout << wizard.CastSpell("abracadabra") << wizard.DrinkPotion("Rosanke") << wizard.Mana();
</pre>
More info can be found:
<ul>
<li><a href="http://stackoverflow.com/questions/14809978/order-of-execution-in-operator">http://stackoverflow.com/questions/14809978/order-of-execution-in-operator</a></li>
<li><a href="https://www.securecoding.cert.org/confluence/display/c/EXP10-C.+Do+not+depend+on+the+order+of+evaluation+of+subexpressions+or+the+order+in+which+side+effects+take+place">EXP10-C. Do not depend on the order of evaluation of subexpressions or the order in which side effects take place</a></li>
</ul>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0Śródmieście, Olsztyn, Polska53.776210498757372 20.47615528106689553.773864498757369 20.471112781066896 53.778556498757375 20.481197781066893tag:blogger.com,1999:blog-5564399451854168940.post-68355253976213163162015-06-01T23:13:00.000+02:002015-06-01T23:13:16.867+02:00Endless extracting of tar.bz2 archiveFunny story. Over a year ago I've backed up a project directory to tar.bz2 archive. Today I've decided to check what's inside. I've encountered a frozen window of 'extract here' option from file context menu.<br />
<br />
I've killed the extract process and rerun it from a terminal:<br />
<pre class="prettyprint">tar -xvvf archive.tar.bz2</pre>
After few normal lines i've seen smth (in fact thousands of such lines):
<pre class="prettyprint">
hrwxr-x--- user/group 0 2013-02-06 23:20 workspace/a/b/a/b/f.go link to workspace/a/b/f.go
hrwxr-x--- user/group 0 2013-02-06 23:20 workspace/a/b/a/b/a/b/f.go link to workspace/a/b/a/b/f.go
</pre>
This clued me, that probably the archive kept the symbolic links.
After Googling and reading <code>tar</code> manual I had this idea: let's remove a symlink from an archive:
<pre class="prettyprint">
tar --delete -f archive.tar workspace/a/b/a
</pre>
This worked just well :-)Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-76391942438425719002015-05-28T20:00:00.003+02:002015-05-28T20:00:27.404+02:00pep8I've got very comfortable with clang-format for C++ and gofmt for Go.
Every time I have to use some other language i'm missing them very much.
Today I've found a solution for Python. It makes life so much easier.
The two awesome tools are:
<br />
<h2>
pep8</h2>
<blockquote class="tr_bq">
pep8 is a tool to check your Python code against some of the style conventions in PEP 8.
</blockquote>
<a href="https://pypi.python.org/pypi/pep8">https://pypi.python.org/pypi/pep8</a>
<h2>
autopep8</h2>
<blockquote class="tr_bq">
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.
</blockquote>
<a href="https://github.com/hhatto/autopep8">https://github.com/hhatto/autopep8</a>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-23054981321601408312015-04-29T12:48:00.001+02:002015-04-29T12:49:01.466+02:00Torch7 weird error and Google Search without a solutionThe below is an error one gets when working with Torch7 from time to time:
<pre class="prettyprint lua">
/usr/local/bin/luajit: /usr/local/share/lua/5.1/nn/Tanh.lua:4: attempt to index field 'nn' (a nil value)
stack traceback:
/usr/local/share/lua/5.1/nn/Tanh.lua:4: in function 'updateOutput'
/usr/local/share/lua/5.1/nn/Sequential.lua:29: in function 'updateOutput'
/usr/local/share/lua/5.1/nn/Sequential.lua:29: in function 'forward'
main.lua:93: in function 'opfunc'
/usr/local/share/lua/5.1/optim/sgd.lua:43: in function 'sgd'
main.lua:117: in function 'train3'
main.lua:142: in main chunk
[C]: in function 'dofile'
/usr/local/lib/luarocks/rocks/trepl/scm-1/bin/th:131: in main chunk
[C]: at 0x00406260
</pre>
Reading a bit about similar errors(<a href="https://groups.google.com/forum/#!searchin/torch7/Tanh.lua$3A$20attempt$20to$20index$20field$20$27nn$27$20(a$20nil$20value)/torch7/znJqpDENAlk/OHxAU1dW4m8J">1</a>) I've concluded that probably the type of <code>Tensor</code> is
wrong. I work on <code>torch.CudaTensor</code> objects, why the problem? I've forgot to import the
correct library:
<pre class="prettyprint lua">
require("cunn")
</pre>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com1tag:blogger.com,1999:blog-5564399451854168940.post-92087553640270703012015-04-22T10:32:00.001+02:002015-05-03T10:24:42.920+02:00Reflex - trigger an execution of command on a file change eventHow many times you were working on some tiny project that needs to be rebuild / restarted after you modify the code? I guess often.<br />
<br />
After experimenting with some solutions I found an ultimate one: <a href="https://github.com/cespare/reflex">https://github.com/cespare/reflex</a><br />
Installation:
<pre class="prettyprint bash">
go get github.com/cespare/reflex
</pre>
Automatically recompiling markdown file into html every time the file is changed:
<br />
<pre class="prettyprint bash">reflex -r '\.md$' blackfriday-tool -page article.md article.html
</pre>
Or you can restart Go app when saved files:
<pre class="prettyprint bash">
reflex -r '\.go$' -- go run main.go
</pre>Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-15766384423622484382015-04-15T23:01:00.001+02:002015-04-16T13:26:41.659+02:00xmonad and screen offIn Ubuntu with default windows manager it just works, with Xmonad it doesn't, turning the screen off and making the eco-people happy (saving your electricity bill).<br />
<br />
The inspiration for solution comes from:<br />
<a href="http://superuser.com/questions/590225/disable-screen-turn-off">http://superuser.com/questions/590225/disable-screen-turn-off</a><br />
<br />
Checking xset tool in terminal shows smth:<br />
<pre>xset
usage: xset [-display host:dpy] option ...
To turn bell off:
-b b off b 0
To set bell volume, pitch and duration:
b [vol [pitch [dur]]] b on
To disable bug compatibility mode:
-bc
To enable bug compatibility mode:
bc
To turn keyclick off:
-c c off c 0
To set keyclick volume:
c [0-100] c on
To control Energy Star (DPMS) features:
-dpms Energy Star features off
+dpms Energy Star features on
dpms [standby [suspend [off]]]
force standby
force suspend
force off
force on
(also implicitly enables DPMS features)
a timeout value of zero disables the mode
To set the font path:
fp= path[,path...]
To restore the default font path:
fp default
To have the server reread font databases:
fp rehash
To remove elements from font path:
-fp path[,path...] fp- path[,path...]
To prepend or append elements to font path:
+fp path[,path...] fp+ path[,path...]
To set LED states off or on:
-led [1-32] led off
led [1-32] led on
-led named 'name' led off
led named 'name' led on
To set mouse acceleration and threshold:
m [acc_mult[/acc_div] [thr]] m default
To set pixel colors:
p pixel_value color_name
To turn auto-repeat off or on:
-r [keycode] r off
r [keycode] r on
r rate [delay [rate]]
For screen-saver control:
s [timeout [cycle]] s default s on
s blank s noblank s off
s expose s noexpose
s activate s reset
For status information: q
To print version: -version
</pre>
<br />
The interesting part is dpms. To force turn off the screen one has to run:
<br />
<pre>xset dpms force off</pre>
And it would be great to integrate it with a 'lock screen' keyboard shortcut in Xmonad configuration:
<br />
<pre> -- lock screen
, ((modm .|. shiftMask, xK_l ), spawn "gnome-screensaver-command -l && xset dpms force off")
</pre>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0tag:blogger.com,1999:blog-5564399451854168940.post-6784641186262356752014-12-18T14:50:00.002+01:002015-03-13T09:59:55.919+01:00Make it work - IntelliJ IDEA and Linux (Xmonad)IntelliJ (and few other Java programs) has a problem with window not appearing or being frozen. Simple solution:
<br />
<pre>$ wmname LG3D
</pre>
More info:
<br />
<ul>
<li><a href="https://bbs.archlinux.org/viewtopic.php?id=142063">https://bbs.archlinux.org/viewtopic.php?id=142063</a> which suggest <code>export _JAVA_AWT_WM_NONREPARENTING=1</code></li>
<li><a href="https://code.google.com/p/android/issues/detail?id=57675">https://code.google.com/p/android/issues/detail?id=57675</a></li>
<li><a href="http://awesome.naquadah.org/wiki/Problems_with_Java">http://awesome.naquadah.org/wiki/Problems_with_Java</a></li>
</ul>
Pawel Szczurhttp://www.blogger.com/profile/02155230153306098405noreply@blogger.com0