Tutorial by Examples: c

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...
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...
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...
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...
using (new Sitecore.SecurityModel.SecurityDisabler()) { var item = Sitecore.Context.Database.GetItem("/sitecore/content/home"); }
var user = Sitecore.Security.Accounts.User.FromName("sitecore/testname", false); using (new Sitecore.Security.Accounts.UserSwitcher(user)) { var item = Sitecore.Context.Database.GetItem("/sitecore/content/home"); }
NSError *e = nil; NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"sam\"}]"; NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutabl...
When you create an argparse ArgumentParser() and run your program with '-h' you get an automated usage message explaining what arguments you can run your software with. By default, positional arguments and conditional arguments are separated into two categories, for example, here is a small script ...
Use associated type when there is a one-to-one relationship between the type implementing the trait and the associated type. It is sometimes also known as the output type, since this is an item given to a type when we apply a trait to it. Creation trait GetItems { type First; // ^~~~ d...
Say you have implemented some logic to detect attempts to modify an object in the database while the client that submitted changes didn't have the latest modifications. If such case happens, you raise a custom exception ConfictError(detailed_message). Now you want to return an HTTP 409 (Confict) st...
A basic User Schema: var mongoose = require('mongoose'); var userSchema = new mongoose.Schema({ name: String, password: String, age: Number, created: {type: Date, default: Date.now} }); var User = mongoose.model('User', userSchema); Schema Types.
Methods can be set on Schemas to help doing things related to that schema(s), and keeping them well organized. userSchema.methods.normalize = function() { this.name = this.name.toLowerCase(); }; Example usage: erik = new User({ 'name': 'Erik', 'password': 'pass' }); erik.nor...
Often times you will see an exception Anti forgery token is meant for user "" but the current user is "username" This is because the Anti-Forgery token is also linked to the current logged-in user. This error appears when a user logs in but their token is still linked to bein...
String to a primitive numeric type or a numeric wrapper type: Each numeric wrapper class provides a parseXxx method that converts a String to the corresponding primitive type. The following code converts a String to an int using the Integer.parseInt method: String string = "59"; int p...
#Post to pagerduty.com notification pagerduty { post = https://events.pagerduty.com/generic/2010-04-15/create_event.json contentType = application/json runOnActions = false body = `{ "service_key": "myservicekey", "incident_key": {{.|jso...

Page 200 of 826