Class Methods help

2009-05-31 Thread bdsatish
Hi,

I have a question regarding the difference b/w "class methods" and
"object methods". Consider for example:

class  MyClass:
x = 10

Now I can access MyClass.x -- I want a similar thing for functions. I
tried

class MyClass:
def  some_func(x):
return x+2

When I call MyClass.some_func(10) -- it fails, with error message:


TypeError: unbound method some_func() must be called with MyClass
instance as first argument (got int instance instead)

OK. I figured out that something like this works:
obj = MyClass()
y = obj.some_func(10)

BUT, this means that we have functions applying for instances. That is
we have "instance method". Now, how do I implement some function which
I can invoke with the class name itself ? Instead of creating a dummy
object & then calling In short, how exactly do I create "class
methods" ??







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


Integer dicision

2008-04-10 Thread bdsatish
How does (a/b) work when both 'a' and 'b' are pure integers ?

>> (9/2)
4

>> (-9/2)
-5

Why is it -5 ? I expect it to be -4 ? Because, in C/C++, 9/2 is 4 and
so negative of it, (-9/2) is -4.

What should I do to get C-like behavior ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Rounding a number to nearest even

2008-04-11 Thread bdsatish
The built-in function round( ) will always "round up", that is 1.5 is
rounded to 2.0 and 2.5 is rounded to 3.0.

If I want to round to the nearest even, that is

my_round(1.5) = 2# As expected
my_round(2.5) = 2# Not 3, which is an odd num

I'm interested in rounding numbers of the form "x.5" depending upon
whether x is odd or even. Any idea about how to implement it ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 3:27 pm, [EMAIL PROTECTED] wrote:
> On 11 avr, 12:14, bdsatish <[EMAIL PROTECTED]> wrote:
>
> > The built-in function round( ) will always "round up", that is 1.5 is
> > rounded to 2.0 and 2.5 is rounded to 3.0.
>
> > If I want to round to the nearest even, that is
>
> > my_round(1.5) = 2# As expected
> > my_round(2.5) = 2# Not 3, which is an odd num
>
> > I'm interested in rounding numbers of the form "x.5" depending upon
> > whether x is odd or even. Any idea about how to implement it ?
>
> When you say "round to the nearest even", you mean new_round(3) <> 3?

No. not at all. The clause "nearest even" comes into picture only when
a number is of form "x.5" or else it's same as builtin round( ).
new_round(3.0) must be 3.0 itself. Here is the mathematical definition
of what I want:

If 'n' is an integer,

new_round(n+0.5) = n  if  n/2 is integer
new_round(n+0.5) = (n+1) if  (n+1)/2 is integer

In all other cases, new_round() behave similarly as round( ). Here are
the results I expect:

new_round(3.2) = 3
new_round(3.6) = 4
new_round(3.5) = 4
new_round(2.5) = 2
new_round(-0.5) = 0.0
new_round(-1.5) = -2.0
new_round(-1.3) = -1.0
new_round(-1.8) = -2
new_round(-2.5) =  -2.0

The built-in function doesnt meet my needs for round(-2.5) or
round(2.5)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:37 pm, Scott David Daniels <[EMAIL PROTECTED]> wrote:
> bdsatish wrote:
> > The built-in function round( ) will always "round up", that is 1.5 is
> def rounded(v):
>  rounded = round(v)
>  if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1:
> if v > 0:
> return rounded - 1
> return rounded + 1
>  return rounded
>
> last = None
> for n in range(-29, 28):
>  x = n * .25
>  r = xr(x)
>  if r != last:
> last = r
> print
>  print '%s->%s' % (x, xr(x)),
>

Hi Scott,
This is what I was looking for.. I forgot about divmod( ) thanks for
reminding.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:19 pm, [EMAIL PROTECTED] wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
>   answer = round(n)
>   # is answer now odd
>   if answer % 2:
> return answer - 1
>   else:
> return answer

