Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 18th, 2012, 01:05 PM
WandyLau WandyLau is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 29 WandyLau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
New to python and needs some help

Hi,guys I am new to python.Recently,I learn it by myself. I met a exercise as following can some body give a clue about how to accomplish it? Thank you in advance.

Question:
Allow the user to edit an existing text file. You
can do this any way you wish. let the user edit line by line . Give users
the option to apply the changes (saving the file) or discard them (leaving the original
file intact), and also ensure the original file is preserved in case the program exits
abnormally during operation.

Reply With Quote
  #2  
Old July 18th, 2012, 01:46 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383
This question is crazily open-ended.

Solution 1: Easiest to program, far and away the best:
Have the python program run emacs. emacs will take care of all the rest for you.

Solution 2: copy the filename.txt to filename.bak , load the text into a tkinter text box as a variable. Let tkinter handle the edits for you.

Solution 3: try to implement a subset of ed following the backups and all.

"anyway you want" conflicts with "line-by-line". What is the true assignment?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old July 18th, 2012, 11:19 PM
WandyLau WandyLau is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 29 WandyLau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
This question is crazily open-ended.

Solution 1: Easiest to program, far and away the best:
Have the python program run emacs. emacs will take care of all the rest for you.

Solution 2: copy the filename.txt to filename.bak , load the text into a tkinter text box as a variable. Let tkinter handle the edits for you.

Solution 3: try to implement a subset of ed following the backups and all.

"anyway you want" conflicts with "line-by-line". What is the true assignment?



Thank you very much.

Reply With Quote
  #4  
Old July 19th, 2012, 12:01 AM
anonciph3r anonciph3r is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 1 anonciph3r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 15 sec
Reputation Power: 0
Hmmm

Hmmm. sound is good, although i'm also a coder probably new to python coding so it's not big issue, anyway it is good.

Reply With Quote
  #5  
Old July 31st, 2012, 02:49 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 482 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 23 m 21 sec
Reputation Power: 63
Using one of the popular GUI toolkits available for Python might help:
Code:
python
'''ps_TextEdit1.py
explore PySide's QTextEdit multiline text entry box
click right mouse button in edit area for popup menu
for undo, cut, copy. paste, select All, etc.
the sample text is loaded using the file dialog widget

PySide is the official LGPL-licensed version of PyQT
you can downloaded the free Windows self-extracting installer
PySide-1.1.0qt474.win32-py2.7.exe
or
PySide-1.1.0qt474.win32-py3.2.exe
from:
http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows
'''

from PySide.QtCore import *
from PySide.QtGui import *

class MyFrame(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 50, 500, 300)
        self.setWindowTitle('QTextEdit() test')

        self.edit = QTextEdit(self)
        self.load_button = QPushButton(self)
        self.load_button.setText('Load a text file')

        # use grid layout manager
        grid = QGridLayout(self)
        # addWidget(QWidget, row, column, rowSpan, columnSpan)
        grid.addWidget(self.load_button, 0, 0, 1, 1)
        grid.addWidget(self.edit, 1, 0, 1, 2)
        self.setLayout(grid)

        # bind the button clicked to action
        # newer connect style used with version 4.5 or higher
        self.load_button.clicked.connect(self.load_file)

    def load_file(self):
        """
        load the selected filename and display in QTextEdit
        """
        caption = 'Open file'
        # use current/working directory
        directory = './'
        filter_mask = "Python/Text files (*.py *.pyw *.txt)"
        filename = QFileDialog.getOpenFileName(self,
            caption, directory, filter_mask)[0]
        print(filename)  # test
        # use try/except to trap any errors
        fh = None
        try:
            fh = QFile(filename)
            # check if file is loaded
            if not fh.open(QIODevice.ReadOnly):
                raise IOError(str(fh.errorString()))
            stream = QTextStream(fh)
            # set the proper decoding
            stream.setCodec("UTF-8")
            # load the text to self.edit
            self.edit.setPlainText(stream.readAll())
            # optional show filename in title bar
            self.setWindowTitle(QFileInfo(filename).fileName())
        except (IOError, OSError) as e:
            QMessageBox.warning(self, "Load Error",
                    "Failed to load %s: %s" % (filename, e))
        finally:
            if fh is not None:
                fh.close()


app = QApplication([])
frame = MyFrame()
frame.show()
app.exec_()
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > New to python and needs some help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap