Skip to content

Commit

Permalink
issue #45: py2 & 3 compatible use of abc (Gridder)
Browse files Browse the repository at this point in the history
making Gridder an abstract class by requiring all derived classes to
override its call method.
  • Loading branch information
dkriegner committed Oct 28, 2017
1 parent de3a6a1 commit 64f4506
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions xrayutilities/gridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
# Copyright (C) 2009-2010, 2013
# Eugen Wintersberger <eugen.wintersberger@desy.de>
# Copyright (C) 2009 Mario Keplinger <mario.keplinger@jku.at>
# Copyright (C) 2009-2016 Dominik Kriegner <dominik.kriegner@gmail.com>
# Copyright (C) 2009-2017 Dominik Kriegner <dominik.kriegner@gmail.com>

import abc

import numpy

from . import config, cxrayutilities, utilities
from .exception import InputError

try: # works in Python >3.4
ABC = abc.ABC
except: # Python 2.7
ABC = abc.ABCMeta('ABC', (object, ), {'__slots__': ()})


def delta(min_value, max_value, n):
"""
Expand Down Expand Up @@ -68,8 +75,7 @@ def ones(*args):
return numpy.ones(args, dtype=numpy.double)


class Gridder(object):

class Gridder(ABC):
"""
Basis class for gridders in xrayutilities. A gridder is a function mapping
irregular spaced data onto a regular grid by binning the data into equally
Expand Down Expand Up @@ -105,9 +111,22 @@ def __init__(self):
# no data initialization necessary in c-code
self.flags = utilities.set_bit(self.flags, 0)

if not hasattr(self, '_gdata'):
self._gdata = numpy.empty(0)
if not hasattr(self, '_gnorm'):
self._gnorm = numpy.empty(0)

if config.VERBOSITY >= config.INFO_ALL:
self.flags = utilities.set_bit(self.flags, 3)

@abc.abstractmethod
def __call__(self):
"""
abstract call method which every implementation of a Gridder has to
override
"""
pass

def Normalize(self, bool):
"""
set or unset the normalization flag. Normalization needs to be done to
Expand Down

1 comment on commit 64f4506

@eugenwintersberger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Go ahead with this. BTW - this is good to know. Maybe I can use it in my own projects too ;)

Please sign in to comment.