Tutorial by Examples: di

dict have no builtin method for searching a value or key because dictionaries are unordered. You can create a function that gets the key (or keys) for a specified value: def getKeysForValue(dictionary, value): foundkeys = [] for keys in dictionary: if dictionary[key] == value: ...
The system header TargetConditionals.h defines several macros which you can use from C and Objective-C to determine which platform you're using. #import <TargetConditionals.h> // imported automatically with Foundation - (void)doSomethingPlatformSpecific { #if TARGET_OS_IOS // code t...
R has several built-in functions that can be used to print or display information, but print and cat are the most basic. As R is an interpreted language, you can try these out directly in the R console: print("Hello World") #[1] "Hello World" cat("Hello World\n") #H...
If your latest commit is not published yet (not pushed to an upstream repository) then you can amend your commit. git commit --amend This will put the currently staged changes onto the previous commit. Note: This can also be used to edit an incorrect commit message. It will bring up the default...
int i = 42; i = i++; /* Assignment changes variable, post-increment as well */ int a = i++ + i--; Code like this often leads to speculations about the "resulting value" of i. Rather than specifying an outcome, however, the C standards specify that evaluating such an expression produc...
Composition provides an alternative to inheritance. A struct may include another type by name in its declaration: type Request struct { Resource string } type AuthenticatedRequest struct { Request Username, Password string } In the example above, AuthenticatedRequest will con...
A variadic function can be called with any number of trailing arguments. Those elements are stored in a slice. package main import "fmt" func variadic(strs ...string) { // strs is a slice of string for i, str := range strs { fmt.Printf("%d: %s\n", i, ...
In order to test the beginning and ending of a given string in Python, one can use the methods str.startswith() and str.endswith(). str.startswith(prefix[, start[, end]]) As it's name implies, str.startswith is used to test whether a given string starts with the given characters in prefix. >...
try { JTextPane pane = new JTextPane(); StyledDocument doc = new DefaultStyledDocument(); doc.insertString(0, "Some text", null); pane.setDocument(doc); //Technically takes any subclass of Document } catch (BadLocationException ex) { //hand...
// zoo.php class Animal { public function eats($food) { echo "Yum, $food!"; } } $animal = new Animal(); $animal->eats('meat'); PHP knows what Animal is before executing new Animal, because PHP reads source files top-to-bottom. But what if we wanted to create ...
// Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // zoo.php require 'Animal.php'; $animal = new Animal; $animal->eats('slop'); // aquarium.php require 'Animal.php'; $animal = new Animal; $animal->eats('shrimp'); H...
// autoload.php spl_autoload_register(function ($class) { require_once "$class.php"; }); // Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // zoo.php require 'autoload.php'; $animal = new Animal; $animal->e...
// autoload.php spl_autoload_register(function ($class) { require_once "$class.php"; }); // Animal.php class Animal { public function eats($food) { echo "Yum, $food!"; } } // Ruminant.php class Ruminant extends Animal { public function eat...
6 Fetch request promises initially return Response objects. These will provide response header information, but they don't directly include the response body, which may not have even loaded yet. Methods on the Response object such as .json() can be used to wait for the response body to load, then p...
If it's not already done in php.ini, error reporting can be set dynamically and should be set to allow most errors to be shown: Syntax int error_reporting ([ int $level ] ) Examples // should always be used prior to 5.4 error_reporting(E_ALL); // -1 will show every possible error, even whe...
Usually, you have to use git add or git rm to add changes to the index before you can git commit them. Pass the -a or --all option to automatically add every change (to tracked files) to the index, including removals: git commit -a If you would like to also add a commit message you would do: g...
One common pitfall when using dictionaries is to access a non-existent key. This typically results in a KeyError exception mydict = {} mydict['not there'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'not there' One way to avoid...
> redirect the standard output (aka STDOUT) of the current command into a file or another descriptor. These examples write the output of the ls command into the file file.txt ls >file.txt > file.txt ls The target file is created if it doesn't exists, otherwise this file is truncated. ...
< reads from its right argument and writes to its left argument. To write a file into STDIN we should read /tmp/a_file and write into STDIN i.e 0</tmp/a_file Note: Internal file descriptor defaults to 0 (STDIN) for < $ echo "b" > /tmp/list.txt $ echo "a" >> ...
File descriptors like 0 and 1 are pointers. We change what file descriptors point to with redirection. >/dev/null means 1 points to /dev/null. First we point 1 (STDOUT) to /dev/null then point 2 (STDERR) to whatever 1 points to. # STDERR is redirect to STDOUT: redirected to /dev/null, # effect...

Page 7 of 164