Showing posts with label pythonic-plsql. Show all posts
Showing posts with label pythonic-plsql. Show all posts

Thursday, April 22, 2010

Pythonic PL/SQL

Pythonic PL/SQL exists!

https://code.launchpad.net/~catherine-devlin/pythonic-plsql/trunk

I imagine it will grow in fits and starts as my job needs dictate, but it shouldn't ever be abandoned, since I'm incorporating it gleefully into my own production code.

So far, format (Python 3-style string formatting) and join are available. I'm not using object-oriented PL/SQL at all (I find it very unsatisfactory), so instead of
','.join(mylist), you use pythonic.join(',', mylist).

I'm very open to contributions from others! There's plenty of room to participate, since implementation has barely begun.

There are Quest Code Tester tests for it, too, but unfortunately that's a closed-source project and I have to check on what the rules are for releasing tests produced with it.

Of course, most hard-core PL/SQL developers will already have written or found code to handle basic, common operations like this. But, by sticking close to names and APIs we're familiar with from another increasingly well-known language, I hope to reduce the amount of brainspace you need to remember how to do it all.

Usage samples::
   
DECLARE
crewlist pythonic.string_list
:= pythonic.string_list ('Wash', 'Kaylee', 'Mal');
crewdictionary pythonic.string_dictionary;
curs SYS_REFCURSOR;
template VARCHAR2 (100);
BEGIN
DBMS_OUTPUT.PUT_LINE (pythonic.join ('; ', crewlist));
-- output: "Wash; Kaylee; Mal"
OPEN curs FOR
SELECT name FROM crew ORDER BY name;
DBMS_OUTPUT.PUT_LINE (pythonic.join (' *** ', curs));
-- output: "Kaylee *** Mal *** Wash"
template := '{2}! {1} says we''d better replace that catalyzer!';
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, NULL, 'Kaylee', 'Captain'));
-- output: "Captain! Kaylee says we'd better replace that catalyzer!"
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, crewlist));
-- output: "Mal! Kaylee says we'd better replace that catalyzer!"
crewdictionary ('First Officer') := 'Zoe';
crewdictionary ('Public Relations') := 'Jayne';
template := '{First Officer}: (to {Public Relations}) "I can hurt you."';
DBMS_OUTPUT.PUT_LINE (pythonic.format (template, crewdictionary));
-- output: Zoe: (to Jayne) "I can hurt you."
END;

Monday, April 19, 2010

P(ythonic)L/SQL?

I'm itching to begin creating a bunch of PL/SQL packages that will emulate certain handy Python features. Before I sink too much time into it, does the LazyWeb have anything to recommend? Or, perhaps, volunteers to help make the dream come true?

I want to start with a package for Pythonic string manipulation:

'The {0} brown fox jumped over the {1} dog'.format('quick','lazy')
.join()
.split()
.splitlines()

... and so forth

I think this package would not just implement the ones that don't exist in SQL or PL/SQL, it will provide Pythonically familiar names and interfaces to the ones that do exist.

Granted, there's only so Pythonic you can get in PL/SQL. A perfect set of packages would be a heroic task. Still, it's easy to imagine helping PL/SQL to suck less...