473,569 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Leo + Python: the ultimate scripting tool

I would like to say a few (actually more than a few) words here about some
recent discoveries I have made concerning the interaction of Leo and Python.
If you don't want to hear an inventor enthuse about his work, please feel
free not to read further :-)

There are at least three, no four, no five, no six, no seven reasons why Leo
and Python work so well together. Most are new (in my mind) with the 4.1
release of Leo. The first several reasons may not seem like much. As you
will see, this appearance is completely misleading: the combination of
features is incredibly powerful.

To keep the individual posts shorter I shall list these 7 reasons and a
conclusion in "replies" to this posting.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #1
50 5450
Reasons why Leo and Python work so well together:

1. You can execute Python scripts directly from anywhere in a Leo outline.

The body text of any node in Leo outline may contain any Python script. The
Execute Script commands executes this script in a pristine environment.
However, it is trivial to get access, _from within any script_, to all the
data in the present outline. For example: the following script prints all
the headlines in the outline in which the script is embedded.

from leoGlobals import c # Get access to all data in the present outline.
c = top() # Get the "commander" associated with the topmost outline.
v = c.rootVnode() # Get the root node of the outline.
while v:
...print v.headString()
...v.threadNext () # Move to the next node in "threading" order.

For that matter, scripts can access any data anywhere on your hard drive, or
all the data available on the web, for that matter. You knew that, of
course, but this "innocent" fact will be important later.

As a result, it becomes very easy to experiment with new scripts from within
Leo.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #2
Reasons why Leo and Python work so well together:

2. Leo's outlines are the perfect tool for organizing scripts.

This is an immediate result of the organizational power of Leo's outline.
However, it continues to surprise me how powerful this really is. For
example, at present, Leo has a "Scripts" menu that also organizes scripts.
It may well be that Leo's outlines alone, combined with features that I
shall shortly discuss, will make the scripts menu redundant.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #3
Reasons why Leo and Python work so well together:

3. Scripts may use the structure of Leo's outlines.

This is a first "big" reason why the combination of Python & Leo is
spectacular. As shown in the example above, scripts may get access to the
headline or body text of any node in a Leo outline. Therefore, scripts may
use the _structure_ of the outline to gain access to a) other scripts, b)
fragments of code for exec or eval or c) other data.

It gets better...

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #4
Reasons why Leo and Python work so well together:

4. Leo's nodes naturally separate data from meta-data.

The body text of a node is the perfect place data itself (including other
scripts). The headline of the node is the perfect place to _describe_ the
data. That is, headlines are a natural place for meta-data. Scripts can
use this meta-data in many creative ways.

I first saw the power of points 2, 3 and 4 when devising regression tests
for Leo. This leads us to...

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #5
Reasons why Leo and Python work so well together:

5. Leo is the ultimate platform on which to run regression tests.

The body text of a node is the perfect place for putting regression test
data. Headlines describe the data, which is exactly what is needed for
organizing tests. Indeed, I use a script to create regression tests based
on the structure of the outline of the children of the node containing the
script (!)

For example, I run the following script to create and run unit tests of
Leo's colorizer.

import unittest,leoTes t
suite = leoTest.makeCol orSuite("colori zerTests","temp Node")
runner = unittest.TextTe stRunner()
runner.run(suit e)

This works as follows: the arguments to makeColorSuite tell it to look up
the tree for of an ancestor of the present node (the node containing this
script) for a node whose headline is "colorizerTests ". makeColorSuite then
looks for children of _that_ node for nodes containing test data. The node
whose headline is "tempNode" is used by the colorizer during the tests.
Here is the actual code of makeColorSuite:

def makeColorSuite( testParentHeadl ine,tempHeadlin e):
..."""Create a colorizer test for every descendant of testParentHeadl ine.."""
...u = testUtils() ; c = top() ; v = c.currentVnode( )
...root = u.findRootNode( v)
...temp_v = u.findNodeInTre e(root,tempHead line)
...vList = u.findSubnodesO f(testParentHea dline)
...# Create the suite and add all test cases.
...suite = unittest.makeSu ite(unittest.Te stCase)
...for v in vList:
.....test = colorTestCase(c ,v,temp_v)
.....suite.addT est(test)
...return suite

The power and simplicity of this code is breathtaking. All that is required
are two utility routines, findNodeInTree and findSubnodesOf that find
various nodes in an outline based on the outline structure and the headlines
of the nodes:

