Čeština
English
  • Vítejte na stránkách NLP Centra!
  • Zapojte se do vývoje softwarových nástrojů!
  • Analýza přirozeného jazyka
  • Vyzkoušejte si korpusy o velikosti knihoven online!
  • Studujte jednu ze specializací!
  • Členové laboratoře

How to Code in a Terminal Window

When you want to develop programs (e.g. in Python) on a server, the easiest connection option usually is via ssh access and the ​command line mode in a ​terminal window.

In case, you are not used to working in a terminal, we have gathered a small set of recommendations and tutorial links. The examples will be using the NLP lab server ​aurora.

  • for the primary access, see the information for various OSes directly at ​https://aurora.fi.muni.cz
  • for basic navigation once you are logged in, see the ​Ubuntu tutorial for working in terminal.
  • for easy repeated access, you should setup a SSH key authorization, see the simple ​aurora SSH keys tutorial.
  • choose and learn (at least) one in-terminal text editor. An easy option is the ​nano editor - just run nano source.py and start editing, save with Ctrl+S, exit with Ctrl+X. An advanced option is the ​vim editor - we suggest e.g. the ​MIT course VIM intro for an initial engagement with vim. When you start vim source.py, you should press i to edit the text and Esc, :w<Enter> to save changes or :q!<Enter> to discard changes.
  • when developing a program, we suggest to work in multiple terminal windows. For each window, start a separate terminal connection to the server (SSH key authorization mentioned above comes very handy here), change to the same directory on the server and use for example:
    • one window for source code editing. Do not exit the editor, just save changes and test in another window.
    • one window for running the code and inspecting the results.
    • one window for running a debug (e.g. via python -mpbd source.py).
    • one window for working with auxiliary files, e.g. edit testing data.
    You may switch windows with Alt+Tab or Ctrl+Tab quickly.
  • for easy data+code sharing consider using an independent Git repository, e.g. ​https://gitlab.fi.muni.cz. In that case, you shall store all your source codes and data files in ​Gitlab and just git clone the repository to the server. Use git add, git commit -a and git push to promote changes that you make on the server back to ​Gitlab. Use git pull to update the previously cloned repository in the server directory with changes made elsewhere. See e.g. the ​MIT Git intro for other details about working with Git.
  • how to discover and fix errors - we suggest two standard options: logging and pdb debugging:
    • logging is based on standard Python ​logging library. It allows to supplement your program with messages of various levels of verbosity which can be turned on and off. A simple program may look like
      import logging
      
      logging.basicConfig( level=logging.DEBUG, format="%(message)s")
      
      logging.info("Program starting")
      a = 2
      b = 2
      logging.debug(f"About to sum {a} and {b}")
      logging.info(f"The result is {a+b}")
      
    • pdb debugging is run from the command line with including the specific pdb module. Start the debugging via python -mpdb source.py, then use line-based commands as described in the ​MIT code page. Command q quits the debugger.
  • for handling external data in your Python code, we suggest to use the wonderful ​argparse library. It allows to specify the expected command line arguments and their types as switches, strings, file inputs, etc. For example, the following code snippet expects a file argument --input with stdin as a default value:
    import sys
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", type=argparse.FileType("r"), default=sys.stdin)
    args = parser.parse_args()
    for line in args.input:
        print(line.strip())
    
    You may run the above code with python source.py --input data.txt to read from a file or with echo My data | python source.py to read from the standard input.
  • if you are curious enough to get to a terminal pro level, we suggest to go through the full MIT course ​The Missing Semester of Your CS Education.