Dictionary complements if, elif, else

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    Dictionary complements if, elif, else

    We are parametrically attaching a bent plate object to the side of a building column for support of a skewed beam. Given a relative rotation between the column and beam and which side of the column to attach to, the following code determines the X and Y direction flags to calculate the exact location and the rotation tuple of the bent plate object. Here's the old code (about 5 years old):[code=Python]if _relrotation > 0.0 and _relrotation < 90.0:
    _caseno = "Case 1"
    _xflg = 1
    _yflg = 1
    elif _relrotation > 90.0 and _relrotation < 180.0:
    _caseno = "Case 2"
    if _hssflgweb == "Short Side":
    _xflg = -1
    _yflg = -1
    else:
    _xflg = -1
    _yflg = 1
    elif _relrotation > 180.0 and _relrotation < 270.0:
    _caseno = "Case 3"
    if _hssflgweb == "Short Side":
    _xflg = -1
    _yflg = 1
    else:
    _xflg = 1
    _yflg = -1
    elif _relrotation > 270.0 and _relrotation < 360.0:
    _caseno = "Case 4"
    if _hssflgweb == "Short Side":
    _xflg = 1
    _yflg = -1
    else:
    _xflg = -1
    _yflg = -1

    if case_no == "Case 1" or case_no == "Case 3":
    rot_argument = [rel_rotation, 0.0, 0.0]
    elif case_no == "Case 2" or case_no == "Case 4":
    rot_argument = [180.0 - rel_rotation, 0.0, 180.0]
    if hss_flg_web == "Long Side":
    rot_argument[0] = 180.0 - rot_argument[0]
    rot_argument[2] = rot_argument[2] + 180.0
    [/code]I just updated the code:[code=Python]caseDict = {"Case 1": {"Long Side": (1,1,(180.0-relRot, 0.0, 180.0)), \
    "Short Side": (1,1,(relRot, 0.0, 0.0))}, \
    "Case 2": {"Long Side": (-1,1,(relRot, 0.0, 0.0)), \
    "Short Side": (-1,-1,(180.0-relRot, 0.0, 180.0))}, \
    "Case 3": {"Long Side": (1,-1,(180.0-relRot, 0.0, 180.0)), \
    "Short Side": (-1,1,(relRot, 0.0, 0.0))}, \
    "Case 4": {"Long Side": (-1,-1,(relRot, 0.0, 0.0)), \
    "Short Side": (1,-1,(180.0-relRot, 0.0, 180.0))}
    }

    if 0.0 < relRot <= 90.0:
    caseno = "Case 1"
    elif 90.0 < relRot <= 180.0:
    caseno = "Case 2"
    elif 180.0 < relRot <= 270.0:
    caseno = "Case 3"
    else:
    caseno = "Case 4"

    self.xflg, self.yflg, self.rot = caseDict[caseno][flgweb][/code]I still have an if, elif, else, but it's cleaner and easier to read.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Very cool. Did you see my "function dispatch" using a dictionary in this thread? It's the same kind of language extension idea, borrowed from another language.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by bartonc
      Very cool. Did you see my "function dispatch" using a dictionary in this thread? It's the same kind of language extension idea, borrowed from another language.
      Yes I did Barton. I like what you did there, and I save it for possible future use. Thanks.

      Comment

      Working...