JS & Dom - Tips & Tricks #3

As you might already know, your browser’s developer tools can be used to debug JavaScript by utilizing breakpoints. Breakpoints allow you to pause script execution as well as step into, out of, and over function calls. Breakpoints are added in your developer tools by means of line numbers where you indicate where you want script execution to be paused. This is fine until you start changing your code. Now a defined breakpoint may no longer be in the place you want it to be....

January 13, 2017 · 2 min · Rezha Julio

JS & Dom - Tips & Tricks #2

Last year, David Walsh published an article called 7 Essential JavaScript Functions where he shared some JavaScript utility/helper snippets. One of the interesting techniques used in one of the functions is where he gets the absolute URL for a page. He does this using something like the following: var getAbsoluteUrl = (function() { var a; return function(url) { if(!a) a = document.createElement('a'); a.href = url; return a.href; }; })(); // Usage getAbsoluteUrl('/something'); // https ://example....

January 9, 2017 · 2 min · Rezha Julio

JS & Dom - Tips & Tricks #1

There are many ways to read content from elements and nodes. Here’s another one that allows you to read and write an individual node’s value. It uses the nodeValue property of the Node interface. The nodeValue of an element will always return null, whereas text nodes, CDATA sections, comment nodes, and even attribute nodes will return the text value of the node. To demonstrate its use, I’ll use the following HTML:...

January 6, 2017 · 2 min · Rezha Julio

Queue in Python - Part 3

Prioritize your queue A PriorityQueue is a type of queue imported from the module with the same name. It uses sort order to decide what to retrieve from it first (your object must have a way of comparing its instances): import queue class Rezha(object): def __init__(self, priority): self.priority = priority def __lt__(self, other): return self.priority > other.priority q = queue.PriorityQueue() q.put(Rezha(11)) q.put(Rezha(55)) q.put(Rezha(100)) while not q.empty(): print(q.get().priority) # output is 100 / 55 / 11 Having defined the __lt__ method, our PriorityQueue knows now how to sort elements of type Rezha....

December 26, 2016 · 1 min · Rezha Julio

Queue in Python - Part 2

Double ended queues with deque The deque class in the collections module makes it easy to create deques or double ended queues. Deques allow you to append and delete elements from both ends more efficiently than in lists. Import the module: from collections import deque Instantiate deque: d = deque() Append to right and left: d.append("b") d.appendleft("a") print(d) # output is: deque(['a', 'b']) In the same fashion, elements can be deleted (popped) :...

November 2, 2016 · 1 min · Rezha Julio