Skip to main content

Simple way to have partially applied formatted strings.

This python package provides a wrapper around the ‘str.format’ functionality in order to skip unresolved formatting parts. All arguments are applied lazily.

Install

Enter the following command:

$ pip install lazy_format

Examples

More examples you can find in the tests.

Common use case:

>>> from lazy_format import LazyFormat
>>>
>>> lazy = LazyFormat('{0} {1} {0} {msg} {var1} {var2} {obj!r} {x:.2f}', 'fist arg', msg='Hello')
>>> lazy
<lazy_format.format.LazyFormat at 0x22a2590>
>>> print lazy
fist arg {1} fist arg Hello {var1} {var2} {obj!r} {x:.2f}
>>>
>>> lazy.format('second arg', obj=LazyFormat, x=22/7.)
"fist arg second arg fist arg Hello {var1} {var2} <class 'lazy_format.format.LazyFormat'> 3.14"
>>>
>>> lazy.format('second arg', obj=LazyFormat, x=22/7., var1='1', var2=2)
"fist arg second arg fist arg Hello 1 2 <class 'lazy_format.format.LazyFormat'> 3.14"
>>>
>>> lazy.format('second arg', obj=LazyFormat, x=22/7., var1='1', var2=2, msg='Bye')
"fist arg second arg fist arg Bye 1 2 <class 'lazy_format.format.LazyFormat'> 3.14"

Global context:

>>> LazyFormat.global_context.update_context_with({'global_var1': 'GV1', 'global_var2': 'GV2'})
>>> lazy2 = LazyFormat('{global_var1}, {global_var2}')
>>> print lazy2
GV1, GV2
>>> print lazy2.format(global_var1='my var1')
my var1, GV2

Extra features:

>>> from lazy_format import Aliases
>>> from lazy_format import LazyAliasFormat
>>>
>>> Aliases.add_alias('project_name', 'LazyFormat')
>>> lazy3 = LazyAliasFormat('{alias.project_name}')
>>> print lazy3
LazyFormat
>>>
>>>
>>> def func(alias, context):
>>>     return 'dyn value for ' + alias
>>>
>>> Aliases.add_alias('val', func)
>>> lazy4 = LazyAliasFormat('{alias.val}')
>>> print lazy4
dyn value for val
>>>
>>>
>>> LazyAliasFormat('{project_dir}', alias='dir')
<lazy_format.format.LazyAliasFormat at 0x22a2a50>
>>> templates = LazyAliasFormat('{alias.dir}/{template_dir_name}',
>>>                             template_dir_name='templates',
>>>                             alias='template_dir')
>>>
>>> my_prj_templates = LazyAliasFormat('{alias.template_dir}',
>>>                                    project_dir='/code/lazy_format',
>>>                                    template_dir_name='tmps')
>>> print templates
{project_dir}/templates
>>> print my_prj_templates
/code/lazy_format/tmps
>>> print my_prj_templates.format(project_dir='/code2')
/code2/tmps

Metadata

Release files for lazy_format 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lazy_format 0.0.2
File Size Uploaded
lazy_format-0.0.2.tar.gz 2.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lazy_format 0.0.2
File Interpreter ABI Platform
lazy_format-0.0.2.linux-x86_64.exe Details

Total release size: 67.1 kB

Release files / lazy_format-0.0.2.tar.gz

Download URL lazy_format-0.0.2.tar.gz
Size 2.2 kB
Tags Source
SHA-256 checksum
How to use checksums
08a2bc589374ea60da87034971c7c8200a9966d8809876593288bed96fdd737e
BLAKE2b-256 checksum
How to use checksums
8f4b494bcfb10f0e01912bc6b7d49dbb6a7430c7957e2f1a59ebaec7689ffe12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / lazy_format-0.0.2.linux-x86_64.exe

Download URL lazy_format-0.0.2.linux-x86_64.exe
Size 64.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f9be9630fbd8233dba3afa3d106a48469d77748c010ffd32ed13e95ec4b83541
BLAKE2b-256 checksum
How to use checksums
2ca6504d07f1cc17aa066520e2b656b1cbc74e97bdc0208c13e12114f6454cf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page