Tutorial by Examples

Use 403 Forbidden when a client has requested a resource that is inaccessible due to existing access controls. For example, if your app has an /admin route that should only be accessible to users with administrative rights, you can use 403 when a normal user requests the page. GET /admin HTTP/1.1 ...
Iterable can be anything for which items are received one by one, forward only. Built-in Python collections are iterable: [1, 2, 3] # list, iterate over items (1, 2, 3) # tuple {1, 2, 3} # set {1: 2, 3: 4} # dict, iterate over keys Generators return iterables: def foo(): # foo ...
s = {1, 2, 3} # get every element in s for a in s: print a # prints 1, then 2, then 3 # copy into list l1 = list(s) # l1 = [1, 2, 3] # use list comprehension l2 = [a * 2 for a in s if a > 2] # l2 = [6]
Use unpacking to extract the first element and ensure it's the only one: a, = iterable def foo(): yield 1 a, = foo() # a = 1 nums = [1, 2, 3] a, = nums # ValueError: too many values to unpack
Start with iter() built-in to get iterator over iterable and use next() to get elements one by one until StopIteration is raised signifying the end: s = {1, 2} # or list or generator or even iterator i = iter(s) # get iterator a = next(i) # a = 1 b = next(i) # b = 2 c = next(i) # raises S...
def gen(): yield 1 iterable = gen() for a in iterable: print a # What was the first item of iterable? No way to get it now. # Only to get a new iterator gen()
You can write the default protocol implementation for a specific class. protocol MyProtocol { func doSomething() } extension MyProtocol where Self: UIViewController { func doSomething() { print("UIViewController default protocol implementation") } } class M...
Generic classes can be inherited: // Models class MyFirstModel { } class MySecondModel: MyFirstModel { } // Generic classes class MyFirstGenericClass<T: MyFirstModel> { func doSomethingWithModel(model: T) { // Do something here } } class MySecondGe...
Setting up settings.py from django.utils.translation import ugettext_lazy as _ USE_I18N = True # Enable Internationalization LANGUAGE_CODE = 'en' # Language in which original texts are written LANGUAGES = [ # Available languages ('en', _("English")), ('de', _("Germ...
In order to efficiently handle cycle detection, we consider each node as part of a tree. When adding an edge, we check if its two component nodes are part of distinct trees. Initially, each node makes up a one-node tree. algorithm kruskalMST'(G: a graph) sort G's edges by their value MST ...
The above forest methodology is actually a disjoint-set data structure, which involves three main operations: subalgo makeSet(v: a node): v.parent = v <- make a new tree rooted at v subalgo findSet(v: a node): if v.parent == v: return v return findSet(v.parent...
We can do two things to improve the simple and sub-optimal disjoint-set subalgorithms: Path compression heuristic: findSet does not need to ever handle a tree with height bigger than 2. If it ends up iterating such a tree, it can link the lower nodes directly to the root, optimizing future trav...
Sort the edges by value and add each one to the MST in sorted order, if it doesn't create a cycle. algorithm kruskalMST(G: a graph) sort G's edges by their value MST = an empty graph for each edge e in G: if adding e to MST does not create a cycle: add e to MST ...
The DOM (Document Object Model) is the programming interface for HTML and XML documents, it defines the logical structure of documents and the way a document is accessed and manipulated. The main implementers of the DOM API are web browsers. Specifications are standardized by the W3C and the WHATW...
ceil() The ceil() method rounds a number upwards to the nearest integer, and returns the result. Syntax: Math.ceil(n); Example: console.log(Math.ceil(0.60)); // 1 console.log(Math.ceil(0.40)); // 1 console.log(Math.ceil(5.1)); // 6 console.log(Math.ceil(-5.1)); // -5 console.log(Math....
You can calculate a number in the Fibonacci sequence using recursion. Following the math theory of F(n) = F(n-2) + F(n-1), for any i > 0, // Returns the i'th Fibonacci number public int fib(int i) { if(i <= 2) { // Base case of the recursive function. // i is either 1...
JSON stands for "JavaScript Object Notation", but it's not JavaScript. Think of it as just a data serialization format that happens to be directly usable as a JavaScript literal. However, it is not advisable to directly run (i.e. through eval()) JSON that is fetched from an external source...
1. Target a device by serial number Use the -s option followed by a device name to select on which device the adb command should run. The -s options should be first in line, before the command. adb -s <device> <command> Example: adb devices List of devices attached emulator-55...
Each requirements files should match the name of a settings files. Read Using multiple settings for more information. Structure djangoproject ├── config │ ├── __init__.py │ ├── requirements │ │ ├── base.txt │ │ ├── dev.txt │ │ ├── test.txt │ │ └── prod.txt │ └── setti...
HTML also provides the tables with the <thead>, <tbody>, <tfoot>, and <caption> elements. These additional elements are useful for adding semantic value to your tables and for providing a place for separate CSS styling. When printing out a table that doesn't fit onto one (pa...

Page 323 of 1336