태그 보관물: org-mode

org-mode

조직 모드 바벨-파이썬에서 대화 형 코드 블록 평가 이 질문

이 질문 과 마찬가지로 “org”모드에서 “입력”명령어를 포함하는 Python 소스 코드 블록을 평가하고 싶지만 평가하는 동안 대화 형 평가 (사용자 입력 포함)를 수행하거나 알려진 입력을 제공하는 방법을 찾을 수 없습니다 사전에 (예를 들어 파일에 저장).

이 제한 사항은 input학생들을위한 교과서에 포함되어야하기 때문에 수업을 명시 적으로 사용해야합니다.

코드 예 :

#+BEGIN_SRC python :results output
a= input("Value")
print(a)
#+END_SRC

그러한 대화식 평가를하거나 시뮬레이션 할 수 있습니까 (소스 코드에 가짜 입력을함으로써)?



답변

다음은 내 보내지 않고 얽힌 파일을 사용하여 입력 기능을 대체하는 대체 방법입니다.

#+BEGIN_SRC python :session :exports none :tangle example1.py
def input(x):
    if x == 'Value of a':
        return 3
    elif x == 'Value of b':
        return 4

#+END_SRC

#+RESULTS:

팁 : 명령을 누르 C-cC-vt거나 사용 하여 파일 M-xorg-babel-tangle을 작성하십시오 (예 : tangle ) example1.py.


#+BEGIN_SRC python :results output :preamble from example1 import *
a = input('Value of a')
b = input('Value of b')
print(a + b)
#+END_SRC

#+RESULTS:
: 7

주 :example1.py 이전 파이썬에서 작성된 파일 SRC블록은 내장을 이용하여 현재 블록에 가져올 프리앰블 : 헤더 값 from example1 import *.


답변

pythonorg-mode에서 리터럴 프로그래밍을 사용하여 코드 블록을 평가 하십시오.

:var헤더를 사용 하여 변수를 할당하고 코드를 테스트하십시오.

참고 : 원하는 경우 및를 사용 elisp (read-string "Prompt: ")하여 (read-number "Prompt: ")emacs 내부에 사용자 입력을 요구하십시오 .


예 1- 인쇄 (a)

  • 할당 hello worlda.


    #+name: ex1-code
    #+header: :var a="hello world"
    #+begin_src python :results verbatim replace output :exports results
      print(a)
    #+end_src
    
    #+begin_src python :eval never :exports code :noweb yes
      a = input('Value of a')
      <<ex1-code>>
    #+end_src
    
    #+results: ex1-code
    : hello world
    

실시 예 2- print(a + b)

  • 할당 1a.

  • 할당 2b.


    #+name: ex2-code
    #+header: :var a=1
    #+header: :var b=2
    #+begin_src python :results replace output  :exports results
      print(a + b)
    #+end_src
    
    #+begin_src python :eval never :exports code :noweb yes
      a = input('Value of a')
      b = input('Value of b')
      <<ex2-code>>
    #+end_src
    
    #+results: ex2-code
    : 3
    

실시 예 3- print(a,b,c)

  • Value of a입력 하라는 메시지가 표시되면Thanks
  • Value of b입력 하라는 프롬프트가 표시되면를 입력하십시오 4.
  • Value of c입력 하라는 프롬프트가 표시되면를 입력하십시오 your question.


    #+NAME: ex3-code
    #+header: :var a=(read-string "Value of a ")
    #+header: :var b=(read-number "Value of b ")
    #+header: :var c=(read-string "Value of c ")
    #+begin_src python :results replace output   :exports results
      print a,b,c
    #+end_src
    
    #+begin_src python :eval never :exports code :noweb yes
      a = input('Value of a')
      b = input('Value of b')
      c = input('Value of c')
      <<ex3-code>>
    #+end_src
    
    #+results: ex3-code
    : Thanks 4 your question
    

조직 파일을 내보낼 때 출력은 아래 예와 유사해야합니다.


실시 예 1- print(a)

  • 할당 hello worlda.

    a = input('Value of a')
    print(a)
    
    hello world
    

실시 예 2- print(a + b)

  • 할당 1a.
  • 할당 2b.

    a = input('Value of a')
    b = input('Value of b')
    print(a + b)
    
    3
    

실시 예 3- print(a,b,c)

  • Value of a입력 하라는 메시지가 표시되면Thanks
  • Value of b입력 하라는 프롬프트가 표시되면를 입력하십시오 4.
  • Value of c입력 하라는 프롬프트가 표시되면를 입력하십시오 your question.

    a = input('Value of a')
    b = input('Value of b')
    c = input('Value of c')
    print a,b,c
    
    Thanks 4 your question
    


이 코드는
GNU Emacs 24.5.1 (x86_64-unknown-cygwin, GTK + 버전 3.14.13)
Org-Mode 버전 : 8.3.2
및 github 에서 테스트되었습니다 .


답변

org-babel을 사용하여 대화 형 Python 입력을 얻는 것이 가능하지 않다고 생각합니다.

프리앰블을 사용하여 입력 함수를 재정 의하여 입력 사용을 시뮬레이션하려는 것을 반환 할 수 있습니다. 예를 들어 여기서는 “3”으로 입력 한 사용자처럼 보이게합니다.

#+BEGIN_SRC python :results output :preamble def input(x): return 3
a = input("value ")
print(a)
#+END_SRC

#+RESULTS:
: 3

학생들이 내 보낸 내용에 따라, 내 보낸 내용을 보지 못할 수도 있습니다.


답변

John Kitchin의 솔루션을 보완하기 위해 input(...)함수를 “공급”하고 str개체를 체계적으로 반환하는 연속적인 값을 제공하기 위해 생성기를 사용할 것을 제안합니다 .

#+BEGIN_SRC python :session :exports none :tangle example2.py :results none
def generate(lst):
    for element in lst:
        yield str(element)

generator =  generate(["Thanks",4,"your help"])

def input(x):
     return generator.__next__()
#+END_SRC


#+BEGIN_SRC python :results output :preamble from example2 import * :exports both
a = input('Value of a')
b = input('Value of b')
c = input('Value of c')
print(a,b,c)
#+END_SRC


답변