473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ 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..thank s!

Jul 12 '06 #1
8 2058

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

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

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*********@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.
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*********@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.
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*********@gma il.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_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:
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.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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1679
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" standalone="yes"?> <Files> <File> <Text>Test</Text> <Name>Test.html</Name> </File>
8
11347
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" standalone="yes"?> <Files> <File> <Text>Test</Text> <Name>Test.html</Name> </File>
12
3073
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 asp editing (like I used to be able to do with 2K/IIS5) where I use VS.NET, open an asp file, make changes, save and refresh my browser. Problem...
0
312
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 should work as encrypter and another program should work as decrypter. The frist program, encrypter should lock (the user should not be able to...
12
2809
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 for Python editing but I'm having (2) problems. 1) When I load a .py file, emacs automatically overrides my wordstar-mode with python-mode, forcing...
2
2069
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 For e.g. I have a file test.txt created and I have the path to this file in a
0
1998
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 of different screens down to a minimum, I'm trying to use the same Windows Forms for both browsing and for updating. This works fine for TextBoxes,...
5
2481
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
2828
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 I recognize the pattern I don't pass it to the new file. Finally I copy the new file to the former. Is there a better way to carry out my task,...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4966
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.