A tool for easily entering Python code

Introduction

In this article, I'll show you how to easily enter Python code using a text input support tool called Paster. Use with editors such as JupyterLab and VS Code.

Tools to use

This time, we will use a software called "Paster" for Windows, which is released as free software. This software is a resident type software that has a "fixed phrase input function" and a "clipboard history management function". It can be used from various apps such as text editors, word processors, and web browsers. However, it cannot be used in the store app.

In the custom menu that is normally used, register the date, signature, user name and password used for logging in to the member site, etc., and paste them in the caret position in the pop-up menu format when necessary. There is also a function such as replacing a specific field in the template file with the specified text item and pasting it.

This custom menu can be freely created by the user according to the purpose. You can create your own menus for entering HTML and CSS tags, menus for entering LaTeX formulas, menus for easy routine processing with Yahoo! Auctions, and so on.

This time, I created a menu to easily enter Python code, so I would like to introduce it here.

By the way, ** This time, we have expanded the list menu of Paster and added a function that allows you to select multiple items and paste them together. In order to use this function, it is necessary to use "Ver7.10" or later released on September 9th. ** **

Installing and using Paster easily

** (Caution) Paster is Windows-only software. We are very sorry, but Mac users cannot use it. ** **

Paster can be downloaded from the following site. ・ Autumn Soft

You can also find a video explanation of how to use it on YouTube. -Installation and basic operation

The custom menu (PythonMenu.txt and other files) used this time is the Paster download page on the above Autumn software site. It can be obtained from.

Custom menu image

Now, let's explain what kind of image you actually use to enter Python code. If you register the menu file (PythonMenu.txt) to be used this time in the paster and call it, the following pop-up menu will be displayed. 2020-09-23_09h24_47.png Here, for example, if you select "Import NumPy", the following code will be pasted. 2020-09-23_09h26_36.png

You can also paste multiple import statements at once. In this case, select multiple items using the list menu. Select the "Newline join" check box at the top right of the dialog. (In the image, only 2 items are selected, but actually 5 items are selected) 2020-09-23_09h30_41.png Then, the following items will be pasted at once. 2020-09-23_09h32_58.png

With this kind of feeling, you can paste the standard Python code more and more. 2020-09-23_09h38_28.png 2020-09-23_09h41_32.png 2020-09-23_09h43_27.png 2020-09-23_09h45_20.png

After pasting the fixed phrase, adjust the details such as arguments. In the above example, what I entered was

X = data.data
y = data.target

Only the part of. Machine learning code such as scikit-learn has a unified interface, so it is especially efficient to input in this way.

Menu file structure

Here, the structure of the menu file is explained. First, create a file called "PythonMenu.txt" and use the import statement to import the menu files for various packages. 2020-09-23_09h24_47.png

PythonMenu.txt


================================================================
+Python boilerplate pattern
@import "PyBasic.txt"
..
-
================================================================
+NumPy related items
@import "PyNumPy.txt"
..
-
================================================================
+Pandas related items
@import "PyPandas.txt"
..
-
================================================================
+Items related to matplotlib
@import "PyMatplotlib.txt"
..
================================================================
+Items related to seaborn
@import "PySeaborn.txt"
..
-
================================================================
+scikit-Learn related items
@import "PyScikitLearn.txt"
..

Then, define the main body of each package in a separate file. In the case of scikit-learn ...

PyScikitLearn.txt


================================================================
#scikit-import of learn...
[Load data set] Iris measurement data (load)_iris) |
		[/]from sklearn.datasets import load_iris
	/E
[Dataset load] Boston city house price (load)_boston) |
		[/]from sklearn.datasets import load_boston
	/E
[Load dataset] Handwritten numbers (load)_digits) |
		[/]from sklearn.datasets import load_digits
	/E
[Load dataset] Wine data (load)_wine) |
		[/]from sklearn.datasets import load_wine
	/E
[Load dataset] Data for diabetics (load)_diabetes) |
		[/]from sklearn.datasets import load_diabetes
	/E
[Load dataset] Celebrity face image (fetch)_lfw_people) |
		[/]from sklearn.datasets import fetch_lfw_people
	/E
[Data set generation] Classification data set generation (make)_blobs) | 
		[/]from sklearn.datasets import make_blobs
	/E
[Data set generation] Classification data set generation (make)_classification) | 
		[/]from sklearn.datasets import make_classification
	/E
