鱼C论坛

 找回密码
 立即注册
查看: 3973|回复: 10

python=35讲,15.1 EgStore,实践中报错,咋整??

[复制链接]
发表于 2014-9-6 20:55:44 | 显示全部楼层 |阅读模式
20鱼币
本帖最后由 欣欣celin 于 2014-9-8 15:00 编辑
  1. #^^^^^^^^^^^^
  2. #creat 'settings', a persistent settings object
  3. #note that the 'filename' argument is required
  4. #the directory for the persistet file must already exist
  5. #^^^^^^^^^^^^
  6. import os
  7. import easygui as g
  8. settingsFilename = os.path.join('E:','FishCApp','settings.txt')    #windows example
  9. settings = settings(settingsFilename)

  10. #we initialize the 'user' and 'server' variables
  11. #in a real application, we'd probaly have the user enter them via enterbox
  12. user = 'Obama'
  13. server = 'WhiteHouse'
  14. #we save the variables as attriutes of the 'settings' object
  15. settings.userId = user
  16. settings.targetServer = server
  17. settings.store() #persist the settings
  18. #run code that gets a new value for userId
  19. #then persist the settings with the new value
  20. user = 'cc'
  21. settings.userId = user
  22. setting.srore()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-6 21:17:31 | 显示全部楼层
本帖最后由 wei_Y 于 2014-9-6 21:23 编辑

第九行,settings = settings(settingsFilename)没有定义过settings不能这样写啊。十六行,字符串没有userID这个对象。

十七行,也没有targetServer这个对象。
十八行,也没有.store()这个对象。


这些要用的话应该建立一个类
class settings:
      userID =
      targetServer =
      def store(self):
                pass
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-9-6 21:35:13 | 显示全部楼层
wei_Y 发表于 2014-9-6 21:17
第九行,settings = settings(settingsFilename)没有定义过settings不能这样写啊。十六行,字符串没有userI ...


#^^^^^^^^^^^^
#creat 'settings', a persistent settings object
#note that the 'filename' argument is required
#the directory for the persistet file must already exist
#^^^^^^^^^^^^
import os
import easygui as g

#we initialize the 'user' and 'server' variables
#in a real application, we'd probaly have the user enter them via enterbox
user = 'Obama'
server = 'WhiteHouse'

class settings:
    userId = user
    targetServer = server
    def store(self):
        pass

settingsFilename = os.path.join('E:','FishCApp','settings.txt') #windows example
settings = settings(settingsFilename)


#we save the variables as attriutes of the 'settings' object
settings.userId = user
settings.targetServer = server
settings.store() #persist the settings
#run code that gets a new value for userId
#then persist the settings with the new value
user = 'cc'
settings.userId = user
settings.srore()


还是报错:
TypeError: object() takes no parameters
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-6 22:06:04 | 显示全部楼层
欣欣celin 发表于 2014-9-6 21:35
#^^^^^^^^^^^^
#creat 'settings', a persistent settings object
#note that the 'filename' argume ...

- -。我那个就是个例子,函数里你别写pass啊。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-9-6 22:20:10 | 显示全部楼层
wei_Y 发表于 2014-9-6 22:06
- -。我那个就是个例子,函数里你别写pass啊。。。

#^^^^^^^^^^^^
#creat 'settings', a persistent settings object
#note that the 'filename' argument is required
#the directory for the persistet file must already exist
#^^^^^^^^^^^^
import os
import easygui as g

#we initialize the 'user' and 'server' variables
#in a real application, we'd probaly have the user enter them via enterbox
user = 'Obama'
server = 'WhiteHouse'

class settings:
    userId = user
    targetServer = server
    def store(settings):
        #pass

        settingsFilename = os.path.join('E:','FishCApp','settings.txt') #windows example
        settings = settings(settingsFilename)


        #we save the variables as attriutes of the 'settings' object
        settings.userId = user
        settings.targetServer = server
        settings.store() #persist the settings
        #run code that gets a new value for userId
        #then persist the settings with the new value
        user = 'cc'
        settings.userId = user
        settings.srore()

这样,终于可以运行了,但什么都没显示哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-6 22:24:27 | 显示全部楼层
欣欣celin 发表于 2014-9-6 22:20
#^^^^^^^^^^^^
#creat 'settings', a persistent settings object
#note that the 'filename' argument ...

你把下面的整个变成这个类里的方法了,并且没有调用。
调用还是会报错- -。
不过我比较菜,没看懂会运行成啥样。。
还是等版主来吧。。
不好意思没有帮到你。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-9-6 22:58:43 | 显示全部楼层
wei_Y 发表于 2014-9-6 22:24
你把下面的整个变成这个类里的方法了,并且没有调用。
调用还是会报错- -。
不过我比较菜,没看懂会运 ...

感谢大神的解答,虽然问题还在
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-11 16:11:44 | 显示全部楼层
两包烟的钱,把不了妹买不了田,不如拿来支持小甲鱼推出更多原创教学视频!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-27 21:50:43 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-28 08:48:49 | 显示全部楼层
学习下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-4-14 18:11:58 | 显示全部楼层
本帖最后由 新房客 于 2017-4-14 18:18 编辑
qq213 发表于 2016-11-25 20:04
#!/usr/bin/env python
#-*- coding:utf-8 -*-


还需要吧egstore.py里面的内容修改
def _restore(self):
               with open(self.filename, 'rb') as f:
            
                              store = pickle.load(f)
               for key, value in store.__dict__.items():
                              self.__dict__[key] = value



修改如下:
def _restore(self):
               with open(self.filename, 'rb') as f:
                            try:
                                       store = pickle.load(f)
                            except EOFError:
                                return None

                for key, value in store.__dict__.items():
                            self.__dict__[key] = value           
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-15 22:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表