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

Database has many "decimal"s which may be null and I need "float"s

bartonc
Expert 5K+
P: 6,596
Most of the lines in this fuction deal with null data and default data:

Expand|Select|Wrap|Line Numbers
  1.     def FillFromDB(self):
  2.         gradeSense = None
  3.         slopeSense = None
  4.         gradeOffset = None
  5.         slopeOffset = None
  6.         varList = (gradeSense, slopeSense, gradeOffset, slopeOffset)
  7.         cursor = self.dbServer.Execute("SELECT * FROM `HETAP_Setup`.`setup`")
  8.         if cursor.rowcount:
  9.             rows = cursor.fetchall()
  10.             for i, var in enumerate(varList):
  11.                 try:
  12.                     var = float(rows[0][i+2])
  13.                 except TypeError:
  14.                     pass
  15.             for row in rows:
  16.                 self.sernumComboBox.Append(row[1])
  17.         self.gradeSense = gradeSense or Multiply.DefaultTiltSense
  18.         self.slopeSense = slopeSense or Multiply.DefaultTiltSense
  19.         self.gradeOffset = gradeOffset or Multiply.DefaultOffset
  20.         self.slopeOffset = slopeOffset or Multiply.DefaultOffset
Nov 28 '06 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
Did anybody notice that this doesn't work? Apparently, in dereferences var...
Dec 1 '06 #2

bartonc
Expert 5K+
P: 6,596
Did anybody notice that this doesn't work? Apparently, in dereferences var...
I just got too cute. I guess this might be even cleaner:


Expand|Select|Wrap|Line Numbers
  1.     def FillFromDB(self):
  2.         varList = []
  3.         cursor = self.dbServer.Execute("SELECT * FROM `HETAP_Setup`.`setup`")
  4.         if cursor.rowcount:
  5.             rows = cursor.fetchall()
  6.             for i in range(4):
  7.                 try:
  8.                     varList.append(float(rows[0][i+2]))
  9.                 except TypeError:
  10.                     varList.append(None)
  11.             for row in rows:
  12.                 self.sernumComboBox.Append(row[1])
  13.         self.gradeSense = varList[0] or Multiply.DefaultTiltSense
  14.         self.slopeSense = varList[1] or Multiply.DefaultTiltSense
  15.         self.gradeOffset = varList[2] or Multiply.DefaultOffset
  16.         self.slopeOffset = varList[3] or Multiply.DefaultOffset
  17.         self.ShowOffsets()
Dec 1 '06 #3

bartonc
Expert 5K+
P: 6,596
Actually, there is a way to initialize a list of variables and have access to them outside the list. This is what I was shooting for in the original post:


>>> [a,b,c,d] = [i for i in range(4)]
>>> print a
0
>>> print d
3
>>>
Dec 2 '06 #4

Post your reply

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