What's the Pythonic way to store a data block in a Python script?

前端 未结 4 1409
孤街浪徒
孤街浪徒 2021-01-01 20:20

Perl allows me to use the __DATA__ token in a script to mark the start of a data block. I can read the data using the DATA filehandle. What\'s the Pythonic wa

4条回答
  •  粉色の甜心
    2021-01-01 20:35

    Not being familiar with Perl's __DATA__ variable Google is telling me that it's often used for testing. Assuming you are also looking into testing your code you may want to consider doctest (http://docs.python.org/library/doctest.html). For example, instead of

    import StringIO
    
    __DATA__ = StringIO.StringIO("""lines
    of data
    from a file
    """)
    

    Assuming you wanted DATA to be a file object that's now what you've got and you can use it like most other file objects going forward. For example:

    if __name__=="__main__":
        # test myfunc with test data:
        lines = __DATA__.readlines()
        myfunc(lines)
    

    But if the only use of DATA is for testing you are probably better off creating a doctest or writing a test case in PyUnit / Nose.

    For example:

    import StringIO
    
    def myfunc(lines):
        r"""Do something to each line
    
        Here's an example:
    
        >>> data = StringIO.StringIO("line 1\nline 2\n")
        >>> myfunc(data)
        ['1', '2']
        """
        return [line[-2] for line in lines]
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    

    Running those tests like this:

    $ python ~/doctest_example.py -v
    Trying:
        data = StringIO.StringIO("line 1\nline 2\n")
    Expecting nothing
    ok
    Trying:
        myfunc(data)
    Expecting:
        ['1', '2']
    ok
    1 items had no tests:
        __main__
    1 items passed all tests:
       2 tests in __main__.myfunc
    2 tests in 2 items.
    2 passed and 0 failed.
    Test passed.
    

    Doctest does a lot of different things including finding python tests in plain text files and running them. Personally, I'm not a big fan and prefer more structured testing approaches (import unittest) but it is unequivocally a pythonic way to test ones code.

提交回复
热议问题