Hello fellow Python Bundle users,
I've been learning more about the Ruby bundle, and I have some ideas on how to make the Python bundle better. Some of them are blatantly stolen from the Ruby bundle, some of them I came up independently myself when I was using BBEdit, and thought they would be useful in the Python bundle.
#1: Create Dictionary From: I thought Ruby could take the selected text and make a hash out of it. (looking in the TextMate book now of course I can't find it). I wrote something similar, turning the following text into a dictionary:
a = 1 b = 2
--> result: {'a': '1', 'b': '2'}
You'll find my script at the bottom of this email
#2: Support for syntax coloring doctests
Wouldn't it be cool to have doctests syntax colored like code, and not comments?
Likewise, it looks like the folding marker for Python comments folds on a blank line. That means folding doesn't work very well for doctests or comments with blank lines in it.
#3: super() in class snippet
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
_______________________________
Bugs: * Evaluate Selection As Python doesn't work (Traceback (most recent call last): File "/tmp/temp_textmate.KE9sFL", line 5, in ? from traceback import format_exc ImportError: cannot import name format_exc )
* Documentation For Current Word doesn't work (global name sh is not defined)
_______________________________
Looking forward to everybody's thoughts on these improvements!, _Ryan Wilcox
On Dec 11, 2006, at 10:09 AM, Ryan Wilcox wrote:
#3: super() in class snippet
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
Is it common practice to use "super" in python? I know of at least one essay:
http://fuhm.net/super-harmful/
which says no, "Python's Super Considered Harmful". Don't use it. I don't know what the standard practices in the Python community actually are in regards to that particular facility.
If it's controversial, I'm not sure that automatically including it in the standard snippet, and then having most people delete it when it appears, would be a good thing.
-- Matt Anderson
Ryan,
#1: why not just use the dict command? You can just write: dict(a=1, b=2)
#2: Yes, folding is somewhat broken in the Python Bundle. This is a limitation of Textmate, and not something we can fix currently. When TM 2.0 is released, we will hopefully be able to fix the folding issue (and also provide block scopes, I can't wait). As for coloring doctests, I'm not sure if I like this idea. I think that it would be somewhat distracting, and could lead to confusion. That said, in TM2.0 it will be relatively easy to "inject" the python grammar into docstrings allowing the highlighting you want to occur.
Bugs:
Thank you for reporting them, I've fixed them.
On Dec 11, 2006, at 8:09 AM, Ryan Wilcox wrote:
Hello fellow Python Bundle users,
I've been learning more about the Ruby bundle, and I have some ideas on how to make the Python bundle better. Some of them are blatantly stolen from the Ruby bundle, some of them I came up independently myself when I was using BBEdit, and thought they would be useful in the Python bundle.
#1: Create Dictionary From: I thought Ruby could take the selected text and make a hash out of it. (looking in the TextMate book now of course I can't find it). I wrote something similar, turning the following text into a dictionary:
a = 1 b = 2 --> result: {'a': '1', 'b': '2'} You'll find my script at the bottom of this email
#2: Support for syntax coloring doctests
Wouldn't it be cool to have doctests syntax colored like code, and not comments? Likewise, it looks like the folding marker for Python comments folds on a blank line. That means folding doesn't work very well for doctests or comments with blank lines in it.
#3: super() in class snippet
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
Bugs: * Evaluate Selection As Python doesn't work (Traceback (most recent call last): File "/tmp/temp_textmate.KE9sFL", line 5, in ? from traceback import format_exc ImportError: cannot import name format_exc )
* Documentation For Current Word doesn't work (global name sh is not defined)
Looking forward to everybody's thoughts on these improvements!, _Ryan Wilcox
-- Wilcox Development Solutions: http://www.wilcoxd.com Toolsmiths for the Internet Age PGP: 0x2F4E9C31
#!/usr/bin/env python
import fileinput import re
""" dictMaker takes a formatted string and generates a Python dictionary from it.
Basically: it's easier to type in my format then to deal with all the quoting required for dictionary creation
The Format is:
key = value
NOTE that at this time quotes are made around the values. So if you're strings just don't add them (elsewise just find and replace the extra quotes)
"""
a = re.compile("(\w+) = (.+)") output = {}
for my_line in fileinput.input(): mtch = a.search(my_line) output[ mtch.group(1) ] = mtch.group(2)
print output
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Ryan Wilcox wrote:
Hello fellow Python Bundle users,
I've been learning more about the Ruby bundle, and I have some ideas on how to make the Python bundle better. Some of them are blatantly stolen from the Ruby bundle, some of them I came up independently myself when I was using BBEdit, and thought they would be useful in the Python bundle.
#1: Create Dictionary From: I thought Ruby could take the selected text and make a hash out of it. (looking in the TextMate book now of course I can't find it). I wrote something similar, turning the following text into a dictionary: a = 1 b = 2 --> result: {'a': '1', 'b': '2'}
You'll find my script at the bottom of this email
Like Alex Ross, I don't think this is particularly useful. Instead, you can just add a few characters, turning:
a = 1 b = 2
into:
dict( a = 1, b = 2 )
Or you can even take out the line breaks quite simply. A very simple regular expression can do this transformation quite handily. The main reason I see no reason for this though, is that you usually don't explicitly construct very big hashes, instead building them from some parsed file or something. Beyond that, your example has at least one problem: how do you know whether the 1 and 2 should be integers or strings?
#2: Support for syntax coloring doctests
Wouldn't it be cool to have doctests syntax colored like code, and not comments?
Yeah, save this for scope injection in 2.0. It will work quite well, I'm sure.
Likewise, it looks like the folding marker for Python comments folds on a blank line. That means folding doesn't work very well for doctests or comments with blank lines in it.
Yeah, this indeed sucks. TextMate's folding is kind of broken at the moment ;). Hopefully this one will be fixed when 2.0 comes out in a few months.
#3: super() in class snippet
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
How would this work? Can you give an example?
-Jacob
On 12/12/06, at 7:05 AM, Jacob Rus said:
#3: super() in class snippet
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
How would this work? Can you give an example?
In Action:
class SuperSnippetInAction(object): """docstring for SuperSnippetInAction""" def __init__(self, arg): super(SuperSnippetInAction, self).__init__() self.arg = arg
The 5th tab selects the whole "super..." line, for easy "fixing". (I suppose tab 5 could be places in between the ()s of the __init__ call, for easy parameter adding too
The Snippet:
class ${1:ClassName}(${2:object}): ${3/.+/"""/}${3:docstring for $1}${3/.+/"""\n/}${3/.+/\t/}def __init__(self${4/([^,])?(.*)/(?1:, )/}${4:arg}): ${5:super($1, self).__init__()} ${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][a-zA-Z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2:\t\tself.$2 = $2\n)/g} ${0:${4/(\A\Z|\A\s*,\s*\Z)|(.+)/(?1:pass)(?2:)/}}
Or, instead of super, I suppose this could be replaced with $2.__init__()
Thanks to all who replied (and pointed me to dict(), which I didn't know about. Cool!)
Hope this helps, _Ryan Wilcox
Ryan Wilcox wrote:
It would be cool if the class snippet inserted super(...) into the constructor for the class. (Is this possible with clever mirroring?)
In Action:
class SuperSnippetInAction(object): """docstring for SuperSnippetInAction""" def __init__(self, arg): super(SuperSnippetInAction, self).__init__() self.arg = arg
The 5th tab selects the whole "super..." line, for easy "fixing". (I suppose tab 5 could be places in between the ()s of the __init__ call, for easy parameter adding too
I think you should make this a separate command. Personally, I'd rather ditch the auto docstrings that were recently added as well, as they are not formatted like the docstrings I write, and often when I use the snippet I want to scribble something super fast without a docstring anyway. Adding too much to these snippets actually makes them less useful, because they just end up cluttered with unnecessary stuff that needs to be deleted half the time. But you are onto something with this. Typing out the whole super… line can be a pain, so doing it automatically would be nice, just not as part of the default class snippet.
-Jacob
On 12/16/06, at 1:12 AM, Jacob Rus said:
Ryan Wilcox wrote:
It would be cool if the class snippet inserted super(...) into the constructor for the class.
In Action:
I think you should make this a separate command.
How about two snippets triggered via "class tab".
You type "class", hit tab, then you have the popup come up, you hit 1 (for no super()), or 2 (for super()), and boom.
This would also work for the "new function" snippet - one for docstrings, one without.
That's my thoughts anyway.
Hope this helps, _Ryan Wilcox
Ryan Wilcox wrote:
On 12/16/06, at 1:12 AM, Jacob Rus said:
I think you should make this a separate command.
How about two snippets triggered via "class tab".
You type "class", hit tab, then you have the popup come up, you hit 1 (for no super()), or 2 (for super()), and boom.
This would also work for the "new function" snippet - one for docstrings, one without.
No, this just creates visual and mental clutter. You're welcome to do such a thing for yourself, but I don't want lots of menus popping up for such common snippets.