Using conditional statements inside ‘expect’

Have to recomment the Exploring Expect book for all expect programmers — invaluable. I’ve rewritten your code: (untested) proc login {user pass} { expect “login:” send “$user\r” expect “password:” send “$pass\r” } set username spongebob set passwords {squarepants rhombuspants} set index 0 spawn telnet 192.168.40.100 login $username [lindex $passwords $index] expect { “login incorrect” { … Read more

Greater Than Condition in Linq Join

You can’t do that with a LINQ joins – LINQ only supports equijoins. However, you can do this: var query = from e in entity.M_Employee from p in entity.M_Position where e.PostionId >= p.PositionId select p; Or a slightly alternative but equivalent approach: var query = entity.M_Employee .SelectMany(e => entity.M_Position .Where(p => e.PostionId >= p.PositionId));

How to do a conditional decorator in python?

Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something completely different. As such, you could create a conditional decorator: def conditional_decorator(dec, condition): def decorator(func): if not condition: # Return the function unchanged, not decorated. return func return dec(func) return decorator Now you can use it like this: @conditional_decorator(timeit, … Read more

How can I conditionally provide a default reference without performing unnecessary computation when it isn’t used?

You don’t have to create the default vector if you don’t use it. You just have to ensure the declaration is done outside the if block. fn accept(input: &Vec<String>) { let def; let vec = if input.is_empty() { def = vec![“empty”.to_string()]; &def } else { input }; // … do something with `vec` } Note … Read more

Resetting the State of a Stream

The code here std::cin.clear(std::istream::failbit); doesn’t actually clear the failbit, it replaces the current state of the stream with failbit. To clear all the bits, just call clear(). The description in the standard is a bit convoluted, stated as the result of other functions void clear(iostate state = goodbit); Postcondition: If rdbuf()!=0 then state == rdstate(); … Read more

Too many if statements

You could possibly use a dictionary. Dictionaries store references, which means functions are perfectly viable to use, like so: operationFuncs = { Operation.START: strategy_objects.StartObject Operation.STOP: strategy_objects.StopObject Operation.STATUS: strategy_objects.StatusObject (…) } It’s good to have a default operation just in case, so when you run it use a try except and handle the exception (ie. the … Read more

Conditional “Browsable” Attribute

I’m not sure this applies to your situation, but you can adjust the “Browsable” decoration at run-time by calling the function below. /// <summary> /// Set the Browsable property. /// NOTE: Be sure to decorate the property with [Browsable(true)] /// </summary> /// <param name=”PropertyName”>Name of the variable</param> /// <param name=”bIsBrowsable”>Browsable Value</param> private void setBrowsableProperty(string strPropertyName, … Read more