Shane | 2 Feb 03:26 2012
Picon

trouble placing code into wxpython

Do I have to rewrite my code directly into wxpython frame
or can i just copy and paste it somehoe
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 2 Feb 03:37 2012

Re: trouble placing code into wxpython

On Wed, Feb 01, 2012 at 09:26:00PM -0500, Shane wrote:
> Do I have to rewrite my code directly into wxpython frame
> or can i just copy and paste it somehoe

I don't know. If you try to copy and paste it, what happens?

I'm afraid your question doesn't make much sense to me, and unless 
there happens to be an expert on wxPython here, you may have better
luck asking it on a dedicated wxPython forum or mailing list.

Failing that, can you explain in more detail what you mean by a 
wxPython frame, and why you would be pasting code directly into 
it instead of in your .py program file?

--

-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

shane | 2 Feb 07:56 2012
Picon

Fwd: Re: trouble placing code into wxpython


















i was trying to put the math.py into a frame Ive watched many tutorials.
But cant seem to find out how to or where to place the code
the files I want combined are attached.
#
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,'hello')
frame.Show(1)
app.MainLoop()

import random

name = raw_input ('Enter Name: ')
def intro(name):
  print 'Hello,', name, 'welcome to Python Type an interactive learning environment'
  print ( 'for python. Please refer to "specific online tutorial"')
  print ('for in depth information on the lessons')
  raw_input('Press Enter to continue...')

intro(name)
def mat():
  class R(object):     
     <at> property
    def x(self):
      return random.randint(500, 1000)
     <at> property
    def y(self):
      return random.randint(500, 1000)
  ran = R()

  	
  print 'Okay',name, 'lets get started first we\'ll do some simple math'
  print 'in this exersize you\'ll use'
  print ' * for multiplying integers'
  print ' + for adding integers'
  print ' - for subtracting numbers and'
  print ' / for dividing'
  print 'Try now by multipying(* = Shift8) enter like so: '
  u = ran.y
  x = ran.x
  print u,'*',x
  def t():

   
    v = u*x
    z = input('Enter Here: ')
    print z
    if z == v:
      print 'Good Job!!'
      fin()
    if z == False :
      print 'Please try again'
      t()
    else:
      print 'Please try again'
      t()
  t()

def fin():

  a = raw_input ('Would you like to(cont)(quit)? ')
  if a == 'cont':
    mat()
  if a == 'quit':
    print   'end'

  else:
    print ("not a command");
    fin()

  
mat()  

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Alan Gauld | 2 Feb 09:54 2012

Re: Fwd: Re: trouble placing code into wxpython

On 02/02/12 06:56, shane wrote:

> i was trying to put the math.py into a frame Ive watched many tutorials.

That sounds like you've been using video tutorials? Those are a good 
start but you are probably better using a written one and working 
through them doing the examples. You can try my GUI topic as a quick 
start. It uses Tkinter but finishes with a wxPython version too so it 
might be enough to get you started. The concepts are similar.

> But cant seem to find out how to or where to place the code
> the files I want combined are attached.

#
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None,-1,'hello')
frame.Show(1)
app.MainLoop()
#

This code creates the GUI(*) but does nothing.
You need to define the event handling functions and
bind them to the widgets/events. In this case you
don't have much to bind things to...

A Frame is just a container, it doesn't do anything as such.
You need to add other widgets such as text labels, entries,
buttons etc to the Frame. Then attach your functions
to the widgets. Any wxPython tutorial will show you
how to do that.

(*) wxPython is very OOP oriented, you normally have to create your own 
sub-class of Frame. There is not much you can do with a standard Frame 
object... If you are not comfortable with creating subclasses then you 
should study OOP a bit more before attempting wxPython. Either that or 
use Tkinter instead which is more amenable to procedural style code.

The other thing is your math code is full of print statements. They 
won't work in a GUI. You need to get your code to return strings and 
then display those strings in the GUI.

Finally, rewrite your fin() method to be non-recursive. Use a while 
loop. Otherwise you are likely to get into all sorts of trouble, its one 
of the most common beginners mistakes but recursion is not a good 
looping technique in Python. However, in a GUI you won't need fin() 
because the GUI mainloop will take over that function.

--

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 2 Feb 13:37 2012

Re: Fwd: Re: trouble placing code into wxpython