- findNodeInTree( root,tempHeadli ne) returns the node in the entire tree
contain the current node whose headline is tempHeadline.

- u.findSubnodesO f(testParentHea dline) returns a list of all children of the
node whose name is testParentHeadl ine.

In short, it is absolutely trivial for a script embedded in a Leo outline to
generate unit tests based on the data in the outline in which the script is
embedded!!

Notice, please, what does _not_ have to be done:

A. No unit tests are created statically: everything is driven by data (the
outline itself)
B. No Python test data is created _at all_. Before I saw this approach, I
was putting text for the syntax-coloring text in Python strings and passing
those strings to the regression tests. Now, the _unchanged_ data in the
body text of nodes becomes the regression test data.
C. There is no need to mark meta-data as separate from test data: headlines
are naturally distinct from body text.

We see now that Reasons 2, 3 and 4 are a lot more potent than they might
appear at first. Indeed, Leo plus Python is the ultimate merging of code,
structure and data. Scripts can use data organized in outlines. Outlines
organize scripts. Outlines (particularly their structure) are _data_ for
scripts. Outlines contains metadata for scripts. Outlines naturally keep
data and meta-data separate.

Please notice, this discussion merely hints at all the games that scripts
could play with outline structure. Just for example, scripts can easily
insert, delete and reorganize nodes. And the uses to which outline
structure may be put are practically unlimited, as we shall see later...

Let's turn now to a completely separate topic...

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #6
Reasons why Leo and Python work so well together:

6. Leo is the ultimate platform for finding and changing text.

A few days ago I have added a script-based option to Leo's Find/Change
panel. For more than 30 years I have been disgusted with tools like regular
expressions. Calling re powerful is an utter joke; it's way too complex and
way too wimpy. Script-based find-change is infinitely more powerful.
Instead of containing find text or change _text_, Leo's Find/Change panel
may contains find or change _scripts. This becomes possible because scripts
have complete access to both the structure of the outline and all of Leo's
code.

It all works so smoothly: The find script is responsible for traversing
the tree and for highlighting the found text or otherwise indicating to the
change script what the found text was. Leo dedicates a Python dict called
app.searchDict for communication between the search script and the change
script. The change script is responsible for:

- changing the text, typically by using v.setBodyString OrPane(newText) , and

- implementing undo, typically by calling
c.frame.onBodyC hanged(v,"Chang e",oldText=oldT ext).

Some details:

- When executing the search and change scripts when the user selects the
Find All or Change All commands, Leo executes the find or change scripts
repeatedly only if app.searchDict. get("continue") evaluates to True.
Otherwise these scripts are only executed once. Otherwise, scripts can use
app.searchDict as they please.

- For simplicity, most find and change scripts will ignore settings in the
Find Panel like "whole word", "pattern match", and "reverse": the scripts
know what to do! However, these settings are available to the scripts via
ivars such as c.whole_word_fl ag, etc. if desired.

- Either the find and change scripts may use Python's re module. For
example, the find script could set app.searchDict["m"] to the match object
returned by re's match method. The change script would then compute the
result, change the text and set the undo info as usual.

In effect, Leo's Find/Change panel becomes a new platform for running
scripts interactively. Leo now has all the find/change capability of
pattern matching languages like Snobol and Icon, using the capabilities of
the plain Python language. Moreover, Leo can deliver these capabilities
interactively or not depending which buttons you push in Leo's Find/Change
panel.

One last point. As I mentioned much earlier, there is no reason to confine
the find and change scripts to operate on data only within a Leo outline.
These scripts could as easily traverse your file system as the Leo outline.
But notice: scripts could pull data from the file system into the outline so
that you can see the effects of changes as the scripts operate. Again, this
can all happen interactively if you like.

No, one more last point. It's convenient to defined an initScriptFind
utility very similar to the makeColorSuite utility, used like this:

# Initialize Leo's find panel using the named children of this node.
from leoGlobals import *
initScriptFind( "Find script","Change script")
# Start searching at the top.
top().selectVno de(c.rootVnode( ))

Put the search script in a child node called "Find script" (no quotes), put
the change script in a child node called "Change script", execute this
script and the following happens:

- The body text of "Find script" node gets put in the find text of Leo's
Find/Change dialog.

- The body text of "Change script" node gets put in the find text of Leo's
Find/Change dialog.

- The Script Find radio buttons gets selected, as does the Script Change
checkbox.

