Tutorial by Examples: ad

Normally, enums can't be recursive (because they would require infinite storage): enum Tree<T> { case leaf(T) case branch(Tree<T>, Tree<T>) // error: recursive enum 'Tree<T>' is not marked 'indirect' } The indirect keyword makes the enum store its payload with...
Decorators normally strip function metadata as they aren't the same. This can cause problems when using meta-programming to dynamically access function metadata. Metadata also includes function's docstrings and its name. functools.wraps makes the decorated function look like the original function b...
git remote add upstream git-repository-url Adds remote git repository represented by git-repository-url as new remote named upstream to the git repository
import java.awt.Image; import javax.imageio.ImageIO; ... try { Image img = ImageIO.read(new File("~/Desktop/cat.png")); } catch (IOException e) { e.printStackTrace(); }
The simplest way to iterate over a file line-by-line: with open('myfile.txt', 'r') as fp: for line in fp: print(line) readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above: with open('myfile.txt', 'r') as fp: ...
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file: for line in in_file: out_file.write(line) Using the shutil module: import shutil shutil.copyfile(src, dst)
The following variables set up the below example: var COOKIE_NAME = "Example Cookie"; /* The cookie's name. */ var COOKIE_VALUE = "Hello, world!"; /* The cookie's value. */ var COOKIE_PATH = "/foo/bar"; /* The cookie's path. */ var COOKIE_EXPIRES; ...
var name = name + "=", cookie_array = document.cookie.split(';'), cookie_value; for(var i=0;i<cookie_array.length;i++) { var cookie=cookie_array[i]; while(cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); if(cookie.indexOf(name)==0) ...
Overview Checkboxes and radio buttons are written with the HTML tag <input>, and their behavior is defined in the HTML specification. The simplest checkbox or radio button is an <input> element with a type attribute of checkbox or radio, respectively: <input type="checkbox&quo...
str.split(sep=None, maxsplit=-1) str.split takes a string and returns a list of substrings of the original string. The behavior differs depending on whether the sep argument is provided or omitted. If sep isn't provided, or is None, then the splitting takes place wherever there is whitespace. Howe...
One of the most commonly used events is waiting for the document to have loaded, including both script files and images. The load event on document is used for this. document.addEventListener('load', function() { console.log("Everything has now loaded!"); }); Sometimes you try to ...
The json module contains functions for both reading and writing to and from unicode strings, and reading and writing to and from files. These are differentiated by a trailing s in the function name. In these examples we use a StringIO object, but the same functions would apply for any file-like obje...
a, b = 1, 2 # Using the "+" operator: a + b # = 3 # Using the "in-place" "+=" operator to add and assign: a += b # a = 3 (equivalent to a = a + b) import operator # contains 2 argument arithmetic functions for the examp...
You can include another Git repository as a folder within your project, tracked by Git: $ git submodule add https://github.com/jquery/jquery.git You should add and commit the new .gitmodules file; this tells Git what submodules should be cloned when git submodule update is run.
Jsoup can be used to extract links and email address from a webpage, thus "Web email address collector bot" First, this code uses a Regular expression to extract the email addresses, and then uses methods provided by Jsoup to extract the URLs of links on the page. public class JSoupTest {...
ALTER TABLE Employees ADD StartingDate date NOT NULL DEFAULT GetDate(), DateOfBirth date NULL The above statement would add columns named StartingDate which cannot be NULL with default value as current date and DateOfBirth which can be NULL in Employees table.
println! (and its sibling, print!) provides a convenient mechanism for producing and printing text that contains dynamic data, similar to the printf family of functions found in many other languages. Its first argument is a format string, which dictates how the other arguments should be printed as t...
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, ...
try { StyledDocument doc = new DefaultStyledDocument(); doc.insertString(0, "This is the beginning text", null); doc.insertString(doc.getLength(), "\nInserting new line at end of doc", null); MutableAttributeSet attrs = new SimpleAttributeSet(); StyleCons...
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...

Page 4 of 114