Tutorial by Examples: c

You can create a tuple and use a switch like so: var str: String? = "hi" var x: Int? = 5 switch (str, x) { case (.Some,.Some): print("Both have values") case (.Some, nil): print("String has a value") case (nil, .Some): print("Int has a value&...
Assuming you have a model called Post defined in your models.py file that contains blog posts, and has a date_published field. Step 1: Write the context processor Create (or add to) a file in your app directory called context_processors.py: from myapp.models import Post def recent_blog_posts...
<script> $( ".inclas").datepicker({ minDate: new Date(2007, 1 - 1, 1) maxDate: new Date(2008, 1 - 1, 1) }); </script> <input type ="text" id="datepick" class="inclas">
Following the Rcpp example in this documentation entry, consider the following tough-to-vectorize function, which creates a vector of length len where the first element is specified (first) and each element x_i is equal to cos(x_{i-1} + 1): repeatedCosPlusOne <- function(first, len) { x <-...
Many-to-One Relationship from django.db import models class Author(models.Model): name = models.CharField(max_length=50) #Book has a foreignkey (many to one) relationship with author class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) publish_...
Django ORM is a powerful abstraction that lets you store and retrieve data from the database without writing sql queries yourself. Let's assume the following models: class Author(models.Model): name = models.CharField(max_length=50) class Book(models.Model): name = models.CharField(max...
This example of the Sortable using a Placeholder is common usage. Sortable is applied to a group of DOM elements, allowing the user to move items around in the list via Drag'n Drop style actions. <!doctype html> <html lang="en"> <head> <meta charset="utf-8&q...
While the math.sqrt function is provided for the specific case of square roots, it's often convenient to use the exponentiation operator (**) with fractional exponents to perform nth-root operations, like cube roots. The inverse of an exponentiation is exponentiation by the exponent's reciprocal. S...
At some point in your use of Django, you may find yourself wanting to interact with tables which have already been created, or with database views. In these cases, you would not want Django to manage the tables through its migrations. To set this up, you need to add only one variable to your model's...
The background-position property is used to specify the starting position for a background image or gradient .myClass { background-image: url('path/to/image.jpg'); background-position: 50% 50%; } The position is set using an X and Y co-ordinate and be set using any of the units used withi...
You can create widgets composed of multiple widgets using MultiWidget. from datetime import date from django.forms.widgets import MultiWidget, Select from django.utils.dates import MONTHS class SelectMonthDateWidget(MultiWidget): """This widget allows the user to fill in ...
It is possible to configure Django to output log to a local or remote syslog service. This configuration uses the python builtin SysLogHandler. from logging.handlers import SysLogHandler LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard':...
This assumes that you have read the documentation about starting a new Django project. Let us assume that the main app in your project is named td (short for test driven). To create your first test, create a file named test_view.py and copy paste the following content into it. from django.test impo...
Getting the Constructor Object You can obtain Constructor class from the Class object like this: Class myClass = ... // get a class object Constructor[] constructors = myClass.getConstructors(); Where the constructors variable will have one Constructor instance for each public constructor decl...
Many C interfaces such as SDL2 have their own deletion functions. This means that you cannot use smart pointers directly: std::unique_ptr<SDL_Surface> a; // won't work, UNSAFE! Instead, you need to define your own deleter. The examples here use the SDL_Surface structure which should be fre...
If you have a f :: Lens' a b and a g :: Lens' b c then f . g is a Lens' a c gotten by following f first and then g. Notably: Lenses compose as functions (really they just are functions) If you think of the view functionality of Lens, it seems like data flows "left to right"—this might ...
main.go: package main import ( "fmt" ) func main() { fmt.Println(Sum(4,5)) } func Sum(a, b int) int { return a + b } main_test.go: package main import ( "testing" ) // Test methods start with `Test` func TestSum(t *testing.T) { go...
If you want to measure benchmarks add a testing method like this: sum.go: package sum // Sum calculates the sum of two integers func Sum(a, b int) int { return a + b } sum_test.go: package sum import "testing" func BenchmarkSum(b *testing.B) { for i := 0; i < ...
This type of tests make sure that your code compiles properly and will appear in the generated documentation for your project. In addition to that, the example tests can assert that your test produces proper output. sum.go: package sum // Sum calculates the sum of two integers func Sum(a, b in...
You can use Optional Chaining in order to call a method, access a property or subscript an optional. This is done by placing a ? between the given optional variable and the given member (method, property or subscript). struct Foo { func doSomething() { print("Hello World!") ...

Page 93 of 826