Convert Java Date to UTC String

tl;dr You asked: I was looking for a one-liner like: Ask and ye shall receive. Convert from terrible legacy class Date to its modern replacement, Instant. myJavaUtilDate.toInstant().toString() 2020-05-05T19:46:12.912Z java.time In Java 8 and later we have the new java.time package built in (Tutorial). Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra … Read more

ValueError: cannot switch from manual field specification to automatic field numbering

return “{0} by {1} on {}”.format(self.title, self.author, self.press) that doesn’t work. If you specify positions, you have to do it through the end: return “{0} by {1} on {2}”.format(self.title, self.author, self.press) In your case, best is to leave python treat that automatically: return “{} by {} on {}”.format(self.title, self.author, self.press)

Print floating point values without leading zero

Here is another way: >>> (“%.4f” % k).lstrip(‘0’) ‘.1337’ It is slightly more general than [1:] in that it also works with numbers >=1. Neither method correctly handles negative numbers, however. The following is better in this respect: >>> re.sub(‘0(?=[.])’, ”, (“%0.4f” % -k)) ‘-.1337′ Not particularly elegant, but right now I can’t think of … Read more

Format strings vs concatenation

It’s just for the looks. You can see at one glance what the format is. Many of us like readability better than micro-optimization. Let’s see what IPython’s %timeit says: Python 3.7.2 (default, Jan 3 2019, 02:55:40) IPython 5.8.0 Intel(R) Core(TM) i5-4590T CPU @ 2.00GHz In [1]: %timeit root = “sample”; output = “output”; path = … Read more

Output single character in C

yes, %c will print a single char: printf(“%c”, ‘h’); also, putchar/putc will work too. From “man putchar”: #include <stdio.h> int fputc(int c, FILE *stream); int putc(int c, FILE *stream); int putchar(int c); * fputc() writes the character c, cast to an unsigned char, to stream. * putc() is equivalent to fputc() except that it may … Read more

How to generically format a boolean to a Yes/No string?

The framework itself does not provide this for you (as far as I know). Translating true/false into yes/no does not strike me as more common than other potential translations (such as on/off, checked/unchecked, read-only/read-write or whatever). I imagine that the easiest way to encapsulate the behavior is to make an extension method that wraps the … Read more

How can I format a decimal bound to TextBox without angering my users?

Try to resolve that on ViewModel level. That it: public class FormattedDecimalViewModel : INotifyPropertyChanged { private readonly string _format; public FormattedDecimalViewModel() : this(“F2”) { } public FormattedDecimalViewModel(string format) { _format = format; } private string _someDecimalAsString; // String value that will be displayed on the view. // Bind this property to your control public string … Read more

SyntaxError with starred expression when unpacking a tuple on its own for string formatting

The error occurs because (a) is just a value surrounded by parenthesis. It’s not a new tuple object. Thus, ‘%d %d’ % (*a) is equivalent to ‘%d %d’ % * a, which is obviously wrong in terms of python syntax. To create a new tuple, with one expression as an initializer, use a comma after … Read more