Package: fazy
Lazy f-strings are the holy grail of Python development. Now it is found.Table of contentsQuick startAdditional featuresThe problemHow does it work?LimitationsBenchmarkQuick startInstall it:pipinstallfazyAnd use:>>>importf>>>>>>number=33>>>f('{number}kittens drink milk')'33 kittens drink milk'Additional featuresYou can execute the string not in lazy mode:>>>f('{number}kittens drink milk',lazy=False)'33 kittens drink milk'By default, you cannot use variables as templates. When you try to do this, you will see anSyntaxError(the example will not work in REPL and in the global scope, that's why you don't see>>>):deffunction():number=33template='{number}kittens drink milk'print(f(template))function()# SyntaxError: Unsafe use of a variable as a template.However, you can disable this check:template='{number}kittens drink milk'f(template,safe=False)All scopes of variable names are available to you, including variables declared in closures. If you disable data extraction from closures, you can greatly speed up the execution of the function:>>>f('{number}kittens drink milk',closures=False)'33 kittens drink milk'The problemThe main problem that this library solves is the transfer of the cost of calculating the extrapolation of a string from the moment when it is determined to the moment when it is used. And all this while preserving the classic look of the f-string, which we are so used to on modern versions of Python.The main use case that the author had in mind is related to logging. The fact is that many messages that are created for logging may eventually not be printed, because the logging level is too low for them. The classic solution to the problem is to calculate the string in alazyway. However, until now, this solution has been incompatible with the convenient syntax of f-strings. You needed to use % expressions for this, it looks much worse.The new way of writing logs is not very different from the old one. Try to execute this piece of code:importloggingimportflogging.basicConfig(level=logging.INFO,format="%(asctime)s[%(levelname)s]%(message)s",handlers=[logging.StreamHandler(),])number=33logging.info(f('{number}kittens drink milk'))You will see a message similar to this:2023-04-18 15:15:43,200 [INFO] 33 kittens drink milkHowever, if you replacelevel=logging.INFOonlevel=logging.ERROR, you won't see anything in the console. If you were using classical f-strings here, the extrapolation would happen anyway. However, using this library in the second case, the string will not be calculated.To feel the advantages of this approach, let's try to display an object whose string representation is calculated for a very long time:fromtimeimportsleepimportloggingimportfclassLongPrintable:def__init__(self,data):self.data=datadef__str__(self):sleep(10.0)returnstr(self.data)logging.basicConfig(level=logging.INFO,format="%(asctime)s[%(levelname)s]%(message)s",handlers=[logging.StreamHandler(),])number=LongPrintable(33)logging.debug(f('{number}kittens drink milk'))This code will be executed almost instantly! However, you will immediately notice the expected delay if you replacelogging.debugwithlogging.info.A side effect of this library is the fact that it allows you to use syntax close to f-strings on Python versions lower than 3.6. However, this feature was not the main one during development and was not tested separately. If you are using fairly old versions of Python - try it, most likely everything will work. If you are only interested in supporting f-syntax on older versions of Python without lazy calculations, you may be interested in another similar library.How does it work?The functionfreturns an object of theLazyStringclass. This class is inherited fromstrand wraps almost all the functionality inherent in ordinary strings. References to variables from multiple scopes are stored inside the returned object. At the first attempt to use the object, the final row is calculated inside it and then the object behaves like it. At the same time, references to variables from all scopes are destroyed and the final row is returned from the cache during subsequent calls.There are 3 scopes of variable names that are extracted from the call stack to allow them to be used inside expressions:Local variables.Global variables.Intermediate variablesthat are used, for example, when creatingclosures. To give them a more precise definition, these are such local variables of all functions higher up the call stack, which refer to functions that in the code wrap the function being executed at the moment (I know this sentence may not be so easy to read and understand the first time). This means that, going through the call stack, we ignore all scopes with local variables for functions that are not the parents of the function being executed at the moment.As you might guess from the size of the description, the most interesting type of scopes is the third - intermediate variables. The expected approach to determine the nesting of functions is an analysis of the source code. However, compiling a large amount of source code just to figure out which function wraps another one would be too costly. Therefore, in this case, a hack is used based on knowledge of how memory management occurs inside the interpreter. The fact is that the garbage collector knows about all the objects that exist in memory, as well as about the links between them. The function in which a particular frame of the call stack is executed can be found by requesting from the gc all objects containing references to it. After retrieving the function object, you can determine whether it is the parent of the currently executed one. To do this, you just need to compare their full names (qualnames) stored in the metadata of the functions.Another couple of words deserves a description of protection from a call with a template in the form of a variable. As you could already understand from the description above, by default you can call the functionfonly with a string literal as an argument. This is necessary to protect the string from attacks that allow arbitrary code to be executed. Checking whether a string is a literal and not a variable is performed by analyzing an abstract syntax tree (AST). Through the stack, we find the function in which the code is currently running. Next, we analyze this code in search of situations where the functionfis called without a literal as an argument. If such a situation occurs at least 1 time in the line in which the code is currently being executed, we raiseSyntaxError. However, it should be borne in mind that this protection cannot work in REPL, because there are difficulties in extracting the source code of the function for AST analysis. There were also problems with how to extract data about the module in which the code is executed, so the protection will not work in the global scope either. And finally, this check does not work on Python 3.7.LimitationsBased on the description of the internal structure of the library, you can understand that there are some limitations that should be taken into account. Here are some of them:The performance is obviously lessthan that of the original f-strings embedded in the interpreter. Read more about this in the section withbenchmark.Mutable objects can change their state between the time the references to them are saved and the final calculation of the string. This means, for example, that if you create aLazyStringobject containing a reference to the list, then add another element to the list, and after that calculate the string - you will see the list in the already changed state in this line.Heavy objects may not be destroyed during garbage collection as long as references to them are stored inside theLazyString. This may look like a memory leak in some rare cases and you should keep this in mind.LazyStringobjects, despite their almost complete similarity tostr, still do not fully replace ordinary strings. For example, such a string cannot be written to a file or serialized using pickle. Many built-in Python functions expect only ordinary strings and it is impossible to fake it in any way. There is no way to get the full functionality of regular strings fromLazyString, except to convertLazyStringtostr, like this:str(f('some string'))Some methods of thestrclass also necessarily expectstrobjects as an argument. These are methods such ascount,replace,center,join,encode,maketrans,splitandrsplit,indexandrindex,removeprefixandremovesuffix,findandrfind,startswithandendswith,ljustandrjust,strip,lstripandrstrip,partitionandrpartition. This behavior cannot be changed, so when using lazy strings, it is most reliable to immediately convert them to regular ones. In addition, expressions using theinoperator like this one will not work correctly:f('some string')in'some another string'A specialf-stringformatting languageis not supported. All that is available is the usual embedding of expressions right inside the string.In most code editors and IDE, specialsyntax highlighting for f-strings will not work.BenchmarkIf you have read the text above, you already know that this implementation is slower than the original f-strings. But for how long?In fact, it is impossible to accurately predict the complexity of operations related to string extrapolation. It depends on many factors, including the depth of the call stack, the number of local and global variables, whether this function is nested in other functions, and also, obviously, the complexity of executing the expression that you have embedded in the string.For example, we will consider the degenerate case:fromtimeimportperf_counterimportft1=perf_counter()fornumberinrange(10000):str(f('the number is{number}'))print(perf_counter()-t1)On my computer (a MacBook Pro with an Apple M1 Pro processor), the execution of this code takes about 2 seconds, that is, about0.0002 seconds for 1 iteration. However, if I replacestr(f('the number is {number}'))withstr(f'the number is {number}'), the execution time will be 0.0022 seconds, about0.00000022 seconds for 1 iteration.So, the original f-strings in this case turned out to be about1000 times faster.However, this does not mean that f-strings are faster in all cases. In real use, you should consider how fast the expressions that you insert into the f-strings are evaluated. If this is significantly slower than actually required for extrapolation, saving on deferred extrapolation may make sense.Most of the extrapolation time is actually taken not by the extrapolation itself, but by collecting various data for display from the stack and the garbage collector. Unfortunately, if you want to completely replicate the behavior of the original f-strings, these costs are unavoidable.