It fails for negative numbers: For -2.5 it gives -4.0 as answer
whereas I expect -2.0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 4:24 pm, [EMAIL PROTECTED] wrote:
> On Apr 11, 1:19 pm, [EMAIL PROTECTED] wrote:
>
> > couldn't you just do.
>
> > #untested
> > new_round(n):
> >   answer = round(n)
> >   # is answer now odd
> >   if answer % 2:
> > return answer - 1
> >   else:
> > return answer
>
> Whoops, this also affects odd numbers...
>
> Will try and find a GOOD solution later...
>
> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...

It also fails for negative numbers. For -2.5 as input, I get -4.0
whereas I expect -2.0

This is a lengthy solution I came-up with:

def round_even(x):
temp = round(abs(x))
if (abs(x) - 0.5)%2.0 == 0.0:  temp=temp-1
return signum(x)*temp

def signum(x):
if x>0: return 1
if x<0: return -1
return 0

But i guess there are better ways. I need it 'cos I'm translating some
code from Mathematica to Python. And Math..ica's Round[ ] behaves this
way (as I requested)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
HI Gerard,

I think you've taken it to the best possible implementation. Thanks !
On Apr 11, 5:14 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote:
> In fact you can avoid the call to the builtin round:
>
> 
> def myround(x):
> n = int(x)
> if abs(x - n) >= 0.5 and n % 2:
> return n + 1 - 2 * int(n<0)
> else:
> return n
>
> assert myround(3.2) == 3
> assert myround(3.6) == 4
> assert myround(3.5) == 4
> assert myround(2.5) == 2
> assert myround(-0.5) == 0.0
> assert myround(-1.5) == -2.0
> assert myround(-1.3) == -1.0
> assert myround(-1.8) == -2
> assert myround(-2.5) ==  -2.0
> 

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


Re: Rounding a number to nearest even

2008-04-11 Thread bdsatish
On Apr 11, 5:33 pm, bdsatish <[EMAIL PROTECTED]> wrote:
> HI Gerard,
>
> I think you've taken it to the best possible implementation. Thanks !
> On Apr 11, 5:14 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote:
>
> > In fact you can avoid the call to the builtin round:
>
> > 
>
> > assert myround(3.2) == 3
> > assert myround(3.6) == 4
> > assert myround(3.5) == 4
> > assert myround(2.5) == 2
> > assert myround(-0.5) == 0.0
> > assert myround(-1.5) == -2.0
> > assert myround(-1.3) == -1.0
> > assert myround(-1.8) == -2
> > assert myround(-2.5) ==  -2.0
> > 

OK, I was  too early to praise Gerard. The following version:

def myround(x):
 n = int(x)
 if abs(x - n) >= 0.5 and n % 2:
  return n + 1 - 2 * int(n<0)
 else:
  return n

of Gerard doesn't work for 0.6 (or 0.7, etc.) It gives the answer 0
but I would expect 1.0 ( because 0.6 doesn't end in 0.5 at all... so
usual rules of round( ) apply)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Java or C++?

2008-04-14 Thread bdsatish
On Apr 14, 12:21 pm, Bob Martin <[EMAIL PROTECTED]> wrote:
> in 342367 20080414 074410 [EMAIL PROTECTED] wrote:
>
> >Hello, I was hoping to get some opinions on a subject. I've been
> >programming Python for almost two years now. Recently I learned Perl,
> >but frankly I'm not very comfortable with it. Now I want to move on
> >two either Java or C++, but I'm not sure which. Which one do you think
> >is a softer transition for a Python programmer? Which one do you think
> >will educate me the best?
>

Certainly Java. It's also easier to find Java jobs than C++ jobs.

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


restructured text in python

2008-04-24 Thread bdsatish
Hi all,

I have a python prog:


#!/usr/bin/env
"""
Author: BDS
Version: 1.0
"""

def  Hello():
""" Prints a Hello World to the screen"""
print "Hello, World"

if __name__ == "__main__":
Hello()

I want to use ReSt (reStructuredText) in my docstrings or comments.
Any example of how to do it, w.r.t. above code ? Also how to invoke
rst on the Python code to do the processing ?

I know how to write in  RST but I want to know how to process the
resulting Python code
--
http://mail.python.org/mailman/listinfo/python-list