1.1 --- a/referencing.py Tue Sep 20 23:14:43 2016 +0200
1.2 +++ b/referencing.py Tue Sep 20 23:56:38 2016 +0200
1.3 @@ -163,6 +163,15 @@
1.4
1.5 return ancestors
1.6
1.7 + def get_types(self):
1.8 +
1.9 + "Return class, instance-only and module types for this reference."
1.10 +
1.11 + class_types = self.has_kind("<class>") and [self.get_origin()] or []
1.12 + instance_types = []
1.13 + module_types = self.has_kind("<module>") and [self.get_origin()] or []
1.14 + return class_types, instance_types, module_types
1.15 +
1.16 def decode_reference(s, name=None):
1.17
1.18 "Decode 's', making a reference."
1.19 @@ -191,4 +200,62 @@
1.20 else:
1.21 return Reference("<module>", s, name)
1.22
1.23 +
1.24 +
1.25 +# Type/reference collection functions.
1.26 +
1.27 +def is_single_class_type(all_types):
1.28 +
1.29 + """
1.30 + Return whether 'all_types' is a mixture of class and instance kinds for
1.31 + a single class type.
1.32 + """
1.33 +
1.34 + kinds = set()
1.35 + types = set()
1.36 +
1.37 + for type in all_types:
1.38 + kinds.add(type.get_kind())
1.39 + types.add(type.get_origin())
1.40 +
1.41 + return len(types) == 1 and kinds == set(["<class>", "<instance>"])
1.42 +
1.43 +def combine_types(class_types, instance_types, module_types):
1.44 +
1.45 + """
1.46 + Combine 'class_types', 'instance_types', 'module_types' into a single
1.47 + list of references.
1.48 + """
1.49 +
1.50 + all_types = []
1.51 + for kind, l in [("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types)]:
1.52 + for t in l:
1.53 + all_types.append(Reference(kind, t))
1.54 + return all_types
1.55 +
1.56 +def separate_types(refs):
1.57 +
1.58 + """
1.59 + Separate 'refs' into type-specific lists, returning a tuple containing
1.60 + lists of class types, instance types, module types, function types and
1.61 + unknown "var" types.
1.62 + """
1.63 +
1.64 + class_types = []
1.65 + instance_types = []
1.66 + module_types = []
1.67 + function_types = []
1.68 + var_types = []
1.69 +
1.70 + for kind, l in [
1.71 + ("<class>", class_types), ("<instance>", instance_types), ("<module>", module_types),
1.72 + ("<function>", function_types), ("<var>", var_types)
1.73 + ]:
1.74 +
1.75 + for ref in refs:
1.76 + if ref.get_kind() == kind:
1.77 + l.append(ref.get_origin())
1.78 +
1.79 + return class_types, instance_types, module_types, function_types, var_types
1.80 +
1.81 # vim: tabstop=4 expandtab shiftwidth=4