Tutorial by Examples

This basic code will launch a "Hello world" GUI window using PyQt4: import sys from PyQt4 import QtGui # create instance of QApplication app = QtGui.QApplication(sys.argv) # create QLabel, without parent it will be shown as window label = QtGui.QLabel('Hello world!') label.show(...
The hashlib module allows creating message digest generators via the new method. These generators will turn an arbitrary string into a fixed-length digest: import hashlib h = hashlib.new('sha256') h.update(b'Nobody expects the Spanish Inquisition.') h.digest() # ==> b'.\xdf\xda\xdaVR[\x12\...
hashlib.new requires the name of an algorithm when you call it to produce a generator. To find out what algorithms are available in the current Python interpreter, use hashlib.algorithms_available: import hashlib hashlib.algorithms_available # ==> {'sha256', 'DSA-SHA', 'SHA512', 'SHA224', 'dsa...
The PBKDF2 algorithm exposed by hashlib module can be used to perform secure password hashing. While this algorithm cannot prevent brute-force attacks in order to recover the original password from the stored hash, it makes such attacks very expensive. import hashlib import os salt = os.urandom...
This method looks for the existence of browser specific things. This would be more difficult to spoof, but is not guaranteed to be future proof. // Opera 8.0+ var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; // Firefox 1.0+ ...
An easier approach for some would be to use an existing JavaScript library. This is because it can be tricky to guarantee browser detection is correct, so it can make sense to use a working solution if one is available. One popular browser-detection library is Bowser. Usage example: if (bowser.ms...
This method gets the user agent and parses it to find the browser. The browser name and version are extracted from the user agent through a regex. Based on these two, the <browser name> <version> is returned. The four conditional blocks following the user agent matching code are meant t...
Let's add a custom operator to multiply a CGSize func *(lhs: CGFloat, rhs: CGSize) -> CGSize{ let height = lhs*rhs.height let width = lhs*rhs.width return CGSize(width: width, height: height) } Now this works let sizeA = CGSize(height:100, width:200) let sizeB = 1.1 * si...
When we have two separate array and we want to make key value pair from that two array, we can use array's reduce function like below: var columns = ["Date", "Number", "Size", "Location", "Age"]; var rows = ["2001", "5", "B...
When you are copying a string into a malloced buffer, always remember to add 1 to strlen. char *dest = malloc(strlen(src)); /* WRONG */ char *dest = malloc(strlen(src) + 1); /* RIGHT */ strcpy(dest, src); This is because strlen does not include the trailing \0 in the length. If you take the ...
A programming best practice is to free any memory that has been allocated directly by your own code, or implicitly by calling an internal or external function, such as a library API like strdup(). Failing to free memory can introduce a memory leak, which could accumulate into a substantial amount of...
char buf[8]; /* tiny buffer, easy to overflow */ printf("What is your name?\n"); scanf("%s", buf); /* WRONG */ scanf("%7s", buf); /* RIGHT */ If the user enters a string longer than 7 characters (- 1 for the null terminator), memory behind the buffer buf will be...
If realloc fails, it returns NULL. If you assign the value of the original buffer to realloc's return value, and if it returns NULL, then the original buffer (the old pointer) is lost, resulting in a memory leak. The solution is to copy into a temporary pointer, and if that temporary is not NULL, th...
Detailed instructions on getting asp.net-mvc-4 set up or installed.
To get the size of a communicator (e.g. MPI_COMM_WORLD) and the local process' rank inside it: int rank, size; int res; MPI_Comm communicator = MPI_COMM_WORLD; res = MPI_Comm_rank (communicator, &rank); if (res != MPI_SUCCESS) { fprintf (stderr, "MPI_Comm_rank failed\n"); ...
Before any MPI commands can be run, the environment needs to be initialized, and finalized in the end: int main(int argc, char** argv) { int res; res = MPI_Init(&argc,&argv); if (res != MPI_SUCCESS) { fprintf (stderr, "MPI_Init failed\n"); exit (...
The following code broadcasts the contents in buffer among all the processes belonging to the MPI_COMM_WORLD communicator (i.e. all the processes running in parallel) using the MPI_Bcast operation. int rank; int res; res = MPI_Comm_rank (MPI_COMM_WORLD, &rank); if (res != MPI_SUCCESS) { ...
Floating point types (float, double and long double) cannot precisely represent some numbers because they have finite precision and represent the values in a binary format. Just like we have repeating decimals in base 10 for fractions such as 1/3, there are fractions that cannot be represented fini...
PREPARE prepares a statement for execution EXECUTE executes a prepared statement DEALLOCATE PREPARE releases a prepared statement SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse'; PREPARE stmt2 FROM @s; SET @a = 6; SET @b = 8; EXECUTE stmt2 USING @a, @b; Result: +------------+ |...
You can get the value of an entry in the dictionary using the 'Item' property: Dim extensions As New Dictionary(Of String, String) From { { "txt", "notepad" }, { "bmp", "paint" }, { "doc", "winword" } } Dim program As St...

Page 326 of 1336