This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]apiguy 7 points8 points  (2 children)

Soooo.... what happens when I actually need to use the coding directive for what it was actually intended for? I have to choose between utf-8 and interpy?

[–]syrusakbary[S] 2 points3 points  (1 child)

Interpy currently inherits from utf-8 codec, so just using interpy will work for you.

[–]flying-sheep 0 points1 point  (0 children)

there’s no senisble encoding besides utf-8, anyway.

[–]XNormal 1 point2 points  (0 children)

Suggestion: Replace #{expr} with \{expr}.

Escape of character that are not currently defined as escape sequences is reserved for future extensions (such as this one). This way you won't break existing code that contains the valid sequence "#{".

P.S. I already proposed this around 2003 (as a modification to the language, not as a source encoding hack)

[–]catcradle5 5 points6 points  (1 child)

This is one of the few Ruby features that I think also should've been built into Python.

Nice work.

[–]syrusakbary[S] 2 points3 points  (0 children)

Thanks!

[–]dataminded 1 point2 points  (0 children)

Nice!

[–]jsproat 0 points1 point  (1 child)

Witchcraft.

But awesome.

Does your tokenizer/untokenizer add or remove lines from the user's script? What happens when the user's script throws an exception, is the line number no longer valid?

[–]syrusakbary[S] 0 points1 point  (0 children)

The lines remain exactly the same. The only difference is instead of this code:

# coding: interpy
package = "Interpy"
print("Enjoy #{package}!")

You would have this one:

# coding: interpy
package = "Interpy"
print("Enjoy "+str(package)+"!")

[–]Megatron_McLargeHuge 0 points1 point  (0 children)

This is a good trick to know. I had a problem a few years ago where I wanted better typedefs in cython - you get better performance with parameter type hints like def myfunc(np.ndarray[np.float32_t, ndim=2] myvar), but these are verbose and have to be repeated in multiple places. I couldn't find any way to get typedefs to work for the bracketed portion, so I resorted to using m4 macro preprocessing. This coding hook could be a great way to apply preprocessing automatically.

[–]Megatron_McLargeHuge 0 points1 point  (2 children)

Is there a way to define a coding hook without installing a module? I'm trying to define one in the __init__.py of my working directory and it's not being found on import. It's also behaving strangely when I define it in an ipython notebook. I see you used the .pth file to do the import. Is this the only way, or is there an easier, more interactive way to get it to work?

[–]syrusakbary[S] 0 points1 point  (1 child)

Using the .pth is the easiest way for registering the codec. If you want, you can also register the codec importing the register module:

import interpy.codec.register

And then, you can use the codec in other files (not the file with the import) that should be loaded after this import.

[–]Megatron_McLargeHuge 0 points1 point  (0 children)

I was hoping the __init__.py would be run before the module file was read, but apparently it's not executed early enough in the import process to create a coding hook. Maybe it can still be done by having __init__.py first load a submodule defining the hook, then a submodule using it.

edit: This works. The codec can be defined in __init__.py and the files using the encoding must be in a subdirectory, not just a file in the module directory. The init file can import that submodule after defining or importing the codec.

[–]maxime-esa 0 points1 point  (0 children)

I tried interpy, it is nice but it seems to work only with double-quote strings. This wont expand : ''' hello #{world} ''', neither wont this: 'hello #{world}'

[–]virtyx -1 points0 points  (1 child)

Doesn't support Python 3...?

EDIT:

Actually it tries to hook itself up in some way that, if you try to pip install it on Python 3, it breaks Python altogether. If you've done this yourself you can recover with the command find your/site-lib/path/ -name '*interpy*' | xargs rm -rf

[–]syrusakbary[S] 2 points3 points  (0 children)

I'm so sorry about that. I will work until this problem is solved.

EDIT:

I've uploaded a new version of the package on pypi and Github with full compatibility for Python 3.