string-formatting

what is the Pythonic way to implement a __str__ method with different format options?

懵懂的女人 提交于 2019-12-02 18:36:30
问题 I'd like to create a __str__ method that creates the string in various formats according to user choice. The best I have come up with is to make a __str__(**kwargs) method, and this seems to work ok, but it isn't compatible with str(obj) or print(obj) . In other words I have to use print(obj.__str__(style='pretty')) rather than print(obj, style='pretty') . 回答1: Implement the object.__format__() method instead, and a user can then specify the formatting required with the format() function and

Python SQL query string formatting

心不动则不痛 提交于 2019-12-02 17:02:29
I'm trying to find the best way to format an sql query string. When I'm debugging my application I'd like to log to file all the sql query strings, and it is important that the string is properly formated. Option 1 def myquery(): sql = "select field1, field2, field3, field4 from table where condition1=1 and condition2=2" con = mymodule.get_connection() ... This is good for printing the sql string. It is not a good solution if the string is long and not fits the standard width of 80 characters. Option 2 def query(): sql = """ select field1, field2, field3, field4 from table where condition1=1

difference between MessageFormat.format and String.format in jdk1.5?

可紊 提交于 2019-12-02 16:56:13
What is the difference between MessageFormat.format and String.format in JDK 1.5? Put simply, the main difference is in format string: MessageFormat.format() format string accepts argument positions (eg. {0} , {1} ). Example: "This is year {0}!" The developer doesn't have to worry about argument types, because they are, most often, recognized and formated according to current Locale . String.format() format string accepts argument type specifiers (eg. %d for numbers, %s for strings). Example: "This is year %d!" String.format() generally gives you much more control over how the argument is

What are the supported Swift String format specifiers?

最后都变了- 提交于 2019-12-02 16:50:00
In Swift, I can format a String with format specifiers: // This will return "0.120" String(format: "%.03f", 0.12) But the official documentation is not giving any information or link regarding the supported format specifiers or how to build a template similar to "%.03f" : https://developer.apple.com/documentation/swift/string/3126742-init It only says: Returns a String object initialized by using a given format string as a template into which the remaining argument values are substituted. Cœur The format specifiers for String formatting in Swift are the same as those in Objective-C NSString

How to use StringFormat in XAML elements?

ε祈祈猫儿з 提交于 2019-12-02 16:31:43
I'm deep in a XAML stack of elements binding to orders. The order date displays as e.g. "12/31/2008 12:00:00 AM". I want it to display as e.g. "31.12.2008". How can I do this? I have seen other stackoverflow questions mention StringFormat but they use multibinding in ways that I can't get to work. Here is the kind of syntax I would like (this is pseudocode), simply specifying StringFormat where you need it, is this possible somehow? <StackPanel> <ListView ItemsSource="{Binding Orders}"> <ListView.View> <GridView> <GridViewColumn Header="Order ID" DisplayMemberBinding="{Binding Path=OrderID}"

Extra spaces when printing

不想你离开。 提交于 2019-12-02 15:31:36
问题 I've read through a number of the python whitespace removal questions and answers but haven't been able to find what I'm looking for. Here is a small program that shows a specific example of the issue. I greatly appreciate your help. import random math_score = random.randint(200,800) math_guess = int(input("\n\nWhat score do you think you earned on the math section (200 to 800)?\t")) print ("\n\n\nOn the math section, you guessed",math_guess,", and your actual score was",math_score,"!") So

How are booleans formatted in Strings in Python?

十年热恋 提交于 2019-12-02 14:45:57
I see I can't do: "%b %b" % (True, False) in Python. I guessed %b for b(oolean). Is there something like this? >>> print "%r, %r" % (True, False) True, False This is not specific to boolean values - %r calls the __repr__ method on the argument. %s (for str ) should also work. Desintegr If you want True False use: "%s %s" % (True, False) because str(True) is 'True' and str(False) is 'False' . or if you want 1 0 use: "%i %i" % (True, False) because int(True) is 1 and int(False) is 0 . You may also use the Formatter class of string print "{0} {1}".format(True, False); print "{0:} {1:}".format

How to replace strings in StringFormat in WPF Binding

泄露秘密 提交于 2019-12-02 13:24:54
I need to replace a , with ,\n (New Line) in my string i want to do it on ClientSide in StringFormat <TextBlock Grid.Row="0" Text="{Binding Address}" Grid.RowSpan="3" /> How can i do this? You can't do this via a StringFormat binding operation, as that doesn't support replacement, only composition of inputs. You really have two options - expose a new property on your VM that has the replaced value, and bind to that, or use an IValueConverter to handle the replacement. A value converter could look like: public class AddNewlineConverter : IValueConverter { public object Convert(object value,

Where am I messing up with output formatting?

大兔子大兔子 提交于 2019-12-02 11:39:00
So I got an error message when I tried to run my code and I can't figure out what exactly the problem is. It says it's a ValueError but I can't figure out which one exactly. Maybe it's just late, but I am at a loss. Here's my code: def sort(count_dict, avg_scores_dict, std_dev_dict): '''sorts and prints the output''' menu = menu_validate("You must choose one of the valid choices of 1, 2, 3, 4 \n Sort Options\n 1. Sort by Avg Ascending\n 2. Sort by Avg Descending\n 3. Sort by Std Deviation Ascending\n 4. Sort by Std Deviation Descending", 1, 4) print ("{}{0:27}{0:39}{0:51}\n{}".format("Word",

How printf function handle %f specification?

流过昼夜 提交于 2019-12-02 10:35:00
I have a couple of programs whose output I cannot understand: Program 1 #include <stdio.h> int main(void) { int i=1; float k=2; printf("k is %f \n",k); printf("i is %f \n",i); return 0; } Output is at http://codepad.org/ZoYsP6dc k is 2.000000 i is 2.000000 Program 2 Now one more example #include <stdio.h> int main(void) { char i='a'; int a=5; printf("i is %d \n",i); // here %d type cast char value in int printf("a is %f \n",a); // hete %f dont typecast float value printf("a is %f \n",(float)a); // if we write (float) with %f then it works return 0; } Here output is at http://codepad.org