hi there serdar gagerlop here for infoworld in this episode of smart python we're going
to look at how python lets you create your own data formatting languages for your own data
types a formatting language is a specialized string typically used in an f-string or in the
format command that indicates how you want some data to be represented for instance dates and
times have their own custom format language the strf time formatting standard you insert
placeholder characters in a string to indicate how to translate the underlying data into text
there's one character for to insert a four digit year another one for inserting a two digit month
and so on and so forth what's not as well known is that you can create your own custom formatting
syntax for classes that you've also created python supports a special method called format
on classes that lets you accomplish this and it's not at all hard to implement
for my example i'm going to create a class called weight and when initialized it takes
in a value in grams and then translates this value internally into a total number of ounces
a number of pounds without ounces and remaining ounces so we can get a value like one pound nine
ounces by using formatting now the way the format method works for this class and the way it works
in general is that it takes in a string which is the formatting string that we will supply through
our format statement i'll explain more detail on that how that works in a moment in that format
string we look for one of a number of different placeholder characters and the way we distinguish
them is by proceeding them with a percent sign for instance percent p returns pounds but no ounces
percent o returns the remaining ounces so again we can do a figure like one pound nine ounces
percent g returns grams and percent k returns kilograms rounded off to three decimal places
all we need to do is perform string substitutions for these values on our incoming format string
and then return the result as a string so each of these placeholders will get
replaced in turn with the appropriate value as declared in our class here
at the bottom of the code i have three examples of how this is going to work for each one i use
an f string with w as our instance of the class weight now with each of these everything after the
colon in the variable reference in the f string is what's getting passed to the format method
so if we run this code you can see how each of these format strings returns a different result
even though they're all referring to the same variable
now this is an exceedingly simple example we could if we wanted to devise a far more sophisticated
formatting mini language within the confines of what we can pass through an f-string expression
we could put anything we want in that function after all we just have to remember not to put
too much computation into the format function if you can help it or that may slow things down
see in this example here we've pre-computed all of the values that we're likely to use and we
do that on the creation of the class for a more complex example if we needed to compute things
on the fly we might do so and then cache the results as needed whenever we're performing the
formatting action so to sum all this up you can create a custom formatting system for any python
class by way of the format method the exact syntax that you want to use for your formatting language
is up to you but you don't have to make the implementation very complex
to make it useful a simple substitution like the one i've shown here
will do nicely that's it for this video if you liked it please leave a comment below
don't forget to subscribe to the tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com