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


Groups > comp.lang.python > #72955

Re: How to use imported function to get current globals

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.glorb.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <1989lzhh@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'static': 0.04; 'modify': 0.07; 'guys.': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; 'crude': 0.16; 'debugger,': 0.16; "module's": 0.16; 'namespace.': 0.16; 'script?': 0.16; 'language': 0.16; 'module': 0.19; 'trying': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'load': 0.23; 'cc:2**0': 0.24; 'compiled': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'said,': 0.30; 'skip:@ 10': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'probably': 0.32; 'quite': 0.32; 'url:python': 0.33; 'could': 0.34; 'connection': 0.35; 'tool': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'so,': 0.37; 'two': 0.37; 'message-id:@gmail.com': 0.38; 'track': 0.38; 'bad': 0.39; 'url:mail': 0.40; 'how': 0.40; 'simple': 0.61; 'header:Message- Id:1': 0.63; 'iphone': 0.65; 'here': 0.66; 'charset:gb2312': 0.80; 'subject:get': 0.81; '8bit%:24': 0.84; 'expose': 0.84; 'snapshot': 0.84; 'angel': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:from:subject:date:to; bh=sPmYDWlUElbgXAC3yTwASgyC0IEZ3AqECXWEpwnqpyE=; b=aqYwVswMmIGxubeVAF8wSR9P8KyXKTk6RysBaVQDFJv80Ytkd2FUjTHS+srWG/rzCS Al8mWS0fncsVQSmSSh7wxIFJ4pdEvd9OM/mtjxt8k9bDzIaL9DvHOLZX2QOYAyxLEYcf o9x30Rbus7j+Z0mlK4ycES8wiKadFlTnic0vrnnIZoZ/8q64B/YHUWmmYuBhjUizauP4 nehSIcb3Gp/iAbRnxvXMsk+IXumsEqJc+cWryKnHd3xDE6BV3kYjHHdvsHB4tfguwxd7 MeGg2kX+DPOnd59T9igoc/L9/hySF599FoWpvnemA/jKvyRYf/SWF1Nht5SZXmWiefDP vWdA==
X-Received by 10.42.81.72 with SMTP id y8mr18344973ick.71.1402206720908; Sat, 07 Jun 2014 22:52:00 -0700 (PDT)
References <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com> <ln0he7$p1u$1@ger.gmane.org>
Mime-Version 1.0 (1.0)
In-Reply-To <ln0he7$p1u$1@ger.gmane.org>
Content-Type text/plain; charset=gb2312
Content-Transfer-Encoding quoted-printable
X-Mailer iPhone Mail (11D201)
From 1989lzhh <1989lzhh@gmail.com>
Subject Re: How to use imported function to get current globals
Date Sun, 8 Jun 2014 13:51:53 +0800
To Dave Angel <davea@davea.name>
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10874.1402206730.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402206730 news.xs4all.nl 2874 [2001:888:2000:d::a6]:44136
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 5225
X-Received-Body-CRC 1786145971
Xref csiph.com comp.lang.python:72955

Show key headers only | View raw


thanks all you guys. I have find the solution which is quite simple by using sys._frame(1).f_locals in function to get the caller's scope
The following is my user case:
I am writing a tool to translate python code to cython code then compiled using decorate.

jit, build=make("mymodule")
#jit function collect python code and signature then translate to cython code
@jit('int(int)',
    locals='''
    int b;
    ''')
def f(a):
    b=1
    return a+1

build()# compile cython code and load compiled module then expose compiled function to current namespace. So this is my purpose to get caller's scope
f()# now f is a compiled function 

    

发自我的 iPhone

> 在 Jun 8, 2014,10:24,Dave Angel <davea@davea.name> 写道:
> 
> 1989lzhh <1989lzhh@gmail.com> Wrote in message:
>> Here is the code
>> m1.py
>> def f():
>>    print globals()
>> 
>> m2.py
>> from m1 import f
>> f()# how to get current module's globals?
> 
> As others have said, it's probably a bad idea.  I can think of 3
> reasons to try: teacher said so, writing a debugger, 
> transliterating code from a crude language into python.
> 
> Could you elaborate on what you really want? Which of those two
> modules is your main script? Which code in which module is trying
> to get which module's globals?  And is the connection static or
> dynamic? And do you want a snapshot of them, or to be able to
> modify and track changes? 
> 
> 
> 
> 
> -- 
> DaveA
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Thread

Re: How to use imported function to get current globals 1989lzhh <1989lzhh@gmail.com> - 2014-06-08 13:51 +0800

csiph-web