Hello all,

I would really appreciate help with this seemingly basic problem.

Here is my code;

import wx
import FloatSpin as FS

class DesignConditions(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Design Conditions',size=(1000,700))
        panel = wx.Panel(self)

        wx.StaticText(panel,-1,'Atmospheric Temperature  (K): ', (25,150))
        wx.StaticText(panel,-1,'Atmospheric Pressure  (kPa): ', (25,200))
        wx.StaticText(panel,-1,'Mach Number: ', (25,250))
        wx.StaticText(panel,-1,'Ratio of Specific Heat Capacities (k) of AIR: ', (25,300))
        wx.StaticText(panel,-1,'Ratio of Specific Heat Capacities (k) of EXHAUST: ', (25,350))
        wx.StaticText(panel,-1,'Gas co-efficient Constant (R - KJ/kg.K) of AIR: ', (25,400))
        wx.StaticText(panel,-1,'Gas co-efficient Constant (R - KJ/kg.K) of EXHAUST: ', (25,450))

        self.temp = FS.FloatSpin(panel,-1,min_val=200,max_val=300,increment=0.01,value=0.01,pos=(200,150))
        self.temp.SetFormat("%f")
        self.temp.SetDigits(3)

        self.pres = FS.FloatSpin(panel,-1,min_val=10,max_val=110,increment=0.01,value=0.01,pos=(200,200))
        self.pres.SetFormat("%f")
        self.pres.SetDigits(3)

        self.mach = FS.FloatSpin(panel,-1,min_val=0,max_val=10,increment=0.01,value=0.01,pos=(200,250))
        self.mach.SetFormat("%f")
        self.mach.SetDigits(3)

        self.K = FS.FloatSpin(panel,-1,min_val=1,max_val=3,increment=0.01,value=1.4,pos=(275,300))
        self.K.SetFormat("%f")
        self.K.SetDigits(3)

        self.Kt = FS.FloatSpin(panel,-1,min_val=1,max_val=3,increment=0.01,value=1.3,pos=(275,350))
        self.Kt.SetFormat("%f")
        self.Kt.SetDigits(3)

        self.R = FS.FloatSpin(panel,-1,min_val=0,max_val=1,increment=0.01,value=0.287,pos=(275,400))
        self.R.SetFormat("%f")
        self.R.SetDigits(3)

        self.Rt = FS.FloatSpin(panel,-1,min_val=0,max_val=1,increment=0.01,value=0.287,pos=(285,450))
        self.Rt.SetFormat("%f")
        self.Rt.SetDigits(3)

        wx.StaticText(panel,-1,'Stagnation Temperature: ', (350,150))
        wx.StaticText(panel,-1,'Stagnation Pressure: ', (350,200))
        wx.StaticText(panel,-1,'Speed of Sound: ', (350,250))
        wx.StaticText(panel,-1,'Specific Heat Capacity of AIR @ Constant Pressure: ', (450,300))
        wx.StaticText(panel,-1,'Specific Heat Capacity of EXHAUST @ Constant Pressure: ', (450,350))

        self.t2 = wx.StaticText(panel,-1,'',(500,150))
        self.p2 = wx.StaticText(panel,-1,'',(500,200))
        self.speed = wx.StaticText(panel,-1,'',(500,250))
        self.cp = wx.StaticText(panel,-1,'',(710,300))
        self.cpe = wx.StaticText(panel,-1,'',(750,350))

        compute_btn = wx.Button(panel,1,'Calculate',(450,450))
        compute_btn.SetFocus()
        clear_btn = wx.Button(panel,2,'Quit',(650,450))

        wx.EVT_BUTTON(panel,1,self.OnCompute)
        wx.EVT_BUTTON(panel,2,self.OnClose)
        wx.EVT_CLOSE(panel,self.OnClose)
                      
    def OnCompute(self,event):

        t = float(self.temp.GetValue())
        p = float(self.pres.GetValue())
        M = float(self.mach.GetValue())
        k = float(self.K.GetValue())
        kt = float(self.Kt.GetValue())
        R = float(self.R.GetValue())
        Rt = float(self.Rt.GetValue())
        
        t2 = t*(1+((k-1)/2)*(M**2))
        p2 = p*((1+((k-1)/2)**(M**2))**((k-1)/k))
        v = (1000*k*R*t)**(0.5)
        cp = (k*R)/(k - 1)
        cpe = (kt*Rt)/(kt - 1)
        
        self.t2.SetLabel(str(t2) + '  K')
        self.p2.SetLabel(str(p2) + '  kPa')
        self.speed.SetLabel(str(v) + '  m/s')
        self.cp.SetLabel(str(cp) + '  KJ/kg.K')
        self.cpe.SetLabel(str(cpe) + '  KJ/kg.K')

        return t2,p2,v,k,kt,cp,cpe

    def OnClose(self,event):
        self.Destroy()

if __name__=='__main__':
    
    app = wx.PySimpleApp()
    frame = DesignConditions(parent=None, id=1)
    frame.Show()
    app.MainLoop()

FloatSpin is a widget that was downloaded online, basically a spinner that handles floats. On command i.e. when the calculate button is pressed variables are calculated i.e. t2,p2,v,k,kt,cp,cpe and displayed on the frame.

What I would like to do is import these calculated variables into another file to do further calculations... If some one can tell me how to this basic pass I would really appreciate it.

Thanks in Advance..

Edited 6 Years Ago by vegaseat: added code tags

I haven't been able to actually test this, but insert this at the bottom of __init__() ...

save_btn = wx.Button(panel,3,'Save Data',(750,450))
        wx.EVT_BUTTON(panel,3,self.OnSave)

    def OnSave(self,event):
        # 1 is a dummy for event
        t2, p2, v, k, kt, cp, cpe = self.OnCompute(1)
        fname = "MyData.dat"
        fout = open(fname, "w")
        # this will be the header
        s1 = "t2,p2,v,k,kt,cp,cpe" + '\n'
        # these are the data separated by commas
        sf = "%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f"
        s2 = sf % (t2, p2, v, k, kt, cp, cpe)
        fout.write(s1+s2)
        fout.close()

Look at file MyData.dat, use the second line which contains the data you want in the order the header shows. Split that line at the comma to get a list of data.

I haven't been able to actually test this, but insert this at the bottom of __init__() ...

save_btn = wx.Button(panel,3,'Save Data',(750,450))
        wx.EVT_BUTTON(panel,3,self.OnSave)

    def OnSave(self,event):
        # 1 is a dummy for event
        t2, p2, v, k, kt, cp, cpe = self.OnCompute(1)
        fname = "MyData.dat"
        fout = open(fname, "w")
        # this will be the header
        s1 = "t2,p2,v,k,kt,cp,cpe" + '\n'
        # these are the data separated by commas
        sf = "%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f"
        s2 = sf % (t2, p2, v, k, kt, cp, cpe)
        fout.write(s1+s2)
        fout.close()

Look at file MyData.dat, use the second line which contains the data you want in the order the header shows. Split that line at the comma to get a list of data.

Firstly I really appreciate the extension its really helpful, but I what I meant was that I would like to export the data in another wxPython .py file for other calculations not just any old .dat file.

I was just wondering if you the code that would transfer the data
into another .py file. Cheers thanks a lot.

This might just do it ...

def OnCompute(self,event):

        t = float(self.temp.GetValue())
        p = float(self.pres.GetValue())
        M = float(self.mach.GetValue())
        k = float(self.K.GetValue())
        kt = float(self.Kt.GetValue())
        R = float(self.R.GetValue())
        Rt = float(self.Rt.GetValue())

        # create module code string and save it
        # for import into another Python program
        sf = \
"""
t = %0.5f
p = %0.5f
M = %0.5f
k = %0.5f
kt = %0.5f
R = %0.5f
Rt = %0.5f
"""
        module_str = sf % (t, p, M, k, kt, R. Rt)
        fname = "raw_data.py"
        fout = open(fname, "w")
        fout.write(module_str)
        fout.close()

        t2 = t*(1+((k-1)/2)*(M**2))
        p2 = p*((1+((k-1)/2)**(M**2))**((k-1)/k))
        v = (1000*k*R*t)**(0.5)
        cp = (k*R)/(k - 1)
        cpe = (kt*Rt)/(kt - 1)
        
        self.t2.SetLabel(str(t2) + '  K')
        self.p2.SetLabel(str(p2) + '  kPa')
        self.speed.SetLabel(str(v) + '  m/s')
        self.cp.SetLabel(str(cp) + '  KJ/kg.K')
        self.cpe.SetLabel(str(cpe) + '  KJ/kg.K')

        return t2,p2,v,k,kt,cp,cpe

Edited 6 Years Ago by vegaseat: correction

This might just do it ...

def OnCompute(self,event):

        t = float(self.temp.GetValue())
        p = float(self.pres.GetValue())
        M = float(self.mach.GetValue())
        k = float(self.K.GetValue())
        kt = float(self.Kt.GetValue())
        R = float(self.R.GetValue())
        Rt = float(self.Rt.GetValue())

        # create module code string and save it
        # for import into another Python program
        sf = \
"""
t = %0.5f
p = %0.5f
M = %0.5f
k = %0.5f
kt = %0.5f
R = %0.5f
Rt = %0.5f
"""
        module_str = sf % (t, p, M, k, kt, R. Rt)
        fname = "raw_data.py"
        fout = open(fname, "w")
        fout.write(module_str)
        fout.close()

        t2 = t*(1+((k-1)/2)*(M**2))
        p2 = p*((1+((k-1)/2)**(M**2))**((k-1)/k))
        v = (1000*k*R*t)**(0.5)
        cp = (k*R)/(k - 1)
        cpe = (kt*Rt)/(kt - 1)
        
        self.t2.SetLabel(str(t2) + '  K')
        self.p2.SetLabel(str(p2) + '  kPa')
        self.speed.SetLabel(str(v) + '  m/s')
        self.cp.SetLabel(str(cp) + '  KJ/kg.K')
        self.cpe.SetLabel(str(cpe) + '  KJ/kg.K')

        return t2,p2,v,k,kt,cp,cpe

I am really sorry to do this but, but this code basically prints out the calculated values in another .py file. What I want to do is pass the calculated variables between .py files i.e. i want to pass or parse t2,p2,v,k,kt,cp,cpe to another module featuring methods and functions and use the variables for further calculation...

I take it the two modules are running at the same time?

I would just write the data to a file like vegaseat has shown you, and then in the other module i would load the data from the file. That is the easiest way you would be able to do it i think.

I am really sorry to do this but, but this code basically prints out the calculated values in another .py file. What I want to do is pass the calculated variables between .py files i.e. i want to pass or parse t2,p2,v,k,kt,cp,cpe to another module featuring methods and functions and use the variables for further calculation...

I was hoping you could figure this out on your own, but here is a hint.

You import the module "raw_data.py" into your other program with
import raw_data
then access that data with ...
raw_data.t
raw_data.p
raw_data.M

... and so on.

Edited 6 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.