npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pyfmt

v0.0.8

Published

Python flavored string formatting

Downloads

202

Readme

pyfmt

Python flavored string formatting

Written by Vladimir Neverov [email protected] in 2014.

Homepage: https://github.com/vne/sortjs/wiki

Description

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.

Usage

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.

What is different from Python

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 '&nbsp;&nbsp;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.

Tests and examples

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.