The preferred way of creating a new element with jQuery

The first option gives you more flexibilty: var $div = $(“<div>”, {id: “foo”, “class”: “a”}); $div.click(function(){ /* … */ }); $(“#box”).append($div); And of course .html(‘*’) overrides the content while .append(‘*’) doesn’t, but I guess, this wasn’t your question. Another good practice is prefixing your jQuery variables with $: Is there any specific reason behind using … Read more

Append values to a set in Python

your_set.update(your_sequence_of_values) e.g, your_set.update([1, 2, 3, 4]). Or, if you have to produce the values in a loop for some other reason, for value in …: your_set.add(value) But, of course, doing it in bulk with a single .update call is faster and handier, when otherwise feasible.

How to insert an element after another element in JavaScript without using a library?

referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); Where referenceNode is the node you want to put newNode after. If referenceNode is the last child within its parent element, that’s fine, because referenceNode.nextSibling will be null and insertBefore handles that case by adding to the end of the list. So: function insertAfter(newNode, referenceNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } You can test it … Read more

Create a Pandas Dataframe by appending one row at a time

You can use df.loc[i], where the row with index i will be what you specify it to be in the dataframe. >>> import pandas as pd >>> from numpy.random import randint >>> df = pd.DataFrame(columns=[‘lib’, ‘qty1’, ‘qty2’]) >>> for i in range(5): >>> df.loc[i] = [‘name’ + str(i)] + list(randint(10, size=2)) >>> df lib qty1 … Read more

How to redirect and append both standard output and standard error to a file with Bash

cmd >>file.txt 2>&1 Bash executes the redirects from left to right as follows: >>file.txt: Open file.txt in append mode and redirect stdout there. 2>&1: Redirect stderr to “where stdout is currently going”. In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently … Read more