0

I am trying to implement alternate to switch functionality in Python.

Example:

>>> def f(x):
        return {
            'report/1/client_1_2001.txt':'10.7.0.151 -> 10.7.0.152',
            'report/1/client_1_3001.txt':'10.7.0.151 -> 10.7.0.153',
        }.get(x,'No mapping')

>>> f('report/1/client_1_3001.txt')
'10.7.0.151 -> 10.7.0.153'

I have written a separate function which prints all the combinations

def msg():
    for x in range(1,9):
        for j in range(1,9):
            if x == j:
                continue
            else:
                print "'report/%s/client_%s_%s00%s.txt':'10.7.0.15%s -> 10.7.0.15%s'"%(x,x,j,x,x,j)

I am calling above function inside another function

def f(x):
    return {msg()}.get(x,'No mapping')

When I am trying to access the value with this:

print f('report/2/client_2_1002.txt')

I see this error:

    return {msg()}.get(x,'No mapping')
AttributeError: 'set' object has no attribute 'get'

Can someone let me know if I am missing anything here?

3
  • 1
    What are you expecting get to do there? You created a set. Sets don't provide any way to get specific items. Commented Apr 21, 2017 at 19:14
  • Is there anyway I can substitute the value of msg() function in same format just like printing the output? so that I don't need to write all the stuff in def f(x) function, my goal is to get '10.7.0.151 -> 10.7.0.153' when I execute f('report/1/client_1_3001.txt') as shown in above "Example". Commented Apr 21, 2017 at 19:16
  • 1
    Modify msg to build a dict instead of print a bunch of stuff. Commented Apr 21, 2017 at 19:19

2 Answers 2

1

Modify msg to build a dict instead of print a bunch of stuff.

def f(x):
    return getmsg().get(x,"No mapping")
def getmsg():
    d = {}
    for x in range(1,9):
        for j in range(1,9):
            if x == j:
                continue
            else:
                d['report/%s/client_%s_%s00%s.txt' % (x,x,j,x)] ='10.7.0.15%s -> 10.7.0.15%s'%(x,j)
    return d

Resulting in:

>>f('report/1/client_1_3001.txt')

'10.7.0.151 -> 10.7.0.153'

Or avoid the nested call to f and getmsg and require an argument in the getmsg function:

def getmsg(val):
    d = {}
    for x in range(1,9):
        for j in range(1,9):
            if x == j:
                continue
            else:
                d['report/%s/client_%s_%s00%s.txt' % (x,x,j,x)] ='10.7.0.15%s -> 10.7.0.15%s'%(x,j)
    return d.get(val,"No mapping")

Outputs:

enter image description here

NB: It seems redundant to rebuild this dict every time you call the function, and there are probably better ways to do this. :)

One way you might do this (and I am by no means an expert with python) is by instantiating a Class which contains the mapping. This way, we build the mapping dict in the __init__ procedure so that no matter how many times we request the mapping, we aren't rebuilding the dict every time.

class Map():
    def __init__(self):
        self.mapping = self.getmapping()
    def find(self,x):
        return self.mapping.get(x,"not mapped")
    def getmapping(self):
        d = {}
        for x in range(1,9):
            for j in range(1,9):
                if x == j:
                    continue
                else:
                    d['report/%s/client_%s_%s00%s.txt' % (x,x,j,x)] ='10.7.0.15%s -> 10.7.0.15%s'%(x,j)
        return d  

use it thusly:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks much @David!
1

Change your msg to return a dictionary:

import itertools as it

def msg():
    return {'report/{x}/client_{x}_{y}00{x}.txt'.format(x=x, y=y):
            '10.7.0.15{x} -> 10.7.0.15{y}'.format(x=x, y=y)
            for x, y in it.product(range(1,9), repeat=2) if x != y}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.