Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22212 > unrolled thread

Re: best way to create warning for obsolete functions and call new one

Started byChris Angelico <rosuav@gmail.com>
First post2012-03-27 08:50 +1100
Last post2012-03-27 08:50 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: best way to create warning for obsolete functions and call new one Chris Angelico <rosuav@gmail.com> - 2012-03-27 08:50 +1100

#22212 — Re: best way to create warning for obsolete functions and call new one

FromChris Angelico <rosuav@gmail.com>
Date2012-03-27 08:50 +1100
SubjectRe: best way to create warning for obsolete functions and call new one
Message-ID<mailman.1020.1332798644.3037.python-list@python.org>
On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N <gelonida@gmail.com> wrote:
> One option I though of would be:
>
> def obsolete_func(func):
>    def call_old(*args, **kwargs):
>        print "func is old psl use new one"
>        return func(*args, **kwargs)
>    return call_old
>
> and
>
> def get_time(a='high'):
>   return a + 'noon'

That's a reasonable idea. Incorporate Dan's suggestion of using
DeprecationWarning.

You may want to try decorator syntax:

def was(oldname):
	def _(func):
		globals()[oldname]=func
		return func
	return _

@was("get_thyme")
def get_time(a='high'):
   return a + 'noon'

That won't raise DeprecationWarning, though. It's a very simple
assignment. The was() internal function could be enhanced to do a bit
more work, but I'm not sure what version of Python you're using and
what introspection facilities you have. But if you're happy with the
old versions coming up with (*args,**kwargs) instead of their
parameter lists, it's not difficult:

def was(oldname):
	def _(func):
		def bounce(*args,**kwargs):
			# raise DeprecationWarning
			return func(*args,**kwargs)
		globals()[oldname]=bounce
		return func
	return _

I've never actually used the Python warnings module, but any line of
code you fill in at the comment will be executed any time the old name
is used. In any case, it's still used with the same convenient
decorator. You could even unify multiple functions under a single new
name:

@was("foo")
@was("bar")
def quux(spam,ham):
    return ham.eat()

Hope that helps!

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web