mailRe: r11192 - /branches/bieri_gui/gui_bieri/components/conversion.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by Edward d'Auvergne on May 19, 2010 - 13:40:
Hi,

You could put these into unit tests.  This will mean that this
function is continually tested.  Future code changes may accidentally
introduce a bug, but if this is unit tested, then the bugs are
instantly caught.  This is the purpose of the unit test, to make sure
that the code is still operating as expected.

Cheers,

Edward


On 19 May 2010 13:12, Michael Bieri <michael.bieri@xxxxxxxxxxxxx> wrote:
Yep, that was what I initially did by running it as an independent
script. Just added print str_to_float('1.5*1e6') and others below the
main script. And it worked fine.

Edward d'Auvergne wrote:
Hi Michael,

A function like this should be unit tested.  You can have a series of
unit tests where you give the function a string and see if it returns
the correct number.  This will be an extremely useful debugging tool
and will save you future debugging pain!

Regards,

Edward



On 19 May 2010 01:52,  <michael.bieri@xxxxxxxxxxxxx> wrote:

Author: michaelbieri
Date: Wed May 19 01:52:06 2010
New Revision: 11192

URL: http://svn.gna.org/viewcvs/relax?rev=11192&view=rev
Log:
Function to convert a float written in a string to a proper float object 
is added.

Modified:
   branches/bieri_gui/gui_bieri/components/conversion.py

Modified: branches/bieri_gui/gui_bieri/components/conversion.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/bieri_gui/gui_bieri/components/conversion.py?rev=11192&r1=11191&r2=11192&view=diff
==============================================================================
--- branches/bieri_gui/gui_bieri/components/conversion.py (original)
+++ branches/bieri_gui/gui_bieri/components/conversion.py Wed May 19 
01:52:06 2010
@@ -1,0 +1,54 @@
+###############################################################################
+#                                                                        
     #
+# Copyright (C) 2010 Michael Bieri                                       
     #
+#                                                                        
     #
+# This file is part of the program relax.                                
     #
+#                                                                        
     #
+# relax is free software; you can redistribute it and/or modify          
     #
+# it under the terms of the GNU General Public License as published by   
     #
+# the Free Software Foundation; either version 2 of the License, or      
     #
+# (at your option) any later version.                                    
     #
+#                                                                        
     #
+# relax is distributed in the hope that it will be useful,               
     #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of         
     #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
     #
+# GNU General Public License for more details.                           
     #
+#                                                                        
     #
+# You should have received a copy of the GNU General Public License      
     #
+# along with relax; if not, write to the Free Software                   
     #
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
USA   #
+#                                                                        
     #
+###############################################################################
+
+# Package docstring.
+"""Package for the different conversion tools used to bring together the 
GUI and API of relax."""
+
+from string import replace, strip, split
+
+
+def str_to_float(string):
+    """Function to convert a float in a string object to a real float 
object.
+
+    such as:    "3.5 * 1e6" to 3.5*1e6
+
+
+    @keyword string     Float in string that will be converted to float 
object.
+    @type string        str
+    """
+
+    # Delete whitespace.
+    string = replace(string, ' ','')
+
+    # Strip string.
+    values = split(string, '*')
+
+    # Detect exponent.
+    if '1e' in values[1]:
+        exponent = float(replace(values[1], '1e', ''))
+    if '10^' in values[1]:
+        exponent = float(replace(values[1], '10^', ''))
+
+    # Calculate float.
+    float_obj = float(values[0]) * 10**exponent
+
+    return float_obj


_______________________________________________
relax (http://nmr-relax.com)

This is the relax-commits mailing list
relax-commits@xxxxxxx

To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-commits



_______________________________________________
relax (http://nmr-relax.com)

This is the relax-devel mailing list
relax-devel@xxxxxxx

To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-devel




_______________________________________________
relax (http://nmr-relax.com)

This is the relax-devel mailing list
relax-devel@xxxxxxx

To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-devel




Related Messages


Powered by MHonArc, Updated Mon May 24 03:20:54 2010