- The root of the outline becomes the selected node.

Presto! Leo is ready for a script search.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #7
Reasons why Leo and Python work so well together:

7. Leo outlines are the ultimate filing cabinet.

One more feature new in Leo 4.0 greatly expands the generality and power of
Leo's outlines combined with Python. Plugins may now attach arbitrary data
to any node of a Leo outline. This is done merely by "injecting" an ivar
called unknownAttribut es ivar into Leo's fundamental vnode or tnode objects
(or adding items to the unknownAttribut es dict if it already exists). The
unknownAttribut es ivar should be a Python dictionary whose keys are xml
attribute names and whose values are the values of those attributes.

When writing a file, Leo will write "foreign" attributes in the <v> or <t>
xml elements of Leo's .leo file if the corresponding vnode or tnode contains
this unknownAttribut es ivar. Similarly, Leo will create an
unknownAttribut es ivar for a vnode or tnode if the corresponding <v> or <t>
element contains an attribute not normally used by Leo. Leo performs the
standard xml escapes when writing unknown attributes, so plugins can put
_anything_ in the unknownAttribut es dictionary. In particular, plugins may
put xml in v.unknownAttrib utes or t.unknownAttrib utes.

This means, for example, that plugins may add anything that can be
represented by xml (and that is _everything_) in any node of a Leo outline.
In effect, Leo's file format is now completely extensible. BTW, plugins can
override any aspect of Leo's behavior, so not only can Leo passively accept
the data in "extended" .leo files, but plugins can extend Leo to _use_ this
data!

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #8
Conclusion

All the power of Leo derives directly from the power, flexibility and
dynamism of Python. But making structure explicit as Leo outlines do adds
an important new dimension, literally and figuratively, to Python.
Moreover, Leo's Find/Change dialog creates a new environment from which to
run Python scripts. At long last the full power of the unification of code
and data is becoming apparent.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@chart er.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #9
In article <vq************ @corp.supernews .com>,
Edward K. Ream <ed*******@char ter.net> wrote:

All the power of Leo derives directly from the power, flexibility and
dynamism of Python. But making structure explicit as Leo outlines do
adds an important new dimension, literally and figuratively, to Python.
Moreover, Leo's Find/Change dialog creates a new environment from which
to run Python scripts. At long last the full power of the unification
of code and data is becoming apparent.


Sounds good. However, I won't try it until there's a shell-based mode;
I do too much of my work remotely.
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2474
by: Martin Maney | last post by:
Apologies if this isn't news here - I've been too busy this last week or so for even skimming the traffic here, in part because I've been messing around with Ubuntu's preview release on a spare machine... and, of course, spending time on their very busy mailing list. :-) I didn't find any mention of Ubuntu in a quick scan of recent messages,...
40
1296
by: Shufen | last post by:
Hi all, Can someone who has use PHP before and know quite well about the language, tell me what are the stuffs that Python offers and PHP doesn't. A few examples will be nice. I know about the date format problem which PHP is having but I need more examples. Thank you for any help. Shufen
63
5105
by: Davor | last post by:
Is it possible to write purely procedural code in Python, or the OO constructs in both language and supporting libraries have got so embedded that it's impossible to avoid them? Also, is anyone aware of any scripting language that could be considered as "Python minus OO stuff"? (As you can see I'm completely new to Python and initially...
68
5807
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
20
2136
by: xeys_00 | last post by:
I posted a article earlier pertaining programming for my boss. Now I am gonna ask a question about programming for myself. I just finished my first C++ Class. Next semester is a class on encryption(and it's probably gonna be a math class too). And finally back in programming in the fall with C++ and Java 1. The C++ will cover pointers, and...
1
1601
by: Josiah Carlson | last post by:
Perhaps some of you would get a kick out of this. - Josiah Like XML, scripting was extremely useful as both a mod tool and an internal development tool. If you don't have any need to expose code and algorithms in a simple and safe way to others, you can argue that providing a scripting language is not worth the effort. However, if you...
8
2112
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
17
12445
by: chewie54 | last post by:
Hello, As an electronics engineer I use some very expensive EDA CAD tool programs that are scriptable using Tcl. I was wondering why these companies have choose to use Tcl instead of Python. Some of these are: Mentor Graphics ModelTech VHDL and Verilog simulator Synopsys Design Compiler and Primetime Static Timing Analyzer Actel...
0
7711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.