[POS-commit] r191 - in Kiwi2: . Kiwi2/Widgets tests
Lorenzo Gil Sanchez
lgs at async.com.br
Wed Mar 23 20:29:27 BRT 2005
Author: lgs
Date: 2005-03-23 20:29:27 -0300 (Wed, 23 Mar 2005)
New Revision: 191
Added:
Kiwi2/tests/test_datatypes.py
Modified:
Kiwi2/ChangeLog
Kiwi2/Kiwi2/Widgets/datatypes.py
Log:
* Kiwi2/Widgets/datatypes.py: added a date type and functions to
set a data from a string a viceversa
* tests/test_datatypes.py: added some tests for the datetypes
Modified: Kiwi2/ChangeLog
===================================================================
--- Kiwi2/ChangeLog 2005-03-23 19:34:17 UTC (rev 190)
+++ Kiwi2/ChangeLog 2005-03-23 23:29:27 UTC (rev 191)
@@ -1,5 +1,10 @@
2005-03-23 Lorenzo Gil Sanchez <lgs at sicem.biz>
+ * Kiwi2/Widgets/datatypes.py: added a date type and functions to
+ set a data from a string a viceversa
+
+ * tests/test_datatypes.py: added some tests for the datetypes
+
* tests/test_Action.py:
* tests/test_BaseView.py:
* tests/test_CheckButton.py:
Modified: Kiwi2/Kiwi2/Widgets/datatypes.py
===================================================================
--- Kiwi2/Kiwi2/Widgets/datatypes.py 2005-03-23 19:34:17 UTC (rev 190)
+++ Kiwi2/Kiwi2/Widgets/datatypes.py 2005-03-23 23:29:27 UTC (rev 191)
@@ -1,13 +1,52 @@
+from datetime import date
+
+# by default locale uses the C locale but our date conversions use the user
+# locale so we need to set the locale to that one
+import locale
+locale.setlocale(locale.LC_ALL, '') # this set the user locale ( $LANG )
+
+import time
+
+date_format = locale.nl_langinfo(locale.D_FMT)
+
+def set_date_format(format):
+ """Set the format for date conversions.
+
+ format is a string suitable for the date.strftime function like %d/%m/%y
+ By default format is the user locale date format and if the format argument
+ is None it revert to this one."""
+ global date_format
+ if format is None:
+ date_format = locale.nl_langinfo(locale.D_FMT)
+ else:
+ if not isinstance(format, str):
+ raise TypeError("Format should be a string, found a %s" % \
+ type(format))
+ date_format = format
+
+def str2date(value):
+ "Convert a string to a date"
+ global date_format
+ dateinfo = time.strptime(value, date_format)
+ year, month, day = dateinfo[0:3]
+ return date(year, month, day)
+
+def date2str(value):
+ "Convert a date to a string"
+ global date_format
+ return value.strftime(date_format)
+
def str2bool(value, default_value=True):
+ "Convert a string to a boolean"
if value.upper() in ('TRUE', '1'):
return True
elif value.upper() in ('FALSE', '0'):
return False
else:
return default_value
+
+supported_types = (str, int, float, bool, date)
-supported_types = (str, int, float, bool)
-
supported_types_names = map(lambda t: t.__name__, supported_types)
TO_STR, FROM_STR = range(2)
@@ -18,12 +57,14 @@
int: str,
float: str,
bool: str,
+ date: date2str,
},
FROM_STR: {
str: lambda v: v,
int: int,
float: float,
bool: str2bool,
+ date: str2date,
}
}
@@ -32,4 +73,5 @@
int: 0,
float: 0.0,
bool: True,
+ date: None,
}
Added: Kiwi2/tests/test_datatypes.py
===================================================================
--- Kiwi2/tests/test_datatypes.py 2005-03-23 19:34:17 UTC (rev 190)
+++ Kiwi2/tests/test_datatypes.py 2005-03-23 23:29:27 UTC (rev 191)
@@ -0,0 +1,69 @@
+import unittest
+import locale
+
+from datetime import date
+from Kiwi2.Widgets import datatypes
+
+class DataTypesTest(unittest.TestCase):
+
+ def teststr2bool(self):
+ self.assertEqual(datatypes.str2bool('TRUE'), True)
+ self.assertEqual(datatypes.str2bool('true'), True)
+ self.assertEqual(datatypes.str2bool('TrUe'), True)
+ self.assertEqual(datatypes.str2bool('1'), True)
+ self.assertEqual(datatypes.str2bool('FALSE'), False)
+ self.assertEqual(datatypes.str2bool('false'), False)
+ self.assertEqual(datatypes.str2bool('FalSE'), False)
+ self.assertEqual(datatypes.str2bool('0'), False)
+
+ # testing with default values
+ self.assertEqual(datatypes.str2bool('something', False), False)
+ self.assertEqual(datatypes.str2bool('something', True), True)
+ self.assertEqual(datatypes.str2bool('', True), True)
+ self.assertEqual(datatypes.str2bool('', False), False)
+
+ # you are not supposed to pass something that is not a string
+ self.assertRaises(AttributeError, datatypes.str2bool, None)
+
+ def teststr2date(self):
+ # set the date format to the spanish one
+ locale.setlocale(locale.LC_TIME, 'es_ES')
+ date_format = locale.nl_langinfo(locale.D_FMT)
+ datatypes.set_date_format(date_format)
+
+ birthdate = date(1979, 2, 12)
+ # in the spanish locale the format of a date is %d/%m/%y
+ self.assertEqual(datatypes.str2date("12/2/79"), birthdate)
+ self.assertEqual(datatypes.str2date("12/02/79"), birthdate)
+
+ # let's try with the portuguese locale
+ locale.setlocale(locale.LC_TIME, 'pt_BR')
+ date_format = locale.nl_langinfo(locale.D_FMT)
+ datatypes.set_date_format(date_format)
+
+ # portuguese format is "%d-%m-%Y"
+ self.assertEqual(datatypes.str2date("12-2-1979"), birthdate)
+ self.assertEqual(datatypes.str2date("12-02-1979"), birthdate)
+
+ # test some invalid dates
+ self.assertRaises(ValueError, datatypes.str2date, "40-10-2005")
+ # february only have 28 days
+ self.assertRaises(ValueError, datatypes.str2date, "30-02-2005")
+
+ def testdate2str(self):
+ locale.setlocale(locale.LC_TIME, 'es_ES')
+ date_format = locale.nl_langinfo(locale.D_FMT)
+ datatypes.set_date_format(date_format)
+
+ birthdate = date(1979, 2, 12)
+
+ self.assertEqual(datatypes.date2str(birthdate), "12/02/79")
+
+ locale.setlocale(locale.LC_TIME, 'pt_BR')
+ date_format = locale.nl_langinfo(locale.D_FMT)
+ datatypes.set_date_format(date_format)
+
+ self.assertEqual(datatypes.date2str(birthdate), "12-02-1979")
+
+if __name__ == "__main__":
+ unittest.main()
More information about the POS-commit
mailing list