[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell


When reading a document in a library that does not have a Japanese translation, you may have a hard time interpreting it. If you can't read English fluently, I think that it is often translated by the function of the browser, but it is troublesome that even the program part is translated even though only comments are required.

So, translate only the comment part for Jupyter Notebook, I wanted to take advantage of the syntax highlighting feature anyway, so I wrote a very simple code to paste into a new cell.


from googletrans import Translator
import re
import pyautogui
import pyperclip as ppc
translator = Translator()

pyautogui.hotkey('b', 'enter')
code = ppc.paste()
en = re.findall("#\s*(.+?)\n", code)
ja = [translator.translate(e, dest="ja").text for e in en]
for e, j in zip(en, ja):
    code = code.replace(e, j)
pyautogui.hotkey('ctrl', 'v')

State of use

Original code
[What is PyRPL?](https://pyrpl.readthedocs.io/en/latest/)
# import pyrpl library
import pyrpl

# create an interface to the Red Pitaya
r = pyrpl.Pyrpl().redpitaya

r.hk.led = 0b10101010  # change led pattern

# measure a few signal values
print("Voltage at analog input1: %.3f" % r.sampler.in1)
print("Voltage at analog output2: %.3f" % r.sampler.out2)
print("Voltage at the digital filter's output: %.3f" % r.sampler.iir)

# output a function U(t) = 0.5 V * sin(2 pi * 10 MHz * t) to output2

# demodulate the output signal from the arbitrary signal generator
r.iq0.setup(input='asg0',   # demodulate the signal from asg0
            frequency=10e6,  # demodulaltion at 10 MHz
            bandwidth=1e5)  # demodulation bandwidth of 100 kHz

# set up a PID controller on the demodulated signal and add result to out2
             output_direct='out2',  # add pid signal to output 2
             setpoint=0.05, # pid setpoint of 50 mV
             p=0.1,  # proportional gain factor of 0.1
             i=100,  # integrator unity-gain-frequency of 100 Hz
             input_filter = [3e3, 10e3])  # add 2 low-passes (3 and 10 kHz)

# modify some parameters in real-time
r.iq0.frequency += 2.3  # add 2.3 Hz to demodulation frequency
r.pid0.i *= 2  # double the integrator unity-gain-frequency

# take oscilloscope traces of the demodulated and pid signal
data = r.scope.curve(input1='iq0', input2='pid0',
                     duration=1.0, trigger_source='immediately')
When you execute the cell of ╩╗In [2]`, the cell below it is automatically generated. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/501281/73a5d60f-8e6f-0792-525b-33b691dda40a.png)

Recommended Posts

[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
I made a program to check the size of a file in Python
[Python] How to save the installed package and install it in a new environment at once Mac environment
To output a value even in the middle of a cell with Jupyter Notebook
The result of making a map album of Italy honeymoon in Python and sharing it
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
Recursively get the Excel list in a specific folder with python and write it to Excel.
I tried to create a Python script to get the value of a cell in Microsoft Excel
An easy way to view the time taken in Python and a smarter way to improve it
[Python] A program to find the number of apples and oranges that can be harvested
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Convert the result of python optparse to dict and utilize it
[Python] A program that rotates the contents of the list to the left
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
[Python] A program that calculates the number of socks to be paired
How to stop a program in python until a specific date and time
How to input a character string in Python and output it as it is or in the opposite direction.
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
What I investigated in the process of expressing (schematicizing) containers in a nested frame with Jupyter and making it
Make a Python program a daemon and run it automatically when the OS starts
Get the caller of a function in Python
How to count the number of elements in Django and output to a template
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Make a copy of the list in Python
How to save the feature point information of an image in a file and use it for matching
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
Build a python environment to learn the theory and implementation of deep learning
Let's write a Python program and run it
[Python] A program that calculates the number of updates of the highest and lowest records
Output in the form of a python array
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
A discussion of the strengths and weaknesses of Python
A tool to insert the country name and country code in the IP address part
How to get a list of files in the same directory with python
[Python] Automatically read prefectural information on the new coronavirus from the PDF of the Ministry of Health, Labor and Welfare and write it in a spreadsheet or Excel.
A Python script that crawls RSS in Azure Status and posts it to Hipchat
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
[Python3] Take a screenshot of a web page on the server and crop it further
How to identify the element with the smallest number of characters in a Python list?
[Python] A program that finds the shortest number of steps in a game that crosses clouds
[Python] Change the text color and background color of a specific keyword in print output
[GCF + Python] How to upload Excel to GCS and create a new table in BigQuery
I want to replace the variables in the python template file and mass-produce it in another file.
How to check in Python if one of the elements of a list is in another list
[Mac] A super-easy way to execute system commands in Python and output the results
A note that runs an external program in Python and parses the resulting line
A story about trying to introduce Linter in the middle of a Python (Flask) project
It is easy to execute SQL with Python and output the result in Excel
[Python] A program that counts the number of valleys
How to get the number of digits in Python
I made a Python module to translate comment outs
Tasks at the start of a new python project
A reminder about the implementation of recommendations in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
[Python] A program that compares the positions of kangaroos.
Python Note: The mystery of assigning a variable to a variable