From 7fe997e9f7a1049c63957ca3c9dd52f8f656c3be Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 00:23:49 +0100 Subject: [PATCH 1/3] Added name and keyword functions to core --- hy/core/language.hy | 33 ++++++++++++++++++++++++++++++--- hy/models/keyword.py | 5 +++-- tests/native_tests/language.hy | 26 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 19e8486..21c7808 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -25,7 +25,8 @@ (import [hy._compat [long-type]]) ; long for python2, int for python3 -(import [hy.models.cons [HyCons]]) +(import [hy.models.cons [HyCons]] + [hy.models.keyword [HyKeyword KEYWORD_PREFIX]]) (defn _numeric-check [x] @@ -351,10 +352,36 @@ (import functools) (map (functools.partial (fn [f args] (apply f args)) func) (apply zip lists)))) +(defn hyify [text] + "Convert text to match hy identifier" + (.replace (string text) "_" "-")) + +(defn keyword [value] + "Create a keyword from the given value. Strings numbers and even objects + with the __name__ magic will work" + (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (hyify value) + (if (string? value) + (HyKeyword (+ ":" (hyify value))) + (try + (hyify (.__name__ value)) + (catch [] (HyKeyword (+ ":" (string value)))))))) + +(defn name [value] + "Convert the given value to a string. Keyword special character will be stripped. + String will be used as is. Even objects with the __name__ magic will work" + (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (hyify (slice value 2)) + (if (string? value) + (hyify value) + (try + (hyify (. value __name__)) + (catch [] (string value)))))) + (def *exports* '[calling-module-name coll? cons cons? cycle dec distinct disassemble drop drop-while empty? even? every? first filter flatten float? gensym identity inc instance? integer - integer? integer-char? iterable? iterate iterator? - list* macroexpand macroexpand-1 neg? nil? none? nth + integer? integer-char? iterable? iterate iterator? keyword + list* macroexpand macroexpand-1 name neg? nil? none? nth numeric? odd? pos? remove repeat repeatedly rest second some string string? take take-nth take-while zero? zipwith]) diff --git a/hy/models/keyword.py b/hy/models/keyword.py index 6d25633..a3d778b 100644 --- a/hy/models/keyword.py +++ b/hy/models/keyword.py @@ -22,6 +22,7 @@ from __future__ import unicode_literals from hy.models import HyObject from hy._compat import str_type +KEYWORD_PREFIX = "\uFDD0" class HyKeyword(HyObject, str_type): """Generic Hy Keyword object. It's either a ``str`` or a ``unicode``, @@ -29,8 +30,8 @@ class HyKeyword(HyObject, str_type): """ def __new__(cls, value): - if not value.startswith("\uFDD0"): - value = "\uFDD0" + value + if not value.startswith(KEYWORD_PREFIX): + value = KEYWORD_PREFIX + value obj = str_type.__new__(cls, value) return obj diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 5e6f6c7..dc91da0 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -988,3 +988,29 @@ "NATIVE: test keyword quoting magic" (assert (= :foo "\ufdd0:foo")) (assert (= `:foo "\ufdd0:foo"))) + +(defn test-keyword-creation [] + "NATIVE: Test keyword creation" + (assert (= (keyword "foo") :foo)) + (assert (= (keyword "foo_bar") :foo-bar)) + (assert (= (keyword `foo) :foo)) + (assert (= (keyword `foo-bar) :foo-bar)) + (assert (= (keyword 'foo) :foo)) + (assert (= (keyword 'foo-bar) :foo-bar)) + (assert (= (keyword 1) :1)) + (assert (= (keyword 1.0) :1.0)) + (assert (= (keyword :foo_bar) :foo-bar))) + +(defn test-name-conversion [] + "NATIVE: Test name conversion" + (assert (= (name "foo") "foo")) + (assert (= (name "foo_bar") "foo-bar")) + (assert (= (name `foo) "foo")) + (assert (= (name `foo_bar) "foo-bar")) + (assert (= (name 'foo) "foo")) + (assert (= (name 'foo_bar) "foo-bar")) + (assert (= (name 1) "1")) + (assert (= (name 1.0) "1.0")) + (assert (= (name :foo) "foo")) + (assert (= (name :foo_bar) "foo-bar")) + (assert (= (name test-name-conversion) "test-name-conversion"))) From a49047b7a3b77e267ec9d148c2ee6ac39cbabf7f Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 00:23:49 +0100 Subject: [PATCH 2/3] Added name and keyword functions to core --- hy/core/language.hy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 21c7808..c65bcbe 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -26,7 +26,7 @@ (import [hy._compat [long-type]]) ; long for python2, int for python3 (import [hy.models.cons [HyCons]] - [hy.models.keyword [HyKeyword KEYWORD_PREFIX]]) + [hy.models.keyword [HyKeyword *keyword-prefix*]]) (defn _numeric-check [x] @@ -359,7 +359,7 @@ (defn keyword [value] "Create a keyword from the given value. Strings numbers and even objects with the __name__ magic will work" - (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (if (and (string? value) (value.startswith *keyword-prefix*)) (hyify value) (if (string? value) (HyKeyword (+ ":" (hyify value))) @@ -370,7 +370,7 @@ (defn name [value] "Convert the given value to a string. Keyword special character will be stripped. String will be used as is. Even objects with the __name__ magic will work" - (if (and (string? value) (value.startswith KEYWORD_PREFIX)) + (if (and (string? value) (value.startswith *keyword-prefix*)) (hyify (slice value 2)) (if (string? value) (hyify value) From c1b83c0265cacfbf1a3f2865c960b485fba28cbd Mon Sep 17 00:00:00 2001 From: schuster-rainer Date: Sat, 1 Mar 2014 01:00:46 +0100 Subject: [PATCH 3/3] Fixed flake8 blank line issue --- hy/models/keyword.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hy/models/keyword.py b/hy/models/keyword.py index a3d778b..32b2306 100644 --- a/hy/models/keyword.py +++ b/hy/models/keyword.py @@ -22,8 +22,10 @@ from __future__ import unicode_literals from hy.models import HyObject from hy._compat import str_type + KEYWORD_PREFIX = "\uFDD0" + class HyKeyword(HyObject, str_type): """Generic Hy Keyword object. It's either a ``str`` or a ``unicode``, depending on the Python version.