2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Python・Jupyter】クリップボードにコピーしたプログラムのコメントを翻訳して新しいセルに挿入する

Last updated at Posted at 2020-10-19

#はじめに
日本語訳のないライブラリのドキュメントを読むときなど、地味に解釈に苦労することなどありますよね。
英語をスラスラ読めない方だとブラウザの機能で翻訳することが多いと思いますが、コメントだけでいいのにプログラム部分まで翻訳されてしまうのは困りものです。

そこで、Jupyter Notebook用にコメント部分だけ翻訳して、
どうせならシンタックスハイライト機能も活かしたいということで、新しいセルに貼り付けるごくシンプルなコードを書いてみました。

#コード

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)
ppc.copy(code)
pyautogui.hotkey('ctrl', 'v')

#使用した様子

元コード
[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
r.asg0.setup(waveform='sin',
             amplitude=0.5,
             frequency=10e6,
             output_direct='out2')

# 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
r.pid0.setup(input='iq0',
             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')
`In[2]`のセルを実行するとその下のセルが自動で生成されます。 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/501281/73a5d60f-8e6f-0792-525b-33b691dda40a.png)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?