Changeset 120:e6e7a471146a


Ignore:
Timestamp:
Sep 2, 2013, 1:31:19 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
df5e75e21bb3a94ce8890b07c732263bfe57c4d0
Message:

Support numeric types

Location:
nagslang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/tests/test_yamlish.py

    r119 r120  
    8787        self.roundtrip({'foo': [True, False, None]})
    8888
     89    def test_numeric(self):
     90        self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
     91
    8992
    9093class TestFromPyYAML(TestRoundTrip):
  • nagslang/yamlish.py

    r119 r120  
    55 * lists, dicts,
    66 * single line strings,
     7 * ints, floats,
    78 * True, False, and None
    89'''
     
    3334
    3435    def _dump(self, data, indent=0):
    35         for type_ in (list, dict, basestring):
     36        for type_ in (list, dict, basestring, int, float):
    3637            if isinstance(data, type_):
    3738                f = getattr(self, '_dump_%s' % type_.__name__)
     
    6768            data = "'%s'" % data
    6869        return [' ' * indent + data]
     70
     71    def _dump_int(self, data, indent):
     72        return ['%s%i' % (' ' * indent, data)]
     73
     74    def _dump_float(self, data, indent):
     75        return ['%s%f' % (' ' * indent, data)]
    6976
    7077    def _dump_literal(self, data, indent):
     
    180187        if value == 'null':
    181188            return None
     189        for type_ in (int, float):
     190            try:
     191                return type_(value)
     192            except ValueError:
     193                pass
    182194        return value
Note: See TracChangeset for help on using the changeset viewer.