mel
mel

Reputation: 2790

Get random element from attributes in an object

I have an object which got 16 double attributes and I want to pick one of them randomly so I create a method which create a random number between 0-15, but this method is not really what I want I think it's a pretty dirty method if you guys got another cleanest method

class Currencies(object):
    EURToUSD = 0.0
    EURToJPY = 0.0
    EURToBTC = 0.0
    USDToEUR = 0.0
    USDToBTC = 0.0
    USDToJPY = 0.0
    BTCToEUR = 0.0
    BTCToJPY = 0.0
    BTCToUSD = 0.0
    JPYToEUR = 0.0
    JPYToUSD = 0.0
    JPYToBTC = 0.0
    EURToEUR = 0.0
    JPYToJPY = 0.0
    USDToUSD = 0.0
    BTCToBTC = 0.0


    def __init__ (self):
        try:
            rates = urllib2.urlopen("http://fx.priceonomics.com/v1/rates/")
        except urllib2.URLError as e:
            return e.reasonx
        res = json.load(rates)
        self.EURToEUR = 1.000000
        self.USDToUSD = 1.000000
        self.JPYToJPY = 1.000000
        self.BTCToBTC = 1.000000
        self.EURToUSD = res['EUR_USD']
        self.EURToJPY = res['EUR_JPY']
        self.EURToBTC = res['EUR_BTC']
        self.USDToEUR = res['USD_EUR']
        self.USDToBTC = res['USD_BTC']
        self.USDToJPY = res['USD_JPY']
        self.BTCToEUR = res['BTC_EUR']
        self.BTCToJPY = res['BTC_JPY']
        self.BTCToUSD = res['BTC_USD']
        self.JPYToEUR = res['JPY_EUR']
        self.JPYToUSD = res['JPY_USD']
        self.JPYToBTC = res['JPY_BTC']

    def getRandomRate():
        randomNumber = randint(0,15)
        if(randomNumber == 1):
            return EURToUSD = 0.0
        if(...)

Upvotes: 0

Views: 743

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122272

You could just select a random attribute from the vars(self) dictionary, filtering on names that match a pattern:

def getRandomRate(self):
    return random.choice([v for attr, v in vars(self).items()
                          if len(attr) == 8 and attr[3:5] == 'To'])

This picks a random value from all attributes whose name is 8 characters long and contain the word To in the middle.

A short demo using your class:

>>> import random
>>> c = Currencies()
>>> vars(c)
{'EURToBTC': u'0.0108027', 'BTCToJPY': u'12816.8350063', 'JPYToJPY': 1.0, 'USDToBTC': u'0.0094131', 'JPYToBTC': u'0.0000702', 'BTCToUSD': u'125.2057142', 'USDToUSD': 1.0, 'USDToJPY': u'116.1736146', 'EURToEUR': 1.0, 'JPYToUSD': u'0.0084464', 'BTCToEUR': u'92.3549138', 'USDToEUR': u'0.8820255', 'BTCToBTC': 1.0, 'JPYToEUR': u'0.0065705', 'EURToUSD': u'1.2338648', 'EURToJPY': u'126.4193644'}
>>> [v for attr, v in vars(c).items() if len(attr) == 8 and attr[3:5] == 'To']
[u'0.0108027', u'12816.8350063', 1.0, u'0.0094131', u'0.0000702', u'125.2057142', 1.0, u'116.1736146', 1.0, u'0.0084464', u'92.3549138', u'0.8820255', 1.0, u'0.0065705', u'1.2338648', u'126.4193644']
>>> random.choice([v for attr, v in vars(c).items() if len(attr) == 8 and attr[3:5] == 'To'])
u'0.0108027'

So the list comprehension extracted the 16 values for each of the conversion rates, and random.choice() then picks one of those rates at random.

Upvotes: 3

Related Questions