Hi everyone,
I am trying to convince my managers that python can replace the outdated and
soon no-longer maintained proprietary system (Tool for Calculator Design) we
use here. In order to achieve this, I need to analyze python code which will
look somethink like this:
def foo(arg_dict):
return arg_dict["one"] + bar(arg_dict)
def bar(other_dict) :
return other_dict["two"] + other_dict[True and "three" or "four"]
The result of my analysis should return a list
['one', 'two', 'three']
Alright, that doesn't sound so bad. With a little RTFM-ing and hacking, I got
-- black magic starts here --
import compiler
def _evaluate(parti al, name=""):
from compiler.ast import Expression
from compiler.misc import set_filename
from compiler.pycode gen import ExpressionCodeG enerator
tree = Expression(part ial)
set_filename(na me, tree)
gen = ExpressionCodeG enerator(tree)
return eval(gen.getCod e())
class RecursiveVisito r(compiler.visi tor.ASTVisitor) :
def __init__(self, name):
self.name = "Name(%s)" % repr(name)
self.names = {}
def visitSubscript( self, node, *args):
if repr(node.expr) == self.name:
try:
name = _evaluate(node. subs[0])
except:
name = str(node.subs[0])
self.names[name] = 1
def visitCallFunc(s elf, node, *args):
try:
from inspect import getsource, getargspec
from compiler import parse, walk
func = _evaluate(node. node)
pos = map(repr, node.args).inde x(self.name)
src, arg = getsource(func) , getargspec(func )[0]
tmp, self.name = self.name, "Name(%s)" % repr(arg[pos])
walk(parse(src) , self)
self.name = tmp
except Exception, e:
print str(e)
if __name__ == "__main__":
from inspect import getsource
from compiler import parse, walk
src = getsource(foo)
mod = parse(src)
visitor = RecursiveVisito r("kw")
walk(mod, visitor)
print visitor.names.k eys()
-- black magic ends here, ouch --
Once again, I'm in total awe at the possibilities python offers us lowly
users. Unfortunately we're all the same: you give us a finger, we want the
whole arm! I want this method to generalize to method calls as in
class baz:
def foo(self, arg_dict):
return arg_dict["one"] + self.bar(arg_di ct)
def bar(self, other_dict):
return other_dict["two"] + other_dict[True and "three" or "four"]
It shouldn't be all that hard. My problem is the lookup of 'self.bar'. In
the AST it looks something like
CallFunc(Getatt r(Name('self'), 'bar'), [Name('arg_dict' )], None, None)
How do I find the corresponding function? Anybody feels like wrapping
their head on this?
Cheers,
Jan Burgy 2 2064
"Jan Burgy" <jb****@hotmail .com> wrote in message
news:80******** *************** ***@posting.goo gle.com... return arg_dict["one"] + self.bar(arg_di ct)
It shouldn't be all that hard. My problem is the lookup of 'self.bar'. In the AST it looks something like
CallFunc(Getatt r(Name('self'), 'bar'), [Name('arg_dict' )], None,
None) How do I find the corresponding function?
The lookup returns a bound method, whose structure is an implementation
detail.
Completely untested suggestion, possibly not correct or possible:
Write an implementation-specific unwrap function. Then either augment the
source code
..... + unwrap(self.bar )(self, arg_dict) # self now an explict arg
or the analysis code to do the unwrapping. For the latter, recognize
Name('self') and replace the result of Getattr(Name('s elf'), 'bar') with
unwrap(thereof) and, again, augment the arg list.
Terry J. Reedy
"Terry Reedy" <tj*****@udel.e du> wrote in message news:<ma******* *************** *************** *@python.org>.. . "Jan Burgy" <jb****@hotmail .com> wrote in message news:80******** *************** ***@posting.goo gle.com... return arg_dict["one"] + self.bar(arg_di ct)
It shouldn't be all that hard. My problem is the lookup of 'self.bar'. In the AST it looks something like
CallFunc(Getatt r(Name('self'), 'bar'), [Name('arg_dict' )], None, None) How do I find the corresponding function?
The lookup returns a bound method, whose structure is an implementation detail.
Completely untested suggestion, possibly not correct or possible:
Write an implementation-specific unwrap function. Then either augment the source code
.... + unwrap(self.bar )(self, arg_dict) # self now an explict arg
or the analysis code to do the unwrapping. For the latter, recognize Name('self') and replace the result of Getattr(Name('s elf'), 'bar') with unwrap(thereof) and, again, augment the arg list.
Terry J. Reedy
Hi Terry,
Thank you very much for your suggestion. I am not sure how it would
help though. What is preventing me now from already replacing the
result of Getattr(Name('s elf'), 'bar') with what I need? That's
precisely my problem however. Because Python is a dynamically typed
language, I have no information about 'self' at compile-time. Short of
saving context information while I am visiting the parse tree, I don't
know how to tell what class I am in.
Would-Meta-Classes-help-me-in-this-case-ly yours?
Jan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: dan |
last post by:
without stirring the pot too much --
could someone please point me to whatever documentation exists on the
philosophy, semantics, and practical implications of how Python
implements the assignment operator? So far I can't find much useful
in the regular documentation. I understand the basic concept that
names are bound to objects which exist on the heap, but that still
doesn't explain why a = b doesn't _always_ cause a to point to the...
|
by: Michael Spencer |
last post by:
Wow - Alex Martelli's 'Black Magic' Pycon notes
http://www.python.org/pycon/2005/papers/36/pyc05_bla_dp.pdf
include this gem:
> Functions 'r descriptors
> def adder(x, y): return x + y
> add23 = adder.__get__(23)
> add42 = adder.__get__(42)
> print add23(100), add42(1000)
> 123 1042
|
by: Alex |
last post by:
Entering the following in the Python shell yields
>>> help(dict.copy)
Help on method_descriptor:
copy(...)
D.copy() -> a shallow copy of D
>>>
|
by: stewart.midwinter |
last post by:
I've been experimenting with the python zipfile module, and have run
into a snag.
I'm able to create a new zipfile with the module's ZipFile class and
add files to it. After closing the file, I'm able to view the contents
of it with WinZip. I can also confirm that it is a python zip file by
using the is_zipfile() method. All good so far. However, under some
circumstances, I am unable to later read the file and extract its
contents. ...
|
by: KronicDeth |
last post by:
I have a package of python modules deployed on an NFS mount on my
network that I use for sysadmin tools. Since some of the machines use
different versions of python I've only put the .py files in the mounted
share. However, I'm getting ImportError: Bad Magic Number when I try
to load any of the modules from the mount point. All the discussions I
can find say that a bad magic number should only occur with a
mismatched interpreter and...
| |
by: Samuel |
last post by:
Hi,
How can I determine the type of a file from "magic bytes", similar to
what the "file" command on unix does? I found
http://docs.python.org/lib/module-mimetypes.html
but this only seems to use the filename (extension) for finding the
type. Any hints?
|
by: per9000 |
last post by:
Hi,
I recently started working a lot more in python than I have done in
the past. And I discovered something that totally removed the pretty
pink clouds of beautifulness that had surrounded my previous python
experiences: magic names (I felt almost as sad as when I discovered
the strange pink worms that eat you in nethack, not to mention the
mind flayers - I really hate them).
I guess all programming languages have magic names to some...
|
by: Larry Hale |
last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy
command; see http://darwinsys.com/file/). Generally, has anybody
built this and worked with it under Windows?
The only thing I've been able to find is the python-magic module at
http://hupp.org/adam/hg/python-magic/.
Is this "THE" python-magic module. (It seems to be to me, but
obviously I don't know. :)
|
by: Larry Hale |
last post by:
Thank you, again, Michael, for all your help many months ago.
I *FINALLY* got a HowTo done up; please see http://wiki.python.org/moin/HowTo/FileMagic
I've also emailed Mr. Hupp to see if he'll re-post the SWIG version;
he's working on a newer binding (forget... ctypes??) and once that
one's working... :)
But for now...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |