GetFileName & GetDirectory Question

Sorry to beat a dead horse again, but I want to make sure I understand
what's going on in the code below (Lines 4-9). And thanks to all who've
already helped. . .

     1 def OnOpen(self,e):
     2 """ Open a file"""
     3 self.dirname = ''
     4 dlg = wx.FileDialog(self, "Choose a file", self.dirname,
"", "*.*", wx.OPEN)
     5 if dlg.ShowModal() == wx.ID_OK:
     6 self.filename = dlg.GetFilename()
     7 self.dirname = dlg.GetDirectory()
     8 f = open(os.path.join(self.dirname, self.filename), 'r')
     9 self.control.SetValue(f.read())
    10 f.close()
    11 dlg.Destroy()

Line 4 Opens the File Dialog Window. If the user chooses a directory &
file, proceed to next line. . .

Line 5 If user chooses a directory & file from the open dialog window &
clicks OK. . .

Line 6 This line stores the filename the user selected in varaible
self.filename?

Line 7 This line stores the directory chosen in variable self.dirname?

Line 8 Creates file object "F" which is the directory & file chosen above?

Line 9 Shows file object "F" in our text control box?

Lines 10,11 Obvious

Is this correct??? (Thanks in advance)

···

From my understanding:

line 5 showmodal is wxpython command and it causes wxpython not to do anything until the dlg is delt with either choosing a file or not… in other words until ok is clicked it wont go onto the next line of code…but for the most part you got it

···

On Mon, Nov 28, 2011 at 9:41 PM, Chris Kavanagh ckava3@gmail.com wrote:

Sorry to beat a dead horse again, but I want to make sure I understand

what’s going on in the code below (Lines 4-9). And thanks to all who’ve

already helped. . .

1     def OnOpen(self,e):

2         """ Open a file"""

3         self.dirname = ''

4         dlg = wx.FileDialog(self, "Choose a file", self.dirname,

“”, “.”, wx.OPEN)

5         if dlg.ShowModal() == wx.ID_OK:

6             self.filename = dlg.GetFilename()

7             self.dirname = dlg.GetDirectory()

8             f = open(os.path.join(self.dirname, self.filename), 'r')

9             self.control.SetValue(f.read())

10 f.close()

11 dlg.Destroy()

From my understanding:

Line 4 Opens the File Dialog Window. If the user chooses a directory &

file, proceed to next line. . .

Line 5 If user chooses a directory & file from the open dialog window &

clicks OK. . .

Line 6 This line stores the filename the user selected in varaible

self.filename?

Line 7 This line stores the directory chosen in variable self.dirname?

Line 8 Creates file object “F” which is the directory & file chosen above?

Line 9 Shows file object “F” in our text control box?

Lines 10,11 Obvious

Is this correct??? (Thanks in advance)

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

  Sorry

to beat a dead horse again, but I want to make sure I understand

  what's going on in the code below (Lines 4-9). And thanks to all

who’ve

  already helped. . .




      1     def OnOpen(self,e):


      2         """ Open a file"""


      3         self.dirname = ''


      4         dlg = wx.FileDialog(self, "Choose a file",

self.dirname,

  "", "*.*", wx.OPEN)


      5         if dlg.ShowModal() == wx.ID_OK:


      6             self.filename = dlg.GetFilename()


      7             self.dirname = dlg.GetDirectory()


      8             f = open(os.path.join(self.dirname,

self.filename), ‘r’)

      9             self.control.SetValue(f.read())


     10             f.close()


     11         dlg.Destroy()






   From my understanding:




  Line 4 Opens the File Dialog Window. If the user chooses a

directory &

  file, proceed to next line. . .

No Line for Creates a dialogue of type File Dialog.

  Line 5 If user chooses a directory & file from the open dialog

window &

  clicks OK. . .
This line shows the user the dialogue and waits for the user to

close the dialogue either by selecting a file, (with the option to
change directory to select one), and clicking OK or by clicking
CANCEL. If the user has done the former the indented code is
executed.

  Line 6

This line stores the filename the user selected in varaible

  self.filename?

Yes

  Line 7

This line stores the directory chosen in variable self.dirname?

Yes

  Line 8

Creates file object “F” which is the directory & file chosen
above?

No this line Opens the selected file in read mode,('r'), and saves

the file handle in a local variable f.

  Line 9 Shows file object "F" in our text control box?
No this line reads the contents of f and then sets the value of

control, self.control, to the contents, which if compatible
will display the same to the user.

  Lines 10,11 Obvious






  Is this correct??? (Thanks in advance)
Nested functions, as denoted by more than one set of parenthesis ()

on a single line, should always be read from the innermost out.
While the style of programming above is often used for its
compactness it is disliked by teachers and testers because it can be
done in a clearer manner. Taking the fragment above, if I was using
it for an introductory course I would re-write it as:

         def OnOpen(self,e):

             """ Handle the file/open menu event."""

             self.dirname = '.' 

default to the current directory

             # Create a file dialogue

             dlg = wx.FileDialog(self, "Choose a file",

self.dirname,
“”, “.”, wx.OPEN)

             # Show it to the user and get the button pressed

             button_pressed = dlg.ShowModal()

             if button_pressed == wx.ID_OK:

Did the user press OK?

                 # Yes

                 self.filename = dlg.GetFilename()

Get the file name

                 self.dirname = dlg.GetDirectory() # and the file

location

                 file_path = os.path.join(self.dirname,

self.filename) # Join them for the file path

                 file_handle = open(file_path, 'r')

Open the selected file in read mode

                 content = file_handle.read() # Read the content

                 self.control.SetValue(content)

Show it to the user

                 file_handle.close()

Close the file

                 dlg.Destroy()

Get rid of the dialogue

            else: # User pressed cancel

                print 'User Cancelled file open'

                self.control.SetValue('Nothing Loaded')

This uses a few more lines, and a few temporary variables explicitly

rather than implicitly but is, I think, a lot clearer.

Gadget/Steve
···

On 29/11/2011 5:41 AM, Chris Kavanagh wrote:

Did you take a look at ... ?

http://groups.google.com/group/wxpython-users/browse_thread/thread/024a7512ad64b811/855c1f6f88a62896#855c1f6f88a62896

jmf

···

On 29 nov, 06:41, Chris Kavanagh <cka...@gmail.com> wrote:

Sorry to beat a dead horse again, but I want to make sure I understand
what's going on in the code below (Lines 4-9). And thanks to all who've
already helped. . .

Thanks so much Steve for the help! This is EXACTLY the kind of response (answer) I was hoping for the 1st time I asked this question, lol.

I may have one more question, which I will reply all if I can't figure it out on my own. Again, thank you!!

···

On 11/29/2011 1:45 AM, Gadget/Steve wrote:

On 29/11/2011 5:41 AM, Chris Kavanagh wrote:

Sorry to beat a dead horse again, but I want to make sure I understand
what's going on in the code below (Lines 4-9). And thanks to all who've
already helped. . .

1 def OnOpen(self,e):
2 """ Open a file"""
3 self.dirname = ''
4 dlg = wx.FileDialog(self, "Choose a file", self.dirname,
"", "*.*", wx.OPEN)
5 if dlg.ShowModal() == wx.ID_OK:
6 self.filename = dlg.GetFilename()
7 self.dirname = dlg.GetDirectory()
8 f = open(os.path.join(self.dirname, self.filename), 'r')
9 self.control.SetValue(f.read())
10 f.close()
11 dlg.Destroy()

From my understanding:

Line 4 Opens the File Dialog Window. If the user chooses a directory &
file, proceed to next line. . .

No Line for Creates a dialogue of type File Dialog.

Line 5 If user chooses a directory & file from the open dialog window &
clicks OK. . .

This line shows the user the dialogue and waits for the user to close
the dialogue either by selecting a file, (with the option to change
directory to select one), and clicking OK or by clicking CANCEL. If the
user has done the former the indented code is executed.

Line 6 This line stores the filename the user selected in varaible
self.filename?

Yes

Line 7 This line stores the directory chosen in variable self.dirname?

Yes

Line 8 Creates file object "F" which is the directory & file chosen
above?

No this line Opens the selected file in read mode,('r'), and saves the
file handle in a local variable f.

Line 9 Shows file object "F" in our text control box?

No this line reads the contents of f and then sets the value of control,
self.control, to the contents, which _if compatible_ will display the
same to the user.

Lines 10,11 Obvious

Is this correct??? (Thanks in advance)

Nested functions, as denoted by more than one set of parenthesis () on a
single line, should always be read from the innermost out. While the
style of programming above is often used for its compactness it is
disliked by teachers and testers because it can be done in a clearer
manner. Taking the fragment above, if I was using it for an introductory
course I would re-write it as:

def OnOpen(self,e):
""" Handle the file/open menu event."""
self.dirname = '.' # default to the current directory
# Create a file dialogue
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*",
wx.OPEN)
# Show it to the user and get the button pressed
button_pressed = dlg.ShowModal()
if button_pressed == wx.ID_OK: # Did the user press OK?
# Yes
self.filename = dlg.GetFilename() # Get the file name
self.dirname = dlg.GetDirectory() # and the file location
file_path = os.path.join(self.dirname, self.filename) # Join them for
the file path
file_handle = open(file_path, 'r') # Open the selected file in read mode
content = file_handle.read() # Read the content
self.control.SetValue(content) # Show it to the user
file_handle.close() # Close the file
dlg.Destroy() # Get rid of the dialogue
else: # User pressed cancel
print 'User Cancelled file open'
self.control.SetValue('Nothing Loaded')

This uses a few more lines, and a few temporary variables explicitly
rather than implicitly but is, I think, a lot clearer.

