Python 3 bytes formatting

As of Python 3.5, % formatting will work for bytes, too! This was part of PEP 461, authored by Ethan Furman: PEP: 461 Title: Adding % formatting to bytes and bytearray Version: $Revision$ Last-Modified: $Date$ Author: Ethan Furman <ethan at stoneleaf.us> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 2014-01-13 Python-Version: 3.5 Post-History: 2014-01-14, 2014-01-15, … Read more

How do I format a number with a variable number of digits in Python?

If you are using it in a formatted string with the format() method which is preferred over the older style ”% formatting >>> ‘One hundred and twenty three with three leading zeros {0:06}.’.format(123) ‘One hundred and twenty three with three leading zeros 000123.’ See http://docs.python.org/library/stdtypes.html#str.format http://docs.python.org/library/string.html#formatstrings Here is an example with variable width >>> ‘{num:0{width}}’.format(num=123, … Read more

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

I just ran into this issue and had trouble filling in the “holes” left in the above example. Here’s a more complete, working version that I used. Hopefully this helps someone: # Custom formatter class MyFormatter(logging.Formatter): err_fmt = “ERROR: %(msg)s” dbg_fmt = “DBG: %(module)s: %(lineno)d: %(msg)s” info_fmt = “%(msg)s” def __init__(self, fmt=”%(levelno)s: %(msg)s”): logging.Formatter.__init__(self, fmt) … Read more

Parsing a Powershell variable

What you’re showing is the stringified form of a custom object, which uses a hashtable-like textual representation. That custom object’s definition looks something like this: # Construct a custom object with .ResourceType, .Id, and .Name properties. $response = [pscustomobject] @{ ResourceType=”UserStory” Id = 202847 Name=”Professional Lines: 2,800,000 Policy Aggregate Limit update” } If you include … Read more

How can I use f-string with a variable, not with a string literal?

f”…” strings are great when interpolating expression results into a literal, but you don’t have a literal, you have a template string in a separate variable. You can use str.format() to apply values to that template: name=[“deep”,”mahesh”,”nirbhay”] user_input = “certi_{element}” # this string i ask from user for value in name: print(user_input.format(element=value)) String formatting placeholders … Read more

Python SQL query string formatting

Sorry for posting to such an old thread — but as someone who also shares a passion for pythonic ‘best’, I thought I’d share our solution. The solution is to build SQL statements using python’s String Literal Concatenation (http://docs.python.org/), which could be qualified a somewhere between Option 2 and Option 4 Code Sample: sql = … Read more

Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?

The formatting routine that VB6 uses is actually built into the operating system. Oleaut32.dll, the VarFormat() function. It’s been around for 15 years and will be around for ever, considering how much code relies on it. Trying to translate the formatting strings to a .NET composite formatting string is a hopeless task. Just use the … Read more