Tutorial by Examples: c

The effect of using the * operator on an argument when calling a function is that of unpacking the list or a tuple argument def print_args(arg1, arg2): print(str(arg1) + str(arg2)) a = [1,2] b = tuple([3,4]) print_args(*a) # 12 print_args(*b) # 34 Note that the length of the starr...
CSS ul { list-style: none; counter-reset: list-item-number; /* self nesting counter as name is same for all levels */ } li { counter-increment: list-item-number; } li:before { content: counters(list-item-number, ".") " "; /* usage of counters() function means val...
In many other languages, if you run the following (Java example) if("asgdsrf" == 0) { //do stuff } ... you'll get an error. You can't just go comparing strings to integers like that. In Python, this is a perfectly legal statement - it'll just resolve to False. A common gotcha ...
In the example of a message bubble illustrated below: the corners of the image should remain unchanged which is specified by UIEdgeInsets, but the borders and center of the image should expand to cover the new size. let insets = UIEdgeInsetsMake(12.0, 20.0, 22.0, 12.0) let image = UIImage(na...
<div tabindex="2">Second</div> <div tabindex="1">First</div> Positive values will insert the element at the tabbing order position of its respective value. Elements without preference (i.e. tabindex="0" or native elements such as button and a...
We can optimize a simple xor function for only architectures that support unaligned reads/writes by creating two files that define the function and prefixing them with a build constraint (for an actual example of the xor code which is out of scope here, see crypto/cipher/xor.go in the standard libra...
This is a simple but robust core-data set-up for iOS 10+. There are exactly two way to access core-data: viewContext. The viewContext can only be used from the main thread, and only for reading. strong enqueueCoreDataBlock. All writing should be done using enqueueCoreDataBlock. There is no ne...
Just some methods of an object can be mocked using spy() of mockito. For example, imagine that method class requires some web service to work. public class UserManager { List<User> users; public UserManager() { user = new LinkedLisk<User>(); } ...
Get the Source Package from http://typo3.org/download/ and upload this package to your web server. Put it one level above the document root. For this manual, we will use the .tar.gz file. Use the shell to execute the according commands: /var/www/site/htdocs/$ cd .. /var/www/site/$ wget get.typo3....
go() method The go() method loads a specific URL from the history list. The parameter can either be a number which goes to the URL within the specific position (-1 goes back one page, 1 goes forward one page), or a string. The string must be a partial or full URL, and the function will go to the f...
The following example shows a basic main GUI window with a label widget, a toolbar, and a status bar using PyQt4. import sys from PyQt4 import QtGui class App(QtGui.QApplication): def __init__(self, sys_argv): super(App, self).__init__(sys_argv) self.build_ui() ...
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\...
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+ ...
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 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 ...
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...
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) { ...

Page 201 of 826