[wxpython] problem writing actual string into file

I have a problem with saving the actual string into an output file. in the
1st run it doesn't save anything. in the 2nd run it save the strings of the
1st run.
how can i make the program to save the strings of the last runs??

Stephan H

from WxPython.wx import *
import os, sys, string
from distutils.core import Command
from distutils.errors import *

wxNewID=01
file_out=open('input.txt','w')

class MainWindow(wxFrame):
    def __init__(self, parent, id):
        
        self.dirname=".."

wxFrame.__init__(self,parent,-2,"MainFrame",wxDefaultPosition,wxSize(600,800
))

        panel1 = wxPanel(self, -1)
        wxTextCtrl(panel1, -1, "Make Choice of Requirements",
                   wxPoint(350, 10),wxSize(160,20),wxTE_READONLY |
wxST_NO_AUTORESIZE)

···

#---------------------------------------------------------------------------
-----------------------------
        choicebox = wxChoice(panel1, -1, (10,50),
                             choices = ['Customer', 'TeamMember',
'TeamLeader'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn = wxButton(panel1, -1, 'Get User Type', (10,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice1)
            EVT_BUTTON(panel1, btn.GetId(), self.OnGet1ChoiceValue)
            self.choice = choicebox
#---------------------------------------------------------------------------
-----------------------------
        choicebox = wxChoice(panel1, -1, (160,50),
                           choices =
['<100','100','150','200','250','300','350','400',
                                      '450','500','>500'])

        for choice in range(1):
            choicebox.Append(str(choice))
            btn2 = wxButton(panel1, -1, 'Range Value', (160,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice2)
            EVT_BUTTON(panel1, btn2.GetId(), self.OnGet2ChoiceValue)
            self.choice=choicebox
#---------------------------------------------------------------------------
-----------------------------
        choicebox = wxChoice(panel1, -1, (270,50),
                             choices = ['3','3.5','4','4.5'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn3 = wxButton(panel1, -1, 'Runway Length Requirement',
(270,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice3)
            EVT_BUTTON(panel1, btn3.GetId(), self.OnGet3ChoiceValue)
            self.choice=choicebox
#---------------------------------------------------------------------------
-----------------------------
        choicebox = wxChoice(panel1, -1, (450,50),
                             choices = ['150','200','250','300','350'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn4 = wxButton(panel1, -1, 'Maximum Cruise Speed Requirement',
(450,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice4)
            EVT_BUTTON(panel1, btn4.GetId(), self.OnGet4ChoiceValue)
            self.choice=choicebox
#---------------------------------------------------------------------------
-----------------------------
    def OnChoice1(self, event):
        print 'User:', event.GetString()
    
    def OnGet1ChoiceValue(self, event):
        print 'Kind of User:', self.choice.GetStringSelection() #writes the
former value of range string instead of actual string

    def OnChoice2(self, event):
        print 'Range [km]:', event.GetString()
        Range_=event.GetString()
        file_out.write(Range_) #writes the former value of range string
instead of actual string
        file_out.write('\n')
        
    def OnGet2ChoiceValue(self, event):
        print 'Range Required [km]:', self.choice.GetStringSelection()
    
    def OnChoice3(self, event):
        print 'Runway Length [km]:', event.GetString()
        Runwaylength=event.GetString()
        file_out.write(Runwaylength)
        file_out.write('\n')
        
    def OnGet3ChoiceValue(self, event):
        print 'Runway Length Requirement [km]:',
self.choice.GetStringSelection() #writes the former value of range string
instead of actual string
                
    def OnChoice4(self, event):
        print 'Maximum Cruise Speed[m/s]:', event.GetString()
        Maxcruisespeed=event.GetString()
        file_out.write(Maxcruisespeed) #writes the former value of range
string instead of actual string
        file_out.write('\n')
        
    def OnGet4ChoiceValue(self, event):
        print 'Maximum Cruise Speed Required [m/s]:',
self.choice.GetStringSelection()
#---------------------------------------------------------------------------
-----------------------------
class TestApp(wxApp):
    def OnInit(self):
        frame = MainWindow(NULL,-1)
        frame.SetSize((800,600))
        frame.Show(1)
        return true
        
app = TestApp(0)
app.MainLoop()

stephan huijgen wrote:

I have a problem with saving the actual string into an output file. in the
1st run it doesn't save anything. in the 2nd run it save the strings of the
1st run.
how can i make the program to save the strings of the last runs??

it works just fine for me under linux.

Here is what gets printed to the terminal:
User: TeamMember
User: TeamLeader
Range [km]: 100
Range [km]: 200
Range [km]: 400
Runway Length [km]: 3
Runway Length [km]: 4
Maximum Cruise Speed[m/s]: 200
Maximum Cruise Speed[m/s]: 300

And this is what gets written to the file:
100
200
400
3
4
200
300

And those are the right values. What is the problem???

By the way, when you post code, include the *.py file as an enclosure,
rather than cutting and pasting into your email. Email clients tend to
wrap the lines, and can mess up the indentation.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Hi,

I'm on WinXP, and get a similar result.

I think the first run saving nothing, followed by the second run saving
first run data, etc. - is due to the file not being closed before the
program terminates.

When the program exits with an open file, the data accumulated in the
buffer is never actually written out to disk.
When you run the program the second time, the Open statement causes the
buffered data from the first run to be written, then the second buffer
of data starts to collect, but isn't written either - and so on.

Adding a statement "file_out.close()" after your last write statement
should correct the problem (or better would be to put the statement in
an exit routine).

Dan Cherry

···

-----Original Message-----
From: stephan huijgen [mailto:s.huijgen@student.tudelft.nl]
Sent: Tuesday, June 03, 2003 12:03 PM
To: wxpython-users@lists.wxwindows.org
Subject: [wxPython-users] [wxpython] problem writing actual string into
file

I have a problem with saving the actual string into an output file. in
the
1st run it doesn't save anything. in the 2nd run it save the strings of
the
1st run.
how can i make the program to save the strings of the last runs??

Stephan H

from WxPython.wx import *
import os, sys, string
from distutils.core import Command
from distutils.errors import *

wxNewID=01
file_out=open('input.txt','w')

class MainWindow(wxFrame):
    def __init__(self, parent, id):
        
        self.dirname=".."

wxFrame.__init__(self,parent,-2,"MainFrame",wxDefaultPosition,wxSize(600
,800
))

        panel1 = wxPanel(self, -1)
        wxTextCtrl(panel1, -1, "Make Choice of Requirements",
                   wxPoint(350, 10),wxSize(160,20),wxTE_READONLY |
wxST_NO_AUTORESIZE)
#-----------------------------------------------------------------------
----
-----------------------------
        choicebox = wxChoice(panel1, -1, (10,50),
                             choices = ['Customer', 'TeamMember',
'TeamLeader'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn = wxButton(panel1, -1, 'Get User Type', (10,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice1)
            EVT_BUTTON(panel1, btn.GetId(), self.OnGet1ChoiceValue)
            self.choice = choicebox
#-----------------------------------------------------------------------
----
-----------------------------
        choicebox = wxChoice(panel1, -1, (160,50),
                           choices =
['<100','100','150','200','250','300','350','400',
                                      '450','500','>500'])

        for choice in range(1):
            choicebox.Append(str(choice))
            btn2 = wxButton(panel1, -1, 'Range Value', (160,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice2)
            EVT_BUTTON(panel1, btn2.GetId(), self.OnGet2ChoiceValue)
            self.choice=choicebox
#-----------------------------------------------------------------------
----
-----------------------------
        choicebox = wxChoice(panel1, -1, (270,50),
                             choices = ['3','3.5','4','4.5'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn3 = wxButton(panel1, -1, 'Runway Length Requirement',
(270,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice3)
            EVT_BUTTON(panel1, btn3.GetId(), self.OnGet3ChoiceValue)
            self.choice=choicebox
#-----------------------------------------------------------------------
----
-----------------------------
        choicebox = wxChoice(panel1, -1, (450,50),
                             choices = ['150','200','250','300','350'])
        for choice in range(1):
            choicebox.Append(str(choice))
            btn4 = wxButton(panel1, -1, 'Maximum Cruise Speed
Requirement',
(450,85))
            EVT_CHOICE(panel1, choicebox.GetId(), self.OnChoice4)
            EVT_BUTTON(panel1, btn4.GetId(), self.OnGet4ChoiceValue)
            self.choice=choicebox
#-----------------------------------------------------------------------
----
-----------------------------
    def OnChoice1(self, event):
        print 'User:', event.GetString()
    
    def OnGet1ChoiceValue(self, event):
        print 'Kind of User:', self.choice.GetStringSelection() #writes
the
former value of range string instead of actual string

    def OnChoice2(self, event):
        print 'Range [km]:', event.GetString()
        Range_=event.GetString()
        file_out.write(Range_) #writes the former value of range
string
instead of actual string
        file_out.write('\n')
        
    def OnGet2ChoiceValue(self, event):
        print 'Range Required [km]:', self.choice.GetStringSelection()
    
    def OnChoice3(self, event):
        print 'Runway Length [km]:', event.GetString()
        Runwaylength=event.GetString()
        file_out.write(Runwaylength)
        file_out.write('\n')
        
    def OnGet3ChoiceValue(self, event):
        print 'Runway Length Requirement [km]:',
self.choice.GetStringSelection() #writes the former value of range
string
instead of actual string
                
    def OnChoice4(self, event):
        print 'Maximum Cruise Speed[m/s]:', event.GetString()
        Maxcruisespeed=event.GetString()
        file_out.write(Maxcruisespeed) #writes the former value of range
string instead of actual string
        file_out.write('\n')
        
    def OnGet4ChoiceValue(self, event):
        print 'Maximum Cruise Speed Required [m/s]:',
self.choice.GetStringSelection()
#-----------------------------------------------------------------------
----
-----------------------------
class TestApp(wxApp):
    def OnInit(self):
        frame = MainWindow(NULL,-1)
        frame.SetSize((800,600))
        frame.Show(1)
        return true
        
app = TestApp(0)
app.MainLoop()

Dan Cherry wrote:

I think the first run saving nothing, followed by the second run saving
first run data, etc. - is due to the file not being closed before the
program terminates.

When the program exits with an open file, the data accumulated in the
buffer is never actually written out to disk.

Well, no. Unless the program crashes, the file should be closed and
flushed when the file object goes out of scope, including when the
program terminates. Apparently this isn't guaranteed behaviour according
to the Python Language Reference, but it is certainly the way that
C-Python behaves.

If you look at the file before exiting the program, there will be data
in th3e buffer that is not flushed to the file.

When you run the program the second time, the Open statement causes the
buffered data from the first run to be written,

When you open an existing file with the 'w' flag, it writes over any
exising file with the same name. It does not append, and there is no way
that it could flush the buffer at that point, the buffer is contolled by
the Python process, which goes comletely away when you terminate the
program, unless you are running this from an IDE, or PyCrust, or
something.

Adding a statement "file_out.close()" after your last write statement

This is a good practice anyway, but I doubt it will make a difference
here.

Frankly, I'm not entirely clear what behaviour your seeing, and, as I
said, it appears to be working perfectly on Linux, and I don't see
anything wrong with the code.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

thanks for the tip for attaching the program file... i will do this next
time
the problem is that when i run the program under windows i do not get those
result, i get the results of the former run session... strange while you get
the right results under unix..

Stephan

···

----- Original Message -----
From: "Chris Barker" <Chris.Barker@noaa.gov>
To: <wxPython-users@lists.wxwindows.org>
Sent: Tuesday, June 03, 2003 4:52 AM
Subject: Re: [wxPython-users] [wxpython] problem writing actual string into
file

stephan huijgen wrote:
>
> I have a problem with saving the actual string into an output file. in

the

> 1st run it doesn't save anything. in the 2nd run it save the strings of

the

> 1st run.
> how can i make the program to save the strings of the last runs??

it works just fine for me under linux.

Here is what gets printed to the terminal:
User: TeamMember
User: TeamLeader
Range [km]: 100
Range [km]: 200
Range [km]: 400
Runway Length [km]: 3
Runway Length [km]: 4
Maximum Cruise Speed[m/s]: 200
Maximum Cruise Speed[m/s]: 300

And this is what gets written to the file:
100
200
400
3
4
200
300

And those are the right values. What is the problem???

By the way, when you post code, include the *.py file as an enclosure,
rather than cutting and pasting into your email. Email clients tend to
wrap the lines, and can mess up the indentation.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Stephan Huijgen wrote:

the problem is that when i run the program under windows i do not get those
result, i get the results of the former run session... strange while you get
the right results under unix..

hmmm. What happens when you do this:

delete any existing input.txt file.

1)run your program

2)pick a few choices

3)quit your program.

4)open input.txt in an editor.

Does input.txt exist? What is in it?

by the way, if you are running it from Boa or another IDE, you may not
be ever closing and flushing and closing your file.

Just for testing, try putting a file.flush() call in after each
file.write() call.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

it seems to work properly now with the right values in the output file..
thanks so far..

instead of using the choicebox i would like to use a textbox as well for
some parameters...is this possible? and how?

Stephan Huijgen

···

----- Original Message -----
From: "Chris Barker" <Chris.Barker@noaa.gov>
To: <wxPython-users@lists.wxwindows.org>
Sent: Tuesday, June 03, 2003 11:25 PM
Subject: Re: [wxPython-users] [wxpython] problem writing actual string into
file

Stephan Huijgen wrote:

> the problem is that when i run the program under windows i do not get

those

> result, i get the results of the former run session... strange while you

get

> the right results under unix..

hmmm. What happens when you do this:

delete any existing input.txt file.

1)run your program

2)pick a few choices

3)quit your program.

4)open input.txt in an editor.

Does input.txt exist? What is in it?

by the way, if you are running it from Boa or another IDE, you may not
be ever closing and flushing and closing your file.

Just for testing, try putting a file.flush() call in after each
file.write() call.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

> I think the first run saving nothing, followed by the second run

saving

> first run data, etc. - is due to the file not being closed before

the

> program terminates.
>
> When the program exits with an open file, the data accumulated in

the

> buffer is never actually written out to disk.

Well, no. Unless the program crashes, the file should be closed and
flushed when the file object goes out of scope, including when the
program terminates. Apparently this isn't guaranteed behaviour

according

to the Python Language Reference, but it is certainly the way that
C-Python behaves.

Correct - see below

If you look at the file before exiting the program, there will be data
in th3e buffer that is not flushed to the file.

> When you run the program the second time, the Open statement causes

the

> buffered data from the first run to be written,

When you open an existing file with the 'w' flag, it writes over any
exising file with the same name. It does not append, and there is no

way

that it could flush the buffer at that point, the buffer is contolled

by

the Python process, which goes comletely away when you terminate the
program, unless you are running this from an IDE, or PyCrust, or
something.

Chris is 100% correct about how the system handles file creation, but
the problem Stephan was having behaved as if he was using IDLE with a
script.
So, I tested the code from IDLE - and a 0 length file called "input.txt"
is created on the first execution and remains 0 length after the script
is closed - if you remain in IDLE after closing the script, re-running
the script writes the data to the file. Then when you close IDLE, the
final data is written. For comparison's sake, I'm on WinXP with python
2.2 and wxPython 2.4.0.7.

I'd be curious to know if the file writes properly under IDLE on Linux
when you remain in IDLE for multiple executions of the script (assuming
IDLE is in the Linux build???). I'm planning to set up my first Linux
computer next weekend, and expect a lot of system differences.

Thanks,
Dan

> Adding a statement "file_out.close()" after your last write

statement

This is a good practice anyway, but I doubt it will make a difference
here.

Frankly, I'm not entirely clear what behaviour your seeing, and, as I
said, it appears to be working perfectly on Linux, and I don't see
anything wrong with the code.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail:

wxPython-users-help@lists.wxwindows.org

Dan Cherry wrote:

So, I tested the code from IDLE - and a 0 length file called "input.txt"
is created on the first execution and remains 0 length after the script
is closed - if you remain in IDLE after closing the script, re-running
the script writes the data to the file. Then when you close IDLE, the
final data is written. For comparison's sake, I'm on WinXP with python
2.2 and wxPython 2.4.0.7.

I'd be curious to know if the file writes properly under IDLE on Linux
when you remain in IDLE for multiple executions of the script

No, I doubt it would work. I havn't used IDLE much (it is supported, and
part of the standard distro), but I ran into similar problems with the
MacPython IDE and PythonWin. In general, none of the IDEs that run your
program in the same Python process as the IDE itself will not work well
with wxPython anyway, unless it is also written in wxPython (ie Boa).

As for files, anything larger than a small script should be structured
so the file is flushed, closed, or at least gone out of scope in some
structured way, rather than kept as a module variable.

By the way, in IDLE (or siilar IDEs) if the module is reload()-ed, the
open() call will get re-run, writing over the old file, throughing out
the data, except on the Mac, where you will get an error because you are
trying to open an already opened file.

IDLE is in the Linux build???). I'm planning to set up my first Linux
computer next weekend, and expect a lot of system differences.

Remarkably few, in fact, when it comes to Python + wxPython.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                        
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov