Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of Patsy 

2# Copyright (C) 2011-2013 Nathaniel Smith <njs@pobox.com> 

3# See file LICENSE.txt for license information. 

4 

5"""patsy is a Python package for describing statistical models and building 

6design matrices. It is closely inspired by the 'formula' mini-language used in 

7R and S.""" 

8 

9import sys 

10 

11from patsy.version import __version__ 

12 

13# Do this first, to make it easy to check for warnings while testing: 

14import os 

15if os.environ.get("PATSY_FORCE_NO_WARNINGS"): 

16 import warnings 

17 warnings.filterwarnings("error", module="^patsy") 

18 del warnings 

19del os 

20 

21import patsy.origin 

22 

23class PatsyError(Exception): 

24 """This is the main error type raised by Patsy functions. 

25 

26 In addition to the usual Python exception features, you can pass a second 

27 argument to this function specifying the origin of the error; this is 

28 included in any error message, and used to help the user locate errors 

29 arising from malformed formulas. This second argument should be an 

30 :class:`Origin` object, or else an arbitrary object with a ``.origin`` 

31 attribute. (If it is neither of these things, then it will simply be 

32 ignored.) 

33 

34 For ordinary display to the user with default formatting, use 

35 ``str(exc)``. If you want to do something cleverer, you can use the 

36 ``.message`` and ``.origin`` attributes directly. (The latter may be 

37 None.) 

38 """ 

39 def __init__(self, message, origin=None): 

40 Exception.__init__(self, message) 

41 self.message = message 

42 self.origin = None 

43 self.set_origin(origin) 

44 

45 def __str__(self): 

46 if self.origin is None: 

47 return self.message 

48 else: 

49 return ("%s\n%s" 

50 % (self.message, self.origin.caretize(indent=4))) 

51 

52 def set_origin(self, origin): 

53 # This is useful to modify an exception to add origin information as 

54 # it "passes by", without losing traceback information. (In Python 3 

55 # we can use the built-in exception wrapping stuff, but it will be 

56 # some time before we can count on that...) 

57 if self.origin is None: 

58 if hasattr(origin, "origin"): 

59 origin = origin.origin 

60 if not isinstance(origin, patsy.origin.Origin): 

61 origin = None 

62 self.origin = origin 

63 

64__all__ = ["PatsyError"] 

65 

66# We make a rich API available for explicit use. To see what exactly is 

67# exported, check each module's __all__, or import this module and look at its 

68# __all__. 

69 

70def _reexport(mod): 

71 __all__.extend(mod.__all__) 

72 for var in mod.__all__: 

73 globals()[var] = getattr(mod, var) 

74 

75# This used to have less copy-paste, but explicit import statements make 

76# packaging tools like py2exe and py2app happier. Sigh. 

77import patsy.highlevel 

78_reexport(patsy.highlevel) 

79 

80import patsy.build 

81_reexport(patsy.build) 

82 

83import patsy.constraint 

84_reexport(patsy.constraint) 

85 

86import patsy.contrasts 

87_reexport(patsy.contrasts) 

88 

89import patsy.desc 

90_reexport(patsy.desc) 

91 

92import patsy.design_info 

93_reexport(patsy.design_info) 

94 

95import patsy.eval 

96_reexport(patsy.eval) 

97 

98import patsy.origin 

99_reexport(patsy.origin) 

100 

101import patsy.state 

102_reexport(patsy.state) 

103 

104import patsy.user_util 

105_reexport(patsy.user_util) 

106 

107import patsy.missing 

108_reexport(patsy.missing) 

109 

110import patsy.splines 

111_reexport(patsy.splines) 

112 

113import patsy.mgcv_cubic_splines 

114_reexport(patsy.mgcv_cubic_splines) 

115 

116# XX FIXME: we aren't exporting any of the explicit parsing interface 

117# yet. Need to figure out how to do that.