[Data set generation] Regression data set generation (make)_regression) | 
		[/]from sklearn.datasets import make_regression
	/E
[Data set generation] Concentric circle data set generation (make)_gaussian_quantiles) | 
		[/]from sklearn.datasets import make_gaussian_quantiles
	/E
[Data set generation] Concentric circle data set generation (make)_circles) | 
		[/]from sklearn.datasets import make_circles
	/E
[Data set generation] Semicircular data set generation (make)_moons) | 
		[/]from sklearn.datasets import make_moons
	/E
	--------------------------------------------------------------- | [$]
Pipeline generation (make)_pipeline) | 
		[/]from sklearn.pipeline import make_pipeline
	/E
	--------------------------------------------------------------- | [$]
	【model_selection] Data division (train)_test_split) | 
		[/]from sklearn.model_selection import train_test_split
	/E
	【model_selection] GridSearch (GridSearchCV)| 
		[/]from sklearn.model_selection import GridSearchCV
	/E

(Omitted below ...)

It looks like this.

In other words, if you don't like the missing packages or what I made (I think they're actually pretty incomplete), you're free to create your own menu file and import it with an import statement.

In addition, these files are saved together in a subfolder created in the data folder of Paster. 2020-09-23_10h20_12.png

The settings for registering and calling PythonMenu.txt are as follows. Specify the shortcut key setting you like. 2020-09-23_10h22_57.png

Three patterns for pasting Python code

When pasting the Python code, this time I mainly use three patterns.

--Paste the code by entering some dummy number in the argument of the function or method. --Use a wizard-style dialog to enter the value of each argument and paste the code. --Use the list menu to have multiple items selected and paste them. At this time, specify whether to combine each item directly or with a line break.

Then, I will explain them individually below.

Paste the code directly

When it is necessary to specify an argument to a method etc., paste it with a dummy value entered. For example, there are the following cases. 2020-09-23_13h13_58.png When you run a menu item like this, the code below will be pasted. 2020-09-23_13h15_11.png

Comment out the first comment if you haven't already imported joblib. And for the file name and compression ratio, dummy text has been entered, so replace it with something appropriate after pasting.

Paste the code using a wizard-style dialog

When it is necessary to specify multiple arguments for a method etc., a wizard-style dialog is displayed and each item is entered. By the way, ** Paster calls such a function a [user-specified] tag. ** **

For example, there are the following cases. 2020-09-23_13h22_41.png When you execute this menu item, the following dialog is displayed, and you can enter the mean value, variance, covariance, number, and so on. WizardDlg.png Then, the following code will be entered. 2020-09-23_14h23_35.png For things with many arguments, such as the multivariate normal distribution, you can easily enter them using a dialog like this without having to remember how to specify the arguments.

By the way, the menu file describes as follows.

PyNumPy.txt


