
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Python flavored string formatting
Written by Vladimir Neverov sanguini@gmail.com in 2014.
Homepage: https://github.com/vne/sortjs/wiki
See https://docs.python.org/2/library/stdtypes.html#string-formatting for information about original Python syntax.
This library is intented to simplify formatting of arbitrary data in strings. All Python format specifiers are supported, but not all are handled in exactly the same manner, although the library tries to do its best. pyfmt currently recognizes the following syntax:
> %(name)[flags]width.precision[length_modifiers]<format>
where only the % sign and are mandatory:
> %(varname)#10.4hlLf
> %(varname)#10.4f
> %(varname)-10.4f
> %(varname) 10.4f
> %(varname)+10.4f
> %(varname)010.4f
> %(varname)10.4f
> %(varname)10f
> %(varname).4f
> %10.4f
> %f
See below for differences from original Python.
pyfmt can be used both in browser and in NodeJS environments. In NodeJS you should require it first:
> var pyfmt = require('pyfmt');
This syntax provides the pyfmt class that can be used as follows:
> var template = new pyfmt("test %d - %d");
> template.format(5, 10)
There is an alternative that mangles the String object prototype and thus is not enabled by default. You can do it manually:
> var pyfmt = require('pyfmt').upgrade();
After this you can use the pyfmt method of any string:
> "test %d - %d".pyfmt(5, 10);
The second method is more like the python syntax, but not everybody likes mangling the built-in objects prototypes, so this decision is left up to you.
You can use arrays and objects as data providers for string formatting. In case of arrays you can not use names in format strings. E.g., the following is incorrect:
> "%(incorrect)d".pyfmt(5)
The exception will be raised in this case. Python behaves the same.
You can pass an array as argument to pyfmt or you can simply specify multiple arguments. Both things lead to the same result.
pyfmt uses JSON for string representation of complex objects for %r format. JSON library must be available. If there is no JSON library and %r format is used, an exception is thrown.
First, you can use nested objects as data providers:
> "%(first.second.data)d".pyfmt({ first: { second: { data: 42 } } })
If the path couldn't be resolved, an empty string is returned instead. Moreover, you can use array elements:
> "%(first.array[1].nested)d".pyfmt({ first: { array: [ { nested: 1 }, { nested: 2 }, { nested: 3 } ] } })
Second, in case when you need left data padding (e.g. when you specify the width of the field: %10s), it is padded by spaces by default. You can change the symbol that is used for that to any string you like.
> var template = new pyfmt("%10s", { space: '_' });
> template.format("test"); // will produce '______test'
You can use special option 'nbsp' to pad data with ' ':
> var template = new pyfmt("%6s", { nbsp: true });
> template.format("test"); // will produce ' test'
Third, %e and %g (and %E and %G) formatters produce effectively the same result.
Fourth, support for %b binary format. This formatter returns string 'true' for any true-like data (numbers that are not zero, non-empty strings, etc) and string 'false' for any false-like data (zeroes, empty strings, empty arrays, etc). Everything that is false in Javascript, returns 'false' here and vice versa.
More examples of library usage can be found in test.js file. To run tests you will need Mocha, the tests themselves use built-in NodeJS assert module.
FAQs
Python flavored string formatting
The npm package pyfmt receives a total of 24 weekly downloads. As such, pyfmt popularity was classified as not popular.
We found that pyfmt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.