hy icon indicating copy to clipboard operation
hy copied to clipboard

Python-> hy converter

Open johanatan opened this issue 5 years ago • 14 comments

Hi,

Does there exist a Python -> hy converter? If not, how difficult would it be to create?

I’d imagine that the project would get a lot more adoption with this in place as it would allow those who will never be convinced of the goodness of s-exprs to interoperate more closely with those who have been convinced.

johanatan avatar Jan 08 '19 06:01 johanatan

https://github.com/woodrush/py2hy

I don't think it's up to date though. See #876.

gilch avatar Jan 08 '19 07:01 gilch

Not an immediately usable answer, but I recently wrote something about Hy <-> Python AST (i.e. round-trip) compilation: Readable Strings and Relational Programming in Hy.

brandonwillard avatar Jan 08 '19 18:01 brandonwillard

@brandonwillard did you mean "equivalent and not exactly equal"? rather than:

might only be equal and not exactly equivalent

johanatan avatar Jan 22 '19 20:01 johanatan

And, very interesting article btw.

Since this doesn't quite seem possible in practice currently, does anyone have advice for mixing Hy in to an existing Python team? Assuming that co-workers want to continue to use Python syntax and I want to use Hy syntax, what is the best way at this point in time to facilitate it?

I was thinking that I could "own" any modules I write in Hy, be responsible for any feature or modification requests to them and if at some point in time others take a keen enough interest in it, do a one-way translation to Python and let them "takeover". Would that work?

johanatan avatar Jan 22 '19 20:01 johanatan

@brandonwillard did you mean "equivalent and not exactly equal"? rather than:

might only be equal and not exactly equivalent

Yeah, looks like it.

brandonwillard avatar Jan 22 '19 20:01 brandonwillard

Since this doesn't quite seem possible in practice currently, does anyone have advice for mixing Hy in to an existing Python team? Assuming that co-workers want to continue to use Python syntax and I want to use Hy syntax, what is the best way at this point in time to facilitate it?

I was thinking that I could "own" any modules I write in Hy, be responsible for any feature or modification requests to them and if at some point in time others take a keen enough interest in it, do a one-way translation to Python and let them "takeover". Would that work?

Short of getting your co-workers interested in Hy themselves, that's probably the way to do it, yeah. The code produced by hy2py isn't always the prettiest, but it's an excellent starting point for a Python translation of a Hy program, if nothing else. And it should be 100% semantically equivalent to the Python original, as of Hy 0.15.

Kodiologist avatar Jan 22 '19 21:01 Kodiologist

From perspective of a hy freshman without lisp experience, it would be a lot better if there's an offical tool. An officail transpiler can make hy -> py and translate back py -> hy, and content stay same.

loynoir avatar Feb 28 '21 13:02 loynoir

Hi, I'm the author of py2hy. I'm sorry I wasn't able to work on py2hy for a long time. I'm now planning to update it for compatibility with the latest version. Congratulations on Hy reaching Version 1.0!

Depending on how the py2hy project turns out, I am thinking of merging py2hy to an official command inside Hy's main repo. The first issue for this is relicensing, for license compatibility with Hy. I initially licensed py2hy with LGPL and am planning to relicense it to Expat to make sure we have license-compatible code. I'm fully OK with this, but since I'm currently having a pull request to py2hy that updates it to be Hy 0.18-compatible, I'm currently asking the author of the pull request if they're OK with relicensing as well. I also sent an email to the author. Even after merging the pull request, I probably still need to update things to make it Hy-1.0 compatible, which I plan to work on as well. Once I get the licensing issue resolved I am planning to start rewriting the code for compatibility.

woodrush avatar Jan 20 '22 12:01 woodrush

To be clear, we're still not at 1.0. Work you do to update your code for the current master will help you get to 1.0 compatibility, but 1.0 still doesn't exist yet.

I am thinking of merging py2hy to an official command inside Hy's main repo

If you can get your program to work without actually being part of Hy itself, then by all means continue with that. Hy is already big and complicated, so there's no benefit to trying to squeeze more stuff into the same repository. I'm open to moving your project into the hylang organization if you'd like to make it more official, though.

LGPL is fine by me. I'm pro-copyleft, and I would've GPLed Hy if I'd been the one to start it.

Kodiologist avatar Jan 20 '22 21:01 Kodiologist

Sorry, I was looking at the repository's tag for the version. I checked pip and saw that we're at 0.20.0 now. I'll be working on the compatibility with that version.

I'm open to moving your project into the hylang organization if you'd like to make it more official, though.

Thanks for your support. I'll definitely be looking into that option once I finish it.

LGPL is fine by me. I'm pro-copyleft, and I would've GPLed Hy if I'd been the one to start it.

Thanks. Due to the current situation with py2hy I might stick with LGPL.

woodrush avatar Jan 21 '22 13:01 woodrush

I checked pip and saw that we're at 0.20.0 now.

~Not exactly; that's just the last non-alpha release. All further releases have been and will be alphas until 1.0, whenever that is. Your choice whether to aim for 0.20.0 or follow the alphas.~ Update: since #2245, we're back to making 0.* releases.

Kodiologist avatar Jan 21 '22 13:01 Kodiologist

I see. I'll make various versions for py2hy as well then, corresponding to the Hy versions.

woodrush avatar Jan 21 '22 13:01 woodrush

One workaround is to wrap the Python code into pys and then call the functions from Hy, like so:

(pys "
def hello_world(): 
    print('Hello, world!')
")
(hello-world)

Outputs: Hello, world!

I find this helpful when I want to use Python code snippets in Hy.

asimjalis avatar Dec 09 '22 17:12 asimjalis

You can also just import Python code from a file, and there are other options; see "Interoperability" in the manual.

Kodiologist avatar Dec 09 '22 17:12 Kodiologist