Archives 2005 - 2019

I think I may have fixed my linux laptop crashing

published Oct 10, 2015 08:55   by admin ( last modified Oct 10, 2015 08:59 )

Disable the graphics card!

Background

Crashing on average once a day is not cool for a linux laptop. It has taken me a long time to figure out what the problem is, because the logs that I can find on my computer have not been that helpful. However there are few things that should be able to crash you Linux computer. But one of them apparantly is the graphics driver. Well, it does not actually crash your computer, but it makes it inaccesible. I noticed things kept on running and playing. For a while the computer seemed more happy with the open source dirver, but alas, that joy did not last. So I decided to try the old use-different-drivers-and-see-what-works again. But then a small detail caught my eye: The Nvidia X server settings applet has the option to completely shut off the graphics card!

So now I am running on the Intel built-in graphics thingamajig. I don't mind; I'm, not a gamer. Hope this stability lasts.

And picked up this protip from the Internet. If your screen freezes, but you can still move the pointer, the problem is the graphics card system.


Configurable console logging inside a promise chain with _.curry

published Oct 09, 2015 12:15   by admin ( last modified Oct 09, 2015 12:20 )

When using promise chains in Javascript, it is easy to think (at least for me) that you can only stick something non-configurable inside of it:

P.resolve(4).then(afunc).tap(console.dir).then...

But in fact with the help of the _.curry function from underscore/lodash you can curry functions, i.e. wrap them so that they can step-wise be configured with their parameters but only fire once all parameters are defined.

With help of the colors library one can define a console logging function with configurable color and message string and nice formatting, like so:

function log(logColor, message, data) {
    console.log(message)
    console.log(colors[logColor](JSON.stringify(data, null, 2)))
}

And we can now configure that logging from inside of our promise chain like this:

P.resolve(4).then(afunc).tap(_.curry(log)('red','Inside the 4 chain')).then...

Above we call the log function with two of the three parameters. But since the last parameter data is missing, the curried function will first be executed when the parameter list is complete, which means the promise chain will trigger it.


Start a promise chain in Bluebird Q-style

published Oct 03, 2015 04:12   by admin ( last modified Oct 03, 2015 04:12 )

You can start a promise chain in the javascript library Q with a data item, like so:

