way to calculate 2**1000 without expanding it?

2011-09-16 Thread zombie
Hi guys,
 i am writing a program to sum up the digits of a number 2**1000?
Is there a way/formula to do it without expanding it?

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


Overloading "if object" unary operator

2006-11-29 Thread Sarcastic Zombie
Good morning,

If I have a class

class A:
__init__(id)
self.id = id

is there any way to overload the 'if' unary usage to detect if a
variable has a value?

For example, in the code:

a = A(56)
if a:
print "Hoo hah!"

how can I insure that the if will come back true and fire off the print
if and only if self.id is defined? I want to do this in an overloaded,
generic way, if possible; I know that I could test for a.id.

Thanks so much!

-Jason Ledbetter

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


Re: Overloading "if object" unary operator

2006-11-29 Thread Sarcastic Zombie


On Nov 29, 11:26 am, Peter Otten <[EMAIL PROTECTED]> wrote:
> Sarcastic Zombie wrote:
> > is there any way to overload the 'if' unary usage to detect if a
> > variable has a value?Define a __nonzero__() or __len__() method.
>
> Peter

Thanks to both of you, it worked perfectly. I must have missed it in
the documentation somehow.

-Jason Ledbetter

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


Over my head with descriptors

2006-12-14 Thread Sarcastic Zombie
Code included below.

Basically, I've created a series of "question" descriptors, which each
hold a managed value. This is so I can implement validation, and render
each field into html automatically for forms.

My problem is this: every instance of my "wizard" class has unique self
values, but they share the exact same descriptor values.

Meaning, if

t = Test("ar")
y = Test("ar")

t is y
False

t.age is y.age
True

t.age = 9
y.age
9

Code below. What am I not understanding?
-

import datetime, re

class Question(object):
def __init__(self, qtext, name, default=None, required=False,
max_length=None, choices=None):
self._name = name
self._qtext = qtext
self._value = default
self._error = None
self._max_length = max_length
self._required = required
self._choices = choices

def __get__(self, instance, owner):
return self

def __set__(self, instance, value):
error = self.validate(value)
if not error:
self._value = self.cast(value)
self._error = None
else:
self._value = value
self._error = error
print error

def __str__(self):
return str(self._value)

def __repr__(self):
return str(self._value)

def cast(self, value):
return value

def validate(self, value):
return True

def html(self):
#ugly html renderer removed; irrelevant to problem
return html

def error(self):
if self._error:
return True


class Q_Integer(Question):
def validate(self, value):
if self._required and not value:
return "Field is required."
elif not value:
return None
try:
int(value)
return None
except:
return "Answer must be a whole number."

def cast(self, value):
if value:
return int(value)

class Q_Float(Question):
def validate(self, value):
if self._required and not value:
return "Field is required."
elif not value:
return None
try:
float(value)
return None
except:
return "Answer must be a decimal number."

def cast(self, value):
if value:
return float(value)

class Q_Chars(Question):
def validate(self, value):
try:
if self._required and not value:
return "Field is required."
elif not value:
return None
if self._max_length:
if len(value) > self._max_length:
return "Too many characters; max of %s 
allowed." %
self._max_length
return None
except:
return "Invalid entry."

def cast(self, value):
if value:
return str(value)

class Q_Long(Question):
def validate(self, value):
try:
if self._required and not value:
return "Field is required."
elif not value:
return None
except:
return "Invalid entry."

def cast(self, value):
if value:
return str(value)

def html(self):
#ugly html renderer removed; irrelevant to problem
return html

class Q_Bool(Question):
def validate(self, value):
return None

def cast(self, value):
return bool(value)

def html(self):
#ugly html renderer removed; irrelevant to problem
return html

class Q_Phone(Question):
def validate(self, value):
try:
if self._required and not value:
return "Field is required."
elif not value:
return None

pieces = value.split("-")
if len(pieces[0]) == 3 and len(pieces[1]) == 3 and 
len(pieces[2]) ==
4:
int(pieces[0])
int(pieces[1])
  

Re: Over my head with descriptors

2006-12-18 Thread Sarcastic Zombie
> > Is there a reason you don't just use __init__ instead of __new__, and use
> > "self.age" and "self.weight" and so on?I was asking myself the same thing...
>
> Chris

"A lack of understanding," he answered sheepishly.

There are attributes (ie, question._qtext) that I do want to be the
same for every instance and thus don't want the extra memory overhead
of storing it anew for each instance.

Using the "instance" reference as you guys suggested, I'm now storing
the instance-specific information that the descriptors validate by
cramming it into instance._foo. Works great now!

Thanks for the additional understanding. It's a lot to wrap one's head
around at first.

-SZ

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