Learn TOML in 10 Minutes (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be explaining what tommel is and how you can get started with using it in your projects in just a couple of minutes so to get started tomal is short for Tom's obvious minimal language which was first introduced in 2013 and it was humbly named after its creator now Tamil prioritizes humans and this was extracted directly from the website it aims to be a minimal configuration file format that is easy to read that maps unambiguously and is easy to pass so that's what toml aims to do now Tamil supports a lot of familiar syntax it has strings it has integers floats booleans so that means if you come from a language such as python you're going to be able to use it very easily a typical Tomo file will look like this and don't worry we're going to go over all of this step by step so first of all we want to create a new Tamil file and to do that open up a folder in any code editor of your choice and tap on new file here I'm going to type in config in lowercase dot tomml as the extension and this will create for us a tummel file and config just stands for configuration so this is a file that will preferably hold the configuration for our code in tomml you can add a hash to write a comment which is very convenient for python developers because that's how we usually write comments so here we can type in something such as config file if we want then below that we can start writing our key value pairs so name can be set to my project for example and then we can add a version number for that which is going to be 1.0.0 and finally we're going to add a website and the website is going to be example.com I don't have a website in particular but for this example we will use example.com and we're going to leave it at that for our first toml file because I also want to show you how we can load this into our script as of python 3.11 we now have the option to use tomlib and with tomlib we can load that Tamil data easier and I'm also going to import from pretty prints the pretty print method but that's just to format the Json or the dictionary so you can see it in a nicer format but here we'll type in Dev load tunnel and it's going to return to us a dictionary and all it's going to do is load Tamil data from a file and this is the copilot code completion so I'm just writing this out by hand but the first thing you want to do here is Type in with open and we want to open that config file and we want to open it in read bytes mode as F which is the file then the Tamil data is going to equal a dictionary and we're going to call tomlib Dot load and we want to load that file then all we have to do is return the Tamil data so that's the entire function we need to load this Tamil and it doesn't have to be a function but I will be using it for this example next we will do if name is equal to Main and First we want to grab that data so the data is going to equal load tunnel and finally we're going to pretty print the data and set the sorted dictionaries key to false which means now when we run this you will notice that our data is going to print in the console we're going to get the name the version and the website thanks to the config toml file which of course can get much more complex than that but for now we were able to grab that information and anytime we want to make a change we just change the toml file and it's going to reflect in our project so that was the first Tamil file you created but there's a lot of crazy syntax that comes along with tomal that I need to show you so now we have the script up and running all we have to do is Rerun the program see the changes so that means any changes we do to this tunnel file will now be reflected inside the console which is great because I can now finally show you what certain syntax does when you use it in the toml file so I will close the console for now and what we're going to do next is create a table so to create a table you just want to add some square brackets and give it a name and a table is essentially a group so everything we put inside here is going to be grouped as items for example inside here we can type in numbers and numbers is going to equal an array of one two and three and we're going to have something called letters which will also be an array of a b and c now if we run this you're going to notice that items is going to hold another dictionary inside it which is going to hold numbers of numbers and letters of letters so that's what happens when you create a table and you can even create a sub table you can say items dot let's say details and inside here we're going to add updated which is going to be set to true and in Tamil you use lowercase true and lowercase faults for booleans which is a bit annoying for python developers but it's good to keep in mind then we're going to give it an author name the author who actually updated this and it's going to be John Doe because that's what co-pilot gave me but now if we run this you'll notice in the console that inside our dictionary or our tummel file we're going to get back first the items which has numbers and letters and then we're going to have a subtable that contains another dictionary of these items over here so a simple way to look at this is that these are sections of our configuration file more realistically you'll probably have something such as a database and the database will have a type such as let's say sqlite so SQL Lite and it's going to have a path of data.db so that's some information that you would choose to include and if you run it you'll see that in that section down below you're going to have data.db and sqlite in the bottom section down here something else I should mention is that all the keys are interpreted as strings so if you type in 10 for example that's perfectly fine but as soon as you run it it's going to be converted to a string also toml files do not have a null type which means we cannot use none and we cannot leave it blank as for the encoding Tamil files use utf-8 which means we can use funky symbols such as the Danish ER and if we run that it will run perfectly fine it can use that as a key and I absolutely forgot to mention that if you do decide to use a symbol that's a bit funky such as the Danish uh you're going to have to surround it by quotation marks this is the exception to using these funky symbols otherwise if you use normal letters and numbers you can just type in those as follows but again with the ER you're going to want to add the double quotation marks and both single and double quotation marks work when it comes to Json and these other files I just prefer to follow the double quotation marks I feel like that's a lot more International in Python it's a much bigger personal preference that I use single quotation marks and even here I mean if I were to follow my own naming convention I would use double quotation marks so I would recommend picking something and sticking with that or whatever your company wants you to do but let's change this back to the file so we have a file called data.db and now the same way we created a subtable we can create sub Keys which means instead of having let's say type we can type in file DOT type and instead of file.file we can type in file dot path so file.path and when we run that this time we're going to get a new dictionary inside that has the type and the path together next you can also create an inline table so if we type in inline table for example here we can type in inline and inline is going to be equal to a dictionary such as name of John Doe and age of 30. and it doesn't really do anything special other than just create a simple dictionary inside the inline table and this syntax can really clutter your tummel file so it's recommended you keep this sweet and short otherwise your imagination is the limit and finally I want to show you that you can create a list of tables so for example just by adding two square brackets we can type in for example table group and this table group is going to be grouped together with other table groups so if we type in fruits and that's equal to Apple and then we create one more table group and we say fruits and that is equal to orange or yeah we'll go for orange and why not just create one more the last one is going to be equal to banana so now we have three table groups here and when we run this you'll notice that a table group is going to hold a list of each one of these groups so each fruit will be held respectively by each one of the groups so that was the basic Syntax for using Tamil files in your projects it's very easy to read and very easy to use I personally love it and I'm just going to bring back the example from the website but you can also use this with timestamps which means if you use a timestamp such as this one and actually it would be a lot cooler if I demonstrated this so I'm going to go back to the config file and I'm going to create a timestamp in items dot details which is going to say timestamp that's going to equal let's say 2019 followed by autocomplete which is going to give me this timestamp if you insert a valid timestamp and you run your program it's going to return to you an object based on that timestamp based in your programming which so in Python we use the time dot date time and it's going to return to us that object which is super cool but I'm not going to jump too deep into this because date and time is a whole different topic but anyways I hope that gave you a very good idea of where to start with using Tamil files as you could see they're not that scary they actually are very easy to use and very easy to create configuration files with so jump straight into it and have fun with it but with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 23,784
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: D_Jb52jw2HY
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Thu May 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.