473,466 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Editing File

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
8 2046

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

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
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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
8
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
0
by: Vanga Sasidhar | last post by:
Hi All, Please help me in the following problem. I am having some files with AVI extension. I want to make two programs in Visual Basic .NET which will work with these AVI Files. One program...
12
by: Thomas Bartkus | last post by:
Does anyone use emacs together with both WordStar key bindings and python mode? I'm afraid that Wordstar editing key commands are burned R/O into my knuckles! I would like to play with emacs...
2
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris ...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
5
by: Zytan | last post by:
I cannot stand being unable to change the code while the debugger is running. Is there a way to do this? thanks, Zytan
10
by: PhilSorum | last post by:
Hi all. I've to edit a txt file, deleting a given word. My approach is to open the file and read it char by char, copying that to a second file. I use a buffer to identify the given word, so if...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.