[cheatsheet] ipython

boychaboy·2023년 7월 7일
0

personal cheatsheet

목록 보기
6/7

배경

  • 업무를 하다보면 빠르게 결과를 찍어보면서 사용하기에 IPython이 굉장히 편리할 때가 있다.
  • 특히 데이터 확인, 전처리, 후처리로 한번만 쓰고 마는 일회용(?) 코딩을 할 때 유용하게 사용한다.
  • 기본 IPython 패키지에 생각보다 유용한 옵션들이 많은데, 여기에 한번 정리해보았다.
  • ipython --help 로 모든 옵션을 볼 수 있다.

참고 문서

내용

import from other directories

다른 directory에 있는 모듈에서 함수, 클래스 등을 import 하려는 경우

>>> import sys
>>> module_path = "해당 디렉토리의 절대 경로"
>>> sys.path.append(module_path)

Logging

처음에 실행할 때부터 특정 파일에 저장하고싶을 경우

ipython --logfile {filename}

  • {filename}에 명령어 로그를 저장한다. (파이썬 스크립트로 저장된다)
  • 입력했던 interactive 명령어들을 모아서 나중에 모듈(.py) 형태로 옮기거나 할 경우에 쉽게 복붙할 수 있다.
  • Options
    • %logstop > %logstart -{o|r|t|q}
      • -o: output logging
      • -r: raw input log
      • -t: timestamping
      • -q: (뭔지 아직 잘 모르겠다)

ipython --logappend {filename}

  • {filename}에 명령어 로그를 추가한다.

History

ipython으로 신나게 코딩하다가 갑자기 export 하고싶어질 경우
*%{magic}: 쉘 안에서 실행하는 'built-in magic commands'

%history [-l] [-f] [-o]

  • 이전 명령어 히스토리를 저장

  • -l: 최근 n개의 line을 저장

  • -f: 저장할 파일 이름

  • -o: ouput도 함께 저장

  • 또는 직접 저장할 line을 아래처럼 명명

    Line 4, current session
    
    4-6
    Lines 4-6, current session
    
    243/1-5
    Lines 1-5, session 243
    
    ~2/7
    Line 7, session 2 before current
    
    ~8/1-~6/5
    From the first line of 8 sessions ago, to the fifth line of 6 sessions ago.
    
    Multiple ranges can be entered, separated by spaces
profile
no vim no code

0개의 댓글