How can I extract keywords from a Python format string?

You can use the string.Formatter() class to parse out the fields in a string, with the Formatter.parse() method: from string import Formatter fieldnames = [fname for _, fname, _, _ in Formatter().parse(yourstring) if fname] Demo: >>> from string import Formatter >>> yourstring = “path/to/{self.category}/{self.name}” >>> [fname for _, fname, _, _ in Formatter().parse(yourstring) if fname] … Read more

Python 3.2: How to pass a dictionary into str.format()

This does the job: stats = { ‘copied’: 5, ‘skipped’: 14 } print( ‘Copied: {copied}, Skipped: {skipped}’.format( **stats ) ) #use ** to “unpack” a dictionary For more info please refer to: http://docs.python.org/py3k/library/string.html#format-examples and http://docs.python.org/py3k/tutorial/controlflow.html#keyword-arguments

Html.DisplayFor decimal format?

Decorate your view model property with the [DisplayFormat] attribute and specify the desired format: [DisplayFormat(DataFormatString = “{0:N}”, ApplyFormatInEditMode = true)] public decimal TAll { get; set; } and then in your view: @Html.DisplayFor(x => x.TAll) Another possibility if you don’t want to do this on the view model you could also do it inside the … Read more

Right padding with zeros in Java

You could use: String.format(“%-5s”, price ).replace(‘ ‘, ‘0’) Can I do this using only the format pattern? String.format uses Formatter.justify just like the String.printf method. From this post you will see that the output space is hard-coded, so using the String.replace is necessary.

How to create a NSString from a format string like @”xxx=%@, yyy=%@” and a NSArray of objects?

It is actually not hard to create a va_list from an NSArray. See Matt Gallagher’s excellent article on the subject. Here is an NSString category to do what you want: @interface NSString (NSArrayFormatExtension) + (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments; @end @implementation NSString (NSArrayFormatExtension) + (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments { char *argList = (char *)malloc(sizeof(NSString *) * … Read more