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

Change expression evaluation package #172

Open
shenwei356 opened this issue Nov 3, 2021 · 5 comments
Open

Change expression evaluation package #172

shenwei356 opened this issue Nov 3, 2021 · 5 comments

Comments

@shenwei356
Copy link
Owner

shenwei356 commented Nov 3, 2021

Since https://github.com/Knetic/govaluate is not active anymore, I plan to replace it with https://github.com/antonmedv/expr.

Related commands: filter2, mutate2.

It looks great! https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md

@shenwei356
Copy link
Owner Author

https://github.com/benhoyt/goawk seems a good choice too.

@shenwei356
Copy link
Owner Author

@shenwei356
Copy link
Owner Author

https://github.com/PaesslerAG/gval looks great!

@moorereason
Copy link
Contributor

I hacked up a quick and dirty mutate3 implementation using https://github.com/expr-lang/expr to see what it could do.

Expr does date math out of the box (which could solve #213):

$ cat ../testdata/datesub.csv 
ID,In,Out
1,2023-08-25 11:24:00,2023-08-27 08:33:02
2,2023-08-25 11:28:00,2023-08-26 14:17:35
3,2023-08-26 11:29:00,2023-08-29 20:43:00

$ cat ../testdata/datesub.csv | ./csvtk mutate3 -n Duration -e 'date($3) - date($2)'
ID,In,Out,Duration
1,2023-08-25 11:24:00,2023-08-27 08:33:02,45h9m2s
2,2023-08-25 11:28:00,2023-08-26 14:17:35,26h49m35s
3,2023-08-26 11:29:00,2023-08-29 20:43:00,81h14m0s

$ cat ../testdata/datesub.csv | ./csvtk mutate3 -n Duration -e 'date($Out) - date($In)'
ID,In,Out,Duration
1,2023-08-25 11:24:00,2023-08-27 08:33:02,45h9m2s
2,2023-08-25 11:28:00,2023-08-26 14:17:35,26h49m35s
3,2023-08-26 11:29:00,2023-08-29 20:43:00,81h14m0s

$ cat ../testdata/datesub.csv | ./csvtk mutate3 -n Duration -e '(date($Out) - date($In)).Hours() | int()'
ID,In,Out,Duration
1,2023-08-25 11:24:00,2023-08-27 08:33:02,45
2,2023-08-25 11:28:00,2023-08-26 14:17:35,26
3,2023-08-26 11:29:00,2023-08-29 20:43:00,81

I basically copied mutate2.go, threw out the custom functions, and replaced govaluate with expr.

@shenwei356
Copy link
Owner Author

@moorereason That's great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants