By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

Dictionary complements if, elif, else

bvdet
Expert Mod 2.5K+
P: 2,851
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):
Expand|Select|Wrap|Line Numbers
  1. if _relrotation > 0.0 and _relrotation < 90.0:
  2.     _caseno = "Case 1"
  3.     _xflg = 1
  4.     _yflg = 1
  5. elif _relrotation > 90.0 and _relrotation < 180.0:
  6.     _caseno = "Case 2"
  7.     if _hssflgweb == "Short Side":
  8.         _xflg = -1
  9.         _yflg = -1
  10.     else:
  11.         _xflg = -1
  12.         _yflg = 1
  13. elif _relrotation > 180.0 and _relrotation < 270.0:
  14.     _caseno = "Case 3"
  15.     if _hssflgweb == "Short Side":
  16.         _xflg = -1
  17.         _yflg = 1
  18.     else:
  19.         _xflg = 1
  20.         _yflg = -1  
  21. elif _relrotation > 270.0 and _relrotation < 360.0:
  22.     _caseno = "Case 4"
  23.     if _hssflgweb == "Short Side":
  24.         _xflg = 1
  25.         _yflg = -1
  26.     else:
  27.         _xflg = -1
  28.         _yflg = -1
  29.  
  30. if case_no == "Case 1" or case_no == "Case 3":
  31.     rot_argument = [rel_rotation, 0.0, 0.0]
  32. elif case_no == "Case 2" or case_no == "Case 4":
  33.     rot_argument = [180.0 - rel_rotation, 0.0, 180.0]
  34. if hss_flg_web == "Long Side":
  35.     rot_argument[0] = 180.0 - rot_argument[0]
  36.     rot_argument[2] = rot_argument[2] + 180.0
  37.  
I just updated the code:
Expand|Select|Wrap|Line Numbers
  1. caseDict = {"Case 1": {"Long Side": (1,1,(180.0-relRot, 0.0, 180.0)), \
  2.                        "Short Side": (1,1,(relRot, 0.0, 0.0))}, \
  3.             "Case 2": {"Long Side": (-1,1,(relRot, 0.0, 0.0)), \
  4.                        "Short Side": (-1,-1,(180.0-relRot, 0.0, 180.0))}, \
  5.             "Case 3": {"Long Side": (1,-1,(180.0-relRot, 0.0, 180.0)), \
  6.                        "Short Side": (-1,1,(relRot, 0.0, 0.0))}, \
  7.             "Case 4": {"Long Side": (-1,-1,(relRot, 0.0, 0.0)), \
  8.                        "Short Side": (1,-1,(180.0-relRot, 0.0, 180.0))}
  9.             }
  10.  
  11. if 0.0 < relRot <= 90.0:
  12.     caseno = "Case 1"
  13. elif 90.0 < relRot <= 180.0:
  14.     caseno = "Case 2"
  15. elif 180.0 < relRot <= 270.0:
  16.     caseno = "Case 3"
  17. else:
  18.     caseno = "Case 4"
  19.  
  20. self.xflg, self.yflg, self.rot = caseDict[caseno][flgweb]
I still have an if, elif, else, but it's cleaner and easier to read.
Nov 18 '07 #1
Share this Question
Share on Google+
2 Replies


bartonc
Expert 5K+
P: 6,596
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.
Nov 18 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Nov 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.