By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Editing File

P: n/a
D
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..thanks!

Jul 12 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

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..thanks!
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

Jul 12 '06 #2

P: n/a

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..thanks!
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.

Jul 12 '06 #3

P: n/a
D
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..

ak*********@gmail.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..thanks!

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.
Jul 12 '06 #4

P: n/a

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 :
http://wiki.python.org/moin/UsingPickle
>
ak*********@gmail.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..thanks!
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.
Jul 12 '06 #5

P: n/a

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>
ak*********@gmail.com wrote:
<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_file, "r"))

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

yaml.dump(config, config_file_obj, default_flow_style=False)

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

feeds:
http://leo.am/podcasts/floss:
name: FLOSS Weekly
mode: dl
http://revision3.com/diggnation/feed/high.mp3.xml:
name: Revision3 - Diggnation w/Kevin Rose & Alex Albrecht
mode: dl
http://geekmuse.net/podcast/:
name: Geek Muse
mode: dl

http://www.itconversations.com/rss/c...hange2005&e=1:
name: Accelerating Change 2005
mode: dl

Nice and clean.

- Jeremy M. Jones

Jul 12 '06 #6

P: n/a
akameswaran <atgmail.com <akameswaran <atgmail.comwrites:
>

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

Jul 12 '06 #7

P: n/a
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.csv').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
Jul 12 '06 #8

P: n/a
D
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.csv').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
Jul 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.