Tutorial by Examples: f

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 ...
Final classes When used in a class declaration, the final modifier prevents other classes from being declared that extend the class. A final class is a "leaf" class in the inheritance class hierarchy. // This declares a final class final class MyFinalClass { /* some code */ } ...
Convert to String var date1 = new Date(); date1.toString(); Returns: "Fri Apr 15 2016 07:48:48 GMT-0400 (Eastern Daylight Time)" Convert to Time String var date1 = new Date(); date1.toTimeString(); Returns: "07:48:48 GMT-0400 (Eastern Daylight Time)" Conve...
var fs = require('fs'); // Save the string "Hello world!" in a file called "hello.txt" in // the directory "/tmp" using the default encoding (utf8). // This operation will be completed in background and the callback // will be called when it is either done or fail...
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: '...
Use Case CASE can be used in conjunction with SUM to return a count of only those items matching a pre-defined condition. (This is similar to COUNTIF in Excel.) The trick is to return binary results indicating matches, so the "1"s returned for matching entries can be summed for a count o...
project/jni/main.c #include <stdio.h> #include <unistd.h> int main(void) { printf("Hello world!\n"); return 0; } project/jni/Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello_world LOCAL_SRC_FILES := main.c include $(BU...
In addition to named imports, you can provide a default export. // circle.js export const PI = 3.14; export default function area(radius) { return PI * radius * radius; } You can use a simplified syntax to import the default export. import circleArea from './circle'; console.log(circle...
Sometimes you have a module that you only want to import so its top-level code gets run. This is useful for polyfills, other globals, or configuration that only runs once when your module is imported. Given a file named test.js: console.log('Initializing...') You can use it like this: import '...
In ECMAScript 6, when using the module syntax (import/export), each file becomes its own module with a private namespace. Top-level functions and variables do not pollute the global namespace. To expose functions, classes, and variables for other modules to import, you can use the export keyword. /...
Given that the module from the Defining a Module section exists in the file test.js, you can import from that module and use its exported members: import {doSomething, MyClass, PI} from './test' doSomething() const mine = new MyClass() mine.test() console.log(PI) The somethingPrivate()...
Opening a file is done in the same way for all 3 file streams (ifstream, ofstream, and fstream). You can open the file directly in the constructor: std::ifstream ifs("foo.txt"); // ifstream: Opens file "foo.txt" for reading only. std::ofstream ofs("foo.txt"); // ...
There are several ways to read data from a file. If you know how the data is formatted, you can use the stream extraction operator (>>). Let's assume you have a file named foo.txt which contains the following data: John Doe 25 4 6 1987 Jane Doe 15 5 24 1976 Then you can use the following...
There are several ways to write to a file. The easiest way is to use an output file stream (ofstream) together with the stream insertion operator (<<): std::ofstream os("foo.txt"); if(os.is_open()){ os << "Hello World!"; } Instead of <<, you can also ...
Explicitly closing a file is rarely necessary in C++, as a file stream will automatically close its associated file in its destructor. However, you should try to limit the lifetime of a file stream object, so that it does not keep the file handle open longer than necessary. For example, this can be ...
Common values from both sets: You can use the intersect(_:) method to create a new set containing all the values common to both sets. let favoriteColors: Set = ["Red", "Blue", "Green"] let newColors: Set = ["Purple", "Orange", "Green"] ...
Let's say you've got a list of restaurants -- maybe you read it from a file. You care about the unique restaurants in the list. The best way to get the unique elements from a list is to turn it into a set: restaurants = ["McDonald's", "Burger King", "McDonald's", &qu...
collections.defaultdict(default_factory) returns a subclass of dict that has a default value for missing keys. The argument should be a function that returns the default value when called with no arguments. If there is nothing passed, it defaults to None. >>> state_capitals = collections.d...
The term "IFrame" means Inline Frame. It can be used to include another page in your page. This will yield a small frame which shows the exact contents of the base.html. <iframe src="base.html"></iframe>
The IFrame can be resized using the width and height attributes, where the values are represented in pixels (HTML 4.01 allowed percentage values, but HTML 5 only allows values in CSS pixels). <iframe src="base.html" width="800" height="600"></iframe>

Page 22 of 457