Permalink
Browse files

constant with leading zero cast to string

  • Loading branch information...
1 parent 95d8432 commit fefacd7c1eeecf2a20939d5c950e3cfe5d83a796 @3kwa committed Oct 16, 2012
Showing with 40 additions and 16 deletions.
  1. +4 −4 LICENCE
  2. +4 −1 README.rst
  3. +1 −0 constants.ini
  4. +2 −8 constants.py
  5. +1 −1 setup.py
  6. +23 −0 test_constants.py
  7. +5 −2 tox.ini
View
@@ -11,14 +11,14 @@ modification, are permitted provided that the following conditions are met:
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- * Neither the name of the 3KWA nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
+ * Neither the name of 3KWA nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DISCLAIMED. IN NO EVENT SHALL EUGENE VAN DEN BULKE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
View
@@ -34,6 +34,7 @@ unless specifically overridden in a section.
[DEFAULT]
something = a_default_value
all = 1
+ a_string = 0350
[a_section]
something = a_section_value
@@ -85,10 +86,12 @@ To access the values, the instance can be used like a dictionary (getitem).
>>> consts['something']
'a_section_value'
-Values are cast into integer or float when possible.
+Values are cast into integer or float when pertinent.
>>> consts['all']
1
+>>> consts.a_string
+'0350'
Values can also be accessed using the . operator (getattr)
View
@@ -1,6 +1,7 @@
[DEFAULT]
something = a_default_value
all = 1
+a_string = 0350
[a_section]
something = a_section_value
View
@@ -47,7 +47,6 @@ def __init__(self, variable=VARIABLE, filename=FILENAME):
environment / config section from default to __CONSTANTS__
filename is the config filename
"""
- # in python 3 prevents an endless recursion getattr / getitem
object.__setattr__(self, 'dict', {})
self.variable = variable
self.filename = filename
@@ -111,14 +110,9 @@ def __getattr__(self, item):
def cast(string):
"""
cast string to int, float or keep as string
-
- >>> Constants.cast('1')
- 1
- >>> Constants.cast('3.14')
- 3.14
- >>> Constants.cast('a_string')
- 'a_string'
"""
+ if hasattr(string, 'startswith') and string.startswith('0'):
+ return string
try:
return int(string)
except ValueError:
View
@@ -3,7 +3,7 @@
setup(
name='constants',
- version='0.4.1',
+ version='0.4.2',
description='The simple way to deal with environment constants.',
long_description=open('README.rst').read(),
author='Eugene Van den Bulke',
View
@@ -0,0 +1,23 @@
+import unittest
+import os
+
+import constants
+
+
+class TestConstants(unittest.TestCase):
+
+ def test_integer_cast(self):
+ result = constants.Constants.cast('1')
+ self.assertEqual(1, result)
+
+ def test_float_cast(self):
+ result = constants.Constants.cast('3.14')
+ self.assertEqual(3.14, result)
+
+ def test_string_cast(self):
+ result = constants.Constants.cast('a_string')
+ self.assertEqual('a_string', result)
+
+ def test_integer_with_leading_zero_not_cast(self):
+ result = constants.Constants.cast('0350')
+ self.assertEqual('0350', result)
View
@@ -1,7 +1,10 @@
[tox]
envlist = py26, py27, py30, py31, py32, py33
+
[testenv]
-deps=pytest
+deps = pytest
setenv=
__CONSTANTS__ = a_section
-commands = py.test README.rst -v
+commands=
+ py.test
+ py.test README.rst

0 comments on commit fefacd7

Please sign in to comment.