Python pattern repository

Bengt Richter bokr at oz.net
Tue Oct 15 19:09:06 EDT 2002


On Tue, 15 Oct 2002 18:33:13 GMT, Robin Munn <rmunn at pobox.com> wrote:

>It's been a couple of days since Holger Krekel's post to the "Buffering
>control in python?" thread and I haven't seen anyone start up a thread
>on this subject, so I'm starting one. I'd also like to see this happen.
>I don't have server space to contribute, but I could help with coding,
>administration, etc.
>
>Holger's original post is duplicated below.
>
>>From: holger krekel <pyth at devel.trillke.net>
>>Newsgroups: comp.lang.python
>>Subject: Re: Buffering control in python?
>>Date: Sun, 13 Oct 2002 10:31:08 +0200
>>
>>Hi Bengt,
>>
>>Bengt Richter wrote:
>>> ...
>>> 
>>> >Yip, proxying is a nice pattern especially with python.
>>> >
>>> Is there a Python pattern repository somewhere?
>>
>>No that i know off. The closest is the aspn-cookbook. 
>>
>>Heck, I can't believe it. 
>>
>>We still don't have a useful repository for all
>>the nice snippets, recipes and patterns posted and
>>discussed everywhere.  Although there is plenty of nice 
>>web-software, mailing lists and whatnot written in python.
>>Sometimes people say that we have a great standard lib
>>aka "batteries included".  True, but i don't think that's
>>enough.  E.g. 
>>
>>1) i have figured out two screens of code
>>   to use the Spread-module for basic things.  
>>   Is there a generic place to publish this example
>>   so that others can find it, use it, discuss it, 
>>   enhance it?
>>
>>2) i have a nice filename class which has all the os.path
>>   methods and filtering walks and and and. It needs some
>>   work but if people use it i would definitely enhance it.
>>   Now am i supposed to setup a sourceforge project for it? 
>>   No, thanks.  
>>
>>3) i have done a new introspecting rlcompleter ...
>>
>>4) i need a small class that ...
>>
>>hopefully, we as a community will finally come up with a
>>hybrid web/mail approach (aka roundup) to serve these needs.
>>I am definitely interested to code, implement/design concepts
>>and offer server-space/cvs for it.
>>
>>sorry, but i couldn't help it :-)
>>
>>regards,
>>
>>    holger
>
>
>-- 
>Robin Munn
>rmunn at pobox.com

==========================================

The fact that there is so much that can be found via Google
(which deserves some kind of Nobel Prize for increasing the
planet's aggregate IQ several notches, IMO) means that there
is a kind of distributed repository already.

It just isn't optimally indexed and organized for retrieval
of python code and info. But my thought was that we could
leverage Google and newsgroups etc. to improve on the retrieval,
rather than trying to concentrate material in various specialized
locations such as Parnassus and the cookbook (which are great,
don't get me wrong).

Introducing PyPAN: Python Pervasive Archive Network ;-)

Here's the concept: If you want to include your code snippet
in the PyPAN, post it embedded in a document that Google will see.

You embed it for easy extraction by putting a PyPAN expression
in the first and last (+/- 1, discussed later[1]) lines of your snippet, e.g.,

# ++PyPAN++ mySnippet.py /clp/forcomment/ -- minimal PyPan snippet
def mySnippet():
    print 'Hello PyPAN!'
# --PyPAN--

I think Google would find '++PyPan++' and show an interesting list. The "/clp/forcomment/"
part of the expression is optional, but the intent is to express the location of mySnippet.py
in a classification hierarchy, to aid in searching, to limit hits to particular topics etc.
mySnippet.py is a recommended file name, and comes first after the '++PyPAN++' tag.

The classification path is also for optional use as an actual directory path
which can be rooted anywhere convenient for the user (e.g., ~/PyPAN or C:\pywk\PyPan etc.)
and thereby support automatic extraction/downloading/placement from e.g., newsgroup archives,
disk files, etc.

One common usage would be to see a PyPAN snippet in a post -- like the above in this post.
To make it available to the extraction tool as a file, I wrote a little program [2] called
getclip.exe which simply gets the text from the windows clipboard and writes it to stdout.
This makes the clipboard visible as a file object using os.popen('getclip') -- which
you can pass to anything that wants to read a file. (getclip is also handy outside of Python,
since you can easily pipe the output or redirect it to a file, without having to go to an
editor an pasting and saving-as. Instead you just type getclip>theFile.txt).

The intent is not to require you to select the exact lines, but just do a select-all, copy
or whatever is easy. Then getclip will make all that available to the actual snippet extractor,
which can put it in particular directories, etc.

I am putting together a PyPAN.py module to provide convenient methods for retrieving PyPAN
snippets from clipboard, files, or urls, etc. by regex pattern matches, but it's not finished.
It will be runnable from the command line or importable for programmatic use. There will be
options for file placement similar to winzip extraction. I.e., you can ignore paths and put
everything in a specified directory, or you can root the paths where you like etc. I guess if
there is no interest in PyPAN, I may only be able to retrieve my own snippets ;-)

In any case, I would be interested in hearing of any standard hierarchy for classifying software.
Is there real librarian in the house?

---------------
[1] Variations on the PyPAN tags:
(Note that PyPAN will search based on space-delimited tags, therefore quoting them as in the
following makes them safe against inadvertently interfering with searching for an actual snippet
like the (not quite) minimal one above).

'++PyPAN++'   => start with current line
'++PyPAN++-'  => start with previous line
'++PyPAN+++'  => start with next line
'++PyPAN--'   => reserved for future expressions within a snippet
'--PyPAN--'   => end with current line
'--PyPAN---'  => end with previous line
'--PyPAN--+'  => end with next line
---------------

[2]
/* ++PyPAN++ getclip.c -- get and write win32 clipboard text to stdout */
/*
** To compile with msvc++60 at command line use
**      cl getclip.c /link /defaultlib:user32
*/

#include <io.h>
#include <string.h>
#include <windows.h>

int main (){
    HANDLE hClipData;                   /* handle to clip data  */ 
    LPSTR lpClipData;                   /* pointer to clip data */ 
    if (!OpenClipboard(NULL)) return 0; /* NULL <=> current task */
    /* get text from the clipboard */ 
    if( (hClipData = GetClipboardData(CF_TEXT)) &&
        (lpClipData = GlobalLock(hClipData))
    ){ 
        write(1, lpClipData, strlen(lpClipData)); /*text string to stdout */
        GlobalUnlock(hClipData); 
        CloseClipboard(); return 0;
    } else {
        CloseClipboard(); return 1; 
    }
}
/* --PyPAN-- */


Note: The cl command assumes environment settings, which
you can set by invoking D:\VC98\Bin\VCVARS32.BAT (or
whatever your path to it is).

BTW, getclip.exe is not big (freshly recompiled):

    02-10-15  15:58                 24,576 getclip.exe

BTW2, ISTM it ought to be possible to make a getclip for unix (maybe talking to gpm or
some gui thing?) so that os.popen(getclip) will be a platform-independent thing to write.

Further ideas?

Regards,
Bengt Richter



More information about the Python-list mailing list