Tutorial by Examples

TypeScript is a typed superset of JavaScript, which means that all JavaScript code is valid TypeScript code. TypeScript adds a lot of new features on top of that. TypeScript makes JavaScript more like a strongly-typed, object-oriented language akin to C# and Java. This means that TypeScript code te...
Note that some syntax elements have different behavior depending on the expression. SyntaxDescription?Match the preceding character or subexpression 0 or 1 times. Also used for non-capturing groups, and named capturing groups.*Match the preceding character or subexpression 0 or more times.+Match th...
Basic Usage cargo test Show program output cargo test -- --nocapture Run specific example cargo test test_name
In Python 3 str is the type for unicode-enabled strings, while bytes is the type for sequences of raw bytes. type("f") == type(u"f") # True, <class 'str'> type(b"f") # <class 'bytes'> In Python 2 a casual string was a sequence of raw byte...
.encode and .decode both have error modes. The default is 'strict', which raises exceptions on error. Other modes are more forgiving. Encoding >>> "£13.55".encode('ascii', errors='replace') b'?13.55' >>> "£13.55".encode('ascii', errors='ignore') b'13.55' ...
Files opened in a non-binary mode (e.g. 'r' or 'w') deal with strings. The deafult encoding is 'utf8'. open(fn, mode='r') # opens file for reading in utf8 open(fn, mode='r', encoding='utf16') # opens file for reading utf16 # ERROR: cannot write bytes when a string is expecte...
Create a ModelForm from an existing Model class, by subclassing ModelForm: from django import forms class OrderForm(forms.ModelForm): class Meta: model = Order fields = ['item', 'order_date', 'customer', 'status']
Let's say you have a simple myblog app with the following model: from django.conf import settings from django.utils import timezone class Article(models.Model): title = models.CharField(max_length=70) slug = models.SlugField(max_length=70, unique=True) author = models.ForeignKe...
Class based views let you focus on what make your views special. A static about page might have nothing special, except the template used. Use a TemplateView! All you have to do is set a template name. Job done. Next. views.py from django.views.generic import TemplateView class AboutView(Tem...
Sometimes, your template need a bit more of information. For example, we would like to have the user in the header of the page, with a link to their profile next to the logout link. In these cases, use the get_context_data method. views.py class BookView(DetailView): template_name = "boo...
Template views are fine for static page and you could use them for everything with get_context_data but it would be barely better than using function as views. Enter ListView and DetailView app/models.py from django.db import models class Pokemon(models.Model): name = models.CharField(max...
Writing a view to create object can be quite boring. You have to display a form, you have to validate it, you have to save the item or return the form with an error. Unless you use one of the generic editing views. app/views.py from django.core.urlresolvers import reverse_lazy from django.views.g...
The basic idea of a class template is that the template parameter gets substituted by a type at compile time. The result is that the same class can be reused for multiple types. The user specifies which type will be used when a variable of the class is declared. Three examples of this are shown in m...
In order to create a random user password we can use the symbols provided in the string module. Specifically punctuation for punctuation symbols, ascii_letters for letters and digits for digits: from string import punctuation, ascii_letters, digits We can then combine all these symbols in a name...
In Go, an interface is just a set of methods. We use an interface to specify a behavior of a given object. type Painter interface { Paint() } The implementing type need not declare that it is implementing the interface. It is enough to define methods of the same signature. type Rembrandt ...
To start a new thread: use std::thread; fn main() { thread::spawn(move || { // The main thread will not wait for this thread to finish. That // might mean that the next println isn't even executed before the // program exits. println!("Hello from spa...
The special variable __name__ is not set by the user. It is mostly used to check whether or not the module is being run by itself or run because an import was performed. To avoid your module to run certain parts of its code when it gets imported, check if __name__ == '__main__'. Let module_1.py be ...
The jQuery UI framework helps to extend and increase the User Interface controls for jQuery JavaScript library. When you wish to use jQuery UI, you will need to add these libraries to your HTML. A quick way to start is using the Content Delivery Network available code sources: jQuery Libraries ht...
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...

Page 151 of 1336