Data binding and flow in VueJS

Two way data binding means that UI fields are bound to model data dynamically such that when a UI field changes, the model data changes with it and vice-versa.

One way data flow means that the model is the single source of truth. Changes in the UI trigger messages that signal user intent to the model (or “store” in React). Only the model has the access to change the app’s state.

The effect is that data always flows in a single direction, which makes it easier to understand.

One way data flows are deterministic, whereas two-way binding can cause side-effects which are harder to follow and understand.

vue logoIn Vue, All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent’s state, which can make your app’s data flow harder to understand.

In addition, every time the parent component is updated, all props in the child component will be refreshed with the latest value. This means you should not attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console.

However, there could be use cases where it would be necessary to mutate a prop:

  1. The prop is used to pass in an initial value; the child component wants to use it as a local data property afterwards. In this case, it’s best to define a local data property that uses the prop as its initial value:
    props: ['initialCounter'],
    data: function () {
      return {
        counter: this.initialCounter
      }
    }
  2. The prop is passed in as a raw value that needs to be transformed. In this case, it’s best to define a computed property using the prop’s value:
    props: ['size'],
    computed: {
      normalizedSize: function () {
        return this.size.trim().toLowerCase()
      }
    }
    

 

Advertisements

Arrays Vs. Lists in Python

In the exploration of Python, I discovered a subtle but interesting difference between Arrays and Lists in Python.

Image result for python difference between array and list

Arrays and lists are both used in Python to store data, but they don’t serve exactly the same purposes. They both can be used to store any data type (real numbers, strings, etc), and they both can be indexed and iterated through, but the similarities between the two end there.

The array.array type is just a thin wrapper on C arrays. It can hold only homogeneous data, all of the same type, and so it uses only sizeof(one object) * length bytes of memory. Mostly, you should use it when you need to expose a C array to an extension or a system call (for example, ioctl or fctnl).

This is how you’d define an array:

import array
x = array.array('i', [1, 2, 3])

The array module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. Like the 'i' type code corresponds to Python’s int and 'b' type code corresponds to char etc.

array.array is also a reasonable way to represent a mutable string in Python 2.x (array('B', bytes)). However, Python 3.x offers a mutable byte string as bytearray.

Python Lists, on the other hand, are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time. If you need to shrink and grow your array time-efficiently and without hassle, they are the way to go. But they use a lot more space than C arrays.

It does take an extra step to use arrays because they have to be declared while lists don’t because they are part of Python’s syntax, so lists are generally used more often between the two, which works fine most of the time.

However, if you want to do the math on a homogeneous array of numeric data, then you’re much better off using NumPy, which can automatically vectorize operations on complex multi-dimensional arrays.

To make a long story short: array.array is useful when you need a homogeneous C array of data for reasons other than doing the math, like you may consider using arrays if you’re storing a large amount of data since arrays will store your data more compactly and efficiently.

Improving Web performance via image optimization

Web performance refers to the speed at which web pages are downloaded and displayed on the user’s web browser.

Faster website download speeds have been shown to increase visitor retention and loyalty and user satisfaction, especially for users with slow internet connections and those on mobile devices. Some aspects which can affect the speed of page load include browser/server cache, image optimization, and encryption (for example SSL), which can affect the time it takes for pages to render.

Images often account for most of the downloaded bytes on a web page and also often occupy a significant amount of visual space. As a result, optimizing images can often yield some of the largest byte savings and performance improvements for your website: the fewer bytes the browser has to download, the less competition there is for the client’s bandwidth and the faster the browser can download and render useful content on the screen.

image-optimization

Image Optimization

Image optimization is an art that you want to master. Optimizing web images is a process of delivering the high-quality images in the right format, dimension, size, and resolution while keeping the smallest possible size without sacrificing quality so that your page load times remain low. It’s also about image SEO. That is, getting your product images and decorative images to rank on Google and other image search engines.

The importance of images in connecting users to your products has been proven. If your website takes more than 3 seconds to load, users are more likely to abandon it which will drastically increase your bounce rate and eventually, it will affect your conversions.

How to optimize images?

Image optimization can be done in different ways be it by resizing the images, caching or by compressing the size. One of the simplest and most effective image optimization techniques is to ensure that we are not shipping any more pixels than needed to display the asset at its intended size in the browser.

There are numerous online tools you can use for image editing. Adobe even has a free image editing application for smartphones and tablets, Photoshop Express. This tool doesn’t have all of the capabilities of the desktop version of Adobe Photoshop, but it covers all the basics of image editing and doesn’t cost an arm and a leg.

Online image editing tools:

  • PicMonkey has been described by experts as a “staggeringly great photo editing tool”.
  • PIXLR is super user-friendly and comes with a 100% free app for your smartphone so you can edit on the go.
  • Canva is another fairly advanced online image editor.

Powerful image processing on the fly:

If you want responsive images on the fly Imgix is a great tool to try. Imgix transforms, optimizes, and intelligently caches your entire image library for fast websites and apps using simple and robust URL parameters. Resizing, cropping, and automatic content negotiation and enhancement are just the beginning of what you can do. Using Imgix, you can overlay text, stylize images, apply masks, add borders and padding, and much more.

In this digital world, every factor related to your website performance matters. And the expectation of visitors is only going to increases with time. One can not ignore the amazing benefits of optimizing images. These benefits are not restricted to the page load speed and SEO ranking only. Image optimization is capable of turning up your conversion and revenue numbers.

Connecting to a PostgreSQL server remotely through pgAdmin

It is a 3 step process to connect to a PostgreSQL server remotely through pgAdmin3.

Note: These steps are tested on Ubuntu 16.04 and PostgreSQL 8.4.

  1. You have to make PostgreSQL listening for remote incoming TCP connections because the default settings allow to listen only for connections on the loopback interface. To be able to reach the server remotely you have to add the following line into the file /etc/postgresql/8.4/main/postgresql.conf:

    listen_addresses = ‘*’

  2. PostgreSQL by default refuses all connections it receives from any remote address, you have to relax these rules by adding this line to /etc/postgresql/8.4/main/pg_hba.conf:

    host all all 0.0.0.0/0 md5

    This is an access control rule that let anybody login in from any address if he can provide a valid password (the md5 keyword). You can use needed network/mask instead of 0.0.0.0/0 .

  3. When you have applied these modifications to your configuration files you need to restart PostgreSQL server. Now it is possible to login to your server remotely, using the username and password.

To start PostgresSQL server you would do sudo /etc/init.d/postgresql stop and sudo /etc/init.d/postgresql start.