On Sun, Aug 21, 2011 at 12:32 PM, Luke <hazelnu...@gmail.com> wrote:
> On Sun, Aug 21, 2011 at 10:42 AM, Aaron Meurer <asmeu...@gmail.com> wrote:
>> On Sat, Aug 20, 2011 at 7:34 PM, Luke <hazelnu...@gmail.com> wrote:
>>> On Sat, Aug 20, 2011 at 3:37 PM, Aaron Meurer <asmeu...@gmail.com> wrote:
>>>> I don't know of any doctests in SymPy that do this.  Why can't you put
>>>> imports in each doctest?  I'd highly recommend it.
>>>
>>> The reason for not wanting to do the imports is as follows.  We have a
>>> class (Kane) that implements an algorithm, and there are many steps to
>>> that algorithm.  Setting up the proper execution context for each
>>> doctest requires that each docstring replicate a lot of import
>>> statements and code that are done in previous docstrings.  Looking at
>>> the docstrings for this gets cumbersome pretty quickly because of all
>>> the "setup" code that is required to be in each and every docstring.
>>
>> Maybe create a helper function that does it all for you and import that.
>>
>>>
>>>>
>>>> To see if this can be done, you'll probably have to do what I would
>>>> end up doing, which is to read the source.  Look at
>>>> sympy/utilities/runtests.py and also the source for Python's
>>>> doctest.py ("import doctest; doctest??" in IPython will tell you where
>>>> it is locally).  I can tell you that how our doctest runner works is
>>>> that it subclasses the Python doctest, over writting stuff that is
>>>> broken there in at least one Python version.
>>>>
>>>
>>> Ok. Something in testmod() must be different from the default doctest
>>> module, I'll give it a look.
>>>
>>>> By the way, when you say "running Python on this file," do you mean
>>>> running "python -m doctest myfile.py"?
>>>>
>>>
>>> I mean "python myfile.py".
>>
>> Running a file and running the doctests in the file are different
>> things.  What does python -m doctest myfile.py give?
>>
>
> Yes, they are two different things.  It fails in the same way running
> sympy's doctest on the file fails.  The reason is that the code at the
> end of the file is not run:
> if __name__ == "__main__":
>    import doctest
>    from sympy import Symbol
>    doctest.testmod(globs={'Symbol' : Symbol})
>
> According to the python man page, the -m flag "runs the file as a
> script", which I presume means "from top to bottom", and __name__ !=
> "__main__" within the execution context.  What is the main motivation
> for executing doctests in this fashion as opposed to the one which
> requires the above if statement?
>
> Is it safe to assume that all sympy doctests should be written such
> that they will pass when "python -m doctest mymodule.py" is run?  Or
> is there a way to change how the doctests are run for a particular
> file, i.e., so that doctests are tested by the if statement above when
> "python mymodule.py" is run?
>
> Luke

-m runs the doctest.py file as a script, with your file as the command
line argument.  This is the standard way to run the standard Python
doctest on a file.  Just running python myfile.py should not run any
doctests, just the code in the module itself.

Aaron Meurer

