Tutorial by Examples

So let's suppose you want to iterate only between some specific lines of a file You can make use of itertools for that import itertools with open('myfile.txt', 'r') as f: for line in itertools.islice(f, 12, 30): # do something here This will read through the lines 13 to 20 as i...
You can use flatMap(_:) in a similar manner to map(_:) in order to create an array by applying a transform to a sequence's elements. extension SequenceType { public func flatMap<T>(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T] } The difference with...
sed -i s/"what to replace"/"with what to replace"/g $file We use -i to select in-place editing on the $file file. In some systems it is required to add suffix after -i flag which will be used to create backup of original file. You can add empty string like -i '' to omit the b...
Aggregate Applies an accumulator function over a sequence. int[] intList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = intList.Aggregate((prevSum, current) => prevSum + current); // sum = 55 At the first step prevSum = 1 At the second prevSum = prevSum(at the first step) + 2 At the i-t...
ansible -i hosts -m ping targethost -i hosts defines the path to inventory file targethost is the name of the host in the hosts file
Inventory is the Ansible way to track all the systems in your infrastructure. Here is a simple static inventory file containing a single system and the login credentials for Ansible. [targethost] 192.168.1.1 ansible_user=mrtuovinen ansible_ssh_pass=PassW0rd Write these lines for example to host...
Define interface //In this interface, you can define messages, which will be send to owner. public interface MyCustomListener { //In this case we have two messages, //the first that is sent when the process is successful. void onSuccess(List<Bitmap> bitmapList); //And Th...
Returns a random integer between min and max: function randomBetween(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } Examples: // randomBetween(0, 10); Math.floor(Math.random() * 11); // randomBetween(1, 10); Math.floor(Math.random() * 10) + 1; // randomBet...
Here's how you can destructure a vector: (def my-vec [1 2 3]) Then, for example within a let block, you can extract values from the vector very succinctly as follows: (let [[x y] my-vec] (println "first element:" x ", second element: " y)) ;; first element: 1 , second ele...
Here's how you can destructure a map: (def my-map {:a 1 :b 2 :c 3}) Then, for example, within a let block you can extract values from the map very succinctly as follows: (let [{x :a y :c} my-map] (println ":a val:" x ", :c val: " y)) ;; :a val: 1 , :c val: 3 Notice th...
Let's say you have a vector like so: (def my-vec [1 2 3 4 5 6]) And you want to extract the first 3 elements and get the remaining elements as a sequence. This can be done as follows: (let [[x y z & remaining] my-vec] (println "first:" x ", second:" y "third:&quot...
The Action Attribute The action attribute defines the action to be performed when the form is submitted, which usually leads to a script that collects the information submitted and works with it. if you leave it blank, it will send it to the same file <form action="action.php"> ...
Swift textField.textAlignment = .Center Objective-C [textField setTextAlignment: NSTextAlignmentCenter]; In the example, we have set the NSTextAlignment to center. You can also set to .Left, .Right, .Justified and .Natural. .Natural is the default alignment for the current localization. Th...
Create a class inheriting from Exception: class FooException(Exception): pass try: raise FooException("insert description here") except FooException: print("A FooException was raised.") or another exception type: class NegativeError(ValueError): pass ...
Flask has a utility called jsonify() that makes it more convenient to return JSON responses from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/get-json') def hello(): return jsonify(hello='world') # Returns HTTP Response with {"hello": "world"} ...
If the mimetype of the HTTP request is application/json, calling request.get_json() will return the parsed JSON data (otherwise it returns None) from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/echo-json', methods=['GET', 'POST', 'DELETE', 'PUT']) ...
Python minimally evaluates Boolean expressions. >>> def true_func(): ... print("true_func()") ... return True ... >>> def false_func(): ... print("false_func()") ... return False ... >>> true_func() or false_func() true_func(...
public void iterateAndFilter() throws IOException { Path dir = Paths.get("C:/foo/bar"); PathMatcher imageFileMatcher = FileSystems.getDefault().getPathMatcher( "regex:.*(?i:jpg|jpeg|png|gif|bmp|jpe|jfif)"); try (Direc...
# For Python 2 compatibility. from __future__ import print_function import lxml.html import requests def main(): r = requests.get("https://httpbin.org") html_source = r.text root_element = lxml.html.fromstring(html_source) # Note root_element.xpath() gives a *...
Given a String and a Character let text = "Hello World" let char: Character = "o" We can count the number of times the Character appears into the String using let sensitiveCount = text.characters.filter { $0 == char }.count // case-sensitive let insensitiveCount = text.low...

Page 219 of 1336