Most viewed recipes tagged "import"http://code.activestate.com/recipes/tags/import/views/2014-03-18T14:11:20-07:00ActiveState Code RecipesVersion-specific import (Python) 2008-09-17T11:25:48-07:00Michaelhttp://code.activestate.com/recipes/users/4166101/http://code.activestate.com/recipes/576508-version-specific-import/ <p style="color: grey"> Python recipe 576508 by <a href="/recipes/users/4166101/">Michael</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/versioning/">versioning</a>, <a href="/recipes/tags/versions/">versions</a>). Revision 5. </p> <p>Let's say you're at a company and you're deploying a package called "tools" on all production boxes. Normally, code on these boxes could "import tools" to use this package.</p> <p>However, over time the API to tools will evolve as you release new versions that add functionality and fix bugs. If there's lots of company code that "imports tools", then you're stuck with backward compatibility forever.</p> <p>This recipe presents a method for letting client code specify on one line which version of "tools" they wish to use -- and then import from the tools package as normal. Behind the scenes, the recipe is making sure that the client works with the version of the package that they requested. If the client ever wants to change versions, it's a one-line change at the top of their code.</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> Decorator to check if needed modules for method are imported (Python) 2014-03-18T14:11:20-07:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578852-decorator-to-check-if-needed-modules-for-method-ar/ <p style="color: grey"> Python recipe 578852 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/import/">import</a>). </p> <p>Check if needed modules imported before run method</p> <pre class="prettyprint"><code>Example:: @require_module(['time'],exception=Exception) def get_time(): return time.time() </code></pre> Custom import script for Salesforce (Python) 2011-01-21T17:43:35-08:00Chris Wolfhttp://code.activestate.com/recipes/users/4173108/http://code.activestate.com/recipes/577550-custom-import-script-for-salesforce/ <p style="color: grey"> Python recipe 577550 by <a href="/recipes/users/4173108/">Chris Wolf</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/dataloader/">dataloader</a>, <a href="/recipes/tags/excel/">excel</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/salesforce/">salesforce</a>). Revision 2. </p> <p>Normally you should try to use the Salesforce import Wizard or Apex Data Loader for importing. This script demonstrates that programmatic import can handle complex conditional logic that the Import Wizard or Data Loader cannot anticipate.</p> Prevent star imports (Python) 2010-05-21T16:22:08-07:00Mike Grahamhttp://code.activestate.com/recipes/users/4174015/http://code.activestate.com/recipes/577237-prevent-star-imports/ <p style="color: grey"> Python recipe 577237 by <a href="/recipes/users/4174015/">Mike Graham</a> (<a href="/recipes/tags/import/">import</a>). </p> <p>Use this code in your module to prevent people using the "from foo import *" syntax with your module.</p> Multiprocessing import wrapper (Python) 2011-08-23T22:11:42-07:00Matt Keranenhttp://code.activestate.com/recipes/users/38288/http://code.activestate.com/recipes/577856-multiprocessing-import-wrapper/ <p style="color: grey"> Python recipe 577856 by <a href="/recipes/users/38288/">Matt Keranen</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/etl/">etl</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>). Revision 2. </p> <p>A script used to launch multiple import scripts using the multiprocessing module. Developed to parallelize loading of multiple log files into a database for aggregate analysis</p> ActiveState recipe importer (Python) 2011-11-23T02:27:51-08:00nosklohttp://code.activestate.com/recipes/users/4166478/http://code.activestate.com/recipes/577958-activestate-recipe-importer/ <p style="color: grey"> Python recipe 577958 by <a href="/recipes/users/4166478/">nosklo</a> (<a href="/recipes/tags/activestate/">activestate</a>, <a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/library/">library</a>). </p> <p>Finally! This code allows you to import any activestate recipe right into your code!</p> <p>Example:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; from activestate.recipe194373 import mreplace &gt;&gt;&gt; print mreplace('ectave steta racipas rock!', ('a', 'e'), ('e', 'a')) active state recipes rock! </code></pre> <p>Save this as <strong>activestate.py</strong></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> Locate and import Python's standard regression tests (Python) 2010-09-17T11:31:31-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577394-locate-and-import-pythons-standard-regression-test/ <p style="color: grey"> Python recipe 577394 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>The Python standard library comes with an extensive set of regression tests. I had need to import and run some of these tests from my own code, but the test directory is not in Python's path. This simple helper function solves the problem.</p> Fix mbox files after importing EML into TB using ImportExportTools (Python) 2010-05-02T13:21:00-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577214-fix-mbox-files-after-importing-eml-into-tb-using-i/ <p style="color: grey"> Python recipe 577214 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/eml/">eml</a>, <a href="/recipes/tags/from/">from</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/importexporttools/">importexporttools</a>, <a href="/recipes/tags/mbox/">mbox</a>, <a href="/recipes/tags/tb/">tb</a>, <a href="/recipes/tags/thunderbird/">thunderbird</a>). Revision 2. </p> <p>I've found a bug in import EML file into Thunderbird using ImportExportTools addon: when I import eml file into TB there are a 'From' line added to mbox followed with EML file contents. TB maintains right 'From' line for messages fetched from mailservers:</p> <pre class="prettyprint"><code>From - Tue Apr 27 19:42:22 2010 </code></pre> <p>ImportExportTools formats this line wrong I suppose that used some system function with default specifier so I saw in mbox file:</p> <pre class="prettyprint"><code>From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time) </code></pre> <p>So there are two errors: 1) sequence 'time year' broken into 'year time' 2) extra trash with GMT info along with time zone name</p> <p>This prevents the mbox file parsing using Python standard library (for sample) because there are a hardcoded regexp for matching From line (file lib/mailbox.py, class UnixMailbox):</p> <pre class="prettyprint"><code>_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \ r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$" </code></pre> <p>Attached script fixes incorrect From lines so parsing those mboxes using Python standard library will become ok.</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> Tracking and Manipulating the Python Import State (Python) 2012-04-22T07:27:29-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578107-tracking-and-manipulating-the-python-import-state/ <p style="color: grey"> Python recipe 578107 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/import/">import</a>). </p> <p>This is a rough analog to the import engine described in PEP 406. Here I've called it ImportState.The focus here is on using it as a context manager to limit changes to the import state to a block of code (in a with statement). Differences from PEP 406 are described below.</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>