Skip to main content

Posts

Showing posts with the label usage

Promise -fication of JS calls.

Found a new pattern to use recently that is called Promise. I really like the way ES6/7 brings new thinking patterns into life nowdays. Here is Promise used instead of old pattern. It was to give JS method a callback function. This splitting code and making a lot of possibilities for error to come out in this place. One would write old times according to MDN : function greeting ( name ) { alert ( 'Hello ' + name ); } function processUserInput ( callback ) { var name = prompt ( 'Please enter your name.' ); callback ( name ); } processUserInput ( greeting ); And now it is made with Promise pattern like so: let promise = new Promise (( resolve , reject ) => { resolve ( prompt ( 'Please enter your name.' )); }); promise . then (( name ) => { alert ( 'Hello ' + name ); }); In general and briefly this now helps to avoid 'callback hell' with functions passed as arguments and write as...

5 most common Rsync command usage examples

rsync is a command for remote sync. It is used to synchronise one location to another in a simple way. Location is meant to be local directory, server or remote web server or whatever accessible by ssh. Advantages of using rsync over other tools is speed and bandwidth requirements. First time rsync copies entire contents of the directory provided and increments changes over next sync times. Command syntax: $ rsync options source destination 1. Synchronising local directories: $ rsync -zvr /tmp/logs/ /tmp/logs1/ building file list ... done created directory /tmp/ logs1 ./ log. log sent 98 bytes received 48 bytes 292.00 bytes/sec total size is 0 speedup is 0.00 $ Used rsync command options here: -z is for compression -v is for verbose output -r is for recursive directory scanning By default rsync does not preserve timestamps.  2. Preserve timestamps and permissions during sync: $ rsync -azvr /tmp/logs/ /tmp/...

Virtualenv for Django

One of main features and troubles for Django projects is the Django version you are using. Example for this would be the occasion of starting project for Django 1.4.x version. Trying to run it under Django 1.7.x would lead to various troubles, counting template changes, misconfiguration errors and so on. Solution is to use virtual environment. The tool for this is called Virtualenv . It is a wrapper for a python interpreter. It enables you to have multiple versions of python packages that run independently. The Virtualenv creates you a standalone environment. Basically it copies and keeps a system Python of yours in a specified directory. All the python packages will be installed there. (In case you have not forgot to activate it first, of course) Lets get started then. We need Virtualenv itself for the first. There are many ways to install it depending on your system . Most obvious to use pip. Your installation command will look something like that: pip install virtualenv Thi...

Python Generators explained simply.

I'm often confused by recent obsession of generators in python. I'll try to explain them as simply as possible. Say you have never used iterators but coded in python. BUT I'm sure you have used dictionaries ( dict ) and have met requirement to iterate through it's keys and/or values. TADA! You have used generators already ;). So generator is a  function  in python. Except for it uses a keyword  yield  in it's code. Thus making it the iterator. So you could call this function in a sequence like you would probably do iterating over a dictionary already. E.g.: # typical iteration through dictionary key: value set for key, value in dict .iteritems(): # do someth # Usage of your own iterator for item in iterator_functuon(): # do something with function generated output So this function returns an iterator generator like the  dict()  type has by default. And has a  next()  function, like usual iterators have. So in attempt t...