Is This PyCharm’s MOST Powerful Feature For Python Developers? (Live Templates)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's it going guys in today's video we're going to be looking at one of the most powerful features in pycharm and this has to do with the live templates so to demonstrate how it works we're going to create some items which are just going to contain one two three four and five so a list of items and if we want to iterate through this we'll just type in ITA and you'll get this great suggestion here which says iterate for ellipses in ellipses and this is a live template so if we tap enter on that we're going to get the fields filled out so here you can pick what kind of interval you want to insert and then if you tap enter it's going to go to the item so you can actually just type in whatever name you want if you don't like item but as you can see it already fills it out automatically for us so we don't have to write all of that if we tap enter again it's going to go down one line so we can just print item but what if we have two groups how does it know which group we want to use or which list we want to use well let's duplicate that and let's call the second one things and things is just going to contain a list of A and B so it's something completely different but this time we're going to use a different live template we're going to use a live template that enumerates the items as well or the things as well so here we can type in ETA and this time we're going to use ETA e so iteri which will use the enumerate keyword as well and if we tap on enter you'll notice it's going to give us the selection between things and items this time we want to go through things so we'll type on things and it's going to automatically replace the item we are iterating through so it's going to change item to thing and we can tap on enter and the last one we want to edit of course is the I which is a very good convention so we're not going to change that and we'll tap on enter one more time now we can just print I think with a separator of a colon and a space and if we run this we'll get one two three four five and we will get a and b enumerated and if you're curious about what live templates you already have in this context you can use Control Plus J or command plus J and it's going to give you all of the live templates that you have so a lot of these are comprehensions such as set comprehensions list comprehensions and you can play around with that I mean you can just remove all of this and you can say a new items it's going to equal comp and we just need to get the list comprehension so comp L and you'll get item per item in items but again we want things so it will replace it two things and it was that fast to use and the most popular one that I use is the if name is equal to main you just enter Main and it fills out the rest then you can print your code whatever is happening you can put it inside that so that was a quick preview on what it can do now it's important to show you how you can actually create your own live template and this is the interesting part so what we're going to be creating is this note to life template so if you type in notes it's going to insert your name the date and whatever text you want to insert so you can say this is a note and it was that fast to enter this and that's exactly what I'm going to be showing you how to do and you can replace it with whatever you want I will also link the documentation in the description box down below so you can get a closer look at what all the settings actually do but let's get started with a simple live template which will be the starting point of all your live templates so first you want to open up your settings so I'm going to use command plus comma and you want to find the live template section and you can also just do that by typing in live templates and once you find it you should have a section such as this that has HTML python shell script then all that stuff and you want to open up the python one so that you can insert a new template so what we need to do is click on ADD and we want to click on live templates and here we're going to get an abbreviation at the bottom and this is what we want to use as the keyword for our live template so we can type in something such as warning in case we want to provide a warning for the user and here we will say provides a warning although you can also show a mini examples such as this intersection here that says iterate for ellipses in ellipses that might be more useful for the user who's using it but you can also just add a message or a description of your choice so what we're going to do here is insert the template and this is where it gets interesting so we're going to type in hash and warning because we want to create a comment with a warning and I'm going to add a parenthesis and inside here we're going to add the username so here we can add a dollar sign it's a user and another dollar sign and surrounding that user by dollar signs turns it into a live template variable so after that we want to provide the warning message so we're just going to type in message and let's also include the date so we'll add another pair parentheses dollar sign dates dollar sign parentheses and you can name this okay and this can be named whatever you want but to make it easier to remember I would type in date if you're going to include the date and that's going to be the live template that we'll be using here next you'll notice at the bottom that it's going to say no applicable context and that means we need to tap on Define and tell pycharm where we want this to actually be applied so in most cases we want it to be applied in Python and you can get more specific if you want but I'm just going to tap on python as a whole so it's going to paste it everywhere in there and you can also just click on everywhere if you wanted to be suggested everywhere but we're not done yet now we have this basic section configured we need to tap on edit variables so the thing is here we can add some Expressions on how we want these variables to be defined or how we want them to be expressed so you can provide a default value and here we want to provide a default value for the message so we can say warning message now for the date how do we get the date well if you tap on this block here and tap on the Arrow you'll notice we will have a lot of functions that we can use to generate text or to manipulate text or to manipulate whatever the variable is so here what we want to get is the date and actually I just skipped it but there's the date method which will just return the date in the user's current Locale and you can also choose to skip if defined so right here we Define the date if we tap on skip if defined it's not going to give us that edit box every time we click on the Node so I'm going to just keep that selected to show you what it does and here there's also an expression for selecting the current user so the user is going to be picked and we're going to make sure that they can edit it if they want so we're not going to tap on skip if defined but we do want them to enter a message and we're not going to allow them to change the date by default but now we can tap on OK and we can tap on apply and we're going to have this warning live template if we actually click on OK again and we remove this message we can now type in warning and you'll notice at the bottom that we're going to get the live template and if we tap on enter it's going to start with the username which we can edit as we want and if we tap on enter it will take us to the warning message and we can say Don't Touch This and if we tap on enter one more time it's going to finish the comment we were unable to change the date because we told the program to skip it if it was already inserted and that's how you can create a basic live template of course you can get much more creative than this and I would recommend going to the live templates and tapping on the other ones to see exactly how they do it tap on edit variables and you'll notice that they're using something called Pi iterable variable and collection element name to use this iterable and this variable to create the for Loop live template so it might be a good idea to explore these in pycharm before making some more and in case you're curious about what all of these do I will leave a link to the documentation in the description box down below where you can read through all of them so you can find out exactly what they do there are just too many for me to explain in this video but for example if you insert capitalize there it's going to capitalize the first letter of a string so in case you want to find out what all of them do just go to this documentation tap on live template variables and explore it so now I have these two live templates that I can use as many times as I want in case I am not Federico I can also type in Bob I can change the date with my other one and I can add a message if I want and that was all using a live template and I've been using pycharm for over two years now and I haven't even touched live templates so I'm going to start exploring the actual capabilities of pycharm because I feel like I'm not really using all the features that come with it so do let me know what you think about this in the comment section down below or whether there's something else in pycharm that you would like me to cover something that makes the workflow much faster or something that's interesting please leave it in the comment section down below I will look at it and try to make a video about it but otherwise with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 9,465
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: VTDU26E5Wxw
Channel Id: undefined
Length: 10min 4sec (604 seconds)
Published: Mon Mar 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.