>
>> Aaron Meurer
>>
>>>
>>> ~Luke
>>>
>>>> Aaron Meurer
>>>>
>>>> On Sat, Aug 20, 2011 at 3:54 PM, Luke <hazelnu...@gmail.com> wrote:
>>>>> Hello,
>>>>>  I would like to alter the execution context of some doctests so that
>>>>> I don't have redundant import statements in every doctest.  Python's
>>>>> doctest.testmod() function allows one to do all the common import
>>>>> statements in one place (at the bottom of your module), and pass those
>>>>> names so that all doctests have those names available when they are
>>>>> run.
>>>>>
>>>>> A simple example of this is:
>>>>> #!/usr/bin/env python
>>>>> def foo(bar):
>>>>>    """
>>>>>    >>> x = Symbol('x')
>>>>>    >>> print(x)
>>>>>    x
>>>>>
>>>>>    """
>>>>>    pass
>>>>>
>>>>> if __name__ == "__main__":
>>>>>    import doctest
>>>>>    from sympy import Symbol
>>>>>    doctest.testmod(globs={'Symbol' : Symbol})
>>>>>
>>>>>
>>>>> Running python on this file works (the tests pass even though Symbol
>>>>> isn't explicitly imported within the foo function's doctest).
>>>>> However, sympy's doctest fails with these errors:
>>>>>
>>>>> ../../bin/doctest doctest_globaldict.py
>>>>> ====================================================================================
>>>>> test process starts
>>>>> =====================================================================================
>>>>> executable:   /usr/bin/python  (2.7.1-final-0)
>>>>> architecture: 64-bit
>>>>> cache:        yes
>>>>> ground types: python
>>>>>
>>>>> sympy/physics/doctest_globaldict.py[1] F
>>>>>
>>>>>                                          [FAIL]
>>>>>
>>>>> ______________________________________________________________________________________________________________________________________________________________________________________________
>>>>> ____________________________________________________________________________
>>>>> sympy.physics.doctest_globaldict.foo
>>>>> ____________________________________________________________________________
>>>>> File "/home/luke/repos/sympy/sympy/physics/doctest_globaldict.py",
>>>>> line 4, in sympy.physics.doctest_globaldict.foo
>>>>> Failed example:
>>>>>    x = Symbol('x')
>>>>> Exception raised:
>>>>>    Traceback (most recent call last):
>>>>>      File "/usr/lib/python2.7/doctest.py", line 1254, in __run
>>>>>        compileflags, 1) in test.globs
>>>>>      File "<doctest sympy.physics.doctest_globaldict.foo[0]>", line
>>>>> 1, in <module>
>>>>>        x = Symbol('x')
>>>>>    NameError: name 'Symbol' is not defined
>>>>> **********************************************************************
>>>>> File "/home/luke/repos/sympy/sympy/physics/doctest_globaldict.py",
>>>>> line 5, in sympy.physics.doctest_globaldict.foo
>>>>> Failed example:
>>>>>    print(x)
>>>>> Exception raised:
>>>>>    Traceback (most recent call last):
>>>>>      File "/usr/lib/python2.7/doctest.py", line 1254, in __run
>>>>>        compileflags, 1) in test.globs
>>>>>      File "<doctest sympy.physics.doctest_globaldict.foo[1]>", line
>>>>> 1, in <module>
>>>>>        print(x)
>>>>>    NameError: name 'x' is not defined
>>>>>
>>>>> ====================================================================
>>>>> tests finished: 0 passed, 1 failed, in 0.04 seconds
>>>>> =====================================================================
>>>>> DO *NOT* COMMIT!
>>>>>
>>>>>
>>>>> Is there way to achieve this same functionality using sympy's doctest?
>>>>>
>>>>> ~Luke
>>>>>
>>>>>
>>>>> --
>>>>> "Those who would give up essential liberty to purchase a little
>>>>> temporary safety deserve neither liberty nor safety."
>>>>>
>>>>> -- Benjamin Franklin, Historical Review of Pennsylvania, 1759
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google Groups 
>>>>> "sympy" group.
>>>>> To post to this group, send email to sympy@googlegroups.com.
>>>>> To unsubscribe from this group, send email to 
>>>>> sympy+unsubscr...@googlegroups.com.
>>>>> For more options, visit this group at 
>>>>> http://groups.google.com/group/sympy?hl=en.
>>>>>
>>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google Groups 
>>>> "sympy" group.
>>>> To post to this group, send email to sympy@googlegroups.com.
>>>> To unsubscribe from this group, send email to 
>>>> sympy+unsubscr...@googlegroups.com.
>>>> For more options, visit this group at 
>>>> http://groups.google.com/group/sympy?hl=en.
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> "Those who would give up essential liberty to purchase a little
>>> temporary safety deserve neither liberty nor safety."
>>>
>>> -- Benjamin Franklin, Historical Review of Pennsylvania, 1759
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sympy" group.
>>> To post to this group, send email to sympy@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sympy+unsubscr...@googlegroups.com.
>>> For more options, visit this group at 
>>> http://groups.google.com/group/sympy?hl=en.
>>>
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "sympy" group.
>> To post to this group, send email to sympy@googlegroups.com.
>> To unsubscribe from this group, send email to 
>> sympy+unsubscr...@googlegroups.com.
>> For more options, visit this group at 
>> http://groups.google.com/group/sympy?hl=en.
>>
>>
>
>
>
> --
> "Those who would give up essential liberty to purchase a little
> temporary safety deserve neither liberty nor safety."
>
> -- Benjamin Franklin, Historical Review of Pennsylvania, 1759
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to sympy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to