Q([foo:'bar}).then( ...

How do you do that in the Bluebird javascript library?

One way is like this:

Promise.resolve([foo:'bar}).then( ...
One of the simplest is calling Promise.resolve() on nothing:

Read more: Link - javascript - Define empty Bluebird promise like in Q - Stack Overflow


Testing for an empty array in javascript

published Oct 01, 2015 12:34   by admin ( last modified Oct 01, 2015 12:34 )

Do:

if (myEmptyArray.length === 0) {} // true

Do not do:

if (myEmptyArray === []) {} // false

nor:

if (myEmptyArray == []) {} // false

nor:

if (!myEmptyArray) {}  // false

Random text string in Javascript

published Sep 28, 2015 09:40   by admin ( last modified Sep 28, 2015 09:43 )

This worked fine:

 

for(var c = ''; c.length < 32;) c += Math.random().toString(36).substr(2, 1)

 

Which reminded me of this uuid one:

"00000000-0000-4000-8000-000000000000".replace(/0/g,function(){return(0|Math.random()*16).toString(16)})

Instructions for use of the TempScan GT-302A Thermometer

published Sep 19, 2015 06:50   by admin ( last modified Sep 19, 2015 06:59 )

Instructions for use of the TempScan GT-302A Thermometer

(Unofficial instructions, use at your own risk)

I just figured out how to use mine again, lost the instructions a decade ago or so. Here goes:

Switching it on
Switch it on by pressing the on/MEM button. This will set the device in memory recall mode, indicated by the “M” symbol to the left in the display. The SCAN button will cycle between the 10 most recent temperature takings, taking #1 being the most recent.

Go to temperature taking mode
Press the on button again to put the device in stand-by mode for temperature monitoring. Over a bit less than 10 seconds, a progress report will appear near the top of the display showing 3 progressively thicker bars and finally a pictogram of an ear, accompanied by a beep.

Take the temperature
When the ear pictogram has appeared, the device is ready for temperature monitoring. Position the tip of the device in ear (with appropriate protection) and depress the SCAN button. Release the SCAN button when the device has beeped. The temperature may now be read from the display and will be stored in memory as reading #1.

Switching off the device
Depress the on/MEM button and wait for the device to switch off the display. Release button.

-----

Shorter version:

Switch it on by pressing the on/MEM button. Press “on” again & wait 10 secs for small ear to appear in display. Take the temp by pressing SCAN until beep.


Avoiding grey-screen in tightvnc under Ubuntu 14.04

published Sep 14, 2015 12:55   by admin ( last modified Sep 14, 2015 12:55 )

This is on the right path although windows are not controllable for me:

#!/bin/sh

def

export XKL_XMODMAP_DISABLE=1

unset SESSION_MANAGER

unset DBUS_SESSION_BUS_ADDRESS


gnome-panel &

gnome-settings-daemon &

metacity &

nautilus &

gnome-terminal &

This and similar just greyscreened:

#!/bin/sh
 
xrdb $HOME/.Xresources
xsetroot -solid grey
export XKL_XMODMAP_DISABLE=1
echo starting gnome
gnome-session --session=ubuntu-2d &

 

#!/bin/sh
 
xrdb $HOME/.Xresources
xsetroot -solid grey
export XKL_XMODMAP_DISABLE=1
echo starting gnome
gnome-session

Read more: Link - Ubuntu VNC Grey Screen | onkea eLearning


Nouveau stopped my Acer V5-537G from crashing

published Sep 14, 2015 12:30   by admin ( last modified Sep 14, 2015 12:53 )

Switching from Ubuntu 32-bit to 64-bit (14.04 and 15.04) I got problems with my Acer V5-537G freezing irrevocably, and only a hard restart would get it back to life again.

However by selecting the open source Nouveau driver for my Nvidia Geforce GT750M graphics card, the deep-freezes stopped. Now there are just temporary freeze-ups.

 

BinaryDriverHowto/Nvidia - Community Help Wiki


ECMAScript 6 tutorial shows similarities to python

published Sep 09, 2015 11:09   by admin ( last modified Sep 09, 2015 11:09 )

Use ECMAScript 6 Today - Tuts+ Code Article is a tutorial that is readable, about ECMAScript (Javascript) 6. It is from 2013 so more features may be available directly in the browsers and in Node.js as of today.

As a python developer, the things that stand out as similar to python in ECMAScript 6 are:

  • for x of - Seems to do what pythons for x in does. Javascript has for x in since long but it returns more primitive things.
  • Maps - allows any object and not only strings to be the key, as in python dictionaries.
  • rest and spread - Seems to be like splat (star) in python, that is * although python also has ** for dictionaries (a.ka. maps in ECMAScript 6) which ECMAScript 6 does not have,
  • Default function parameters -seems to work as in python
  • Destructuring - Works as in python, but better. In python you can only return tuples, but in ECMAScript 6 you can also unpack things from objects
  • Array comprehensions - Just like list comprehensions in python, but using of instead of in syntax-wise as noted above.


 

In this article, we'll explore ten new features, with a significant focus on tools, browsers and transpilers.


Read more: Link - Use ECMAScript 6 Today - Tuts+ Code Article



Fast mass inserts into PostgreSQL

published Sep 04, 2015 03:32   by admin ( last modified Sep 04, 2015 03:32 )

The fastest way seems to be with a special tool:

pg_bulkload

Other than that, one can use COPY which is almost as fast and it means you have a data file that postgres can suck in: PostgreSQL: Documentation: 9.4: Populating a Database

This guy has tested it all and concludes that if for whatever reason you cannot use COPY you can make multirow INSERTS: select * from depesz; » Blog Archive » how to insert data to database – as fast as possible

Here is an example of how a multirow insert looks like in PostgreSQL

INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');

 

There is an alternative to using COPY, which is the multirow values syntax that Postgres supports. From the documentation:


Read more: Link - postgresql - Whats the fastest way to do a bulk insert into Postgres? - Stack Overflow


Python dictionaries are pretty fast

published Sep 04, 2015 01:50   by admin ( last modified Sep 04, 2015 01:50 )

Yesterday I found myself looking at Google dense hash maps in C++. Googling around about them, I found a page from incise.org benchmarking different hashes in C++.

The author, on a lark he writes, decided to include dictionaries from Python and the corresponding data structure from Ruby. It turns out that python dictionaries aren't slow at all. Here two diagrams from the post:

 

They are fast, but take up a fair bit of memory. More diagrams and te text in the original post:

 

 

 

I've put together a set of benchmarks of what I consider to be the most prominent C and C++ hash table implementations. I've made the code available at Github. If you have any critiques or corrections, please post a comment below, email me, or fork the code and send me a pull request on Github.


 

Read more: Link - Hash Table Benchmarks


Run bitcoin-abe with parameters, from a script

published Sep 01, 2015 10:55   by admin ( last modified Sep 01, 2015 10:54 )

Bitcoin-abe takes its config values either from the command line or from a config file. What if you want to run it from inside of another script, how do you configure it? Please check the comments in the source code below:

 

from Abe import abe
# Your config settings
from myconfig import config

# A complex setting, a dictionary in a list, that
# we want bitcoin-abe to be configured with
datadir = [{
       "dirname": config.bitcoin_regtest_data_dir,
       "chain": "Regtest"
      }]


# It turns out that bitcoin-abe accepts JSON as values
# for its command-line arguments. It means we can pass
# in as complex data as we want

datadir_arg = '--datadir=' + json.dumps(datadir)
port_arg = '--port='+ str(config.abe_port)
config_arg = '--config='+ config.abe_config_location

# argv contains the command line arguments, so we just fake one
argv = [datadir_arg, port_arg, config_arg]

# Call the main function in abe with the command line arguments
def main():
    abe.main(argv)

A logger for multi threaded python that does not make python segfault

published Aug 26, 2015 11:19   by admin ( last modified Aug 26, 2015 11:19 )

I introduced logging to a a python application that is multi threaded, and it started to segfault. I switched to ConcurrentLogHandler but it still segfaulted. I then switched to log over http with restapi-logging-handler, and in that way introduced a bit of complexity, but eventually decided to take another sweep at pypi to see if there wasn't a simpler solution and found this:

multiprocessing-logging 0.2.1 : Python Package Index

It seems to work like a charm in initial tests. I use it like this (abbreviated code):

import multiprocessing_logging

log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs', 'app.log')
mhandler = multiprocessing_logging.MultiProcessingHandler('worker-logger', sub_handler= logging.handlers.RotatingFileHandler(
              log_file, maxBytes=20000000, backupCount=0))

 

 


Prevent Spotify stealing your icons on Ubuntu 14.04

published Aug 26, 2015 11:10   by admin ( last modified Aug 26, 2015 11:25 )

Well, it isn't actually stealing your icons but when interacting with Spotify on Ubuntu 14.04 Linux, it has a tendency to make your icons for applications and other stuff disappear. It seems to do this by switching themes somehow or some sorts. However there is a way of preventing that.

AppArmor is a program that can control what other programs can and cannot do, with regards to file access. It is mainly used for security reasons to make sure that programs, especially binary-only ones whose code you cannot scrutinize (such as Spotify), are not secretly veering off stealing data from your hard disk or mucking about in other ways.

AppArmor can be configured to prevent Spotify from doing this icon-disappearing thingy. In your AppArmor profile for Spotify, add these two lines (tested by me):

# block sni-qt
deny /usr/lib/i386-linux-gnu/qt4/plugins/systemtrayicon/libsni-qt.so r,
deny /usr/lib/x86_64-linux-gnu/qt4/plugins/systemtrayicon/libsni-qt.so r,

Or use the entire profile here https://raw.githubusercontent.com/ozcanesen/spotify-indicator/master/spotify (not tested by me)

"First you need to disable Spotify's indicator. Spotify is using sni-qt package to show indicator. Removing that package will disable indicator but this solution will affect other Qt based applications like Skype. But we got another option to do that. after some research i discovered that this task can be done using AppArmor. I wrote an experimental AppArmor profile, copy this profile and reload AppArmor."

 

 

 


How to update one file from the same file in another branch (git)

published Aug 24, 2015 04:10   by admin ( last modified Aug 24, 2015 05:31 )

Untested by me. My need is to carry over just the changes in one file to the same file in another branch in the same git repository.

 

A simple command already solved the problem for me if I assume that all changes are committed in both branches A and B:
git checkout A
git checkout --patch B f


Read more: Link - git - How do I merge changes to a single file, rather than merging commits? - Stack Overflow


How to create a desktop item on Ubuntu 14.04

published Aug 15, 2015 12:47   by admin ( last modified Aug 15, 2015 12:47 )
In GNOME desktop, you can use gnome-desktop-item-edit to configure a desktop shortcut easily.
$ gnome-desktop-item-edit ~/.local/share/applications --create-new
In this example, gnome-desktop-item-edit will automatically create a desktop launcher file in ~/.local/share/applications.
If gnome-desktop-item-edit is not available (e.g., on Ubuntu), you can install it as follows.
$ sudo apt-get install --no-install-recommends gnome-panel


Read more: Link - How to create desktop shortcut or launcher on Linux - Xmodulo


How to run individual tests in python

published Aug 15, 2015 02:40   by admin ( last modified Sep 01, 2015 11:42 )

Tested by me.

You specify the path to the file where the tests are residing. After the path you add a string that specifies the place of the test inside that file, in dotted format:

python <path_to_file> <class_name>.<test_method_name>
python testMyCase.py MyCase.testItIsHot


Read more: Link - python - Running single test from unittest.TestCase via command line - Stack Overflow


How to kill Linux process by pattern of process name

published Aug 15, 2015 02:00   by admin ( last modified Aug 15, 2015 02:00 )

Don't point pkill at anything you do not have the intention to kill.
Use

pgrep -f pattern

first, so you know what you are killing.

Use pkill -f, which matches the pattern for any part of the command line
pkill -f my_pattern


Read more: Link - linux - How to kill all processes with a given partial name? - Stack Overflow


What's the best way of writing readable javascript applications?

published Jul 29, 2015 03:11   by admin ( last modified Jul 29, 2015 03:11 )

Update 2014-08-02, see also these links:

Which take a Haskell/FP perspective on improvements

 

Understanding the code in a javascript application can be hard work. There are at least two reasons for that:

1. TIMTOWDI (There is more than one way to do it)

There's more than one way to do it, a term I believe originally coined by Perl creator Larry Wall. C++ and  Scala are also often said to suffer from different coding styles, while python does not. Javascript is by many felt as lacking a lot of features, but can be cajoled to emulate these features.

Different people have different preferences and decide to go about "fixing" these things in different ways. The result of this is that there are different ways of importing functionality, tracking dependencies, emulating a class base OO language and so on and so forth. All these extensions still rely on javascript syntax which makes them contrived in source code.

Even such a common solution as the object literal demands a lot of knowledge of the coder about anonymous functions, closures and so on to figure out what's going on. Also other ways of going about things become syntax-heavy:

(function() {
  this.Example || (this.Example = {});
  this.Example.Models = { Product: employee };
  this.Example.Collections = { Products: employees }

}).call(this);

On top of that there is a plethora of frameworks each one if their own idiosyncratic view of the world.

2. Javascript is asynchronous

"Callback hell" is a term often used for the default way of handling asynchronicity in javascript. It makes it hard to follow the program execution by reading the source code. There are other ways of handling asynchronicity in javascript, but then we are back to reason number 1 above: a plethora of solutions. Another aspect, put forth by my friend Mikael, is that javascript has exceptions, which doesn't sit very well with the structure of the language. I guess because it becomes quite unclear through what an exception is supposed to bubble up.

Ideas

What can one do in order to write javascript that is readable and understandable by another coder?

  • One way is to present upfront the unchangeable parts of the application, whatever they might be. Basically present the conceptual model for the implementation of the application.
  • Frameworks, as much as I am not fond of frameworks in general, do make this possible.  Frameworks suffer from that you need to lean them and then use their tools which often aren't as useful and universally applicable as the language underneath: You end up trying to screw in screws with a hammer.
  • One idea is to divide program flow into two categories: hierarchical and sequential, where the file indicates level of hierarchy. So there is a top file that deals with overall logic of the program, and then there are files that each contain code that carry out lower-level tasks. Ideally there should be no leakage of these abstractions: You should be able to rely on a file being its own universe, and ideally testable on its own. This is called a Multilayered architecture.
  • Another idea is to use some kind of actor model and disentagle callback hell into objects and messages.
  • Or write in another language altogether and transcompile it to javascript, hopefully with a source map.
  • Or use Dart or Typescript.
  • Typescript contains a lot of extensions to javascript on the language level (instead of on a framework level), which may make the syntax cleaner.

There is no publicly available code for tracing a node.js program line by line

Or at least I cannot find any. Jetbrains has acquired spy.js but that one is not open source. The node-inspector people seem less enthusiastic  Enhancement: Full trace of program execution · Issue #366 · node-inspector/node-inspector

There is something close to tracing though for node.js (and I have tested it):

(alpha) njstrace lets you easily instrument and trace you code, see all function calls, arguments, return values, as well as the time spent in each function.