Text

I was facing a challenge, where I needed check domain name of an URL. Unfortunatelly urlparse gives you domain and subdomain in one property, which is not 100% right.

Fortunatelly there is great library called tldextract, that can do this.

You can install it like this:

pip install tldextract

and extract domain like this:

result = tldextract.extract(“http://google.com”)
result.domain

Variable result contains also subdomain (if any) and suffix (tld).

Hope it helped.

Text

From time to time you need to send a package (i.e. tgz) of your repository to someone. You can do so with great git command archive. You can get many formats. For list of available formats run:

git archive —list

Let’s say I need to send tarball of my previous vetsion (one commit back) to my friend. The syntax is following:

git archive —format=tgz —output=my_repo.tgz HEAD^

If you need to send another snapshot of your repo, for example another branch, just swap “HEAD^” for your branch name.

Text

If you have a big sitemap.xml which is unformated (just one line XML) and you would like to know how many pages it contains, here is short python script that will do it.

#!/usr/bin/env python

import csv
import re

regex = re.compile(r”<loc>”)
count = 0

with open(“sitemap.xml”) as xml:
    for l in xml:
        count += len(regex.findall(l))

print count

Hope it helped.

Text

While I was searching for weather widget to Awesome WM all what I found was insufficient to my idea, so I’ve decided to write my own one.

The thing was that I can’t write anything in Lua language. Hopefully there is solution. You can write the core in another language and just run in from Lua as CLI script and get the output. And this is exactly what I did.

Python part (expects country ID as param):

#!/usr/bin/python

# -*- encoding: UTF-8 -*-

from json import loads
from urllib2 import urlopen
import sys

base_url = “http://api.openweathermap.org/data/2.5/weather?id=%s&units=metric”
data = loads(urlopen(base_url % sys.argv[1]).read())

print “%s %s” % (int(data[“main”][“temp”]), data[“weather”][0][“main”])

And rc.lua part (passes country ID to python script):

weatherwidget = widget({ type = “textbox” })
weatherwidget.text = ” — “
weatherwidgettimer = timer({ timeout = 120 })
weatherwidgettimer:add_signal(“timeout”,
    
function()
        
temp = assert(io.popen(“/home/grafa/.config/awesome/weather.py 3067696”, “r”))
        
weatherwidget.text = “<span color=’#ff892c’>”..temp:read(“*l”)..”</span>”
        t
emp:close()
    
end 
)
weatherwidgettimer:start()

Result (the first orange part):

image

Text

Let’s assume we created backup file with dd command and now, we would like to open that backup and read the files. The solution is simple…. just mount the *.dd file with loop option.

But…it’s not so simple as it can seems to be. We have to add another option and it’s offset =xxx. Hopefully the offset number we can get when we run:

fdisk -l the_file.dd

and the result is in the partition table in column “Start”. In my case it’s 8192. If we multiply this number by 512 we will get the offset and successfully run:

sudo mount -o loop,offset=4194304 -t vfat htc_bu.dd /media/usb/

Text

Let’s assume following:

You have a database of locations, for example list of all zip codes in your country including GPS positions and you would like to get nearest 10 zip codes to specific one. Or you have a GPS of your house. you have a list of all bars in your city including theirs GPS positions and you would like to get nearest 10 ones.

You can get these results quite simple with Postgres database.

I’m going to demostrate this on zip codes:

Firstly we have to load following extension into database:

CREATE EXTENSION cude;
CREATE EXTENSION earthdistance;

If you don’t have these extensions installed, you can simply intall them:

sudo apt-get install postgresql-contrib-9.1

Now let’s create the table:

CREATE TABLE zip_code (
    
id serial NOT NULL,
    z
ip_code integer NOT NULL,      
    
region character varying(100) NOT NULL,
    
latitude numeric(6,4) NOT NULL,
    
longitude numeric(6,4) NOT NULL,
    
earth_coords cube,
    
CONSTRAINT zipcode_pkey PRIMARY KEY (id)
);

insert some data:

INSERT INTO zip_code (zip_code, region, latitude, longitude) VALUES (10200, ‘Prague 1’, 50.0833, 14.4275)

and finally create the index:

CREATE INDEX ON zip_code USING GIST (earth_coords)

Now you have the table with data and index on the column, which you will need for conditioning. But you have to do one more thing:

UPDATE zip_code SET earth_coords = ll_to_earth(latitude, longitude);

This will fill the earth_coords column with GPS positions converted into earth data type for each record in the table.

Now you have to know GPS position of the zip code which you would like get nearest zip codes to. Let’s say the GPS position is:

50.0794, 14.43400

So you can run something like:

SELECT * FROM zipcode WHERE earth_box(ll_to_earth(50.0794, 14.43400), 5000) @ earth_coords LIMIT 10

which will select all zip codes from your current one in 5 km radius (that’s why 5000).

On my local machine (Intel i5) with 6.8k records the query takes 142ms.

Text

Basically you have 2 options. First one is a bit dirty, because it uses ia32-libs package, which contains tons of 32 bit libs. Second one is how it should be done (according to me).

So 1st option:

sudo apt-get install googlearth-package ia32-libs
make-googleearth-package
sudo dpkg -i GENERATED_PACKAGE_BY_PREV_COMMAND.deb

Second option is about to add i386 architecture to your repository list, then install needed libs.

sudo apt-get install lsb-core
sudo dpkg —add-architecture i386
cd /tmp
wget -c http://goo.gl/hZQ6K -O google-earth-stable_amd64.deb
sudo dpkg -i google-earth-stable_amd64.deb

The last command will probably fail, because it will demands a few 32 bit libs + ia32-libs package. So install each 32 lib but do not install the ia32-libs one. It’s just dependency thing. Instead of that run this:

sudo dpkg —force-all -i google-earth-stable_amd64.deb

It will instll the Google Earth regardless ia32-libs package error.

Here is the proof:

image

Peace.

Text

If you need to remove accent from text, for example for comparsion with another text (fulltext search solution), there is simple way how to do that:

#!/usr/bin/python
# -*- encoding: UTF-8 -*-

import unicodedata

def unaccent(text):

    s
 = text.decode(“utf-8”)
    s
 = unicodedata.normalize(“NFKD”, s)
    s = “”.join(c for c in s if ord(c) < 127)

    return s

print unaccent(“Výhry a výherci”)

This script can be used for either in PostgreSQL as an function

Text

If your browser don’t want to accelerate graphics, you can force him to do so.

Solutions is simple. Try to run your browser with next two flags

google-chrome —enable-webgl —ignore-gpu-blacklist

Now open your browser and go to:

about:gpu

and you should see this:

image

If those flags did the work, you can tell the browser to make this settings default. Open this URL:

about:flags

and find out this:

Override software rendering list 

and then click to Enable it.

Enjoy your hardware accelerated browser for example with Google MapsGL.

Text

Each time you install PostgresSQL database server, you will need access to that server under some user with full access (alike on MySQL root user).

PostgreSQL has such user, its “postgres”. But server default configuration is not friendly to you, so u cannot login into database with this user. It requires a few changes.

First of all we need to change postgres’s password. We can do it simply by login under postgres user into system, then run database console and change that password. Database console will not require password for login for now.

sudo su - postgres
psql
alter user postgres with password ‘our password’;

We configured postgres user. Its time to say to PostgreSQL, that user postgres, trying to login from this machine, will be authenticated by password, which is in md5 (that password was crypted by database automaticaly).

We need edit this conf file: pg_hba.conf. So it should be something like this:

sudo vim /etc/postgresql/9.1/main/pg_hba.conf

Then we need to put this line into that file:

local   all             postgres                                md5

It says that user “postgres” from local machine will be authenticated via password which is in md5. Just what we need.

Last step is to restart postgres, because we changed config file.

sudo /etc/init.d/postgres restart

Done.