Skip to content

Commit

Permalink
Migrate to Hy 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MonsieurV committed Nov 5, 2021
1 parent ec63b7e commit c826174
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 331 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 0.1.0

* Requires Hy `>=1.0a1`

# 0.0.5

* Works with Hy `0.20.0`
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ verify_ssl = true
name = "pypi"

[packages]
hy = "*"
hy = ">=1.0a2"
toolz = "*"

[dev-packages]
black = "*"
Expand Down
555 changes: 297 additions & 258 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="tqwgp-parser",
version="0.0.5",
version="0.1.0",
url="https://github.com/YtoTech/talk-quote-work-getpaid-parser",
license="AGPL-3.0",
author="Yoan Tournade",
Expand All @@ -24,5 +24,5 @@
},
zip_safe=False,
platforms="any",
install_requires=["hy"],
install_requires=["hy>=1.0a2", "toolz"],
)
75 changes: 5 additions & 70 deletions tqwgp_parser/parser.hy
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,12 @@
Parse the definitions of TWWGP quotes and invoices.
:copyright: (c) 2017-2021 Yoan Tournade.
"""
(import copy)
(import os)
(require [hy.extra.anaphoric [*]])

;; Utils.

(defn parse-dict-values [value mandatories optionals]
;; Use dict-comp http://docs.hylang.org/en/stable/language/api.html#dict-comp?
(setv parsed-dict {})
(for [key (.keys value)] ((fn [key]
(if (or (in key mandatories) (in key optionals))
(assoc parsed-dict key (get value key))
(print (.format "Ignoring key {}" key)))) key))
;; Check all mandatories are here.
(for [mandatory mandatories] ((fn [mandatory]
(if (not (in mandatory parsed-dict))
(raise (ValueError (.format "Missing key {}" mandatory))))) mandatory))
;; Affect None to non-set optionals.
(for [optional optionals] ((fn [optional]
(if (not (in optional parsed-dict))
(assoc parsed-dict optional None))) optional))
parsed-dict)

(defn merge-dicts [dicts]
(setv merged (.deepcopy copy (get dicts 0)))
(for [one-dict (drop 1 dicts)] ((fn [one-dict]
(.update merged one-dict)) one-dict))
merged)

(defn filter-dict [a-dict pred]
(setv new-dict {})
(ap-each (.keys a-dict)
(if (pred (get a-dict it) it)
(assoc new-dict it (get a-dict it))))
new-dict)

(defn find-in-list [l pred]
(setv element None)
(for [item l] ((fn [item])
(if (pred item)
(do
(setv element item)
(break))) l))
element)

(defn pred-has-entry-value [key value]
(fn [element]
(= (get element key) value)))

(defn get-default [value key default]
(if (in key value)
(get value key)
default))

(defn pick-by [pred value]
(setv new-value {})
(for [key (.keys value)] ((fn [key]
(if (pred key)
(assoc new-value key (get value key)))) key))
new-value)

(defn simplest-numerical-format [price]
(if (.is_integer (float price))
(int price)
price))

(defn none-or-true? [value]
(or (none? value) (bool value)))
(import [.utils [*]])

;; Data parsing and normalization.

(defn parse-all-prestations [prestations &optional [section None]]
(defn parse-all-prestations [prestations [section None]]
"""
Parse all prestations, returning a flattened list of prestations (in all-prestations)
and a list of sections.
Expand Down Expand Up @@ -123,7 +57,7 @@
"optional" (get-default section "optional" False)
}]))

(defn compute-price [prestations &optional [count-optional False]]
(defn compute-price [prestations [count-optional False]]
"""
Parse price of a flattened list of prestations
(actually any list with object containing a price property),
Expand Down Expand Up @@ -153,7 +87,7 @@
"""
(simplest-numerical-format (* (/ vat-rate 100) price)))

(defn compute-price-vat [prestations &optional [count-optional False] [vat-rate None]]
(defn compute-price-vat [prestations [count-optional False] [vat-rate None]]
"""
Compute price, as an object including VAT component, total with VAT excluded, total with VAT included ;
from a list of objects containing a price (numerical) property.
Expand All @@ -180,6 +114,7 @@
None
(str batch)))


;; Data recomposition/derivation from previously parsed data.

(defn has-section? [prestation]
Expand Down
92 changes: 92 additions & 0 deletions tqwgp_parser/utils.hy
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
; (require [hy.extra.anaphoric [ap-each]])

(import copy)
(import [toolz.itertoolz [drop]])

; Predicates.

(defn numeric? [v]
(isinstance v (, int float)))

(defn none? [v]
(= v None))

(defn none-or-true? [value]
(or (none? value) (bool value)))


; Dicts.

(defn parse-dict-values [value mandatories optionals]
;; Use dict-comp http://docs.hylang.org/en/stable/language/api.html#dict-comp?
(setv parsed-dict {})
(for [key (.keys value)] ((fn [key]
(if (or (in key mandatories) (in key optionals))
(assoc parsed-dict key (get value key))
(print (.format "Ignoring key {}" key)))) key))
;; Check all mandatories are here.
(for [mandatory mandatories] ((fn [mandatory]
(if (not (in mandatory parsed-dict))
(raise (ValueError (.format "Missing key {}" mandatory))))) mandatory))
;; Affect None to non-set optionals.
(for [optional optionals] ((fn [optional]
(if (not (in optional parsed-dict))
(assoc parsed-dict optional None))) optional))
parsed-dict)

(defn merge-dicts [dicts]
(setv merged (.deepcopy copy (get dicts 0)))
(for [one-dict (drop 1 dicts)] ((fn [one-dict]
(.update merged one-dict)) one-dict))
merged)

; (defn filter-dict [a-dict pred]
; (setv new-dict {})
; (ap-each (.keys a-dict)
; (if (pred (get a-dict it) it)
; (assoc new-dict it (get a-dict it))))
; new-dict)
(defn filter-dict [a-dict pred]
(setv new-dict {})
(for [key (.keys a-dict)]
((fn [key]
(if (pred (get a-dict key) key)
(assoc new-dict key (get a-dict key))))
key))
new-dict)

(defn pick-by [pred value]
(setv new-value {})
(for [key (.keys value)] ((fn [key]
(if (pred key)
(assoc new-value key (get value key)))) key))
new-value)

(defn get-default [value key default]
(if (in key value)
(get value key)
default))


; Lists.

(defn find-in-list [l pred]
(setv element None)
(for [item l] ((fn [item])
(if (pred item)
(do
(setv element item)
(break))) l))
element)


; Numbers.

(defn simplest-numerical-format [price]
(if (.is_integer (float price))
(int price)
price))


; Misc.

0 comments on commit c826174

Please sign in to comment.