def _debug_repr_model(model): cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds] code_fmt = ut.codeblock( ''' import numpy as np import pgmpy import pgmpy.inference import pgmpy.factors import pgmpy.models {cpds} cpd_list = {nodes} input_graph = {edges} model = pgmpy.models.BayesianModel(input_graph) model.add_cpds(*cpd_list) infr = pgmpy.inference.BeliefPropagation(model) ''') code = code_fmt.format( cpds='\n'.join(cpd_code_list), nodes=ut.repr2(sorted(model.nodes()), strvals=True), edges=ut.repr2(sorted(model.edges()), nl=1), ) ut.print_code(code) ut.copy_text_to_clipboard(code)
def _debug_repr_model(model): cpd_code_list = [_debug_repr_cpd(cpd) for cpd in model.cpds] code_fmt = ut.codeblock(''' import numpy as np import pgmpy import pgmpy.inference import pgmpy.factors import pgmpy.models {cpds} cpd_list = {nodes} input_graph = {edges} model = pgmpy.models.BayesianModel(input_graph) model.add_cpds(*cpd_list) infr = pgmpy.inference.BeliefPropagation(model) ''') code = code_fmt.format( cpds='\n'.join(cpd_code_list), nodes=ut.repr2(sorted(model.nodes()), strvals=True), edges=ut.repr2(sorted(model.edges()), nl=1), ) ut.print_code(code) ut.copy_text_to_clipboard(code)
def enter_text_in_terminal(text, return_to_vim=True): """ Takes a block of text, copies it to the clipboard, pastes it into the most recently used terminal, presses enter (if needed) to run what presumably is a command or script, and then returns to vim. DEPRICATE: use vimtk instead TODO: * User specified terminal pattern * User specified paste keypress * Allow usage from non-gui terminal vim. (ensure we can detect if we are running in a terminal and register our window as the active vim, and then paste into the second mru terminal) """ import utool as ut # Copy the text to the clipboard ut.copy_text_to_clipboard(text) # Build xdtool script import sys if sys.platform.startswith('win32'): print('win32 cannot copy to terminal yet. Just copied to clipboard. ' ' Needs AHK support for motion?') return terminal_pattern = wmctrl_terminal_pattern() # Sequence of key presses that will trigger a paste event paste_keypress = 'ctrl+shift+v' doscript = [ ('remember_window_id', 'ACTIVE_GVIM'), ('focus', terminal_pattern), ('key', paste_keypress), ('key', 'KP_Enter'), ] if '\n' in text: # Press enter twice for multiline texts doscript += [ ('key', 'KP_Enter'), ] if return_to_vim: doscript += [ ('focus_id', '$ACTIVE_GVIM'), ] # execute script ut.util_ubuntu.XCtrl.do(*doscript, sleeptime=.01)
def current_gvim_edit(op='e', fpath=''): r""" CommandLine: python -m utool.util_ubuntu XCtrl.current_gvim_edit sp ~/.bashrc """ import utool as ut fpath = ut.unexpanduser(ut.truepath(fpath)) # print('fpath = %r' % (fpath,)) ut.copy_text_to_clipboard(fpath) # print(ut.get_clipboard()) doscript = [ ('focus', 'gvim'), ('key', 'Escape'), ('type2', ';' + op + ' ' + fpath), # ('type2', ';' + op + ' '), # ('key', 'ctrl+v'), ('key', 'KP_Enter'), ] XCtrl.do(*doscript, verbose=0, sleeptime=.001)
def copy_gvim_to_terminal_script(text, return_to_win="1", verbose=0, sleeptime=.02): """ import utool.util_ubuntu utool.util_ubuntu.XCtrl.copy_gvim_to_terminal_script('print("hi")', verbose=1) python -m utool.util_ubuntu XCtrl.copy_gvim_to_terminal_script "echo hi" 1 1 If this doesn't work make sure pyperclip is installed and set to xsel print('foobar') echo hi """ # Prepare to send text to xdotool import utool as ut import utool.util_ubuntu ut.copy_text_to_clipboard(text) if verbose: print('text = %r' % (text,)) print(ut.get_clipboard()) # Build xdtool script doscript = [ ('remember_window_id', 'ACTIVE_WIN'), ('focus', 'x-terminal-emulator.X-terminal-emulator'), ('key', 'ctrl+shift+v'), ('key', 'KP_Enter'), ] if '\n' in text: # Press enter twice for multiline texts doscript += [ ('key', 'KP_Enter'), ] if return_to_win == "1": doscript += [ ('focus_id', '$ACTIVE_WIN'), ] # execute script # verbose = 1 utool.util_ubuntu.XCtrl.do(*doscript, sleeptime=sleeptime, verbose=verbose)
def send_public_key_to_server(username, server): """ Can just use this instead ssh-copy-id id@server ssh-copy-id [email protected] ssh-copy-id [email protected] ssh-copy-id [email protected] ut.copy_text_to_clipboard(remote_cmdstr) chmod 700 ~git/.ssh chmod 600 ~git/.ssh/authorized_keys """ public_key = ut.read_from(ut.truepath('~/.ssh/id_rsa.pub')) fmtstr = 'ssh {user}@{server} "{remote_cmdstr}"' remote_cmdstr = 'echo {public_key} >> ~{username}/.ssh/authorized_keys'.format(public_key=public_key.replace( '\\', '\\\\'), username=username) sshcmdstr = fmtstr.format(server=server, remote_cmdstr=remote_cmdstr) ut.copy_text_to_clipboard(sshcmdstr) print('You need to run the command in your clipboard')
def send_public_key_to_server(username, server): """ Can just use this instead ssh-copy-id id@server ssh-copy-id [email protected] ssh-copy-id [email protected] ssh-copy-id [email protected] ut.copy_text_to_clipboard(remote_cmdstr) chmod 700 ~git/.ssh chmod 600 ~git/.ssh/authorized_keys """ public_key = ut.read_from(ut.truepath('~/.ssh/id_rsa.pub')) fmtstr = 'ssh {user}@{server} "{remote_cmdstr}"' remote_cmdstr = 'echo {public_key} >> ~{username}/.ssh/authorized_keys'.format( public_key=public_key.replace('\\', '\\\\'), username=username) sshcmdstr = fmtstr.format(server=server, remote_cmdstr=remote_cmdstr) ut.copy_text_to_clipboard(sshcmdstr) print('You need to run the command in your clipboard')