/usr/lib/python3/dist-packages/patsy/builtins.py is in python3-patsy 0.4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # This file is part of Patsy
# Copyright (C) 2011-2013 Nathaniel Smith <njs@pobox.com>
# See file LICENSE.txt for license information.
# This module sets up the namespace of stuff that is available to formulas by
# default. All formulas are interpreted in an environment that acts as if
# from patsy.builtins import *
# has been executed. (Of course, you can also execute this yourself if you
# want to use these in your regular code for some reason.)
__all__ = ["I", "Q"]
from patsy.contrasts import ContrastMatrix, Treatment, Poly, Sum, Helmert, Diff
__all__ += ["ContrastMatrix", "Treatment", "Poly", "Sum", "Helmert", "Diff"]
from patsy.categorical import C
__all__ += ["C"]
from patsy.state import center, standardize, scale
__all__ += ["center", "standardize", "scale"]
from patsy.splines import bs
__all__ += ["bs"]
from patsy.mgcv_cubic_splines import cr, cc, te
__all__ += ["cr", "cc", "te"]
def I(x):
"""The identity function. Simply returns its input unchanged.
Since Patsy's formula parser ignores anything inside a function call
syntax, this is useful to 'hide' arithmetic operations from it. For
instance::
y ~ x1 + x2
has ``x1`` and ``x2`` as two separate predictors. But in::
y ~ I(x1 + x2)
we instead have a single predictor, defined to be the sum of ``x1`` and
``x2``."""
return x
def test_I():
assert I(1) == 1
assert I(None) is None
def Q(name):
"""A way to 'quote' variable names, especially ones that do not otherwise
meet Python's variable name rules.
If ``x`` is a variable, ``Q("x")`` returns the value of ``x``. (Note that
``Q`` takes the *string* ``"x"``, not the value of ``x`` itself.) This
works even if instead of ``x``, we have a variable name that would not
otherwise be legal in Python.
For example, if you have a column of data named ``weight.in.kg``, then you
can't write::
y ~ weight.in.kg
because Python will try to find a variable named ``weight``, that has an
attribute named ``in``, that has an attribute named ``kg``. (And worse
yet, ``in`` is a reserved word, which makes this example doubly broken.)
Instead, write::
y ~ Q("weight.in.kg")
and all will be well. Note, though, that this requires embedding a Python
string inside your formula, which may require some care with your quote
marks. Some standard options include::
my_fit_function("y ~ Q('weight.in.kg')", ...)
my_fit_function('y ~ Q("weight.in.kg")', ...)
my_fit_function("y ~ Q(\\"weight.in.kg\\")", ...)
Note also that ``Q`` is an ordinary Python function, which means that you
can use it in more complex expressions. For example, this is a legal
formula::
y ~ np.sqrt(Q("weight.in.kg"))
"""
from patsy.eval import EvalEnvironment
env = EvalEnvironment.capture(1)
try:
return env.namespace[name]
except KeyError:
raise NameError("no data named %r found" % (name,))
def test_Q():
a = 1
assert Q("a") == 1
assert Q("Q") is Q
from nose.tools import assert_raises
assert_raises(NameError, Q, "asdfsadfdsad")
|