We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,503 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Catching string argument to initialization to int/long subclass

I would like to take string argument for class BalancedTrinary intializer, but the string type of argument is processed before it enters the init function. I guess I must override any class method which deals with those for int/long?

def val(number_sequence, base):
    """ produce integer value of number_sequence as base number """
    print number_sequence
    return sum(int(n) * base ** p for p, n in enumerate(reversed(number_sequence)))

def balanced_ternary_value(number):
    """ produce integer value of balanced ternary number """
    #print 'balanced_ternary_value', number, type(number)
    return val([(-1 if c == '-' else (1 if c == '+' else 0))
                for c in str(number)], 3)

def make_balanced(n):
    n = int(n)
    if not n:
        return '0'
    rest, this = divmod(n, 3)
    if this < 2:
        return (make_balanced(rest) if rest else '') + ('0+'[this]) 
    else:
        rest = (n+1) // 3
        return (make_balanced(rest) if rest else '') + '-'

class BalancedTernary(long):
    def __init__(self, n):
        long.__init__(balanced_ternary_value(n)
                      if isinstance(n, str) else n)

    def __repr__(self):
        return make_balanced(self)

    __str__ = __repr__


if __name__  == '__main__':
    print('%10s %10s' % ('dec', 'balanced ternary'))
    for t in range(-27,+28):
        print('%10s %10s' % (t, BalancedTernary(t)))

    print balanced_ternary_value('++-0+')
    print BalancedTernary('++-0+')

ERROR MESSAGE

Traceback (most recent call last):
  File "I:\test\balanced.py", line 40, in <module>
    print BalancedTernary('++-0+')
ValueError: invalid literal for long() with base 10: '++-0+'
2
Contributors
1
Reply
6 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
pyTony
pyMod
Moderator
6,314 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26

This is a typical use case for the __new__() method:

class BalancedTernary(long):
    def __new__(cls, n):
        instance = long.__new__(cls,
            balanced_ternary_value(n) if isinstance(n, str) else n)
        return instance

    def __repr__(self):
        return make_balanced(self)

    __str__ = __repr__

Your code seems to work now.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Question Answered as of 1 Year Ago by Gribouillis

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0660 seconds using 2.85MB