LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2016 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 from past.builtins import basestring
25 
26 from collections.abc import Sequence, Set, Mapping
27 
28 
29 # -*- python -*-
30 
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 
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 
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 
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 
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
daf::base::PropertySet * set
Definition: fits.cc:884
def doImport(pythonType)
Definition: utils.py:106
daf::base::PropertyList * list
Definition: fits.cc:885