diff options
author | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-10-07 17:31:38 -0700 |
---|---|---|
committer | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-10-07 17:31:38 -0700 |
commit | 09269cb684c37be6d2ac5641b228eb9b76beab8a (patch) | |
tree | 8e5c856967bdbd935e2b06dd3e420e30b1333758 /scss/types.py | |
parent | 0c9c6c56a65e54c516aae97fda4ce62e07d16372 (diff) | |
download | pyscss-09269cb684c37be6d2ac5641b228eb9b76beab8a.tar.gz |
Add new string and list functions from Sass 3.3; update docs re 3.3.
Diffstat (limited to 'scss/types.py')
-rw-r--r-- | scss/types.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/scss/types.py b/scss/types.py index 7ef6dc3..f6e6c10 100644 --- a/scss/types.py +++ b/scss/types.py @@ -447,6 +447,29 @@ class Number(Value): return wrapped + def to_python_index(self, length, check_bounds=True): + """Return a plain Python integer appropriate for indexing a sequence of + the given length. Raise if this is impossible for any reason + whatsoever. + """ + if not self.is_unitless: + raise ValueError("Index cannot have units: {0!r}".format(self)) + + ret = int(self.value) + if ret != self.value: + raise ValueError("Index must be an integer: {0!r}".format(ret)) + + if ret == 0: + raise ValueError("Index cannot be zero") + + if check_bounds and abs(ret) > length: + raise ValueError("Index {0!r} out of bounds for length {1}".format(ret, length)) + + if ret > 0: + return ret - 1 + else: + return ret + @property def has_simple_unit(self): """Returns True iff the unit is expressible in CSS, i.e., has no |