gpt4 book ai didi

Python:在使用相同后端的同时联网的 IDLE/Redo IDLE 前端?

转载 作者:太空狗 更新时间:2023-10-30 00:19:54 25 4
gpt4 key购买 nike

是否有任何现有的网络应用程序可以让多个用户同时使用交互式 IDLE 类型 session ?

类似于:

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

如果没有,我想创建一个。有没有我可以使用的模块来模拟交互式 session ?我想要这样的界面:

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 创建一个新的前端。我该怎么做?

更新: 或者我可以通过 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()

这行得通吗,或者我偏离了方向/这种方法不可行/我在重复工作,而我应该使用已经构建的东西?

更新 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 工具来执行此操作?

最佳答案

我可以在 Nevow 中快速实现类似的东西.显然,访问需要受到严格限制,因为这样做涉及允许某人通过 HTTP 访问 Python 控制台。

我要做的是为控制台创建一个 Athena 小部件,它使用了 code.InteractiveInterpreter 的自定义子类的实例。这对所有登录用户都是通用的。

更新:好的,您在 GAE 中有了类似聊天的东西。如果您只是将代码行提交给看起来像这样的 code.InteractiveInterpreter 子类,它应该适合您。请注意,该界面与您描述的 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

关于Python:在使用相同后端的同时联网的 IDLE/Redo IDLE 前端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2893401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com