LSSTApplications  17.0+11,17.0+113,17.0+64,18.0.0+13,18.0.0+28,18.0.0+5,18.0.0+66,18.0.0-4-g68ffd23,18.1.0-1-g0001055+8,18.1.0-1-g03d53ef+1,18.1.0-1-g1349e88+42,18.1.0-1-g2505f39+33,18.1.0-1-g5315e5e+1,18.1.0-1-g5e4b7ea+10,18.1.0-1-g7e8fceb+1,18.1.0-1-g85f8cd4+35,18.1.0-1-gd55f500+24,18.1.0-12-g42eabe8e+26,18.1.0-14-g259bd21+5,18.1.0-14-gd04256d+31,18.1.0-2-g4903023+9,18.1.0-2-g5f9922c+11,18.1.0-2-gd3b74e5+2,18.1.0-2-gfbf3545+19,18.1.0-2-gfefb8b5+30,18.1.0-20-g4b62d031a,18.1.0-21-gb3d55290+13,18.1.0-22-gcd16eb0+1,18.1.0-3-g52aa583+16,18.1.0-3-g8f4a2b1+29,18.1.0-3-gb69f684+26,18.1.0-4-g1ee41a7+1,18.1.0-5-g6dbcb01+27,18.1.0-5-gc286bb7+3,18.1.0-6-g857e778+2,18.1.0-7-gae09a6d+14,18.1.0-8-g42b2ab3+8,18.1.0-8-gc69d46e+13,18.1.0-9-gee19f03,w.2019.42
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:902
def doImport(pythonType)
Definition: utils.py:106
daf::base::PropertyList * list
Definition: fits.cc:903