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.
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. ** **
** (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.
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. Here, for example, if you select "Import NumPy", the following code will be pasted.
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) Then, the following items will be pasted at once.
With this kind of feeling, you can paste the standard Python code more and more.
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.
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.
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.
The settings for registering and calling PythonMenu.txt are as follows. Specify the shortcut key setting you like.
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.
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. When you run a menu item like this, the code below will be pasted.
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.
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. When you execute this menu item, the following dialog is displayed, and you can enter the mean value, variance, covariance, number, and so on. Then, the following code will be entered. 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
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. 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". Then, the following code will be entered. 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"])
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.
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