Popular recipes tagged "modules"http://code.activestate.com/recipes/tags/modules/2016-10-06T17:21:42-07:00ActiveState Code RecipesGet names and types of all attributes of a Python module (Python) 2016-10-06T17:21:42-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580705-get-names-and-types-of-all-attributes-of-a-python-/ <p style="color: grey"> Python recipe 580705 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/documentation/">documentation</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/reflection/">reflection</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to get the names and types of all the attributes of a Python module. This can be useful when exploring new modules (either built-in or third-party), because attributes are mostly a) data elements or b) functions or methods, and for either of those, you would like to know the type of the attribute, so that, if it is a data element, you can print it, and if it is a function or method, you can print its docstring to get brief help on its arguments, processsing and outputs or return values, as a way of learning how to use it.</p> <p>The code for the recipe includes an example call to it, at the end of the code. Note that you first have to import the modules that you want to introspect in this way.</p> Clear Only Project Modules (Python) 2014-12-17T19:03:15-08:00Cornelius Jatniel Prinsloohttp://code.activestate.com/recipes/users/4191358/http://code.activestate.com/recipes/578982-clear-only-project-modules/ <p style="color: grey"> Python recipe 578982 by <a href="/recipes/users/4191358/">Cornelius Jatniel Prinsloo</a> (<a href="/recipes/tags/clear/">clear</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/project/">project</a>, <a href="/recipes/tags/reload/">reload</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/startup/">startup</a>). </p> <p>Useful for python sessions that have a long startup time because of external dependancies [In my case that would be pygame and pymunk] I've got a slow computer so it takes a while to startup this is the solution I came up with</p> <p>import this before any other of your project imports example: import reloading # The modules in your project folder get cleared then load the rest of your project modules</p> <p>make sure that the reloading script is in your project folder, or else it won't work</p> <p>You might be able to extend this by changing line</p> Apply decorators to all functions in a module (Python) 2011-06-09T22:51:28-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577742-apply-decorators-to-all-functions-in-a-module/ <p style="color: grey"> Python recipe 577742 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>). Revision 2. </p> <p>Use my modulehacker recipe (<a href="http://code.activestate.com/recipes/577740/">recipe 577740</a>) to apply decorators to any number of modules, or even (nearly) all of them.</p> using `from somename.api import *` when somename is a single module (Python) 2011-08-12T20:24:11-07:00Ethan Furmanhttp://code.activestate.com/recipes/users/4177684/http://code.activestate.com/recipes/577839-using-from-somenameapi-import-when-somename-is-a-s/ <p style="color: grey"> Python recipe 577839 by <a href="/recipes/users/4177684/">Ethan Furman</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/__all__/">__all__</a>). </p> <p>Allow a module to export a subset of __all__, so a user can do <code>from module.api import *</code></p> Use Simple Plugins to Seemlessly Modify Modules at Import Time (Python) 2011-07-12T18:55:18-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577740-use-simple-plugins-to-seemlessly-modify-modules-at/ <p style="color: grey"> Python recipe 577740 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>). Revision 4. </p> <p>This recipe uses the PEP 302 import hooks to expose all imported modules to devious behavior.</p> <p>Simply put, the module is imported like normal and then passed to a hacker object that gets to do whatever it wants to the module. Then the return value from the hack call is put into sys.modules.</p> <p><a href="http://code.activestate.com/recipes/577741/">Recipe 577741</a> and <a href="http://code.activestate.com/recipes/577742/">recipe 577742</a> are more concrete examples of using this recipe.</p> Turn Your Boring Old Modules Into Dynamic Powerhouses (Python) 2011-08-12T23:39:58-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577741-turn-your-boring-old-modules-into-dynamic-powerhou/ <p style="color: grey"> Python recipe 577741 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>). Revision 2. </p> <p>Customize your module objects by using this recipe! </p> <p>All you have to do is import the module containing this code, register the custom module class you want, and stick __moduleclass__ the module that you want customized. The example should explain it all.</p> <p>This recipe uses <a href="http://code.activestate.com/recipes/577740/">recipe 577740</a>.</p> Trace decorator for debugging (Python) 2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/ <p style="color: grey"> Python recipe 577551 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>). Revision 2. </p> <p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p> <p>It also provides functionality for adding decorators to existing classes or modules.</p> Open a python module given it's name in Emacs (Bash) 2010-07-19T10:25:06-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577317-open-a-python-module-given-its-name-in-emacs/ <p style="color: grey"> Bash recipe 577317 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/emacs/">emacs</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_developer_tools/">python_developer_tools</a>, <a href="/recipes/tags/zsh/">zsh</a>). </p> <p>A tiny little <code>zsh</code> function to open a python module in <code>Emacs</code>. Assumes that an <code>Emacs</code> server process is running. </p> PluginManager - Extending Project Functionality By Using Custom Modules/Plugins On The Fly. (Python) 2010-05-07T15:32:10-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577216-pluginmanager-extending-project-functionality-by-u/ <p style="color: grey"> Python recipe 577216 by <a href="/recipes/users/4173476/">AJ. Mayorga</a> (<a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/plugin/">plugin</a>). Revision 4. </p> <p>This demo shows how you can create and manage your own custom plugins for extending functionality in your Python projects. There are no safety wrappers in this demo for restricting plugins aside from that fact that plugins are run as an extention of a management class which is run in its own instance only receiving data passed to it by the RumModules method, that said security should ideally be applied to the ModuleAPI class, by restricting __builtins__ on eval calls and/or validating plugin content and parameters which can be done by extending the Prepaser Class. For a great recipe/demo of restricting eval call see <a href="http://code.activestate.com/recipes/496746" rel="nofollow">http://code.activestate.com/recipes/496746</a>. </p> <p>That aside it was alot of fun to write and use. Enjoy</p> <p>PS: you will need <a href="http://code.activestate.com/recipes/577144/" rel="nofollow">http://code.activestate.com/recipes/577144/</a> to import Xceptions</p> Temporary substitution of object in modules via with statement (Python) 2010-01-24T04:19:12-08:00Jacob Oscarsonhttp://code.activestate.com/recipes/users/1355144/http://code.activestate.com/recipes/577020-temporary-substitution-of-object-in-modules-via-wi/ <p style="color: grey"> Python recipe 577020 by <a href="/recipes/users/1355144/">Jacob Oscarson</a> (<a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/with_statement/">with_statement</a>). </p> <p>See docstring in the code</p> Getting version number of modules (Python) 2008-09-06T05:28:46-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576491-getting-version-number-of-modules/ <p style="color: grey"> Python recipe 576491 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/modules/">modules</a>). </p> <p>How to print out the version number of a module</p>