string-formatting

Can Python's logging format be modified depending on the message log level?

淺唱寂寞╮ 提交于 2019-11-28 16:58:03
问题 I'm using Python's logging mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "info." For example: logger.error("Running cmd failed") logger.info("Running cmd passed") In this example, I would like the format of the error to be printed differently: # error Aug 27, 2009 -

Format an Integer using Java String Format

落花浮王杯 提交于 2019-11-28 16:35:58
I am wondering if it is possible, using the String.format method in Java, to give an integer preceding zeros? For example: 1 would become 001 2 would become 002 ... 11 would become 011 12 would become 012 ... 526 would remain as 526 ...etc At the moment I have tried the following code: String imageName = "_%3d" + "_%s"; for( int i = 0; i < 1000; i++ ){ System.out.println( String.format( imageName, i, "foo" ) ); } Unfortunately, this precedes the number with 3 empty spaces. Is it possible to precede the number with zeros instead? Use %03d in the format specifier for the integer. The 0 means

What is Ruby equivalent of Python's `s= “hello, %s. Where is %s?” % (“John”,“Mary”)`

扶醉桌前 提交于 2019-11-28 14:31:58
问题 In Python, this idiom for string formatting is quite common s = "hello, %s. Where is %s?" % ("John","Mary") What is the equivalent in Ruby? 回答1: The easiest way is string interpolation. You can inject little pieces of Ruby code directly into your strings. name1 = "John" name2 = "Mary" "hello, #{name1}. Where is #{name2}?" You can also do format strings in Ruby. "hello, %s. Where is %s?" % ["John", "Mary"] Remember to use square brackets there. Ruby doesn't have tuples, just arrays, and those

How to use Single Quotes in Eval Format String

喜你入骨 提交于 2019-11-28 13:35:13
I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField. The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview. The SelectCommand has a WHERE clause which is to compare a string. Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause. How do I add single quotes inside an Eval FormatString? I have tried using ' Replace '. I have tried using ' Special Characters ' (... WHERE StringField = '{0}' ...) No luck so far. I appreciate any help you may be able to

Java - truncate string from left with formatter flag

可紊 提交于 2019-11-28 13:20:47
I have a string, say: String s = "0123456789"; I want to pad it with a formatter. I can do this two ways: String.format("[%1$15s]", s); //returns [ 0123456789] or String.format("[%1$-15s]", s); // returns [0123456789 ] if I want to truncate text I do String.format("[%1$.5s]", s); // returns [01234] if I want to truncate from the left, I thought I could do this: String.format("[%1$-.5s]", s); // throws MissingFormatWidthException but this failed, so I tried this: String.format("[%1$-0.5s]", s); // throws MissingFormatWidthException as well as this: String.format("[%1$.-5s]", s); // throws

How to escape curly braces in a format string in Rust

你离开我真会死。 提交于 2019-11-28 13:15:32
I want to write this write!(f, "{ hash:{}, subject: {} }", self.hash, self.subject) But since curly braces have special meaning for formatting it's clear that I can't place the outer curly braces like that without escaping. So I tried to escape them. write!(f, "\{ hash:{}, subject: {} \}", self.hash, self.subject) Rust doesn't like that either. Then I read this: The literal characters {, }, or # may be included in a string by preceding them with the \ character. Since \ is already an escape character in Rust strings, a string literal using this escape will look like "\{". So I tried write!(f,

Optimal Base-10 only itoa() function? [closed]

送分小仙女□ 提交于 2019-11-28 12:51:50
In 20+ years programming in C I've used a base other than 10 once, so when I found my trusty MSVC's _itoa() missing in another environment, I set out to write one that only does base 10, and puts the destination buffer argument, pointing to the storage returned by the function, on the left, instead of on the right, like all of the string functions in the C Standard Library. I believe this code is also thread-safe. Is there a faster way to do this? I was also going to ask about correctness, but I believe the included test code proves it works, even for the particular case of LONG_MIN, which is

Remove More Than 2 Trailing zero

心已入冬 提交于 2019-11-28 12:51:48
I have read many question in stack overflow, what I want is remove 2 or more than two trailing zero behind the decimal. i.e: 12.00 ==> 12 12.30 ==> 12.30 12.35 ==> 12.35 12.345678 ==> 12.34 NSNumberFormatter *twoDecimalPlacesFormatter = [[[NSNumberFormatter alloc] init] autorelease]; [twoDecimalPlacesFormatter setMaximumFractionDigits:2]; [twoDecimalPlacesFormatter setMinimumFractionDigits:0]; return [twoDecimalPlacesFormatter stringFromNumber:number]; I like @dorada's answer, here is a complete test: NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter

Using a custom formatter in a DataGridView

人走茶凉 提交于 2019-11-28 11:37:17
So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project. What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value "0" or "1" or whatever. One option I considered was to do the whole normalization thing: add a table in the DataSet with the enum names in it, keyed on the enum values, and have my first table hold a reference to this table. But this is an enum -specific idea. I

How to use Python string formatting to convert an integer representing cents to a float representing dollars?

南笙酒味 提交于 2019-11-28 11:29:16
I have an integer representing a price in cents. Using Python format strings, how can I convert this value into dollars with two decimal places? Examples: 1234 => 12.34 5 => 0.05 999 => 9.99 EDIT: I should give some background. I am storing prices in a database as integers in order to make sure I don't loose precision. I don't want to use the Decimal datatype because these values will also be used in calculations in Javascript, so integers will be simplest to work with for that. I want to be able to display in a Django template the formatted value using the stringformat tag. As such, dividing