Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8412

Re: Project-wide variable...

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'socket': 0.04; 'imports': 0.07; 'terry': 0.07; 'python': 0.08; 'caller': 0.09; 'dependency': 0.09; 'imported.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.13; 'wrote:': 0.15; '7:30': 0.16; 'callee': 0.16; 'def.': 0.16; 'module:': 0.16; 'reedy': 0.16; 'variable.': 0.16; 'this:': 0.16; '>>>': 0.16; 'def': 0.16; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'controlling': 0.23; 'testing': 0.25; 'statement': 0.25; 'function': 0.26; '(in': 0.26; 'import': 0.29; '24,': 0.29; 'constant': 0.29; 'module': 0.30; 'complete,': 0.30; 'yes.': 0.30; 'looks': 0.30; 'class': 0.31; 'header:User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.34; 'to:addr:python-list': 0.34; '...': 0.34; 'test': 0.34; 'useful': 0.37; 'passed': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'case,': 0.38; 'header :Mime-Version:1': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'where': 0.40; 'your': 0.61; 'free': 0.63; 'jun': 0.67; 'share': 0.68; 'production': 0.69; '12:27': 0.84; 'wish.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Project-wide variable...
Date Fri, 24 Jun 2011 17:19:44 -0400
References <7c6cf5e3-8ba0-45c8-86f8-bf3fc5fa2422@v11g2000prn.googlegroups.com> <mailman.321.1308837590.1164.python-list@python.org> <2cc8ba29-b669-4f1c-aa42-2e518402917c@p13g2000yqh.googlegroups.com> <mailman.339.1308852668.1164.python-list@python.org> <fb66f37c-c133-4905-b44f-c99b674f2ee2@x12g2000yql.googlegroups.com> <4e03f259$0$29975$c3e8da3$5496439d@news.astraweb.com> <a93ad81c-8c00-4183-a297-98475494b9c2@n5g2000yqh.googlegroups.com> <mailman.353.1308896848.1164.python-list@python.org> <27b92a01-767f-4f18-883e-603945d02778@l9g2000yqk.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host rain.gmane.org
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.18) Gecko/20110616 Lightning/1.0b2 Thunderbird/3.1.11
In-Reply-To <27b92a01-767f-4f18-883e-603945d02778@l9g2000yqk.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.391.1308950892.1164.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 82.94.164.166
X-Trace 1308950892 news.xs4all.nl 199 [::ffff:82.94.164.166]:51886
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8412

Show key headers only | View raw


On 6/24/2011 7:30 AM, Gnarlodious wrote:
> On Jun 24, 12:27 am, Terry Reedy wrote:
>
>>> 1) Can I tell Executable.py to share Data with ModuleTest.py?
>>
>> After the import is complete, yes.
>> import ModuleTest
>> ModuleTest.Data = Data
>>
>> This works if the use of Data is inside a function that is not called
>> during import, not if the use of Data is at toplevel or in a class
>> statement outside a def.
>
> That works! The solution looks like this:
>
> # controlling program:
> from Module import Data
> import ModuleTest
> ModuleTest.Data = Data
> ModuleTest.getData()
>
> # module:
> def getData():
>      print(Data.Plist.Structure)

This is a form of dependency injection, where a caller injects into a 
callee a dependency (callee) of the callee. It can be used even if the 
callee imports a dependency when it is imported. It is useful for 
testing when you want the callee to use a different dependency for 
testing. Simple example:

# MyModule
import socket
def myconnect(*args):
    ... socket.bind()

# Test_MyModule
import mock_socket
import MyModule
MyModule.socket = mock_socket
... test of MyModule, including MyModule.myconnect

Python makes this trivial without requiring that the otherwise constant 
depedency always be injected or passed (in normal production use) as a 
variable.

In your case, you are free to inject different forms of Data if you wish.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 06:41 -0700
  Re: Project-wide variable... Calvin Spealman <ironfroggy@gmail.com> - 2011-06-23 09:51 -0400
  Re: Project-wide variable... Noah Hall <enalicho@gmail.com> - 2011-06-23 14:59 +0100
    Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 07:09 -0700
      Re: Project-wide variable... Peter Otten <__peter__@web.de> - 2011-06-23 16:42 +0200
        Re: Project-wide variable... "bruno.desthuilliers@gmail.com" <bruno.desthuilliers@gmail.com> - 2011-06-23 08:20 -0700
        Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 10:48 -0700
      Re: Project-wide variable... Noah Hall <enalicho@gmail.com> - 2011-06-23 15:55 +0100
      Re: Project-wide variable... Terry Reedy <tjreedy@udel.edu> - 2011-06-23 14:10 -0400
        Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 19:01 -0700
          Re: Project-wide variable... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-24 02:11 +0000
            Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 20:49 -0700
              Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 21:01 -0700
                Re: Project-wide variable... alex23 <wuwei23@gmail.com> - 2011-06-23 21:04 -0700
              Re: Project-wide variable... Terry Reedy <tjreedy@udel.edu> - 2011-06-24 02:27 -0400
                Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-24 04:30 -0700
                Re: Project-wide variable... Terry Reedy <tjreedy@udel.edu> - 2011-06-24 17:19 -0400
  Re: Project-wide variable... Guillaume Martel-Genest <guillaumemg@gmail.com> - 2011-06-23 10:18 -0700
    Re: Project-wide variable... Noah Hall <enalicho@gmail.com> - 2011-06-23 18:42 +0100
      Re: Project-wide variable... Gnarlodious <gnarlodious@gmail.com> - 2011-06-23 11:01 -0700

csiph-web