Skip to main content

UNKNOWN

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazy_format-0.0.2.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_format-0.0.2.linux-x86_64.exe (64.9 kB view details)

Uploaded Source

File details

Details for the file lazy_format-0.0.2.tar.gz.

File metadata

  • Download URL: lazy_format-0.0.2.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lazy_format-0.0.2.tar.gz
Algorithm Hash digest
SHA256 08a2bc589374ea60da87034971c7c8200a9966d8809876593288bed96fdd737e
MD5 a49d523ed46d2f6d3b86069119f09840
BLAKE2b-256 8f4b494bcfb10f0e01912bc6b7d49dbb6a7430c7957e2f1a59ebaec7689ffe12

See more details on using hashes here.

File details

Details for the file lazy_format-0.0.2.linux-x86_64.exe.

File metadata

File hashes

Hashes for lazy_format-0.0.2.linux-x86_64.exe
Algorithm Hash digest
SHA256 f9be9630fbd8233dba3afa3d106a48469d77748c010ffd32ed13e95ec4b83541
MD5 4ac34c3ec7f8f503f32f26d77e28253d
BLAKE2b-256 2ca6504d07f1cc17aa066520e2b656b1cbc74e97bdc0208c13e12114f6454cf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page