Random numbers with multivariate normal distribution (specify mean, variance and covariance) [two-dimensional]... |
	[/][#|Enter the average value 1|Enter the average value 2|Enter variance 1|Enter variance 2|
Enter the covariance|Enter the number]
	[/]mean = np.array([&1][#1], [#2][&2])[!K "ENTER"]
	[/]cov = np.array([&1][&1][#3], [#5][&2], [&1][#5], [#4][&2][&2])[!K "ENTER"]
	[/]x = np.random.multivariate_normal(mean, cov, size = [#6])
/E

Paste multiple items at once using the list menu.

When it is necessary to specify multiple optional arguments for a method etc., display the list menu and ask each item to be entered. For example, there are the following cases. 2020-09-23_13h53_19.png When you execute this menu item, the following list menu will be displayed. Select the argument you want to use freely (hold down the Ctrl key and click). At this time, be sure to select the first "method name" and the last "closing parenthesis". 2020-09-23_13h55_43.png Then, the following code will be entered. 2020-09-23_13h57_41.png As an aside, an extra comma is inserted just before the last right parenthesis, so the following key operation (backspace key twice) is executed to delete it.

PyPandas.txt


(The first half is omitted ...)
[Argument] squeeze: Convert one column of csv to Series| squeeze=True,[$ ]
[Closed parenthesis]) | [!K2 "BACK"])

Notes

There are some caveats when writing a menu file.

First of all, ** Paster has a function called "tag" to modify the date, time, and selected text. ** This tag is described using square brackets as shown below.

MenuItem.txt


signature[Date version] | [Y1]/[T1]/[D1]([W3]) [H1]:[M2]nickname<[email protected]>[&R]

This collides with the symbols used to write ** Python lists ([1, 2, 3]). ** ** Therefore, in Paster, when entering the square brackets themselves, the following tags are used. ([& 1] and [& 2])

; '[' --> [&1], ']' --> [&2]It is described as.
test| [&1]1, 2, 3[&2]

Next is ** adjusting Python indentation. ** ** In both JupyterLab and VS Code, if you break a line in a function definition or in a for statement, the editor will automatically adjust the indentation for the next line. However, if you paste multiple lines of text from the clipboard etc., this automatic indentation will not work. Therefore, in the menu file created this time, when pasting multiple lines of code, the [Enter] key is pressed at the end of each line.

PyNumPy.txt


Random numbers with multivariate normal distribution (specify mean, variance and covariance) [two-dimensional]... |
	[/][#|Enter the average value 1|Enter the average value 2|Enter variance 1|Enter variance 2|
Enter the covariance|Enter the number]
	[/]mean = np.array([&1][#1], [#2][&2])[!K "ENTER"]
	[/]cov = np.array([&1][&1][#3], [#5][&2], [&1][#5], [#4][&2][&2])[!K "ENTER"]
	[/]x = np.random.multivariate_normal(mean, cov, size = [#6])
/E

It's a bit annoying, but to make auto-indent work, write it like this.

Summary

The above is a brief explanation of the menu file. At first, I created it with one menu file, but as I added the packages, the file became bigger and bigger, so I divided it into each package.

I really wanted to add a scraping related package, but I'm out of physical strength ... I'd like to add it in the future. (Sweat;

We are also looking for volunteers for other packages, so we would be grateful if you could create one.

I wrote it for a long time, but thank you for reading this far.

Recommended Posts

A tool for easily entering Python code
Python code memo for yourself
[Python] Sample code for Python grammar
[Python] Create a screen for HTTP status code 403/404/500 with Django
Install Cheminformatics Tool RDKit for Python
A note for writing Python-like code
[Python] Creating a scraping tool Memo
Easily expand shortened URLs for Python
A memorandum of understanding for the Python package management tool ez_setup
Create a Python environment for professionals in VS Code on Windows
Python> I made a test code for my own external file
Created a library for python that can easily handle morpheme division
I made a scaffolding tool for the Python web framework Bottle
Python visualization tool for data analysis work
Python environment tool comparison chart for Rubyist
Let's create a virtual environment for Python
[Mac] Building a virtual environment for Python
Make Qt for Python app a desktop app
python> coding guide> PEP 0008 --Style Guide for Python Code
Get a token for conoha in python
Run Python code on A2019 Community Edition
I created a password tool in Python.
Building a Python development environment for AI development
R code compatible sheet for Python users
A textbook for beginners made by Python beginners
Prepare a Python virtual environment for your project with venv with VS Code
Code reading of Safe, a library for checking password strength in Python
2016-10-30 else for Python3> for:
[Python, Scala] Do a tutorial for Apache Spark
python [for myself]
A tool for creating symbolic links on Windows
Created a Python wrapper for the Qiita API
I made a useful tool for Digital Ocean
[Python] A tool that allows intuitive relative import
Get a ticket for a theme park with python
Is there a good sample code for nosetests?
Create a LINE BOT with Minette for Python
Created a header-only library management tool for C / C ++
Try using Sourcetrail, a source code visualization tool
Procedure for creating a LineBot made with Python
You can easily create a GUI with Python
python character code
Python: Prepare a serializer for the class instance:
A memorandum for touching python Flask on heroku
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
Code for checking the operation of Python Matplotlib
I tried running alembic, a Python migration tool
Settings for Python coding in Visual Studio Code
Create a Python console application easily with Click
A memo for creating a python environment by a beginner
Let's make a module for Python using SWIG
Code reading for m3u8, a library for manipulating HLS video format m3u8 files in Python
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Build a python execution environment with VS Code
Script to easily create a client device environment for AWS IoT (Python v2 version)
Environment construction for those who want to study python easily with VS Code (for Mac)
Tool to make mask image for ETC in Python
I want to easily implement a timeout in python
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Write about building a Python environment for writing Qiita Qiita