2010-05-23 60 views
6

是否有任何現有的Web應用程序可讓多個用戶同時使用交互式IDLE類型會話?Python:聯網的IDLE /重做IDLE前端,同時使用相同的後端?

喜歡的東西:

IDLE 2.6.4 
Morgan: >>> letters = list("abcdefg") 
Morgan: >>> # now, how would you iterate over letters? 
Jack: >>> for char in letters: 
    print "char %s" % char 


char a 
char b 
char c 
char d 
char e 
char f 
char g 
Morgan: >>> # nice nice 

如果沒有,我想創建一個。有一些模塊可以用來模擬交互式會話嗎?我想這樣的一個接口:

def class InteractiveSession(): 
    ''' An interactive Python session ''' 

    def putLine(line): 
     ''' Evaluates line ''' 
     pass 

    def outputLines(): 
     ''' A list of all lines that have been output by the session ''' 
     pass 

    def currentVars(): 
     ''' A dictionary of currently defined variables and their values ''' 
     pass 

(雖然這最後一個函數會更額外的功能。)

制訂我的問題的另一種方式:我想創建一個新的前結束IDLE。我怎樣才能做到這一點?

UPDATE:或者我可以通過eval()來模擬IDLE?

更新2:如果我做了這樣的事情:

  • 我已經有一個簡單的GAE的Python聊天應用程序設置,能夠讓用戶登錄,使聊天室和聊天彼此。

  • 而不是僅僅節約傳入郵件數據存儲,我可以做這樣的事情:


def putLine(line, user, chat_room): 
''' Evaluates line for the session used by chat_room ''' 

# get the interactive session for this chat room 
curr_vars = InteractiveSession.objects.where("chatRoom = %s" % chat_room).get() 

result = eval(prepared_line, curr_vars.state, {}) 

curr_vars.state = curr_globals 

curr_vars.lines.append((user, line)) 
if result: 
    curr_vars.lines.append(('SELF', result.__str__())) 

curr_vars.put() 

的InteractiveSession模型:

def class InteractiveSession(db.Model): 


# a dictionary mapping variables to values 
    # it looks like GAE doesn't actually have a dictionary field, so what would be best to use here? 
    state = db.DictionaryProperty() 

    # a transcript of the session 
    # 
    # a list of tuples of the form (user, line_entered) 
    # 
    # looks something like: 
    # 
    # [('Morgan', '# hello'), 
    # ('Jack', 'x = []'), 
    # ('Morgan', 'x.append(1)'), 
    # ('Jack', 'x'), 
    # ('SELF', '[1]')] 
    lines = db.ListProperty() 
莫非這部作品

,或者我離開/這種方法是不可行的/我正在複製工作,當我應該使用已經構建的東西?

UPDATE 3此外,假設我得到了一切工作,我想要語法突出顯示。理想情況下,我會使用一些API或服務來解析代碼並適當地設置它。

for c in "characters": 

將成爲:

<span class="keyword">for</span> <span class="var">c</span> <span class="keyword">in</span> <span class="string>"characters"</span><span class="punctuation">:</span> 

是否有一個良好的Python現有的工具來做到這一點?

+1

非常有趣的想法,但如果這樣的事情已經存在,我會感到驚訝。 – 2010-05-23 20:52:58

回答

-1

這可能是即將實施的使用0MQ後端的IPython的實現。

+0

你能詳細說一下嗎?什麼是0MQ? – 2010-05-23 23:00:18

+0

0mq是一個快速消息隊列,ipython最近已經開始將它的前端與後端分開,這可能允許多用戶會話,儘管我沒有明確地聽說過這種特殊用途。你可以在他們的郵件列表上詢問這是一個設計目標還是不是:http://mail.scipy.org/mailman/listinfo/ipython-dev – Autoplectic 2010-05-24 15:59:57

0

作爲概念證明,您可以使用套接字和命令行會話將某些東西放在一起。

+0

這個命令行會話是什麼樣的? – 2010-05-24 02:58:37

+0

它看起來像一個常規的交互式會話,只有多人一次提供一行代碼。 – exupero 2010-05-24 09:52:41

1

我可以在Nevow中很快實現這樣的事情。顯然,訪問需要受到相當的限制,因爲做這種事情需要通過HTTP訪問某個Python控制檯。

我會做的就是創建一個雅典娜部件爲控制檯,所使用的code.InteractiveInterpreter定製子類的實例,它是適用於所有用戶登錄

UPDATE:好了,你有在GAE中聊天。如果您只是將代碼行提交給看起來像這樣的代碼.teractiveInterpreter子類,它應該適合您。需要注意的是界面非常相似,你描述InteractiveSession類:

class SharedConsole(code.InteractiveInterpreter): 
    def __init__(self): 
     self.users = [] 

    def write(self, data): 
     # broadcast output to connected clients here 
     for user in self.users: 
      user.addOutput(data) 

class ConnectedUser(object): 
    def __init__(self, sharedConsole): 
     self.sharedConsole = sharedConsole 
     sharedConsole.users.append(self) # reference look, should use weak refs 

    def addOutput(self, data): 
     pass # do GAE magic to send data to connected client 

    # this is a hook for submitted code lines; call it from GAE when a user submits code 
    def gotCommand(self, command): 
     needsMore = self.sharedConsole.runsource(command) 
     if needsMore: 
      pass # tell the client to change the command line to a textarea 
       # or otherwise add more lines of code to complete the statement 
1

最近的Python解釋器,我知道的是什麼,你正在尋找的,在接口方面,爲DreamPie。它具有獨立的輸入和輸出區域,非常像聊天界面。另外,DreamPie在子進程中運行所有代碼。 DreamPie也完成了完成和語法着色,就像IDLE一樣,這意味着它不僅可以輸入和輸出子進程 - 它已經實現了你正在尋找的抽象。

如果您希望開發一個桌面應用程序(而不是Web應用程序),我建議您將工作放在DreamPie上,並添加多前端功能。

更新:對於語法高亮顯示(包括HTML),請參閱Pygments項目。但這是完全不同的問題;請在這裏一次提出一個問題。

+0

DreamPie看起來不錯...如果我可以使用它作爲前端,並把它放在網上它可以很好地工作。 – 2010-05-29 17:00:48

-1

我會使用ipythonscreen。使用這種方法,您將不得不創建共享登錄,但您都可以連接到共享屏幕會話。一個缺點是你會同時出現在同一個用戶中。