Tutorial by Examples: ast

It is not possible to use eval or exec to execute code from untrusted user securely. Even ast.literal_eval is prone to crashes in the parser. It is sometimes possible to guard against malicious code execution, but it doesn't exclude the possibility of outright crashes in the parser or the tokenizer....
Shortest match: $ a='I am a string' $ echo "${a#*a}" m a string Longest match: $ echo "${a##*a}" string
Shortest match: $ a='I am a string' $ echo "${a%a*}" I am Longest match: $ echo "${a%%a*}" I
You've made a stash and wish to checkout only some of the files in that stash. git checkout stash@{0} -- myfile.txt
Given the following definitions : public interface IMyInterface1 { string GetName(); } public interface IMyInterface2 { string GetName(); } public class MyClass : IMyInterface1, IMyInterface2 { string IMyInterface1.GetName() { return "IMyInterface1"...
string outsidetext = "I am outside of bracket"; string.Format("{{I am in brackets!}} {0}", outsidetext); //Outputs "{I am in brackets!} I am outside of bracket"
Suppose that you have a pointer to an object of a polymorphic class: Shape *ps; // see example on defining a polymorphic class ps = get_a_new_random_shape(); // if you don't have such a function yet, you // could just write ps = new Square...
Prerequisites In order to run Elasticsearch, a Java Runtime Environment (JRE) is required on the machine. Elasticsearch requires Java 7 or higher and recommends Oracle JDK version 1.8.0_73. Install Oracle Java 8 sudo add-apt-repository -y ppa:webupd8team/java sudo apt-get update echo "or...
import std.format; void main() { string s = "Name Surname 18"; string name, surname; int age; formattedRead(s, "%s %s %s", &name, &surname, &age); // %s selects a format based on the corresponding argument's type } Official documentatio...
git blame <file> will show the file with each line annotated with the commit that last modified it.
To get the value of the last result from your last expression in the console, use an underscore _. >>> 2 + 2 4 >>> _ 4 >>> _ + 6 10 This magic underscore value is only updated when using a python expression that results in a value. Defining functions or for loops ...
NSString *testString = @"There are 42 sheep and 8672 cows."; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d+)" options:NSRegularExpressionCaseIns...
NSString *testString1 = @"(555) 123-5678"; NSString *testString2 = @"not a phone number"; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^\\(\\d{3}\\) \\d{3}\\-\\d{4}$" ...
The following code broadcasts the contents in buffer among all the processes belonging to the MPI_COMM_WORLD communicator (i.e. all the processes running in parallel) using the MPI_Bcast operation. int rank; int res; res = MPI_Comm_rank (MPI_COMM_WORLD, &rank); if (res != MPI_SUCCESS) { ...
iex> String.split("Elixir, Antidote, Panacea", ",") ["Elixir", "Antidote", "Panacea"]
Three methods are provided that offer the ability to strip leading and trailing characters from a string: str.strip, str.rstrip and str.lstrip. All three methods have the same signature and all three return a new string object with unwanted characters removed. str.strip([chars]) str.strip acts o...
If you know that a value is of a specific type, you can explicitly cast it to that type in order to use it in a context where that type is needed. object value = -1; int number = (int) value; Console.WriteLine(Math.Abs(number)); If we tried passing value directly to Math.Abs(), we would get a ...
If you aren't sure whether a value is of the type you think it is, you can safely cast it using the as operator. If the value is not of that type, the resulting value will be null. object value = "-1"; int? number = value as int?; if(number != null) { Console.WriteLine(Math.Abs(nu...
A value will automatically be cast to the appropriate type if the compiler knows that it can always be converted to that type. int number = -1; object value = number; Console.WriteLine(value); In this example, we didn't need to use the typical explicit casting syntax because the compiler knows...
If you need to know whether a value's type extends or implements a given type, but you don't want to actually cast it as that type, you can use the is operator. if(value is int) { Console.WriteLine(value + "is an int"); }

Page 8 of 26