Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added octal numbers #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions strictyaml/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ def to_yaml(self, data):
raise YAMLSerializationError("'{}' not a hexademial integer.".format(data))


class OctInt(ScalarValidator):
def validate_scalar(self, chunk):
val = chunk.contents
if not utils.is_octal(val):
chunk.expecting_but_found("when expecting an octal integer")
return int(val, 8)

def to_yaml(self, data):
if utils.is_octal(data):
if isinstance(data, int):
return oct(data)
else:
return data
raise YAMLSerializationError("'{}' not an octal integer.".format(data))



class Bool(ScalarValidator):
def validate_scalar(self, chunk):
val = chunk.contents
Expand Down
25 changes: 25 additions & 0 deletions strictyaml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ def is_integer(value):
return compile(r"^[-+]?[0-9_]+$").match(value) is not None


def is_octal(value):
"""
Is a string a string of a octal integer?

>>> is_octal("0o15")
True

>>> is_octal("0O15")
True

>>> is_octal("0o19")
False

>>> is_octal("o15")
False

>>> is_octal("16")
False

>>> is_octal("1")
False
"""
return compile(r"^0[oO][0-7]+$").match(value) is not None


def is_hexadecimal(value):
"""
Is a string a string of a hexademcial integer?
Expand Down