Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reducing import try/except boilerplate

Reply
Thread Tools

Reducing import try/except boilerplate

 
 
Nick Coghlan
Guest
Posts: n/a
 
      12-21-2005
Several standard library modules (e.g., cPickle/pickle, cStringIO/StringIO,
threading/dummy_threading) have versions which may not be available on all
platforms, and pure Python fallbacks that work on any platform Python
supports. Flicking through the latest version of the Python Cookbook, I
noticed many recipes that included module fallback suggestions along the lines of:

try:
import cPickle as pickle
except ImportError:
import pickle

try:
import threading
except ImportError
import dummy_threading as threading

That seems rather verbose for something that isn't that uncommon ("these
module all expose the same API, so just give me one of them in this order of
preference"). So what about:

import cPickle or pickle as pickle
import threading or dummy_threading as threading
# 'as' clause required since Python can't guess the name the programmer wants

Also:

from threading or dummy_threading import Thread
# No 'as' clause needed since the module name isn't bound


Insomnia-induced-random-ideas-are-fun-'ly yours,
Nick.

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      12-21-2005
On Thu, 22 Dec 2005 00:44:52 +1000, Nick Coghlan <(E-Mail Removed)> wrote:

>Several standard library modules (e.g., cPickle/pickle, cStringIO/StringIO,
>threading/dummy_threading) have versions which may not be available on all
>platforms, and pure Python fallbacks that work on any platform Python
>supports. Flicking through the latest version of the Python Cookbook, I
>noticed many recipes that included module fallback suggestions along the lines of:
>
>try:
> import cPickle as pickle
>except ImportError:
> import pickle
>
>try:
> import threading
>except ImportError
> import dummy_threading as threading
>
>That seems rather verbose for something that isn't that uncommon ("these
>module all expose the same API, so just give me one of them in this order of
>preference"). So what about:
>
>import cPickle or pickle as pickle
>import threading or dummy_threading as threading
># 'as' clause required since Python can't guess the name the programmer wants
>
>Also:
>
> from threading or dummy_threading import Thread
> # No 'as' clause needed since the module name isn't bound
>
>
>Insomnia-induced-random-ideas-are-fun-'ly yours,
>Nick.

Yup. Maybe we should have BIOTW (Best Idea Of The Week) nominations.
I'd say this is a candidate.

The grammar for import stuff is messy though. Have you looked to see
how this would work in? And BTW, I suppose 'or' could be an arbitrarily
long shortcutting 'or' expression? I.e.,

import x or y or z or etc as std_module_name
from x or y or x or etc import std_module_name

Anyway, +1

Hm, not to muddle your idea with featuritis, but how about allowing
string expressions in the place of names, differentiating between
bare names and bare-name-expressions at the same level as import thus:

g = 'gname'
import g as mod # => mod = __import__('g')
import (g) as mod # => mod = __import__('gname')

h = ''
import g or (h or g) or h as mod # (h or g) is a string expression => 'gname' here

Also, a bare 'and' could make its predecessor term be treated like an ordinary expression (even if
a bare name), allowing bare guard condition expressions, e.g.,

import cond and name or alternate as mod # <==> import (cond and 'name') or alternate as mod

Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM
Boilerplate template code for database add/update/delete dwok ASP .Net 2 05-11-2005 03:38 PM



Advertisments