LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
dataset.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 """
24 classes for describing datasets.
25 @author Ray Plante
26 """
27 from __future__ import with_statement
28 
29 from lsst.pex.policy import Policy
30 
31 import os
32 
33 class Dataset(object):
34  """
35  a description of a dataset.
36 
37  This description is characterized by a dataset type name and a
38  set of identifiers. These attributes are access via public member
39  variables 'type' (a string) and ids (a dictionary), respectively.
40  """
41 
42  def __init__(self, type, path=None, valid=True, ids=None, **kw):
43  """
44  create the dataset
45  @param type the dataset type name
46  @param path a filesystem pathname to the file. If None, the
47  path is not known/applicable
48  @param valid a boolean flag indicating whether this refers to
49  valid dataset. This is set to False, for example,
50  if the dataset was not successfully created.
51  @param ids a dictionary of identifiers, mapping names to values.
52  the type of the identifier is context specific.
53  @param * additional named parameters are taken as
54  identifiers to be set with the given values
55  """
56  self.type = type
57  self.path = path
58  self.valid = valid
59 
60  self.ids = None
61  if ids:
62  self.ids = dict(ids)
63  if kw:
64  if self.ids is None:
65  self.ids = {}
66  for key in kw.keys():
67  self.ids[key] = kw[key]
68 
69  def __eq__(self, other):
70  """
71  return True if the given Dataset describes the same data as this
72  one.
73  """
74  if not isinstance(other, Dataset):
75  return False
76  if other.type != self.type:
77  return False
78  if len(filter(lambda d: d.ids is None, [self, other])) == 1:
79  return False
80 
81  keys = other.ids.keys()
82  if len(keys) != len(self.ids):
83  return False
84  for key in keys:
85  if not self.ids.has_key(key) or other.ids[key] != self.ids[key]:
86  return False
87  return True
88 
89  def toString(self, usePath=True):
90  """
91  return a string form if this dataset's contents
92  @param usePath if true, the path will be used available
93  """
94  if usePath and self.path:
95  return self.path
96  out = self.type
97  if self.ids is not None:
98  names = self.ids.keys()
99  names.sort()
100  for id in names:
101  out += "-%s%s" % (id, self.ids[id])
102  return out
103 
104  def __str__(self):
105  return self.toString()
106 
107  def toPolicy(self, policy=None):
108  """
109  return a policy that describes this dataset.
110  @param policy a policy instance to write into. If not provided
111  (default) a new one is created.
112  @return Policy the policy containing the description of this dataset.
113  """
114  if not policy:
115  policy = Policy()
116  if self.type: policy.set("type", self.type)
117 
118  if self.ids:
119  ids = Policy()
120  policy.set("ids", ids)
121  for id in self.ids.keys():
122  ids.set(id, self.ids[id])
123 
124  if self.path: policy.set("path", self.path)
125  if self.valid is not None: policy.set("valid", self.valid)
126 
127  return policy
128 
129  def _policy_(self):
130  return self.toPolicy()
131 
132  @staticmethod
133  def fromPolicy(policy):
134  """
135  unserialize a dataset description from a policy
136  """
137  valid = type = ids = path = None
138 
139  if policy.exists("type"): type = policy.getString("type")
140  if policy.exists("path"): path = policy.getString("path")
141  if policy.exists("valid"): valid = policy.getBool("valid")
142  if policy.exists("ids"):
143  idp = policy.getPolicy("ids")
144  ids = {}
145  for name in idp.paramNames():
146  ids[name] = idp.get(name)
147 
148  return Dataset(type, path, valid, ids)
149 
150 
151 
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169