Test if variable matches any of several strings w/o long if-elsif chain, or case-when

Perhaps you didn’t know that you can put multiple conditions on a single case: case mystr when “abc”, “def”, “ghi”, “xyz” .. end But for this specific string-based test, I would use regex: if mystr =~ /\A(?:abc|def|ghi|xyz)\z/ If you don’t want to construct a regex, and you don’t want a case statement, you can create … Read more

Interview question: In php, is 123==0123?

This code: var_dump(123); var_dump(0123); will get you: int 123 int 83 This is because 0123 is octal notation (because of the 0 at the beginning), while 123 is decimal. For more information, you can take a look at the Integer section of the manual. An even trickier question would have been to ask about 79 … Read more

Openpyxl check for empty cell

To do something when cell is not empty add: if cell.value: which in python is the same as if cell value is not None (i.e.: if not cell.value == None:) Note to avoid checking empty cells you can use worksheet.get_highest_row() and worksheet.get_highest_column() Also I found it useful (although might not be a nice solution) if … Read more

Find the closest time from a list of times

var closestTime = listOfTimes.OrderBy(t => Math.Abs((t – fileCreateTime).Ticks)) .First(); If you don’t want the performance overhead of the OrderBy call then you could use something like the MinBy extension method from MoreLINQ instead: var closestTime = listOfTimes.MinBy(t => Math.Abs((t – fileCreateTime).Ticks)); Something like this: DateTime fileDate, closestDate; ArrayList theDates; long min = long.MaxValue; foreach (DateTime … Read more