473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dictionary complements if, elif, else

bvdet
2,851 Recognized Expert Moderator Specialist
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
2 1772
bartonc
6,596 Recognized Expert Expert
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
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

8
1898
by: Jack Carter | last post by:
I have been delegated to produce a tool that has python embedded in it. The desire is to have a command line interface that inherits all the python scripting functionality so people can use the tool either with a flat commandline entry like you would a debugger or with a script using scoping, loops and conditional. I am going from knowing nothing about python to almost nothing so the learning curve is rather
16
2066
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say four two. I found out that I have to make a dictionary like this: List = { 1:"one", 2:"two" and so on )
8
7049
by: rh0dium | last post by:
Hi all, I have a dict which looks like this.. dict={'130nm': {'umc': }, '180nm': {'chartered': , 'tsmc': }, '250nm': {'umc': , 'tsmc': } }
4
2244
by: Nico Grubert | last post by:
Hi there, I am looking for a way to sort a list containing dictionaries. This is my example list: I want to sort the list by dictionary's key 'from_datetime' so the sorted list should be:
8
13345
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more dictionaries which themselves may contain ints, None and more dictionaries. Each of the sub-dictionaries is also arbitrarily large. When pretty printing A, in the context I'm using A for, it's rather helpful to remove all key:value pairs where value...
2
2598
by: Charles Sullivan | last post by:
I'm trying to maintain some older C code (FOSS) which has been patched by various individuals over the years for portability to multiple Unix-like operating systems, to wit: Linux, SunOS, Solaris, Free/Open/NetBSD, Mac OS X, AT&T SysV r4, SCO Unix, AIX, OSF, NextStep. I want to add some conditionals like this: #if defined(USECODE_A) /*use this code */
2
4099
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else statement. I am going to post the first 82 lines of the script, since the error message points at line 80: from abaqusConstants import * from abaqus import *
6
1708
by: asincero | last post by:
Which is better: using an if/else construct to simulate a C switch or use a dictionary? Example: def foo(): if c == 1: doCase1() elif c == 2: doCase2() elif c == 3: doCase3()
6
3015
by: dudeja.rajat | last post by:
Hi, How to check if something is a list or a dictionary or just a string? Eg: for item in self.__libVerDict.itervalues(): self.cbAnalysisLibVersion(END, item) where __libVerDict is a dictionary that holds values as strings or
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.