o3bvv / lazy-string

Python library for defining strings with delayed evaluation

Home Page:https://pypi.org/project/lazy-string

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maybe add some particularly useful examples to the docs?

rpatterson opened this issue · comments

Awesome library! I wish I'd thought to google for this when writing dozens of DEBUG log messages with expensive string formatting, usually pprint. For example the following is incomplete, the expensive call to pprint.pformat may still happen for some logging configurations:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug("Processing large data shape:\n%s", pprint.pformat(data_dict))

This might make a good example to include in the docs to help potential users imagine why they might use this. Untested, but in this case the expensive call should never happen unless this logging record is actually going to be emitted:

logger.debug(
    "Processing large data shape:\n%s",
    lazy_string.LazyString(functools.partial(pprint.pformat, data_dict)),
)

Hello! Thank you for the feedback. I will try to improve the docs when I find free time for that.

The library was born during creation of another library oblalex/verboselib, you might also find it useful. There is an example of lazy translations.

You might also be interested in oblalex/candv library, which has example of constants with "verbose" values.

I would really appreciate if you could tell whether those examples are understandable enough, so I could provide similar examples for lazy-string library.

I would really appreciate if you could tell whether those examples are understandable enough, so I could provide similar examples for lazy-string library.

Thanks for the followup, @oblalex!

The library was born during creation of another library oblalex/verboselib, you might also find it useful. There is an example of lazy translations.

Definitely not proud of this, but I don't have much experience with translation beyond just denoting strings that need to translatable. IOW, not much more than mimicking the _("...") stuff I see elsewhere in code I'm working on. So I tried to make sense of this example, but I think it requires that the reader have some familiarity with the internals of the translation framework. I also didn't see anywhere where the lazy strings from this library are used.

You might also be interested in oblalex/candv library, which has example of constants with "verbose" values.

This one is similar in that I'm not quite sure what I'm looking at and it seems like there's something else I'd have to understand in more depth in order to get much out of it. Also, the lazy example there seems like the same lazy translation from above and I also didn't see anywhere where the lazy strings from this library are used.

In summary, these examples wouldn't really help me understand better why one might find this library useful. Is there something about the example I included in this issue's description that you don't think works?

Hi everyone,

I found this repo/library while searching for resources about "lazy logging" and perhaps that might be have some good references to check out/consider

https://medium.com/swlh/why-it-matters-how-you-log-in-python-1a1085851205
https://medium.com/flowe-ita/logging-should-be-lazy-bc6ac9816906

While basic lazy logging is quite nice, I have found instances where some logging needs values that are only used in the logging, so I got to thinking about this topic and then I found this repo. Something as simple as a log message that includes the length of a list could use

log.debug('the list has %s values', LazyString(len, my_list))

instead of

log.debug('the list has %s values', len(my_list))

This code could also be extended to work with str.__mod__, str.format and string.Template.safe_substitute as common Callable values for LazyString instances. These could be class methods or subclasses.

I am also a bit disappointed that there are no unit tests for this repo, though it could be that the unit tests of the other repo did the testing of what is now in this repo. Still, now that it is separate, it would be good to have its own unit tests. These too could assist the understanding of this repo.

Hello! Thank you for all of your detailed feedbacks, I really appreciate that! Sorry for not replying in a while.

I'm planning to revisit my repos in the near future, address issues, create a couple of new libs, and write articles. It would be a pleasure to hear your feedbacks. Please, let me know if you would like to be notified.