Tutorial by Examples: ch

All built-in collections in Python implement a way to check element membership using in. List alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 5 in alist # True 10 in alist # False Tuple atuple = ('0', '1', '2', '3', '4') 4 in atuple # False '4' in atuple # True String astring = 'i am a s...
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: ...
Searching in nested sequences like a list of tuple requires an approach like searching the keys for values in dict but needs customized functions. The index of the outermost sequence if the value was found in the sequence: def outer_index(nested_sequence, value): return next(index for index, ...
Rebasing reapplies a series of commits on top of another commit. To rebase a branch, checkout the branch and then rebase it on top of another branch. git checkout topic git rebase master # rebase current branch onto master branch This would cause: A---B---C topic / D---E---F---G...
A submodule is always checked out at a specific commit SHA1 (the "gitlink", special entry in the index of the parent repo) But one can request to update that submodule to the latest commit of a branch of the submodule remote repo. Rather than going in each submodule, doing a git checkout...
The trap is reset for subshells, so the sleep will still act on the SIGINT signal sent by ^C (usually by quitting), but the parent process (i.e. the shell script) won't. #!/bin/sh # Run a command on signal 2 (SIGINT, which is what ^C sends) sigint() { echo "Killed subshell!" } ...
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. >...
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...
a = 1 - Math.abs(1 - a % 2); // This will throw an error if my arithmetic above is wrong. assert a >= 0 && a <= 1 : "Calculated value of " + a + " is outside of expected bounds"; return a;
Git provides multiple commands for listing branches. All commands use the function of git branch, which will provide a list of a certain branches, depending on which options are put on the command line. Git will if possible, indicate the currently selected branch with a star next to it. GoalCommand...
A module map can simply import mymodule by configuring it to read C header files and make them appear as Swift functions. Place a file named module.modulemap inside a directory named mymodule: Inside the module map file: // mymodule/module.modulemap module mymodule { header "defs.h&q...
git reset <filePath>
The GlobalFetch interface exposes the fetch function, which can be used to request resources. fetch('/path/to/resource.json') .then(response => { if (!response.ok()) { throw new Error("Request failed!"); } return response.json(...
If you need to use the ^ character in a character class (Character classes ), either put it somewhere other than the beginning of the class: [12^3] Or escape the ^ using a backslash \: [\^123] If you want to match the caret character itself outside a character class, you need to escape it: ...
The searched CASE returns results when a boolean expression is TRUE. (This differs from the simple case, which can only check for equivalency with an input.) SELECT Id, ItemId, Price, CASE WHEN Price < 10 THEN 'CHEAP' WHEN Price < 20 THEN 'AFFORDABLE' ELSE 'EXPENSIVE' E...
To allow the use of in for custom classes the class must either provide the magic method __contains__ or, failing that, an __iter__-method. Suppose you have a class containing a list of lists: class ListList: def __init__(self, value): self.value = value # Create a set of al...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the contains(_:) method to check whether a set contains a value. It will return true if the set contains that value. if favorite...
Employ the EAFP coding style and try to open it. import errno try: with open(path) as f: # File exists except IOError as e: # Raise the exception if it is not ENOENT (No such file or directory) if e.errno != errno.ENOENT: raise # No such file or directory ...
There are several ways to extract characters from a std::string and each is subtly different. std::string str("Hello world!"); operator[](n) Returns a reference to the character at index n. std::string::operator[] is not bounds-checked and does not throw an exception. The caller is...
Use the filesystem module for all file operations: const fs = require('fs'); With Encoding In this example, read hello.txt from the directory /tmp. This operation will be completed in the background and the callback occurs on completion or failure: fs.readFile('/tmp/hello.txt', { encoding: '...

Page 4 of 109