shane wrote:

> i was trying to put the math.py into a frame Ive watched many tutorials.
> But cant seem to find out how to or where to place the code
> the files I want combined are attached.

Do you mean you want to show the code in the GUI? You need to know the 
location of the file, then read the file and put the text into the frame. The 
fact that the text is actually code is irrelevant -- it could be poetry, a 
shopping list, code, or random gibberish.

I'm afraid I don't know enough about wxPython to tell you how to display text 
in the frame, but you can read the file like this:

fp = open(r'C:\path\to\my\file\module.py', 'r')
text = fp.read()
fp.close()

Also I see that you have a file called "math.py". That is a bad idea, because 
it will clash with a standard Python module also called "math". Best to find 
another name for it.

--

-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Tonu Mikk | 2 Feb 18:36 2012
Picon

exercise with classes

I am learning Python using the "Learn Python the Hard Way" book by Zed Shaw.  I reached exercise 42 where we learn about Python classes.  The exercise shows a game with one class that includes all the definitions for playing the game.  For extra credit we are asked to create another version of this game where we use two classes - one for the room definitions and the other for the playing the game.  I feel stumped and don't know how to go about creating this game with two classes.

So far I have searched for info on how to pass variables from one class to another and have been able to create a small two class program (attached).   But I seem unable to generalize from here and apply this to the game exercise.  What would you suggest for me to try next?  

Thank you,

Tonu 

Attachment (classes_test.py): application/octet-stream, 486 bytes
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Prasad, Ramit | 2 Feb 21:32 2012
Picon

Re: exercise with classes

>I am learning Python using the "Learn Python the Hard Way" book by Zed Shaw.  I reached exercise 42 where we
learn about Python classes.  The exercise shows a game with one class that includes all the definitions for
playing the game.  For extra credit we are asked to create another version of this game where we use two
classes - one for the room definitions and the other for the playing the game.  I feel stumped and don't know
how to go about creating this game with two classes.
> So far I have searched for info on how to pass variables from one class to another and have been able to create
a small two class program (attached).   But I seem unable to generalize from here and apply this to the game
exercise.  What would you suggest for me to try next?  

It is usually better to include short code directly in your email 
or link to something like pastebin for longer samples. Many of the people
on this list do access it from email and they do not get attachments.

As for the extra credit: You can store the possible actions 
in the room (dodge, shoot, etc.). So for central_corridor (an instance
of class Room) you have the player do something like 