Gadget/Steve

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Well, yes, since it was my actual question, I read the emails. Being new to programming in general, there were a few answers in those emails I didn't quite understand. So, I read another 12 or so pages of Google & tried to get a better understanding. Which I did. I just wanted to clarify what I hoped I learned, lol.

And thanks for pointing that out to me Jim. Your help is most appreciated. Glad you guys are around for novices like me. Of course, thanks to everyone!

···

On 11/29/2011 2:30 AM, jmfauth wrote:

On 29 nov, 06:41, Chris Kavanagh<cka...@gmail.com> wrote:

Sorry to beat a dead horse again, but I want to make sure I understand
what's going on in the code below (Lines 4-9). And thanks to all who've
already helped. . .

Did you take a look at ... ?

http://groups.google.com/group/wxpython-users/browse_thread/thread/024a7512ad64b811/855c1f6f88a62896#855c1f6f88a62896

jmf

Thanks so much for the help Clay. It is much appreciated!

···

On 11/29/2011 1:03 AM, Clay Richmond wrote:

line 5 showmodal is wxpython command and it causes wxpython not to do
anything until the dlg is delt with either choosing a file or not... in
other words until ok is clicked it wont go onto the next line of
code....but for the most part you got it

On Mon, Nov 28, 2011 at 9:41 PM, Chris Kavanagh <ckava3@gmail.com > <mailto:ckava3@gmail.com>> wrote:

    Sorry to beat a dead horse again, but I want to make sure I understand
    what's going on in the code below (Lines 4-9). And thanks to all who've
    already helped. . .

        1 def OnOpen(self,e):
        2 """ Open a file"""
        3 self.dirname = ''
        4 dlg = wx.FileDialog(self, "Choose a file", self.dirname,
    "", "*.*", wx.OPEN)
        5 if dlg.ShowModal() == wx.ID_OK:
        6 self.filename = dlg.GetFilename()
        7 self.dirname = dlg.GetDirectory()
        8 f = open(os.path.join(self.__dirname,
    self.filename), 'r')
        9 self.control.SetValue(f.read()__)
       10 f.close()
       11 dlg.Destroy()

      From my understanding:

    Line 4 Opens the File Dialog Window. If the user chooses a directory &
    file, proceed to next line. . .

    Line 5 If user chooses a directory & file from the open dialog window &
    clicks OK. . .

    Line 6 This line stores the filename the user selected in varaible
    self.filename?

    Line 7 This line stores the directory chosen in variable self.dirname?

    Line 8 Creates file object "F" which is the directory & file chosen
    above?

    Line 9 Shows file object "F" in our text control box?

    Lines 10,11 Obvious

    Is this correct??? (Thanks in advance)

    --
    To unsubscribe, send email to
    wxPython-users+unsubscribe@__googlegroups.com
    <mailto:wxPython-users%2Bunsubscribe@googlegroups.com>
    or visit http://groups.google.com/__group/wxPython-users?hl=en
    <http://groups.google.com/group/wxPython-users?hl=en&gt;

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Well, it was not really clear to me, what was
really the bottleneck (gui, Python, class attributes).

My verbose, not complete, code is, in my mind,
pedagogically quite correct. It shows at least
several points.

- Separation between gui code/Python code - getting the
filename/reading the file.
- It uses the io module and it takes care of the coding
of the characters. (The io module, the Python 3 "standard",
has not been naively backported to Python 2.7).
- Reading (correctly) a file implies, you implement
an error handling. This is practically mandatory.

Have fun.

jmf

···

On 29 nov, 08:39, Chris Kavanagh <cka...@gmail.com> wrote:

On 11/29/2011 2:30 AM, jmfauth wrote:

Well, yes, since it was my actual question, I read the emails. Being new
to programming in general, ...

Yes, it was definitely complete Jim. It was just my ignorance that caused my confusion. After going back and looking closely at your original email, I realized it answered ALL my questions. . .So, again, thanks for taking the time to do that for me!!!

            Thanks,
              Chris

···

On 11/29/2011 3:49 AM, jmfauth wrote:

On 29 nov, 08:39, Chris Kavanagh<cka...@gmail.com> wrote:

On 11/29/2011 2:30 AM, jmfauth wrote:

Well, yes, since it was my actual question, I read the emails. Being new
to programming in general, ...

Well, it was not really clear to me, what was
really the bottleneck (gui, Python, class attributes).

My verbose, not complete, code is, in my mind,
pedagogically quite correct. It shows at least
several points.

- Separation between gui code/Python code - getting the
filename/reading the file.
- It uses the io module and it takes care of the coding
of the characters. (The io module, the Python 3 "standard",
has not been naively backported to Python 2.7).
- Reading (correctly) a file implies, you implement
an error handling. This is practically mandatory.

Have fun.

jmf

...

jmf

Yes, it was definitely complete Jim.

JMF must have overlooked it that you "renamed" him.

It is Jean-Michel, IIRC.

Werner

···

On 11/29/2011 07:14 PM, Chris Kavanagh wrote: