Tutorial by Examples: c

sqldf() from the package sqldf allows the use of SQLite queries to select and manipulate data in R. SQL queries are entered as character strings. To select the first 10 rows of the "diamonds" dataset from the package ggplot2, for example: data("diamonds") head(diamonds) #...
Rescue from record not found error instead of showing an exception or white page: class ApplicationController < ActionController::Base # ... your other stuff here rescue_from ActiveRecord::RecordNotFound do |exception| redirect_to root_path, 404, alert: 'Record not found' en...
Arithmetic if statement allows one to use three branches depending on the result of an arithmetic expression if (arith_expr) label1, label2, label3 This if statement transfers control flow to one of the labels in a code. If the result of arith_expr is negative label1 is involved, if the result i...
This validation restricts the insertion of only numeric values. class Player < ApplicationRecord validates :points, numericality: true validates :games_played, numericality: { only_integer: true } end Besides :only_integer, this helper also accepts the following options to add constrai...
This helper validates that the specified attributes are not empty. class Person < ApplicationRecord validates :name, presence: true end Person.create(name: "John").valid? # => true Person.create(name: nil).valid? # => false You can use the absence helper to validate th...
The special attribute __name__ of a function, class or module is a string containing its name. import os class C: pass def f(x): x += 2 return x print(f) # <function f at 0x029976B0> print(f.__name__) # f print(C) # <class '__main__.C'> print(C.__name__...
Overload resolution occurs after name lookup. This means that a better-matching function will not be selected by overload resolution if it loses name lookup: void f(int x); struct S { void f(double x); void g() { f(42); } // calls S::f because global f is not visible here, ...
While it might seem counterintuitive, you can use logical operators to determine whether or not a statement is run. For instance: File.exist?(filename) or STDERR.puts "#{filename} does not exist!" This will check to see if the file exists and only print the error message if it doesn't....
You can create parser error messages according to your script needs. This is through the argparse.ArgumentParser.error function. The below example shows the script printing a usage and an error message to stderr when --foo is given but not --bar. import argparse parser = argparse.ArgumentParser...
Boolean logic expressions, in addition to evaluating to True or False, return the value that was interpreted as True or False. It is Pythonic way to represent logic that might otherwise require an if-else test. And operator The and operator evaluates all expressions and returns the last expressi...
Say we have the following tree: root - A - AA - AB - B - BA - BB - BBA Now, if we wish to list all the names of the elements, we could do this with a simple for-loop. We assume there is a function get_name() to return a string of the name of a node, a function get_children() t...
If you have your data stored in a list and you want to convert this list to a data frame the do.call function is an easy way to achieve this. However, it is important that all list elements have the same length in order to prevent unintended recycling of values. dataList <- list(1:3,4:6,7:9) ...
class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def index @posts = Post.all end def show end def new @post = Post.new end def edit end def create @post = Post.new(post_par...
Number of unique elements in a series: In [1]: id_numbers = pd.Series([111, 112, 112, 114, 115, 118, 114, 118, 112]) In [2]: id_numbers.nunique() Out[2]: 5 Get unique elements in a series: In [3]: id_numbers.unique() Out[3]: array([111, 112, 114, 115, 118], dtype=int64) In [4]: df = pd.Da...
You can create your ssh key using ssh-keygen, it's a program that is part of the ssh installation. To do so just run it and follow the instructions on screen. Here's an example: $ ssh-keygen Generating public/private rsa key pair. The default directory where you ssh key pair will be saved is i...
Ever wanted to call a multicast delegate but you want the entire invokation list to be called even if an exception occurs in any in the chain. Then you are in luck, I have created an extension method that does just that, throwing an AggregateException only after execution of the entire list complete...
To increase the maximum amount of heap memory used Eclipse, edit the eclipse.ini file located in the Eclipse installation directory. This file specifies options for the startup of Eclipse, such as which JVM to use, and the options for the JVM. Specifically, you need to edit the value of the -Xmx JV...
class File(): def __init__(self, filename, mode): self.filename = filename self.mode = mode def __enter__(self): self.open_file = open(self.filename, self.mode) return self.open_file def __exit__(self, *args): self.open_file.close() ...
Swallowing Exceptions One should always re-throw exception in the following way: try { ... } catch (Exception ex) { ... throw; } Re-throwing an exception like below will obfuscate the original exception and will lose the original stack trace. One should never do this! The st...
Who says you cannot throw multiple exceptions in one method. If you are not used to playing around with AggregateExceptions you may be tempted to create your own data-structure to represent many things going wrong. There are of course were another data-structure that is not an exception would be mor...

Page 161 of 826