Skip to content

Commit

Permalink
ENH: Avoid poluting the global namespace
Browse files Browse the repository at this point in the history
Encapsulate intialization in a staticmember function
to avoid poluting the global namespace.
  • Loading branch information
hjmjohnson committed Oct 19, 2020
1 parent 44083b6 commit bb1c7b5
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions Wrapping/Generators/Python/itkTypes.py
Expand Up @@ -16,11 +16,10 @@
#
# ==========================================================================*/

import os
import numpy as np
from typing import Dict, Any


# noinspection PyPep8Naming
class itkCType:
__c_types__: Dict[str, Any] = {}
__c_types_for_dtype__: Dict[str, Any] = {}
Expand Down Expand Up @@ -66,23 +65,49 @@ def GetCTypeForDType(np_dtype):
except KeyError:
return None

@staticmethod
def initialize_c_types_once():
"""
This function is intended to be run only one time
"""
import os
import numpy as np

_F = itkCType("float", "F", np.float32)
_D = itkCType("double", "D", np.float64)
_UC = itkCType("unsigned char", "UC", np.uint8)
_US = itkCType("unsigned short", "US", np.uint16)
_UI = itkCType("unsigned int", "UI", np.uint32)
if os.name == "nt":
_UL = itkCType("unsigned long", "UL", np.uint32)
_SL = itkCType("signed long", "SL", np.int32)
_LD = itkCType("long double", "LD")
else:
_UL = itkCType("unsigned long", "UL", np.uint64)
_SL = itkCType("signed long", "SL", np.int64)
_LD = itkCType("long double", "LD", np.float128)
_ULL = itkCType("unsigned long long", "ULL", np.uint64)
_SC = itkCType("signed char", "SC", np.int8)
_SS = itkCType("signed short", "SS", np.int16)
_SI = itkCType("signed int", "SI", np.int32)
_SLL = itkCType("signed long long", "SLL", np.int64)
_B = itkCType("bool", "B", np.bool)
return _F, _D, _UC, _US, _UI, _UL, _SL, _LD, _ULL, _SC, _SS, _SI, _SLL, _B


F = itkCType("float", "F", np.float32)
D = itkCType("double", "D", np.float64)
UC = itkCType("unsigned char", "UC", np.uint8)
US = itkCType("unsigned short", "US", np.uint16)
UI = itkCType("unsigned int", "UI", np.uint32)
if os.name == "nt":
UL = itkCType("unsigned long", "UL", np.uint32)
SL = itkCType("signed long", "SL", np.int32)
LD = itkCType("long double", "LD")
else:
UL = itkCType("unsigned long", "UL", np.uint64)
SL = itkCType("signed long", "SL", np.int64)
LD = itkCType("long double", "LD", np.float128)
ULL = itkCType("unsigned long long", "ULL", np.uint64)
SC = itkCType("signed char", "SC", np.int8)
SS = itkCType("signed short", "SS", np.int16)
SI = itkCType("signed int", "SI", np.int32)
SLL = itkCType("signed long long", "SLL", np.int64)
B = itkCType("bool", "B", np.bool)
(
F,
D,
UC,
US,
UI,
UL,
SL,
LD,
ULL,
SC,
SS,
SI,
SLL,
B,
) = itkCType.initialize_c_types_once()

0 comments on commit bb1c7b5

Please sign in to comment.