{ "info": { "author": "Brandon Craig Rhodes", "author_email": "brandon@rhodesmill.org", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Topic :: Software Development :: Libraries" ], "description": "This package provides a clean solution for the case where you want to\nwrite a Python module that can safely be invoked from the command line\nbut which also needs to be imported by other modules. Simply write each\nmodule that would normally make a ``__name__`` ``==`` ``'__main__'``\ncheck like this instead::\n\n 'Your module docstring'\n\n import demain\n __main__ = demain.fix()\n\n # Your other import statements\n # Your classes and functions\n\n if __main__:\n # Your code that gets invoked from the command line\n\nIn cases where your module has simply been imported by another module,\nthe ``fix()`` function does nothing and returns ``False``. But if your\nmodule has been invoked directly, either as a script or using Python's\n``-m`` option, then ``fix()`` will:\n\n* Replace your module's ``__name__`` global variable with its true name.\n* Re-register your module in ``sys.modules`` under its true name, so that\n later ``import`` statements by other Python modules can find it.\n* If your module is beneath a package, then the module is inserted into\n the package under its correct name.\n* After making these corrections, the function returns ``True`` so that\n your module can detect that it is running as the main program.\n\nI advocate that a future version of Python stop mangling module names\naltogether, and instead set a ``__main__`` boolean in every module like\n`demain` does. But until then, use ``demain`` to bring your programs\nsome sanity, safely, and fewer mysterious breakages and error messages.\n\nBackground\n----------\n\nWhen you run a Python script from the command line, or invoke a module\nusing the ``-m`` option, Python gives that script or module the fake\nname ``'__main__'`` so that it can detect that it is running as the\n\u201cmain program.\u201d Consider a small script that looks like this::\n\n # foo.py\n print __name__\n\nInvoking it from the command line obliterates the normal ``foo`` name\nwhich it would have otherwise (as we can demonstrate, by importing the\nmodule from the command line)::\n\n $ python foo.py\n __main__\n $ python -m foo\n __main__\n $ python -c 'import foo'\n foo\n\nThis name mangling causes no end of trouble when used with modules that\nthemselves are targets of importation, because the first attempt to\nimport the module under its real name results in a duplicate of the\nmodule being created, with its own copy of every object and class. The\nbugs that this causes tend to be particularly difficult to track down.\n\nThe ``demain.fix()`` function is designed to prevent this problem from\neven starting, by being the very first thing that you call at the top of\nevery module that you intend to be invoked directly. Be careful not\nonly to avoid doing other imports before before running ``fix()`` but\nalso make sure that you create no classes first; otherwise, the name\n``'__main__'`` will get copied into your classes as their module's name.\n\nI knew that I finally had to do something about this problem when I read\n*Defending Pyramid's Design* and reached Chris McDonough's anguished\nplea in the section titled, \u201cApplication Programmers Don\u2019t Control The\nModule-Scope Codepath.\u201d\n\nhttp://docs.pylonshq.com/pyramid/dev/designdefense.html\n\nThere, his long experience with Python and his sharply-honed coding\npractices lead him to make some excellent recommendations against the\ndangers that small Python applications \u2014 and in particular those written\nusing Web micro-frameworks \u2014 incur when they treat a script as though it\nwere a normal module. My hope is that ``demain`` will solve this\nproblem by making an invoked Python script a safe place for objects,\ninstead of having them all duplicated when the same Python file is\nimported from elsewhere in the application using a normal import.\n\nThis package comes with a modest test suite, built from a few hand-built\nexamples, as well as McDonough's sample decorator framework that shows\nthe problem with Python's handling of script invocation. To run it,\nsimply type::\n\n $ python -m demain.tests", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://bitbucket.org/brandon/demain", "keywords": null, "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "demain", "package_url": "https://pypi.org/project/demain/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/demain/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://bitbucket.org/brandon/demain" }, "release_url": "https://pypi.org/project/demain/0.9/", "requires_dist": null, "requires_python": null, "summary": "Routine that fixes Python's broken __name__ == '__main__' behavior.", "version": "0.9" }, "last_serial": 788901, "releases": { "0.9": [ { "comment_text": "", "digests": { "md5": "5a460478a7fde033a333030531603911", "sha256": "3aeb112111dc2d51c577aad54634e42eb9bcf8581c4c51341512fddf97635e5a" }, "downloads": -1, "filename": "demain-0.9.tar.gz", "has_sig": false, "md5_digest": "5a460478a7fde033a333030531603911", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4384, "upload_time": "2010-11-06T20:46:20", "url": "https://files.pythonhosted.org/packages/fc/80/ac79d438912167cd788eb1e6a07b2a32c42d27cf3799dbcd8a45b94aa3fa/demain-0.9.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5a460478a7fde033a333030531603911", "sha256": "3aeb112111dc2d51c577aad54634e42eb9bcf8581c4c51341512fddf97635e5a" }, "downloads": -1, "filename": "demain-0.9.tar.gz", "has_sig": false, "md5_digest": "5a460478a7fde033a333030531603911", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4384, "upload_time": "2010-11-06T20:46:20", "url": "https://files.pythonhosted.org/packages/fc/80/ac79d438912167cd788eb1e6a07b2a32c42d27cf3799dbcd8a45b94aa3fa/demain-0.9.tar.gz" } ] }