Editing File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D

    #1

    Editing File

    Hi, I currently have a Python app with a Tkinter GUI frontend that I
    use for system administration. Everytime it launches, it reads a text
    file which contains info about each host I wish to monitor - each field
    (such as IP, hostname, etc.) is delimited by !!. Now, I want to be
    able to edit host information from within the GUI - what would be the
    best way to go about this? Basically I just need to either edit the
    original host line, or write a new host line and delete the
    original..thank s!

  • Jeremy Jones

    #2
    Re: Editing File


    D wrote:
    Hi, I currently have a Python app with a Tkinter GUI frontend that I
    use for system administration. Everytime it launches, it reads a text
    file which contains info about each host I wish to monitor - each field
    (such as IP, hostname, etc.) is delimited by !!. Now, I want to be
    able to edit host information from within the GUI - what would be the
    best way to go about this? Basically I just need to either edit the
    original host line, or write a new host line and delete the
    original..thank s!
    I would create a data structure of the contents of the file and let the
    application reference that data structure. Sounds like it's going to
    be a list of lists or a list of dicts. Each line of the file is going
    to be an element of the "main" list. Each element of the list is going
    to be a dict or a list of the details of that particular host. Make it
    so that if your app changes the datastructure, you re-serialize it back
    to the file. This should work the same with adding a new host to
    monitor.

    It might be easier to use something like Yaml. I'm doing something
    similar with a little podcast grabber I'm working on. Here's some old
    code where I first incorporate using Yaml (down at the bottom of the
    page): http://jeremymjones.com/articles/sim...rabber-python/
    The version I have in SVN right now creates a configgish object off of
    the Yaml and on re-assignment of either of the two main attributes, it
    automatically reserializes it.

    Anyway, hope this helps.

    - Jeremy M. Jones

    Comment

    • akameswaran@gmail.com

      #3
      Re: Editing File


      D wrote:
      Hi, I currently have a Python app with a Tkinter GUI frontend that I
      use for system administration. Everytime it launches, it reads a text
      file which contains info about each host I wish to monitor - each field
      (such as IP, hostname, etc.) is delimited by !!. Now, I want to be
      able to edit host information from within the GUI - what would be the
      best way to go about this? Basically I just need to either edit the
      original host line, or write a new host line and delete the
      original..thank s!
      Might be overkill - but pickle the data memeber that contains the
      information. If you use text instead of binary pickling it should
      still be editable by hand. for a single line of text it may be a bit
      much - but it's still probably quicker than opening a file, parsing
      etc.

      Comment

      • D

        #4
        Re: Editing File

        Thanks, guys. So overall, would it just be easier (and not too rigged)
        if any changes were made by just editing the text file? I want to do
        this project the right way, but if it's going to be a big pain to
        implement the edit function, just modifying the text file directly
        isn't that big of a deal..

        akameswaran@gma il.com wrote:
        D wrote:
        Hi, I currently have a Python app with a Tkinter GUI frontend that I
        use for system administration. Everytime it launches, it reads a text
        file which contains info about each host I wish to monitor - each field
        (such as IP, hostname, etc.) is delimited by !!. Now, I want to be
        able to edit host information from within the GUI - what would be the
        best way to go about this? Basically I just need to either edit the
        original host line, or write a new host line and delete the
        original..thank s!
        >
        Might be overkill - but pickle the data memeber that contains the
        information. If you use text instead of binary pickling it should
        still be editable by hand. for a single line of text it may be a bit
        much - but it's still probably quicker than opening a file, parsing
        etc.

        Comment

        • akameswaran@gmail.com

          #5
          Re: Editing File


          D wrote:
          Thanks, guys. So overall, would it just be easier (and not too rigged)
          if any changes were made by just editing the text file? I want to do
          this project the right way, but if it's going to be a big pain to
          implement the edit function, just modifying the text file directly
          isn't that big of a deal..
          have you used pickle? if the data is as simple as you say it is, you
          will be able to read the pickle file. 2nd, it will be a lot less code
          really. You just load and unpickle the file into a variable. After
          any edit in the gui, repickle it to the same file. You would have to
          do the same if you edited the text file, except you would need a couple
          of lines code to parse the string, etc.

          check here for a simple example :

          >
          akameswaran@gma il.com wrote:
          D wrote:
          Hi, I currently have a Python app with a Tkinter GUI frontend that I
          use for system administration. Everytime it launches, it reads a text
          file which contains info about each host I wish to monitor - each field
          (such as IP, hostname, etc.) is delimited by !!. Now, I want to be
          able to edit host information from within the GUI - what would be the
          best way to go about this? Basically I just need to either edit the
          original host line, or write a new host line and delete the
          original..thank s!
          Might be overkill - but pickle the data memeber that contains the
          information. If you use text instead of binary pickling it should
          still be editable by hand. for a single line of text it may be a bit
          much - but it's still probably quicker than opening a file, parsing
          etc.

          Comment

          • Jeremy Jones

            #6
            Re: Editing File


            D wrote:
            Thanks, guys. So overall, would it just be easier (and not too rigged)
            if any changes were made by just editing the text file? I want to do
            <snip>
            <snip>
            Might be overkill - but pickle the data memeber that contains the
            information. If you use text instead of binary pickling it should
            still be editable by hand. for a single line of text it may be a bit
            much - but it's still probably quicker than opening a file, parsing
            etc.
            Look at pickle, but I'd recommend against it if you're anticipating
            needing to edit the file by hand. It's just a little on the ugly side.
            Glance at Yaml (I think it's the pyyaml project in the cheeseshop) as
            well. Here's the code needed to "parse" in a .yaml file:

            config = yaml.load(open( self.config_fil e, "r"))

            Here's the code needed to serialize it back in a pretty format:

            yaml.dump(confi g, config_file_obj , default_flow_st yle=False)

            And here's a piece of a .yaml file itself:

            feeds:

            name: FLOSS Weekly
            mode: dl

            name: Revision3 - Diggnation w/Kevin Rose & Alex Albrecht
            mode: dl

            name: Geek Muse
            mode: dl


            name: Accelerating Change 2005
            mode: dl

            Nice and clean.

            - Jeremy M. Jones

            Comment

            • Tal Einat

              #7
              Re: Editing File

              akameswaran <atgmail.com <akameswaran <atgmail.comwri tes:
              >
              >
              D wrote:
              Thanks, guys. So overall, would it just be easier (and not too rigged)
              if any changes were made by just editing the text file?
              [snip]
              have you used pickle? if the data is as simple as you say it is, you
              will be able to read the pickle file. 2nd, it will be a lot less code
              really. You just load and unpickle the file into a variable. After
              any edit in the gui, repickle it to the same file. You would have to
              do the same if you edited the text file, except you would need a couple
              of lines code to parse the string, etc.
              >
              If you don't plan to edit your data by hand, Pickle is a nice and simple choice.
              To save you from coding the persistency bits, you can use Python's built-in
              shelve module.

              Shelve uses Pickle to serialize Python objects but it does most of the
              persistency stuff for you. It is very simple and clean to use. One nice feature
              is that can open a shelve in auto-writeback mode, so upon assignment to one of
              the shelve's keys (it's like a dict) the shelve is automatically re-serialized
              to the file on disk. This saves headaches like remembering to serialize the data
              upon exit cleanup, crashes resulting in data loss, etc.

              Shelve is the simplest tool I know of. YAML sounds nice and readable, I haven't
              tryed it out yet. You could use XML easily enough with ElementTree.

              Good luck!

              - Tal



              Comment

              • Maric Michaud

                #8
                Re: Editing File

                Le mercredi 12 juillet 2006 17:00, D a écrit :
                Thanks, guys. So overall, would it just be easier (and not too rigged)
                if any changes were made by just editing the text file? I want to do
                this project the right way, but if it's going to be a big pain to
                implement the edit function, just modifying the text file directly
                isn't that big of a deal..
                If you don't want to rely on third party libraries, and want a human-readable
                format I'll suggest using csv file format :

                and this way ?

                In [47]: class Z(object) :
                ....: def __init__(self, val) : self.val = val
                ....:
                ....:

                In [48]: lst = [Z(i) for i in range(2) + [ str(e) for e in range(2) ] ]

                In [49]: [ (e, e.val) for e in lst ]
                Out[49]:
                [(<__main__.Z object at 0xa76c252c>, 0),
                (<__main__.Z object at 0xa76c27ac>, 1),
                (<__main__.Z object at 0xa76c23ac>, '0'),
                (<__main__.Z object at 0xa76c23ec>, '1')]

                In [51]: csv.writer(file ('config.csv', 'w')).writerows ([
                (type(i.val).__ name__, i.val) for i in lst])

                In [52]: print file('config.cs v').read()
                int,0
                int,1
                str,0
                str,1


                In [53]: l = [ Z(eval(class_)( val)) for class_, val in
                csv.reader(file ('config.csv')) ]

                In [54]: [ (e, e.val) for e in l ]
                Out[54]:
                [(<__main__.Z object at 0xa76c218c>, 0),
                (<__main__.Z object at 0xa76c260c>, 1),
                (<__main__.Z object at 0xa76c256c>, '0'),
                (<__main__.Z object at 0xa76c25cc>, '1')]




                --
                _____________

                Maric Michaud
                _____________

                Aristote - www.aristote.info
                3 place des tapis
                69004 Lyon
                Tel: +33 426 880 097

                Comment

                • D

                  #9
                  Re: Editing File

                  Thanks to all for the suggestions - I am going to give them a try this
                  afternoon. I am still fairly new to Python, so this will definitely be
                  a good learning experience. :)

                  Maric Michaud wrote:
                  Le mercredi 12 juillet 2006 17:00, D a écrit :
                  Thanks, guys. So overall, would it just be easier (and not too rigged)
                  if any changes were made by just editing the text file? I want to do
                  this project the right way, but if it's going to be a big pain to
                  implement the edit function, just modifying the text file directly
                  isn't that big of a deal..
                  >
                  If you don't want to rely on third party libraries, and want a human-readable
                  format I'll suggest using csv file format :
                  >
                  and this way ?
                  >
                  In [47]: class Z(object) :
                  ....: def __init__(self, val) : self.val = val
                  ....:
                  ....:
                  >
                  In [48]: lst = [Z(i) for i in range(2) + [ str(e) for e in range(2) ] ]
                  >
                  In [49]: [ (e, e.val) for e in lst ]
                  Out[49]:
                  [(<__main__.Z object at 0xa76c252c>, 0),
                  (<__main__.Z object at 0xa76c27ac>, 1),
                  (<__main__.Z object at 0xa76c23ac>, '0'),
                  (<__main__.Z object at 0xa76c23ec>, '1')]
                  >
                  In [51]: csv.writer(file ('config.csv', 'w')).writerows ([
                  (type(i.val).__ name__, i.val) for i in lst])
                  >
                  In [52]: print file('config.cs v').read()
                  int,0
                  int,1
                  str,0
                  str,1
                  >
                  >
                  In [53]: l = [ Z(eval(class_)( val)) for class_, val in
                  csv.reader(file ('config.csv')) ]
                  >
                  In [54]: [ (e, e.val) for e in l ]
                  Out[54]:
                  [(<__main__.Z object at 0xa76c218c>, 0),
                  (<__main__.Z object at 0xa76c260c>, 1),
                  (<__main__.Z object at 0xa76c256c>, '0'),
                  (<__main__.Z object at 0xa76c25cc>, '1')]
                  >
                  >
                  >
                  >
                  --
                  _____________
                  >
                  Maric Michaud
                  _____________
                  >
                  Aristote - www.aristote.info
                  3 place des tapis
                  69004 Lyon
                  Tel: +33 426 880 097

                  Comment

                  Working...