Re: Command parsing... best module to use?

2009-11-03 Thread Collin D
Thanks for the replies. Pyparsing looks just like what I need.

-- 
http://mail.python.org/mailman/listinfo/python-list


Command parsing... best module to use?

2009-11-02 Thread Collin D
Hey everyone.

I am writing a game in python, and it includes a text console somewhat
like the one in WoW and Runescape. I want to be able to include /
commands, like IRC, and was wondering what the best module would be to
parse these.

Thanks a lot,
Collin D
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter - problem closing window

2009-01-05 Thread Collin D
On Jan 5, 6:25 am, Djames Suhanko djames.suha...@gmail.com wrote:
 Hello!
 I'm sorry my terrible english (my native language is portuguese).
 I has a litle program that open another window. When I close de root
 window in quit button, I need clicking 2 times to close. is where the
 problem?

 The source:
   1 #!/usr/bin/env python
   2 from Tkinter import *
   3 import sys
   4 import random
   5 class App:
   6  def __init__(self, master):
   7    frame = Frame(master)
   8    frame.pack()
   9    rotulo = Label(frame, text=Clique em 'Gerar' e boa
 sorte!,borderwidth=2,bg=gray,justify=C    ENTER,relief=SUNKEN)
  10    rotulo.pack()
  11
  12    self.button = Button(frame, text=Sair, fg=red,
 command=frame.quit,borderwidth=1)
  13    self.button.pack(side=LEFT)
  14    self.hi_there = Button(frame, text=Gerar Numero,
 command=self.say_hi,borderwidth=1)
  15    self.hi_there.pack(side=RIGHT,padx=2,pady=2)
  16
  17  def gera_seis(self):
  18    a = {}
  19    for i in range(6):
  20       a[i] = %02d %  int (random.randint(0,60))
  21    resultadoA = %s-%s-%s-%s-%s-%s %
 (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
  22    return resultadoA
  23
  24  def say_hi(self):
  25    resultado = self.gera_seis()
  26    raiz = Tk()
  27    F = Frame(raiz)
  28    F.pack()
  29    hello = Label(F, text=resultado)
  30    hello.pack()
  31    F.mainloop()
  32
  33 root = Tk()
  34 root.title($$$ Loteria $$$)
  35 app = App(root)
  36 root.mainloop()

 --
 Djames Suhanko
 LinuxUser 158.760

Also for style, you might want to group the import lines so they look
like this:

from Tkinter import *
import sys, random

A bit more pythonic. :P
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter - problem closing window

2009-01-05 Thread Collin D
On Jan 5, 9:21 am, Roger rdcol...@gmail.com wrote:
 On Jan 5, 11:52 am, Collin D collin.da...@gmail.com wrote:



  On Jan 5, 6:25 am, Djames Suhanko djames.suha...@gmail.com wrote:

   Hello!
   I'm sorry my terrible english (my native language is portuguese).
   I has a litle program that open another window. When I close de root
   window in quit button, I need clicking 2 times to close. is where the
   problem?

   The source:
     1 #!/usr/bin/env python
     2 from Tkinter import *
     3 import sys
     4 import random
     5 class App:
     6  def __init__(self, master):
     7    frame = Frame(master)
     8    frame.pack()
     9    rotulo = Label(frame, text=Clique em 'Gerar' e boa
   sorte!,borderwidth=2,bg=gray,justify=C    ENTER,relief=SUNKEN)
    10    rotulo.pack()
    11
    12    self.button = Button(frame, text=Sair, fg=red,
   command=frame.quit,borderwidth=1)
    13    self.button.pack(side=LEFT)
    14    self.hi_there = Button(frame, text=Gerar Numero,
   command=self.say_hi,borderwidth=1)
    15    self.hi_there.pack(side=RIGHT,padx=2,pady=2)
    16
    17  def gera_seis(self):
    18    a = {}
    19    for i in range(6):
    20       a[i] = %02d %  int (random.randint(0,60))
    21    resultadoA = %s-%s-%s-%s-%s-%s %
   (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
    22    return resultadoA
    23
    24  def say_hi(self):
    25    resultado = self.gera_seis()
    26    raiz = Tk()
    27    F = Frame(raiz)
    28    F.pack()
    29    hello = Label(F, text=resultado)
    30    hello.pack()
    31    F.mainloop()
    32
    33 root = Tk()
    34 root.title($$$ Loteria $$$)
    35 app = App(root)
    36 root.mainloop()

   --
   Djames Suhanko
   LinuxUser 158.760

  Also for style, you might want to group the import lines so they look
  like this:

  from Tkinter import *
  import sys, random

  A bit more pythonic. :P

 In that case you probably want to take out the 'from' import and:

 import Tkinter, sys, random

 in order to avoid any namespace issues especially if you have a large
 project with lots of gui manipulations.  But that's just me being
 pedantic. ;)

I agree... you could have conflicting functions.. not fun. XD
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 11:52 am, eric e...@ericaro.net wrote:
 On Dec 18, 8:37 pm, collin.da...@gmail.com wrote:

  I am trying to write a simple application to factor polynomials. I
  wrote (simple) raw_input lines to collect the a, b, and c values from
  the user, but I dont know how to implement the quadratic equation

  x = (-b +or- (b^2 - 4ac)^1/2) / 2a

  into python. Any ideas?

 with numpy:
 from numpy import *

 s=[1,-1]
 x = -b+s*sqrt( b**2-4*a*c )/(2*a)

 Erichttp://codeslash.blogspot.com

Ahh. Great.. thank you. I didnt know about the sqrt function.. saves
me from doing ^1/2. Thanks again.
-CD
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 1:09 pm, Mark Dickinson dicki...@gmail.com wrote:
 On Dec 18, 8:47 pm, Scott David Daniels scott.dani...@acm.org wrote:

       else: # a single result (discriminant is zero)
           return (-b / (2 * a),)

 Maybe make that (-b / (2. * a)) to avoid getting funny results
 when a and b are integers.  (Or do a from __future__ import
 division, or use Python 3.0, or )

 And to make the function more bullet-proof, you might want to
 do something like (untested):

     from math import copysign

     [rest of example as in Scott's post]

     if discriminant: # two results
         root1 = (-b - copysign(discriminant, b))/(2*a)
         root2 = c/(a*root1)
         return (root1, root2)

 to avoid numerical problems when b*b is much larger
 than abs(a*c). Compare with the results of the usual
 formula when a = c = 1, b = 10**9, for example.  But
 that still doesn't help you when the computation
 of the discriminant underflows or overflows...

 Isn't floating-point a wonderful thing!  :)

 Mark

Thanks for all your help! Its good to know how to do it w/ without
numpy.
And yes, floating point is the best thing since sliced bread. ^^

-CD
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 4:41 pm, James Mills prolo...@shortcircuit.net.au
wrote:
 On Fri, Dec 19, 2008 at 10:11 AM, Gabriel Genellina

 gagsl-...@yahoo.com.ar wrote:
  En Thu, 18 Dec 2008 17:37:35 -0200, collin.da...@gmail.com escribió:

  I am trying to write a simple application to factor polynomials. I
  wrote (simple) raw_input lines to collect the a, b, and c values from
  the user, but I dont know how to implement the quadratic equation

  x = (-b +or- (b^2 - 4ac)^1/2) / 2a

 Why is this so hard ? This is simple simple
 expression. Reading through the Python
 tutorial and reading up on how to define
 functions is all you need! :)

 Here goes:

  def f(a, b, c):

 ...     x = (-1 * b) + ((b**2 - (4 * a * c)) / (2 * a))
 ...     return (-1 * x), x
 ...

  f(1, 2, 3)
 (6, -6)

 cheers
 James

Hiya James!

Just one small problem with your equation above...
The quadratic formula is:
x = -b +or- (b**2 - (4 * a * c))^1/2 / 2a

You just forgot the square root which makes quadratic a bit more
complicated.
You would have to download and import sqrt() from numpy or **.5

Also.. I need to build in functionality so the user does not have to
directly call the function like:
f(a,b,c)

Instead.. they should be able to just raw_input their values.
Also.. as with some of the examples above its a good idea to analyze
the discriminant to make sure we have a real solution.
Of course.. thats all pretty simple to build in. Thanks a lot!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 5:30 pm, James Mills prolo...@shortcircuit.net.au
wrote:
 UPDATE:

 jmi...@atomant:~/tmp$ cat polycalc.py
 #!/usr/bin/env python

 from math import sqrt

 def f(a, b, c):
     if (b**2 - (4 * a * c))  0:
         return None, None # Can't solve
     x1 = -b - (sqrt(b**2 - (4 * a * c)) / (2 * a))
     x2 = -b + (sqrt(b**2 - (4 * a * c)) / (2 * a))
     return x1, x2

 print Polynomial Solver...
 print

 while True:
     a = float(raw_input(a: ))
     b = float(raw_input(b: ))
     c = float(raw_input(c: ))

     x = f(a, b, c)
     if None in x:
         print Can't solve!
     else:
         print x = (%0.2f, %0.2f) % x
 jmi...@atomant:~/tmp$ ./polycalc.py
 Polynomial Solver...

 a: 1
 b: 8
 c: 5
 x = (-11.32, -4.68)

Ahh. Great.. that answers a lot of questions.
Originally I was using just a = raw_input('a: ')
And was getting errors because you cant perform mathmatical operations
on strings. .
Thanks again!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 5:10 pm, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Thu, 18 Dec 2008 11:37:35 -0800, collin.day.0 wrote:
  I am trying to write a simple application to factor polynomials. I wrote
  (simple) raw_input lines to collect the a, b, and c values from the
  user, but I dont know how to implement the quadratic equation

  x = (-b +or- (b^2 - 4ac)^1/2) / 2a

  into python. Any ideas?

 def quadratic_solution(a, b, c):
     sol1 = (-b + (b**2 - 4*a*c)**0.5)/2*a
     sol2 = (-b - (b**2 - 4*a*c)**0.5)/2*a
     return (sol1, sol2)

 Because this looks like homework, I've deliberately left in two errors in
 the above. One of them is duplicated in the two lines above the return,
 and you must fix it or you'll get radically wrong answers.

 The second is more subtle, and quite frankly if this is homework you
 could probably leave it in and probably not even lose marks. You will
 need to do significant research into numerical methods to learn what it
 is, but you will then get significantly more accurate results.

 --
 Steven

The corrected function is:
def quadratic_solution(a,b,c)
sol1 = -1*b + ((b**2 - 4*a*c)**.5)/2*a
sol1 = -1*b - ((b**2 - 4*a*c)**.5)/2*a
return (sol1, sol2)

Squaring the -b would give you some strange solutions :D

-CD
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 11:37 am, collin.da...@gmail.com wrote:
 I am trying to write a simple application to factor polynomials. I
 wrote (simple) raw_input lines to collect the a, b, and c values from
 the user, but I dont know how to implement the quadratic equation

 x = (-b +or- (b^2 - 4ac)^1/2) / 2a

 into python. Any ideas?

I completed the code:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
if b**2 - 4*a*c  0:
return 'No real solution.'
else:
sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
return (sol1, sol2)

# execute
print solver(a,b,c)

Thanks to everyone who helped...
This really expanded my knowledge on some of the mathematical
functions in Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 6:12 pm, Collin D collin.da...@gmail.com wrote:
 On Dec 18, 11:37 am, collin.da...@gmail.com wrote:

  I am trying to write a simple application to factor polynomials. I
  wrote (simple) raw_input lines to collect the a, b, and c values from
  the user, but I dont know how to implement the quadratic equation

  x = (-b +or- (b^2 - 4ac)^1/2) / 2a

  into python. Any ideas?

 I completed the code:

 #import
 from math import sqrt

 # collect data
 a = float(raw_input('Type a value: '))
 b = float(raw_input('Type b value: '))
 c = float(raw_input('Type c value: '))

 # create solver
 def solver(a,b,c):
     if b**2 - 4*a*c  0:
         return 'No real solution.'
     else:
         sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
         sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
         return (sol1, sol2)

 # execute
 print solver(a,b,c)

 Thanks to everyone who helped...
 This really expanded my knowledge on some of the mathematical
 functions in Python.

UPDATE:
'

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
if b**2 - 4*a*c  0:
return 'No real solution.'
else:
sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
return (sol1, sol2)

# execute
print solver(a,b,c)

--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 6:23 pm, Russ P. russ.paie...@gmail.com wrote:
 On Dec 18, 6:17 pm, Collin D collin.da...@gmail.com wrote:





  On Dec 18, 6:12 pm, Collin D collin.da...@gmail.com wrote:

   On Dec 18, 11:37 am, collin.da...@gmail.com wrote:

I am trying to write a simple application to factor polynomials. I
wrote (simple) raw_input lines to collect the a, b, and c values from
the user, but I dont know how to implement the quadratic equation

x = (-b +or- (b^2 - 4ac)^1/2) / 2a

into python. Any ideas?

   I completed the code:

   #import
   from math import sqrt

   # collect data
   a = float(raw_input('Type a value: '))
   b = float(raw_input('Type b value: '))
   c = float(raw_input('Type c value: '))

   # create solver
   def solver(a,b,c):
       if b**2 - 4*a*c  0:
           return 'No real solution.'
       else:
           sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
           sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
           return (sol1, sol2)

   # execute
   print solver(a,b,c)

   Thanks to everyone who helped...
   This really expanded my knowledge on some of the mathematical
   functions in Python.

  UPDATE:
  '

  #import
  from math import sqrt

  # collect data
  a = float(raw_input('Type a value: '))
  b = float(raw_input('Type b value: '))
  c = float(raw_input('Type c value: '))

  # create solver
  def solver(a,b,c):
      if b**2 - 4*a*c  0:
          return 'No real solution.'
      else:
          sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
          sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
          return (sol1, sol2)

  # execute
  print solver(a,b,c)

 You need to put your denominator, 2*a, in parens. The way it stands,
 you are dividing by 2, then multiplying by a. That's not what you
 want.

 Also, for better style, I suggest you compute the discriminanat once
 and store it for reuse rather than repeating the expression three
 times.- Hide quoted text -

 - Show quoted text -

I see what you mean on the denominator and discriminant. Ill do that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 6:27 pm, Collin D collin.da...@gmail.com wrote:
 On Dec 18, 6:23 pm, Russ P. russ.paie...@gmail.com wrote:





  On Dec 18, 6:17 pm, Collin D collin.da...@gmail.com wrote:

   On Dec 18, 6:12 pm, Collin D collin.da...@gmail.com wrote:

On Dec 18, 11:37 am, collin.da...@gmail.com wrote:

 I am trying to write a simple application to factor polynomials. I
 wrote (simple) raw_input lines to collect the a, b, and c values from
 the user, but I dont know how to implement the quadratic equation

 x = (-b +or- (b^2 - 4ac)^1/2) / 2a

 into python. Any ideas?

I completed the code:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
    if b**2 - 4*a*c  0:
        return 'No real solution.'
    else:
        sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
        sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
        return (sol1, sol2)

# execute
print solver(a,b,c)

Thanks to everyone who helped...
This really expanded my knowledge on some of the mathematical
functions in Python.

   UPDATE:
   '

   #import
   from math import sqrt

   # collect data
   a = float(raw_input('Type a value: '))
   b = float(raw_input('Type b value: '))
   c = float(raw_input('Type c value: '))

   # create solver
   def solver(a,b,c):
       if b**2 - 4*a*c  0:
           return 'No real solution.'
       else:
           sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
           sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
           return (sol1, sol2)

   # execute
   print solver(a,b,c)

  You need to put your denominator, 2*a, in parens. The way it stands,
  you are dividing by 2, then multiplying by a. That's not what you
  want.

  Also, for better style, I suggest you compute the discriminanat once
  and store it for reuse rather than repeating the expression three
  times.- Hide quoted text -

  - Show quoted text -

 I see what you mean on the denominator and discriminant. Ill do that.- Hide 
 quoted text -

 - Show quoted text -

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# find discriminant
disc = b**2 - 4*a*c

# create solver
def solver(a,b,c):
if disc  0:
return 'No real solution.'
else:
sol1 = (-1 * b + (sqrt(disc))) / (2*a)
sol2 = (-1 * b - (sqrt(disc))) / (2*a)
return (sol1, sol2)

# execute
print solver(a,b,c)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Factoring Polynomials

2008-12-18 Thread Collin D
On Dec 18, 6:41 pm, Russ P. russ.paie...@gmail.com wrote:
 On Dec 18, 6:31 pm, Collin D collin.da...@gmail.com wrote:





  On Dec 18, 6:27 pm, Collin D collin.da...@gmail.com wrote:

   On Dec 18, 6:23 pm, Russ P. russ.paie...@gmail.com wrote:

On Dec 18, 6:17 pm, Collin D collin.da...@gmail.com wrote:

 On Dec 18, 6:12 pm, Collin D collin.da...@gmail.com wrote:

  On Dec 18, 11:37 am, collin.da...@gmail.com wrote:

   I am trying to write a simple application to factor polynomials. I
   wrote (simple) raw_input lines to collect the a, b, and c values 
   from
   the user, but I dont know how to implement the quadratic equation

   x = (-b +or- (b^2 - 4ac)^1/2) / 2a

   into python. Any ideas?

  I completed the code:

  #import
  from math import sqrt

  # collect data
  a = float(raw_input('Type a value: '))
  b = float(raw_input('Type b value: '))
  c = float(raw_input('Type c value: '))

  # create solver
  def solver(a,b,c):
      if b**2 - 4*a*c  0:
          return 'No real solution.'
      else:
          sol1 = -1 * b + (sqrt(b**2 - 4*a*c)) / 2*a
          sol2 = -1 * b - (sqrt(b**2 - 4*a*c)) / 2*a
          return (sol1, sol2)

  # execute
  print solver(a,b,c)

  Thanks to everyone who helped...
  This really expanded my knowledge on some of the mathematical
  functions in Python.

 UPDATE:
 '

 #import
 from math import sqrt

 # collect data
 a = float(raw_input('Type a value: '))
 b = float(raw_input('Type b value: '))
 c = float(raw_input('Type c value: '))

 # create solver
 def solver(a,b,c):
     if b**2 - 4*a*c  0:
         return 'No real solution.'
     else:
         sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
         sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
         return (sol1, sol2)

 # execute
 print solver(a,b,c)

You need to put your denominator, 2*a, in parens. The way it stands,
you are dividing by 2, then multiplying by a. That's not what you
want.

Also, for better style, I suggest you compute the discriminanat once
and store it for reuse rather than repeating the expression three
times.- Hide quoted text -

- Show quoted text -

   I see what you mean on the denominator and discriminant. Ill do that.- 
   Hide quoted text -

   - Show quoted text -

  UPDATE:

  #import
  from math import sqrt

  # collect data
  a = float(raw_input('Type a value: '))
  b = float(raw_input('Type b value: '))
  c = float(raw_input('Type c value: '))

  # find discriminant
  disc = b**2 - 4*a*c

  # create solver
  def solver(a,b,c):
      if disc  0:
          return 'No real solution.'
      else:
          sol1 = (-1 * b + (sqrt(disc))) / (2*a)
          sol2 = (-1 * b - (sqrt(disc))) / (2*a)
          return (sol1, sol2)

  # execute
  print solver(a,b,c)

 A couple of style points. I would use -b rather than -1 * b. Also, you
 don't need the else clause. You can simplify it to

 def solver(a, b, c):

     disc = b**2 - 4 * a * c

     if disc  0: return No real solution.

     sol1 = (-b + sqrt(disc)) / (2*a)
     sol2 = (-b - sqrt(disc)) / (2*a)

     return sol1, sol2- Hide quoted text -

 - Show quoted text -

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
disc = b**2 - 4*a*c
if disc  0:
return 'No real solution.'
else:
sol1 = (-b + (sqrt(disc))) / (2*a)
sol2 = (-b - (sqrt(disc))) / (2*a)
return (sol1, sol2)

# execute
print solver(a,b,c)
--
http://mail.python.org/mailman/listinfo/python-list