Techno Blender
Digitally Yours.

Increase Your Productivity with VSCode Snippets | by Nik Piepenbreier | Oct, 2022

0 61


And three useful snippets for data science in Python

Photo by Mohammad Rahmani on Unsplash

Visual Studio Code (VSCode) is one of the most customizable and productive code editors available today. Using snippets is a great way to increase your productivity — but did you know you can create your own snippets? This tutorial will explore how you can create your own snippets to boost your data science productivity!

Code snippets are templates for reusable coding patterns. They let you automate keyboard commands and even let you cycle through different parameters.

This is better explained visually, so let’s take a look:

Snippets in VSCode are a lot of fun! (Source: author)

Snippets can be simple text expansions. But as the example above shows, you can even cycle through different parameters of the expansion!

Convinced? Let’s see how we can create a snippet of our own!

Not only does VS Code let you create your own snippets, it even lets you define what language they’re applicable to. To get started with making your own snippets, you can follow the steps below:

  1. Open the command palette and search for Snippets: Configure User Snippets.
  2. Select python.json to create snippets for Python.

This opens up the following editor, which gives you a template for creating snippets:

We can see that each snippet is given a name and then inside of a dictionary, with the following kets:

  • prefix: the keyboard command to use
  • body: the content of the snippet
  • description: a string to describe the snippet itself

You can insert placeholders for where to include cursor points to tab through by using the $1 values. These placeholders can be repeated and start at 1. $0 refers to the end position of the placeholders.

Let’s start with creating a few more snippets!

Below are three snippets to get your creative juices flowing and create some of your own:

Modify a Pandas Column

We can modify a Pandas column by re-assigning it to itself. Personally, I find this a bit tedious to write out, especially with longer column names. The snippet below makes the process much easier:

The snippet allows you to write the DataFrame name and column name only a single time, then hops to the end of the line.

Modifying a Pandas column snippet (Source: author)

Filter a Pandas DataFrame

We can filter a Pandas DataFrame by using different modifiers, such as == or >=. VSCode lets you define these as a drop-down by wrapping them in || characters.

Let’s see what this looks like:

Filtering a column in Pandas made easy (source: author)

if __name__ == “__main__”

Given the prevalence of this, writing this safeguarding command will make you wonder why you’ve never used snippets before!

This looks like the following:

Simplifying the if __name__ … statement (Source: author)

VSCode snippets allow you to simplify repetitive code. However, they also go much beyond text expanders, giving you the option to use placeholders you can easily tab through. I encourage you to make your own if you find yourself writing the same code over and over again!


And three useful snippets for data science in Python

Photo by Mohammad Rahmani on Unsplash

Visual Studio Code (VSCode) is one of the most customizable and productive code editors available today. Using snippets is a great way to increase your productivity — but did you know you can create your own snippets? This tutorial will explore how you can create your own snippets to boost your data science productivity!

Code snippets are templates for reusable coding patterns. They let you automate keyboard commands and even let you cycle through different parameters.

This is better explained visually, so let’s take a look:

Snippets in VSCode are a lot of fun! (Source: author)

Snippets can be simple text expansions. But as the example above shows, you can even cycle through different parameters of the expansion!

Convinced? Let’s see how we can create a snippet of our own!

Not only does VS Code let you create your own snippets, it even lets you define what language they’re applicable to. To get started with making your own snippets, you can follow the steps below:

  1. Open the command palette and search for Snippets: Configure User Snippets.
  2. Select python.json to create snippets for Python.

This opens up the following editor, which gives you a template for creating snippets:

We can see that each snippet is given a name and then inside of a dictionary, with the following kets:

  • prefix: the keyboard command to use
  • body: the content of the snippet
  • description: a string to describe the snippet itself

You can insert placeholders for where to include cursor points to tab through by using the $1 values. These placeholders can be repeated and start at 1. $0 refers to the end position of the placeholders.

Let’s start with creating a few more snippets!

Below are three snippets to get your creative juices flowing and create some of your own:

Modify a Pandas Column

We can modify a Pandas column by re-assigning it to itself. Personally, I find this a bit tedious to write out, especially with longer column names. The snippet below makes the process much easier:

The snippet allows you to write the DataFrame name and column name only a single time, then hops to the end of the line.

Modifying a Pandas column snippet (Source: author)

Filter a Pandas DataFrame

We can filter a Pandas DataFrame by using different modifiers, such as == or >=. VSCode lets you define these as a drop-down by wrapping them in || characters.

Let’s see what this looks like:

Filtering a column in Pandas made easy (source: author)

if __name__ == “__main__”

Given the prevalence of this, writing this safeguarding command will make you wonder why you’ve never used snippets before!

This looks like the following:

Simplifying the if __name__ … statement (Source: author)

VSCode snippets allow you to simplify repetitive code. However, they also go much beyond text expanders, giving you the option to use placeholders you can easily tab through. I encourage you to make your own if you find yourself writing the same code over and over again!

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment