LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Functions
lsst.daf.persistence.utils Namespace Reference

Functions

def listify (x)
 
def iterify (x)
 
def sequencify (x)
 
def setify (x)
 
def doImport (pythonType)
 

Function Documentation

◆ doImport()

def lsst.daf.persistence.utils.doImport (   pythonType)
Import a python object given an importable string

Definition at line 106 of file utils.py.

106 def doImport(pythonType):
107  """Import a python object given an importable string"""
108  try:
109  if not isinstance(pythonType, basestring):
110  raise TypeError("Unhandled type of pythonType, val:%s" % pythonType)
111  # import this pythonType dynamically
112  # pythonType is sometimes unicode with Python 2 and pybind11; this breaks the interpreter
113  pythonTypeTokenList = str(pythonType).split('.')
114  importClassString = pythonTypeTokenList.pop()
115  importClassString = importClassString.strip()
116  importPackage = ".".join(pythonTypeTokenList)
117  importType = __import__(importPackage, globals(), locals(), [importClassString], 0)
118  pythonType = getattr(importType, importClassString)
119  return pythonType
120  except ImportError:
121  pass
122  # maybe python type is a member function, in the form: path.to.object.Class.funcname
123  pythonTypeTokenList = pythonType.split('.')
124  importClassString = '.'.join(pythonTypeTokenList[0:-1])
125  importedClass = doImport(importClassString)
126  pythonType = getattr(importedClass, pythonTypeTokenList[-1])
127  return pythonType

◆ iterify()

def lsst.daf.persistence.utils.iterify (   x)
Takes any object. Returns it if it is iterable. If it
is not iterable it puts the object in a list and returns
the list. None will return an empty list. If a new list
is always required use listify(). Strings will be placed
in a list with a single element.

Definition at line 51 of file utils.py.

51 def iterify(x):
52  """Takes any object. Returns it if it is iterable. If it
53  is not iterable it puts the object in a list and returns
54  the list. None will return an empty list. If a new list
55  is always required use listify(). Strings will be placed
56  in a list with a single element.
57  """
58  if x is None:
59  x = []
60  elif isinstance(x, basestring):
61  x = [x]
62  elif hasattr(x, '__iter__'):
63  pass
64  else:
65  x = [x]
66  return x
67 
68 

◆ listify()

def lsst.daf.persistence.utils.listify (   x)
Takes any object and puts that whole object in a list:
- strings will be made into a single element in the list
- tuples will be converted to list
- lists will remain as lists
- None will be made into an empty list

Definition at line 31 of file utils.py.

31 def listify(x):
32  """Takes any object and puts that whole object in a list:
33  - strings will be made into a single element in the list
34  - tuples will be converted to list
35  - lists will remain as lists
36  - None will be made into an empty list
37  """
38  if x is None:
39  x = []
40  elif isinstance(x, basestring):
41  x = [x]
42  elif isinstance(x, dict):
43  x = [x]
44  elif hasattr(x, '__iter__'):
45  x = list(x)
46  else:
47  x = [x]
48  return x
49 
50 
daf::base::PropertyList * list
Definition: fits.cc:885

◆ sequencify()

def lsst.daf.persistence.utils.sequencify (   x)
Takes an object, if it is a sequence return it,
else put it in a tuple. Strings are not sequences.
If x is a dict, returns a sorted tuple of keys.

Definition at line 69 of file utils.py.

69 def sequencify(x):
70  """Takes an object, if it is a sequence return it,
71  else put it in a tuple. Strings are not sequences.
72  If x is a dict, returns a sorted tuple of keys."""
73  if isinstance(x, (Sequence, Set)) and not isinstance(x, basestring):
74  pass
75  elif isinstance(x, Mapping):
76  x = tuple(sorted(x.keys()))
77  else:
78  x = (x, )
79  return x
80 
81 

◆ setify()

def lsst.daf.persistence.utils.setify (   x)
Take an object x and return it in a set.

If x is a container, will create a set from the contents of the container.
If x is an object, will create a set with a single item in it.
If x is a string, will treat the string as a single object (i.e. not as a list of chars)

Definition at line 82 of file utils.py.

82 def setify(x):
83  """Take an object x and return it in a set.
84 
85  If x is a container, will create a set from the contents of the container.
86  If x is an object, will create a set with a single item in it.
87  If x is a string, will treat the string as a single object (i.e. not as a list of chars)"""
88  if x is None:
89  x = set()
90 
91  # Here we have to explicity for strings because the set initializer will use each character in a string as
92  # a separate element. We cannot use the braces initialization because x might be a list, and we do not
93  # want the list to be an item; we want each item in the list to be represented by an item in the set.
94  # Then, we have to fall back to braces init because if the item is NOT a list then the set initializer
95  # won't take it.
96  if isinstance(x, basestring):
97  x = set([x])
98  else:
99  try:
100  x = set(x)
101  except TypeError:
102  x = set([x])
103  return x
104 
105 
daf::base::PropertySet * set
Definition: fits.cc:884