Tutorial by Examples: context

public class Person { //Id property can be read by other classes, but only set by the Person class public int Id {get; private set;} //Name property can be retrieved or assigned public string Name {get; set;} private DateTime dob; //Date of Birth property is st...
in myapp/context_processors.py: from django.conf import settings def debug(request): return {'DEBUG': settings.DEBUG} in settings.py: TEMPLATES = [ { ... 'OPTIONS': { 'context_processors': [ ... 'myapp.context_processor...
A context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the with statement. It takes care of the notifying. For example, file objects are context managers. When a context ends, the file object is closed automatically: open_file = ...
A context manager is any object that implements two magic methods __enter__() and __exit__() (although it can implement other methods as well): class AContextManager(): def __enter__(self): print("Entered") # optionally return an object return "A-ins...
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...
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...
It is also possible to write a context manager using generator syntax thanks to the contextlib.contextmanager decorator: import contextlib @contextlib.contextmanager def context_manager(num): print('Enter') yield num + 1 print('Exit') with context_manager(2) as cm: # the ...
You can open several content managers at the same time: with open(input_path) as input_file, open(output_path, 'w') as output_file: # do something with both files. # e.g. copy the contents of input_file into output_file for line in input_file: output_file.write(line...
CREATE CONTEXT my_ctx USING my_pkg; This creates a context that can only be set by routines in the database package my_pkg, e.g.: CREATE PACKAGE my_pkg AS PROCEDURE set_ctx; END my_pkg; CREATE PACKAGE BODY my_pkg AS PROCEDURE set_ctx IS BEGIN DBMS_SESSION.set_context('MY_CTX','...
In order to work with bindings in WPF, you need to define a DataContext. The DataContext tells bindings where to get their data from by default. <Window x:Class="StackOverflowDataBindingExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q...
This example will show you how to create a simple animation using the canvas and the 2D context. It is assumed you know how to create and add a canvas to the DOM and obtain the context // this example assumes ctx and canvas have been created const textToDisplay = "This is an example that uses...
The two possible ways of issuing a context.become (replacing or adding the new behavior) are offered separately to enable a clutter-free notation of nested receives: val a = actor(new Act { become { // this will replace the initial (empty) behavior case "info" ⇒ sender() ! "A...
A simple context tree (containing some common values that might be request scoped and included in a context) built from Go code like the following: // Pseudo-Go ctx := context.WithValue( context.WithDeadline( context.WithValue(context.Background(), sidKey, sid), time.Now().A...
Strategy: Strategy is a behavioural pattern, which allows to change the algorithm dynamically from a family of related algorithms. UML of Strategy pattern from Wikipedia : import java.util.*; /* Interface for Strategy */ interface OfferStrategy { public String getName(); public dou...
Passing a context with a timeout (or with a cancel function) to a long running function can be used to cancel that functions work: ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond) for { select { case <-ctx.Done(): return ctx.Err() default: ...
Conditional contexts in Lua (if, elseif, while, until) do not require a boolean. Like many languages, any Lua value can appear in a condition. The rules for evaluation are simple: false and nil count as false. Everything else counts as true. if 1 then print("Numbers work.") ...
Whenever a template is called upon, the default data context of the template is implicitly gained from the caller as in example the childTemplate gains the data context of the parentTemplate i.e caller template <template name="parentTemplate"> {{#with someHelperGettingDataForPa...
Make sure your image object is fully loaded before you try to draw it on the canvas with context.drawImage. Otherwise the image will silently fail to display. In JavaScript, images are not loaded immediately. Instead, images are loaded asynchronously and during the time they take to load JavaScript...
Often you will want to wrap some of Android's classes in easier to use utility classes. Those utility classes often require a context to access the android OS or your apps' resources. A common example of this is a wrapper for the SharedPreferences class. In order to access Androids shared preference...
The following uses the Chrome Logging API. If the .group() syntax is used in multiple templates, it will graphically organize the console logs from different templates into a hierarchical tree. You can also see how to inspect the current data context, and how to stringify data. Template.landingPa...

Page 1 of 4