action = raw_input('>)
consequence = central_corridor.do_action( action )
new_room = Game.getRoom( consequence ) 
# Might need to replace Game with self depending on where this code is located.
action = raw_input('>)
consequence = new_room.do_action( action )
...

Ramit

Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Brett Longworth | 2 Feb 21:50 2012

better looping construct for replacing elements in file?

Hello,

Today I wrote a quick script to replace elements in multiple lines of a 
file with corresponding elements from a single line in another file, 
linking the two via an index element. The code iterates through the 
entire source file to find the matching index for each line of the 
destination file. This works, but it's clearly a terrible way to solve 
the problem. Can someone point me to a more efficient, pythonic solution?

thanks,
-Brett

Code:

wheel = "A794"
wheelfile = "A794.txt"
statusfile = "CURRENT.ams"

sfile = csv.reader(open(statusfile), delimiter='\t')
statusWriter = csv.writer(open('statustest.txt', 'wb'), delimiter='\t', 
quotechar='|', quoting=csv.QUOTE_MINIMAL)

for sline in sfile:
   #print sline
   wfile = csv.reader(open(wheelfile))
   for line in wfile:
     #print line[0]
     #print sline[18]
     if line[0] == sline[18]:
       sline[0] = line [1]
       sline[1] = "OSG"+str(line[4])
       sline[17] = wheel
       sline[21] = line[9]
       statusWriter.writerow(sline)

Excerpt of wheelfile:

"2","X496","02/01/12","OSG","106788","85411","GS-13365","Outside Primary 
Standard | > Modern (1950)","2.43","149177"
"3","C655","02/01/12","OSG","106534","83028","HY-19231","Outside Blank | 
 > 30,000","3.63","149178"

Excerpt of statusfile:

Y002    BET2918    10/18/06 15:32:52    160.00    174    1.000    
16408    1.306E-12    1.213E-10    402.6    405.9    -42.7    3.2    
1.2242    -0.0220    1.822    -12.66    A499    2    1    5631    
86523    data    3.7E-6
Y002    BET2918    10/18/06 15:35:46    150.00    162    1.000    
15654    1.313E-12    1.226E-10    407.6    410.3    -43.9    2.0    
1.2180    -0.0243    1.894    -13.03    A499    2    1    5631    
86523        3.7E-6
0003    BET7147    10/18/06 15:55:33    170.00    186    1.000    
3442    2.903E-13    2.693E-11    357.7    359.3    -46.1    2.5    
1.2000    0.0276    1.734    -12.86    A499    3    1    5631    
86524        3.3E-6
0003    BET7147    10/18/06 15:58:49    170.00    185    1.000    
3232    2.772E-13    2.598E-11    351.8    353.4    -46.1    3.5    
1.2000    0.0149    1.761    -12.66    A499    3    1    5631    
86524        3.2E-6
0003    BET7147    10/18/06 16:02:06    170.00    185    1.000    
3399    2.955E-13    2.753E-11    346.9

--

-- 
Brett Longworth
Research Associate
Woods Hole Oceanographic Institution
ph: 508.289.3559
fax: 508.457.2183

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Joel Goldstick | 2 Feb 22:16 2012
Picon

Re: better looping construct for replacing elements in file?

On Thu, Feb 2, 2012 at 3:50 PM, Brett Longworth <blongworth <at> whoi.edu> wrote:
> Hello,
>
> Today I wrote a quick script to replace elements in multiple lines of a file
> with corresponding elements from a single line in another file, linking the
> two via an index element. The code iterates through the entire source file
> to find the matching index for each line of the destination file. This
> works, but it's clearly a terrible way to solve the problem. Can someone
> point me to a more efficient, pythonic solution?
>
> thanks,
> -Brett
>
> Code:
>
> wheel = "A794"
> wheelfile = "A794.txt"
> statusfile = "CURRENT.ams"
>
> sfile = csv.reader(open(statusfile), delimiter='\t')
> statusWriter = csv.writer(open('statustest.txt', 'wb'), delimiter='\t',
> quotechar='|', quoting=csv.QUOTE_MINIMAL)
>
> for sline in sfile:
>  #print sline
>  wfile = csv.reader(open(wheelfile))
>  for line in wfile:
>    #print line[0]
>    #print sline[18]
>    if line[0] == sline[18]:
>      sline[0] = line [1]
>      sline[1] = "OSG"+str(line[4])
>      sline[17] = wheel
>      sline[21] = line[9]
>      statusWriter.writerow(sline)
>
> Excerpt of wheelfile:
>
> "2","X496","02/01/12","OSG","106788","85411","GS-13365","Outside Primary
> Standard | > Modern (1950)","2.43","149177"
> "3","C655","02/01/12","OSG","106534","83028","HY-19231","Outside Blank | >
> 30,000","3.63","149178"
>
> Excerpt of statusfile:
>
> Y002    BET2918    10/18/06 15:32:52    160.00    174    1.000    16408
>  1.306E-12    1.213E-10    402.6    405.9    -42.7    3.2    1.2242
>  -0.0220    1.822    -12.66    A499    2    1    5631    86523    data
>  3.7E-6
> Y002    BET2918    10/18/06 15:35:46    150.00    162    1.000    15654
>  1.313E-12    1.226E-10    407.6    410.3    -43.9    2.0    1.2180
>  -0.0243    1.894    -13.03    A499    2    1    5631    86523        3.7E-6
> 0003    BET7147    10/18/06 15:55:33    170.00    186    1.000    3442
>  2.903E-13    2.693E-11    357.7    359.3    -46.1    2.5    1.2000
>  0.0276    1.734    -12.86    A499    3    1    5631    86524        3.3E-6
> 0003    BET7147    10/18/06 15:58:49    170.00    185    1.000    3232
>  2.772E-13    2.598E-11    351.8    353.4    -46.1    3.5    1.2000
>  0.0149    1.761    -12.66    A499    3    1    5631    86524        3.2E-6
> 0003    BET7147    10/18/06 16:02:06    170.00    185    1.000    3399
>  2.955E-13    2.753E-11    346.9
>
>
> --
> Brett Longworth
> Research Associate
> Woods Hole Oceanographic Institution
> ph: 508.289.3559
> fax: 508.457.2183
>
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

If you are a little savvy with sql you could write each csv to tables,
Since you have csv file input why not write each to a db table.  Then
you could join on  line[0] == sline[18] and update your 4 fields.

Then dump table as csv

--

-- 
Joel Goldstick
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Brett Longworth | 2 Feb 22:29 2012

Re: better looping construct for replacing elements in file?

Hi Joel,

Thanks for the reply. The little voice in my head was yelling, "Easier 
with SQL!" the entire time, but I'm trying to learn Python.

-Brett

On 2/2/2012 4:16 PM, Joel Goldstick wrote:
> On Thu, Feb 2, 2012 at 3:50 PM, Brett Longworth<blongworth <at> whoi.edu>  wrote:
>> Hello,
>>
>> Today I wrote a quick script to replace elements in multiple lines of a file
>> with corresponding elements from a single line in another file, linking the
>> two via an index element. The code iterates through the entire source file
>> to find the matching index for each line of the destination file. This
>> works, but it's clearly a terrible way to solve the problem. Can someone
>> point me to a more efficient, pythonic solution?
>>
>> thanks,
>> -Brett
>>
>> Code:
>>
>> wheel = "A794"
>> wheelfile = "A794.txt"
>> statusfile = "CURRENT.ams"
>>
>> sfile = csv.reader(open(statusfile), delimiter='\t')
>> statusWriter = csv.writer(open('statustest.txt', 'wb'), delimiter='\t',
>> quotechar='|', quoting=csv.QUOTE_MINIMAL)
>>
>> for sline in sfile:
>>   #print sline
>>   wfile = csv.reader(open(wheelfile))
>>   for line in wfile:
>>     #print line[0]
>>     #print sline[18]
>>     if line[0] == sline[18]:
>>       sline[0] = line [1]
>>       sline[1] = "OSG"+str(line[4])
>>       sline[17] = wheel
>>       sline[21] = line[9]
>>       statusWriter.writerow(sline)
>>
>> Excerpt of wheelfile:
>>
>> "2","X496","02/01/12","OSG","106788","85411","GS-13365","Outside Primary
>> Standard |>  Modern (1950)","2.43","149177"
>> "3","C655","02/01/12","OSG","106534","83028","HY-19231","Outside Blank |>
>> 30,000","3.63","149178"
>>
>> Excerpt of statusfile:
>>
>> Y002    BET2918    10/18/06 15:32:52    160.00    174    1.000    16408
>>   1.306E-12    1.213E-10    402.6    405.9    -42.7    3.2    1.2242
>>   -0.0220    1.822    -12.66    A499    2    1    5631    86523    data
>>   3.7E-6
>> Y002    BET2918    10/18/06 15:35:46    150.00    162    1.000    15654
>>   1.313E-12    1.226E-10    407.6    410.3    -43.9    2.0    1.2180
>>   -0.0243    1.894    -13.03    A499    2    1    5631    86523        3.7E-6
>> 0003    BET7147    10/18/06 15:55:33    170.00    186    1.000    3442
>>   2.903E-13    2.693E-11    357.7    359.3    -46.1    2.5    1.2000
>>   0.0276    1.734    -12.86    A499    3    1    5631    86524        3.3E-6
>> 0003    BET7147    10/18/06 15:58:49    170.00    185    1.000    3232
>>   2.772E-13    2.598E-11    351.8    353.4    -46.1    3.5    1.2000
>>   0.0149    1.761    -12.66    A499    3    1    5631    86524        3.2E-6
>> 0003    BET7147    10/18/06 16:02:06    170.00    185    1.000    3399
>>   2.955E-13    2.753E-11    346.9
>>
>>
>> --
>> Brett Longworth
>> Research Associate
>> Woods Hole Oceanographic Institution
>> ph: 508.289.3559
>> fax: 508.457.2183
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor <at> python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
> If you are a little savvy with sql you could write each csv to tables,
> Since you have csv file input why not write each to a db table.  Then
> you could join on  line[0] == sline[18] and update your 4 fields.
>
> Then dump table as csv
>

--

-- 
Brett Longworth
Research Associate
Woods Hole Oceanographic Institution
ph: 508.289.3559
fax: 508.457.2183

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Gmane