A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/112593.html below:

Using cPickle, pickle or marshal to preserve attribute values of a class instance

Using cPickle, pickle or marshal to preserve attribute values of a class instanceEdwardT edwardt at trillium.com
Sat Apr 21 13:03:44 EDT 2001
I have got one more question, the script I have involves TkInter, wil this
work ...??? Here is part of the code .....
So if I ge this correctly, then when the user first invoke the program it
should depersist and load the object since there is no object file to read
from, afterwards the program should read fromthe object files and retain all
the previous state of the last time the progam is run. I have made some
insertn of the code but not sure if they are sound and robust enough. My new
code for the cPickle are preceeded with # cPickle insertion comment lines.
They are in the import section and the main() function down in the file ...

IS this kindof application usual using cPickle? Can I do it using marshal,
as I just discovered the sys admin  does not install cPickle or pickle only
marshal .... :(  .... Thanks

from Tkinter import *
from tkFileDialog import *
from Dialog import Dialog
import re
import string
import time
import os
import stat
import math
# cPickle insertion
import cPickle

def makeMenu(gui)
    'blar blar blar'

Class Gui:
  def __init__(self,master,img):
        self.master = master;
        # define each frame
        #   main frame is at the very top
        #   button frame is the command button frame
        #   list frame is used for all of the list boxes
        #   quit frame is used for the quit button
        self.headerframe = Frame(self.master, width = 80)
        self.headerframe.grid(row=0, column=0, sticky='W')
        self.menuframe = Frame(self.headerframe, relief=RAISED,
            borderwidth=2, width = 80)
        self.menuframe.grid(row=0, column=0, sticky='W')
        makeMenu(self)
        self.logoframe = Frame(self.headerframe, width = 80)
        self.logoframe.grid(row=0, column=0, sticky='E')
        # column affects locaton of logo
        self.buttonframe = Frame(self.master, width = 80)
        self.buttonframe.grid(row=1, column=0, sticky='W')
        self.octetframe = Frame(self.master, width = 80)
        self.octetframe.grid(row=2, column=0, sticky='W')

        self.octetframe2 = Frame(self.master, width = 5)
        self.octetframe2.grid(row=80, column=0, sticky='W')

        self.listFrame = Frame(self.master, width = 80)
        self.groupsList = Frame(self.listFrame, width = 50)
        self.groupsList.grid(row=0, column=0, sticky=N)
        self.inputName = StringVar()
        self.outputName = StringVar()
        self.inputName.set("")
        self.outputName.set("")
        self.inputFileList = []
        self.outputFileList = []
        self.stackname = StringVar()
        self.stackname.set("")
        self.name = StringVar()
        self.iut = StringVar()
        self.name.set("")
        self.iut.set("")
        # define all buttons
        self.type = IntVar()
        self.counter = IntVar()

        self.ie = Checkbutton(self.buttonframe,
                text="Build ALL ",
                variable=self.type, onvalue = 1, offvalue =0)
        self.ie.pack()
        self.ie.grid(row=0, column=1, pady=10, sticky=W)

        self.build = Button(self.buttonframe,
                text="Build", command=self.buildMSC)
        self.build.grid(row=0, column=3)

        self.clear = Button(self.buttonframe,
                text="Clear", command=self.clear)
        self.clear.grid(row=0, column=4)

        # define all labels
        Label(self.logoframe, image=img).grid(row=0,column=4,sticky=E)

        # define all listboxes
        self.pdudfn = Text(self.octetframe,
                font='*-Courier-Bold-R-Normal-*-120-*',
                height=20,
                width=80)
        self.pdudfn.grid(row=0, column=0, sticky=W)
        self.pdudfn.tag_config('HIGHLIGHT', background="White")

        self.pdubuild = Text(self.octetframe2,
                font='*-Courier-Bold-R-Normal-*-120-*',
                height=5,
                width=80)
        self.pdubuild.grid(row=0, column=0, sticky=W)
        self.pdubuild.tag_config('HIGHLIGHT', background="White")

        # define all scrollbars
        self.pdudfn.scrollY = Scrollbar(self.octetframe,
                orient=VERTICAL)
        self.pdudfn['yscrollcommand'] = self.pdudfn.scrollY.set
        self.pdudfn.scrollY['command'] = self.pdudfn.yview
        self.pdudfn.scrollY.grid(row=0, column=1,sticky='NS')

        self.pdubuild.scrollY = Scrollbar(self.octetframe2,
                orient=VERTICAL)
        self.pdubuild['yscrollcommand'] = self.pdubuild.scrollY.set
        self.pdubuild.scrollY['command'] = self.pdubuild.yview
        self.pdubuild.scrollY.grid(row=0, column=1,sticky='NS')
   # alll the internal class functions follows ....

# Main function, run as a standalone program
def main():
    sep = os.sep

    root = Tk()
    tharnbase = os.environ['THARN']

    img =
PhotoImage(file=tharnbase+sep+'modules'+sep+'gui'+sep+'trillium.gif')

    # cPickle insertion
    try:
       gui = cPickle.load(open("msc.dat","rb"))
    except:
       gui = Gui(root,img)

    root.protocol('WM_DELETE_WINDOW', root.quit)
    root.title("TRILLIUM MSC Builder")
    root.iconname("MSCBld")
    root.mainloop()
    # cPickle insertion
    cPickle.dump(gui, open("msc.dat","wb"))

if __name__ == '__main__':


    main()

--- In python-list at y..., "Alex Martelli" <aleaxit at y...> wrote:
> "EdwardT" <edwardt at t...> wrote in message
> news:9bpra9$q0l at n...
> > Hello, I current have a script that do certain processing on files, and
> > requires user to enter certain info such as:
> > 1. inputfile name
> > 2. output file name
> > 3. whether it would like process all files recusively or just one
(stored
> as
> > a Int instance in class, value got from CheckButton widget in runtime )
> > 4. a string that supplies the protocol stack it is building. (stored as
a
> > string instance in class)
>
> By "in class" I guess you mean, as an attribute in an instance
> of some class?
>
> >
> > Instead of writing some code to save all these some sort of  text file;
> and
> > re-read the next time ithe script got invoked.  Does python offer the
> option
> > to save the state of the whole script, as some kind of persistence
> objects?
>
> "The state of the whole script" would be more of a problem -- isn't it
> enough to save the state of an instance, as you ask before and in the
> subject (assuming that's what you mean by 'state of a class')?
>
> Standard module pickle does that (and cPickle does it, too, but
> faster).  Here's a simple example -- assume the following is
> scriptfile flup.py:
>
> import sys, cPickle
>
> class Words:
>     def __init__(self, *words):
>         self.words = words
>
> if len(sys.argv)>1:
>     # build and persist an object
>     obj = Words(*sys.argv[1:])
>     cPickle.dump(obj, open("flup.dat","wb"))
> else:
>     # depersist and display an object
>     obj = cPickle.load(open("flup.dat","rb"))
>     for word in obj.words:
>         print word,
>     print
>
> An example usage might be:
>
> D:\ian>python flup.py be bop blippo
>
> D:\ian>python flup.py
> be bop blippo
>
> D:\ian>
>
>
> Lots more info, and examples of more advanced usage, are in
> the standard Python library reference manual.
>
>
> Alex
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
"



More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4