strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Parse hexadecimal integers

Open wrobell opened this issue 4 years ago • 18 comments

YAML supports multiple integer representations

https://yaml.org/type/int.html

for example - 0x77 parses as [119].

StrictYAML raises validation error

In [1]: import strictyaml
In [2]: strictyaml.load('- 0x77', strictyaml.Seq(strictyaml.Int()))
...
YAMLValidationError: when expecting an integer
found arbitrary text
  in "<unicode string>", line 1, column 1:
    - '0x77'
     ^ (line: 1)

This ticket is about parsing hexadecimal integers as they are quite common.

wrobell avatar May 18 '20 23:05 wrobell

Would a HexInt validator serve your purposes?

On Tue, 19 May 2020, 00:50 wrobell, [email protected] wrote:

YAML supports multiple integer representations

https://yaml.org/type/int.html

for example - 0x77 parses as [119].

StrictYAML raises validation error

In [1]: import strictyaml In [2]: strictyaml.load('- 0x77', strictyaml.Seq(strictyaml.Int())) ... YAMLValidationError: when expecting an integer found arbitrary text in "", line 1, column 1: - '0x77' ^ (line: 1)

This ticket is about parsing hexadecimal integers as they are quite common.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNPJJPI6WTZIEGADMLTRSHCSRANCNFSM4NEQJWFA .

crdoconnor avatar May 19 '20 08:05 crdoconnor

For my purposes Int() | HexInt() would be more than enough. But I think that general expectation would be to follow YAML specification, i.e. Int() (or YAMLInt()?) should allow both forms.

wrobell avatar May 19 '20 09:05 wrobell

The reason this project exists is because I wanted to strip out extraneous features from the YAML spec and pare it down to something much simpler and less fragile.

I'm happy to accommodate this use case with Int() | HexInt() though.

On Tue, 19 May 2020, 10:42 wrobell, [email protected] wrote:

For my purposes Int() | HexInt() would be more than enough. But I think that general expectation would be to follow YAML specification, i.e. Int() (or YAMLInt()?) should allow both forms.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-630710397, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNMOATBQVNNVBQZ6XYDRSJIAHANCNFSM4NEQJWFA .

crdoconnor avatar May 19 '20 09:05 crdoconnor

IMHO, the question is: is there a valid use case for schema to allow only integers with specific base in the schema?

wrobell avatar Jun 01 '20 09:06 wrobell

Potentially, yes. 0x1a is not really very readable for non technical people. Use of hex is quite a niche thing.

On Mon, 1 Jun 2020, 10:42 wrobell, [email protected] wrote:

IMHO, the question is: is there a valid use case for schema to allow only integers with specific base in the schema?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-636742953, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNMUQDK4JXWCCQYR2FLRUNZWXANCNFSM4NEQJWFA .

crdoconnor avatar Jun 01 '20 10:06 crdoconnor

Hello, I run into the same issue. While transpiling a specification published as PDF to parseable YAML, I had to rewrite 0xFFFF (found verbatim in the PDF specification) to 65535. IMO, non-technical people would appreciate if there is a one-to-one correlation between the specs and the YAML code.

Having said that, I'll be quite happy once the proposed Int()|HexInt() solution is implemented :-)

eulores avatar Jan 30 '21 10:01 eulores

This is a dirty patch that I use in my code to let Int() also parse hexadecimal encoded integers, such as 0xFF.

from strictyaml import Int

def validate_hexint(self, chunk):
  val = chunk.contents
  try:
    return int(val.replace("_", ""), 0)
  except:
    chunk.expecting_but_found("when expecting an integer")

Int.validate_scalar = validate_hexint

eulores avatar Jan 30 '21 15:01 eulores

Are there plans for a HexInt validator to be part of strictyaml? I have implemented it myself but I'm curious if I wont have to in the future or shouldn't even right now.

jnichols0 avatar Sep 30 '21 23:09 jnichols0

Yeah, I can absolutely do this. Thanks for reminding me.

Can you share me yours?

On Fri, 1 Oct 2021, 00:52 James Nichols, @.***> wrote:

Are there plans for a HexInt validator to be part of strictyaml? I have implemented it myself but I'm curious if I wont have to in the future or shouldn't even right now.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-931785009, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNO3K6HARM4FY6BWPNTUETZ3FANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 01 '21 07:10 crdoconnor

Yes definitely, mind if I submit a PR? I would love to "officially" contribute.

jnichols0 avatar Oct 01 '21 22:10 jnichols0

Not at all, I'd love that.

Please bear in mind that the tooling around the project might have issues due to some code rot.

On Fri, Oct 1, 2021 at 11:31 PM James Nichols @.***> wrote:

Yes definitely, mind if I submit a PR? I would love to "officially" contribute.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-932613924, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNMO6QNCJW5Y5BG7UNTUEYZEDANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 03 '21 16:10 crdoconnor

This is something I will try to fix in the next few days.

On Sun, 3 Oct 2021, 17:10 Colm O'Connor, @.***> wrote:

Not at all, I'd love that.

Please bear in mind that the tooling around the project might have issues due to some code rot.

On Fri, Oct 1, 2021 at 11:31 PM James Nichols @.***> wrote:

Yes definitely, mind if I submit a PR? I would love to "officially" contribute.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-932613924, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNMO6QNCJW5Y5BG7UNTUEYZEDANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 03 '21 16:10 crdoconnor

What I threw together earlier was kind of hacky so I started over.

Take a look if you like: https://github.com/jnichols0/strictyaml/tree/hexint

I'll work on conforming to the requested PR format and adding a story test.

jnichols0 avatar Oct 05 '21 05:10 jnichols0

Submitted a PR, made a story to test it.

jnichols0 avatar Oct 09 '21 21:10 jnichols0

Thanks! I'll take a look a little later on.

On Sat, 9 Oct 2021, 22:49 James Nichols, @.***> wrote:

Submitted a PR, made a story to test it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-939366411, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNLVFC6AQMOW7YIRCUDUGC2GLANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 10 '21 12:10 crdoconnor

LGTM. I'll merge it and release a new version a bit later.

On Sun, 10 Oct 2021, 13:33 Colm O'Connor, @.***> wrote:

Thanks! I'll take a look a little later on.

On Sat, 9 Oct 2021, 22:49 James Nichols, @.***> wrote:

Submitted a PR, made a story to test it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-939366411, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNLVFC6AQMOW7YIRCUDUGC2GLANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 10 '21 12:10 crdoconnor

I realized HexInt, as implemented, doesn't support signed expressions. Python evaluates -0x1 to -1 but -0x1 doesn't pass validation for HexInt() | Int() which seems wrong. I'll submit a PR updating the regex if you agree.

jnichols0 avatar Oct 19 '21 20:10 jnichols0

Yes, of course. Thank you.

On Tue, 19 Oct 2021, 21:43 James Nichols, @.***> wrote:

I realized HexInt, as implemented, doesn't support signed expressions. Python evaluates -0x1 to -1 but -0x1 doesn't pass validation for HexInt() | Int() which seems wrong. I'll submit a PR updating the regex if you agree.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/105#issuecomment-947089919, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNLWFPS4SW275XEUH63UHXJ7NANCNFSM4NEQJWFA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

crdoconnor avatar Oct 20 '21 10:10 crdoconnor