如何使数据输入到StringIO的顺序不相关?Python

2024-09-30 03:22:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个非常简单的应用程序,它基于用户输入生成一个配置文件。但是,数据从StringIO转储到实际conf文件的顺序对使用该文件的程序很重要。我在代码中解决这个问题的方法是自顶向下的数据输入模型。但是,如果用户输入的数据顺序不对,这将导致程序失败或产生的conf文件将变得无用。有没有办法重新协调随机数据输入顺序并确保StringIO中的数据按特定顺序插入?你知道吗

目前的代码看起来是这样的(在你们的帮助下它发展到了这个阶段!)你知道吗

        self.output = StringIO.StringIO()     

    context = self.toolbar.get_style_context()
    context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)



def on_servername_activate(self, widget):
    output = StringIO.StringIO()         
    servername = widget.get_text()
    self.output.write("USHARE_NAME="+servername+'\n')

def on_netif_changed(self, widget):
    netif = widget.get_active_text()
    self.output.write("USHARE_IFACE="+netif+'\n')

def on_port_activate(self, widget):
    port = widget.get_text()
    self.output.write("USHARE_PORT="+port+'\n')

def on_telprt_activate(self, widget):
    telprt = widget.get_text()
    self.output.write("USHARE_TELNET_PORT="+telprt+'\n')

def on_dirs_activate(self, widget):
    dirs = widget.get_text()
    self.output.write("USHARE_DIR="+dirs+'\n')

def on_iconv_toggled(self, widget):
    iconv = widget.get_active()
    if iconv == True:
        self.output.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
    else:
        self.output.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')

def on_webif_toggled(self, widget):
    webif = widget.get_active()
    if webif == True:
       self.output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
    else:
       self.output.write("USHARE_ENABLE_WEB="+"no"+'\n')

def on_telif_toggled(self, widget):
    telif = widget.get_active()
    if telif == True:
        self.output.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
    else:
        self.output.write("USHARE_ENABLE_TELNET="+"no"+'\n')

def on_xbox_toggled(self, widget):
    xbox = widget.get_active()
    if xbox == True:
        self.output.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
    else:
        self.output.write("USHARE_ENABLE_XBOX="+"no"+'\n')

def on_dlna_toggled(self, widget):
    dlna = widget.get_active()
    if dlna == True:
        self.output.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
    else:
        self.output.write("USHARE_ENABLE_DLNA="+"no"+'\n')

def on_commit_clicked(self, widget):
    commit = self.output.getvalue()
    logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
    logfile.write(commit)

def on_endprogram_clicked(self, widget):
    sys.exit(0)

Tags: 数据textselftrueoutputgetonenable
3条回答

您需要将数据收集与输出生成分开。想象一个独立的ConfigBuilder类,比如说,telnet_portushare_ifaceushare_dir属性和一个build()方法,它只返回一个生成的字符串blob。然后您的方法将只设置builder的字段:

def on_servername_activate(self, widget):
    servername = widget.get_text()
    self.builder.ushare_name = servername

当用户单击提交按钮时,生成配置并将其写入文件:

def on_commit_clicked(self, widget):
    logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
    logfile.write(self.builder.build())

如果您不想有一个单独的生成器类,您可以在字典中甚至窗口类的成员字段中收集输入数据:

class MyApp(...):

    ushare_name = None
    ... more fields to store user input

    def can_build(self):
        if self.ushare_name and
           self.ushare_xxx and
           self.ushare_yyy and
           self.phase_of_moon_is_right():
            return True
        return False

    def build_config(self):
        return "BLAH =" + self.ushare_name + ...

    def on_servername_activate(self, widget):
        servername = widget.get_text()
        self.ushare_name = servername

    def on_commit_clicked(self, widget):
        if self.can_build():
            logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
            logfile.write(self.build_config())
        else:
            display_some_warning_message("Data incomplete, the commit button should've been hidden/disabled so the user can't click until the app has all data it needs")

修饰每个需要它的函数(一个简单的方法,它接受widget并返回一个合适的-或者可能是一个类修饰器,blitz的'on.*'方法)-你有很多重复,并让修饰器附加到一个列表或其他东西。你知道吗

def on_webif_toggled(self, widget):
    webif = widget.get_active()
    if webif == True:
       self.output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
    else:
       self.output.write("USHARE_ENABLE_WEB="+"no"+'\n')

我应该注意到== True应该正确地写为is True或者仅仅是if webif:。身份检查比相等检查更有效。你知道吗

重写代码,这样就不用在字段更改时编写配置文件字符串,而是更改内存中dictionary的值。然后,让您的on\u commit\u clicked函数使用该字典完全按照需要构建配置文件字符串。你知道吗

相关问题 更多 >

    热门问题