#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Need help with list


    I need to simulate a robot.
    a robot has a head and two hands, while each hand has fingers.

    While debugging my code, i noticed something weird.
    I'll let you see for yourself, here's the code (scroll down to see "main"):

    Code:
    class Head:
        #initializing Head
        def __init__(self, direction="right"):
            self.setDirection(direction)
        
        #set method
        def setDirection(self, direction):
            if (direction=="right" or direction=="left"):
                self._lORr_=direction
            else:
                self._lORr_="right"
        
        #custom string
        def __str__(self):
            return "Head is turning "+self._lORr_
    
    
    class Hand:
        #initializing Hand
        def __init__(self):
            self._fingers_=[]
        
        def addFinger(self, finger):
            self._fingers_.append(finger)
        
        def removeFinger(self, finger):
            if finger in self._fingers_:
                self._fingers_.remove(finger)
                
            
    
    class Finger:
        #initializing Finger
        def __init__(self, fingerType="little"):
            self.setFinger(fingerType)
        
        #set method
        def setFinger(self, fingerType):
            types=("thumb", "index", "middle", "ring", "little")
            if fingerType in types:
                self._fingerType_=fingerType
            else:
                self._fingerType_="little"
        
        #get method
        def getFinger(self):
            return self._fingerType_
        
        #custom string
        def __str__(self):
            return self._fingerType_
            
    
    class Robot:
        #initializing Robot
        def __init__(self, init_name=None):
            self._name_=init_name
            self._head_=Head()
            self._Rhand_=Hand()
            self._Lhand_=Hand()
        
        def turnHead(self, direction):
            if (direction!="right" and direction!="left"):
                return
            self._head_._lORr_=direction
        
        def addFinger(self, hand, fingerType):
            newFinger=Finger(fingerType)
            if (hand=="right"):
                self._Rhand_.addFinger(newFinger)
            elif (hand=="left"):
                self._Lhand_.addFinger(newFinger)
            else:
                return
            
        def removeFinger(self, hand, fingerType):
            if (hand=="right"):
                self._Rhand_.removeFinger(fingerType)
            elif (hand=="left"):
                self._Lhand_.removeFinger(fingerType)
            else:
                return
    
    
    
    #main prgram:
    iRobot=Robot("ER2013")
    iRobot.addFinger("left", "thumb")
    iRobot.addFinger("left", "index")
    iRobot.addFinger("left", "little")
    
    #printing left palm:
    for i in range(len(iRobot._Lhand_._fingers_)):
        print(iRobot._Lhand_._fingers_[i])
        
    #looking for the "little" finger in the list:
    if "little" in iRobot._Lhand_._fingers_:
        print("Yes!")
    else:
        print("No!")
    
    print("Exit flag")
    the output for this code is:

    Code:
    thumb
    index
    little
    No!
    Exit flag
    obviously "little" is in the list, as you can see from the print out,
    So, why does it print "No!"?

    Thanks in advanced.
    Last edited by so.very.tired; March 16th, 2013 at 07:54 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    Print it to see what you are trying to access
    print iRobot._Lhand_._fingers_
    which should show the problem. The layout seems overly complex (to me at least.) You could use a dictionary within the Robot class for example, pointing to a list that contains any fingers, or direction with "head" as the key, as the second element.
    Code:
    hand_dict = {"right": ["pinkie"], "left": ["thumb", "index"]}
    for key in hand_dict:
        print "fingers for %s hand =" % (key),
        for finger in hand_dict[key]:
            print finger,
        print
    Last edited by dwblas; March 16th, 2013 at 12:09 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481

    Late late late!


    As dwblas said, you deceived yourself with your __str__ method.

    And the code is clearly in early development, it looks like eventually you'll never want to refer to these attributes
    _this_ and _that_ .

    I suppose that gets back to (your?) earlier question about getters and setter.

    If your robot needs to respond to

    'little left finger' in R2D2 # ER2013

    then write code for it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Thanks for the help. appreciate it.


    OK, I see the problem now...
    there's the output for iRobot._Lhand_._fingers_:

    Code:
    [<__main__.Finger object at 0x00000000028D9518>, <__main__.Finger object at 0x00000000028D92B0>, <__main__.Finger object at 0x00000000028D92E8>]
    As you said, I decieved myself with __str__
    The list holds instances, and the statement "if "little" in iRobot._Lhand_._fingers_" looks for a string.
    Is it possible to iterate over the contained strings?

    b49P23TIvg, what are the _this_ and _that_ attributes?
    I couldn't find info about it on the net.
    and yes, it allways gets back to earlier questions.
    but more accurately, I asked about class within another class, and wasn't sure if I should use it here.
    eventually I didn't use it, as you can see.

    dwblas, I can't use dictionary here, since my assignment specifically requires that I'll use the above classes (Robot, Head, Hand, Finger).
    using dictionary makes the Hand class redundant.
    Last edited by so.very.tired; March 17th, 2013 at 05:28 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    How about:

    Code:
    iRobot=Robot("ER2013")
    iRobot.addFinger("left", "thumb")
    iRobot.addFinger("left", "index")
    iRobot.addFinger("left", "little")
    
    #looking for "little" in left palm:
    for i in range(len(iRobot._Lhand_._fingers_)):
        if ("little"==iRobot._Lhand_._fingers_[i]._fingerType_):
            print("Yes!")
    That seems to work.
    any thoughts?

    (feel free to throw general tips as well, i'm always eager to learn)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Sorry, _this_ and _that_ were sample attributes that are hidden.

    Your Hand class could be (untested)
    Code:
    class Hand:
        #initializing Hand
        def __init__(self):
            self._fingers_=[]
    
        def addFinger(self, finger):
            self._fingers_.append(finger)
    
        def removeFinger(self, finger):
            if finger in self._fingers_:
                self._fingers_.remove(finger)
    
        def __contains__(self, fingerType):
            return fingerType in map(str, self._fingers_)
    
        def getFinger(self, fingerType):
            for (i, o,) in map(str, self._fingers_):
                if o == fingerType:
                    return o
            #raise an error or return None
    __contains__ supports the `in' operator. I've used str(finger_object) to compare against fingers identified by strings. Let's add a give method to Robot so that robots can attempt to swap parts. We can write
    Code:
    hand = C_3P0._Rhand
    if 'middle' in hand:
        C_3P0 . give (Luke, hand.getFinger('middle'))
    You'll need more code to eliminate the ugly ._Rhand
    Last edited by b49P23TIvg; March 17th, 2013 at 07:40 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
        def removeFinger(self, finger):
            if finger in self._fingers_:
                self._fingers_.remove(finger)
    the problem here is that the parameter "finger" is a string.

    so this is how I wrote it:

    Code:
        def removeFinger(self, finger):
            for i in range(len(self._fingers_[i]._fingerType_)):
                if (finger==self._fingers_[i]._fingerType_):
                    self._fingers_.pop(i)
                    return
    (actually, that was the purpose of this thread, I didn't know how to implement removeFinger)


    sorry for the ignorance, but I couldn't quite undestand what you did here:
    Code:
    hand = C_3P0._Rhand
    if 'middle' in hand:
        C_3P0 . give (Luke, hand.getFinger('middle'))
    what's "C_3P0", and what is the "give" attribute?
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    You didn't try the program with my adjustment!
    Look at the end.
    if "little" in iRobot._Lhand_:
    It uses a string for the test, and the program prints Yes!
    How does it work? I implemented __contains__ method of Hand. c_3p0 and r2d2 are robot names from Star Wars. I suspect others will enjoy the stupid joke I presented but forget about it. Just learn the python.
    Code:
    class Head:
        #initializing Head
        def __init__(self, direction="right"):
            self.setDirection(direction)
        
        #set method
        def setDirection(self, direction):
            if (direction=="right" or direction=="left"):
                self._lORr_=direction
            else:
                self._lORr_="right"
        
        #custom string
        def __str__(self):
            return "Head is turning "+self._lORr_
    
    
    class Hand:
        #initializing Hand
        def __init__(self):
            self._fingers_=[]
    
        def addFinger(self, finger):
            self._fingers_.append(finger)
    
        def removeFinger(self, finger):
            if finger in self._fingers_:
                self._fingers_.remove(finger)
    
        def __contains__(self, fingerType):
            return fingerType in map(str, self._fingers_)
    
        def getFinger(self, fingerType):
            for (i, o,) in map(str, self._fingers_):
                if o == fingerType:
                    return o
            #raise an error or return None
    
    class Finger:
        #initializing Finger
        def __init__(self, fingerType="little"):
            self.setFinger(fingerType)
    
        #set method
        def setFinger(self, fingerType):
            types=("thumb", "index", "middle", "ring", "little")
            if fingerType in types:
                self._fingerType_=fingerType
            else:
                self._fingerType_="little"
    
        #get method
        def getFinger(self):
            return self._fingerType_
    
        #custom string
        def __str__(self):
            return self._fingerType_
    
    
    
    class Robot:
        #initializing Robot
        def __init__(self, init_name=None):
            self._name_=init_name
            self._head_=Head()
            self._Rhand_=Hand()
            self._Lhand_=Hand()
        
        def turnHead(self, direction):
            if (direction!="right" and direction!="left"):
                return
            self._head_._lORr_=direction
        
        def addFinger(self, hand, fingerType):
            newFinger=Finger(fingerType)
            if (hand=="right"):
                self._Rhand_.addFinger(newFinger)
            elif (hand=="left"):
                self._Lhand_.addFinger(newFinger)
            else:
                return
            
        def removeFinger(self, hand, fingerType):
            if (hand=="right"):
                self._Rhand_.removeFinger(fingerType)
            elif (hand=="left"):
                self._Lhand_.removeFinger(fingerType)
            else:
                return
    
    
    
    #main prgram:
    iRobot=Robot("ER2013")
    iRobot.addFinger("left", "thumb")
    iRobot.addFinger("left", "index")
    iRobot.addFinger("left", "little")
    
    #printing left palm:
    for i in range(len(iRobot._Lhand_._fingers_)):
        print(iRobot._Lhand_._fingers_[i])
        
    #looking for the "little" finger in the list:
    if "little" in iRobot._Lhand_:
        print("Yes!")
    else:
        print("No!")
    
    print("Exit flag")
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    OK, now it's much more clear.
    Thanks for the help, appreciate it!

    BTW, I didn't see Star Wars, so... you can understand my confusion.

IMN logo majestic logo threadwatch logo seochat tools logo