Tutorial by Examples: ai

To allow the use of in for custom classes the class must either provide the magic method __contains__ or, failing that, an __iter__-method. Suppose you have a class containing a list of lists: class ListList: def __init__(self, value): self.value = value # Create a set of al...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the contains(_:) method to check whether a set contains a value. It will return true if the set contains that value. if favorite...
To get the “available” area of the screen (i.e. not including any bars on the edges of the screen, but including window chrome and other windows: var availableArea = { pos: { x: window.screen.availLeft, y: window.screen.availTop }, size: { width: window.scr...
password_verify() is the built-in function provided (as of PHP 5.5) to verify the validity of a password against a known hash. <?php if (password_verify($plaintextPassword, $hashedPassword)) { echo 'Valid Password'; } else { echo 'Invalid Password.'; } ?> All supported hashi...
Sometimes we will need to run commands against a lot of files. This can be done using xargs. find . -type d -print | xargs -r chmod 770 The above command will recursively find all directories (-type d) relative to . (which is your current working directory), and execute chmod 770 on them. The -...
#include <stdio.h> #define is_const_int(x) _Generic((&x), \ const int *: "a const int", \ int *: "a non-const int", \ default: "of other type") int main(void) { const int i = 1; int j = 1; double...
A very common guideline in object oriented design is "as little as possible but as much as necessary". This also applies to the strategy pattern: It is usually advisable to hide implementation details, for example which classes actually implement strategies. For simple strategies which do...
The struct template std::pair can bundle together exactly two return values, of any two types: #include <utility> std::pair<int, int> foo(int a, int b) { return std::make_pair(a+b, a-b); } With C++11 or later, an initializer list can be used instead of std::make_pair: C++11 ...
Whilst extracting dependencies out of your code so that they can be injected makes your code easier to test, it pushes the problem further up the hierarchy and can also result in objects that are difficult to construct. Various dependency injection frameworks / Inversion of Control Containers have ...
To send email notifications you need to add the following settings to your config file: #Using a company SMTP server (note only one can be define) smtpHost = mail.example.com:25 emailFrom = [email protected] #Using Gmail with TLS and username/password smtpHost = smtp.gmail.com:587 emailFrom ...
When an API is marked with NS_REFINED_FOR_SWIFT, it will be prefixed with two underscores (__) when imported to Swift: @interface MyClass : NSObject - (NSInteger)indexOfObject:(id)obj NS_REFINED_FOR_SWIFT; @end The generated interface looks like this: public class MyClass : NSObject { publ...
In this case, building against API 23, so permissions are handled too. You must add in the Manifest the following permission (wherever the API level you're using): <uses-permission android:name="android.permission.CAMERA"/> We're about to create an activity (Camera2Activity.java...
A failing test will provide helpful output as to what went wrong: # projectroot/tests/test_code.py from module import code def test_add__failing(): assert code.add(10, 11) == 33 Results: $ py.test ================================================== test session starts ===============...
A typical email has three main components: A recipient (represented as an email address) A subject A message body Sending mail in PHP can be as simple as calling the built-in function mail(). mail() takes up to five parameters but the first three are all that is required to send an email (al...
When ASP.NET handles a request, a thread is assigned from the thread pool and a request context is created. The request context contains information about the current request which can be accessed through the static HttpContext.Current property. The request context for the request is then assigned t...
The pipe operator, %>%, is used to insert an argument into a function. It is not a base feature of the language and can only be used after attaching a package that provides it, such as magrittr. The pipe operator takes the left-hand side (LHS) of the pipe and uses it as the first argument of the ...
If you want to get the versioned project's data, but you don't need any of the version control capabilities offered by Subversion, you could run svn export <URL> command. Here is an example: $ svn export https://svn.example.com/svn/MyRepo/MyProject/trunk As a result, you will get the proje...
This example uses the Car Table from the Example Databases. SELECT * FROM Cars WHERE TotalCost IN (100, 200, 300) This query will return Car #2 which costs 200 and Car #3 which costs 100. Note that this is equivalent to using multiple clauses with OR, e.g.: SELECT * FROM Cars WHERE TotalCos...
This example shows the usage of the ILGenerator by generating code that makes use of already existing and new created members as well as basic Exception handling. The following code emits a DynamicAssembly that contains an equivalent to this c# code: public static class UnixTimeHelper { priva...
Method Chaining is a technique explained in Martin Fowler's book Domain Specific Languages. Method Chaining is summarized as Makes modifier methods return the host object, so that multiple modifiers can be invoked in a single expression. Consider this non-chaining/regular piece of code (ported...

Page 3 of 47