format-string

Where to hold common strftime strings like (“%d/%m/%Y”)

99封情书 提交于 2019-12-12 09:37:45
问题 In my app I find myself using stftime a lot, and mostly with 2 strings formats - ("%d/%m/%Y") and ("%H:%M") Instead of writing the string each time, I want to store those strings in some global var or something, so I can define the format strings in just one place in my app. What is the pythonic way of doing that? Should I use a global dict, a class, a function, or maybe something else? Maybe like this? class TimeFormats(): def __init__(self): self.date = "%d/%m/%Y" self.time = "%H:%M" Or

decimal custom format string with fixed precision and no period

给你一囗甜甜゛ 提交于 2019-12-11 12:51:52
问题 I've run into a situation which I think is beyond what you can do with custom format strings. But the code I've written is so gross I thought I would ask anyway. What I need is for a decimal to be displayed as either a 6 or 7 digit string, like so: number = 12345.67M (optional) tenthousands thousands hundreds tens ones tenths hundredths 1 2 3 4 5 6 7 Here's the code I've written to achieve this: public static string ConvertDecimalToString(decimal easting, int length) { var formatString = "{0

.net Masked Text Box

旧时模样 提交于 2019-12-11 05:46:55
问题 What is the mask for " percentage ", in a WinForms application (VB.net)? 回答1: Per the documentation here: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx \ Escape. Escapes a mask character, turning it into a literal. " \\ " is the escape sequence for a backslash. So the mask for a % sign is \% Before posting, I made up a quick and dirty winforms app, tried it and it works. Edit - added although this next item in the documentation makes it look like just a

Why doesn't gcc -Wformat warn about printf %d on an unsigned int?

前提是你 提交于 2019-12-10 02:51:58
问题 The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x = -100; // This is fine, becomes UINT_MAX - 100 printf("%d\n", x); // This is undefined behavior. return 0; } C99 7.19.6.1p8 states %d expects an int argument. C99 7.19.6.1p9 states "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined ." However, gcc -Wformat (which is included with -Wall ) will not complain about the above program, why

How to check that two format strings are compatible?

人走茶凉 提交于 2019-12-09 07:28:35
问题 Examples: "Something %d" and "Something else %d" // Compatible "Something %d" and "Something else %f" // Not Compatible "Something %d" and "Something %d else %d" // Not Compatible "Something %d and %f" and "Something %2$f and %1$d" // Compatible I figured there should be some C function for this, but I'm not getting any relevant search results. I mean the compiler is checking that the format string and the arguments match, so the code for checking this is already written. The only question is

size limit of printf conversion specification

随声附和 提交于 2019-12-08 21:04:45
问题 printf conversion specifications are % followed by flags, width, precision, length modifier and conversion specifier. Is there practical limit to size of a conversion specification? I.e. %s is 2 chars long, while %08.2f is 6 chars long. My question is, what is the length of the maximal single specification in a format string that can be created, according to C99 standard? 回答1: There is no such conversion specification of maximum length. If you think you've found such a spec, I can come up

Is it possible to use format strings to align NSStrings like numbers can be?

*爱你&永不变心* 提交于 2019-12-06 18:29:58
问题 I'm using NSLog() to print some tabular data consisting of an NSString and an associated integer . Assume I know the length of the longest word. Is there a way using format strings to get this kind of column alignment: word:tree rank:5 word:frog rank:3 word:house rank:2 word:peppercorn rank:2 word:sword rank:2 word:antlion rank:1 The reason I'm asking about formatting strings is I'm hoping for a lightweight way to format my ghetto debugging output. Here is what I tried: NSString *word = @

How to have a SpannableStringBuilder append a span that's inside a formatted string?

雨燕双飞 提交于 2019-12-05 07:31:04
问题 Background Suppose I use SpannableStringBuilder to append multiple stuff into it, and one of them is string that I format from the strings.xml file, which has a span inside: SpannableStringBuilder stringBuilder = new SpannableStringBuilder (); stringBuilder.append(...)... final SpannableString span = new SpannableString(...); span.setSpan(new BackgroundColorSpan(0xff990000), ...,...,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); stringBuilder.append(getString(R.string.string_to_format, span));

Why doesn't gcc -Wformat warn about printf %d on an unsigned int?

孤者浪人 提交于 2019-12-05 02:27:31
The following program has undefined behavior: #include <stdio.h> int main(void) { unsigned int x = -100; // This is fine, becomes UINT_MAX - 100 printf("%d\n", x); // This is undefined behavior. return 0; } C99 7.19.6.1p8 states %d expects an int argument. C99 7.19.6.1p9 states "If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined ." However, gcc -Wformat (which is included with -Wall ) will not complain about the above program, why? Is this a bug, or a deliberate omission? From the gcc manpage: -Wformat Check calls to "printf" and

UnicodeDecodeError using Django and format-strings

落花浮王杯 提交于 2019-12-05 01:45:34
I wrote a small example of the issue for everybody to see what's going on using Python 2.7 and Django 1.10.8 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, unicode_literals, print_function import time from django import setup setup() from django.contrib.auth.models import Group group = Group(name='schön') print(type(repr(group))) print(type(str(group))) print(type(unicode(group))) print(group) print(repr(group)) print(str(group)) print(unicode(group)) time.sleep(1.0) print('%s' % group) print('%r' % group) # fails print('%s' % [group]) # fails print('%r' % [group]) #