0

I'm working on a function that automatically takes some code I've written and creates checksums in python to be uploaded along with the source code.

The issue is I need to dynamically discover other functions within the same module that I'm currently in.

This is so we can continue to write more code and have it automatically updated.

TL:DR; Making An Automatic Source Code Checksum and Upload Tool For Functions In Same Source File. How do I Locate All The Other Functions?

eg:

def func1(y):
    some random stuff.....
    return x

def autochecksum():
    for func in module:
        checksum = md5(inspect.code(func)).hexdigest
        othermodule.upload(inspect.code(func), checksum)

Closest I Could get was (pure fluke all the function names started with get_):

def update_jms_directory():
    import appdata.transfer.uploads as uploads
    for function in dir(uploads):
        if function.startswith("get_"):
            package_function()
3
  • You're working on the wrong level of abstraction. Function objects don't have a 1-1 correspondence to source code. Neither with byte code (you need the environment too), but at there is bytecode for all Python functions (though not really for decorators which are actually objects). You also don't want to share bytecode, as it's not portable at all and one can trivially create bytecode which segfaults interpreters. Why not work at file level? Commented Sep 17, 2012 at 14:02
  • the autochecksum function is being written to send each functions source code to a postgresql server with a checksum to ensure it's how we expect it at the other end. the idea is automation of what i'm having to do with each edit of the source file Commented Sep 17, 2012 at 14:07
  • the only reason i need to have it autolocate functions is so that when i add another one i don't have to update this function Commented Sep 17, 2012 at 14:08

1 Answer 1

3

Check out the dir function, http://docs.python.org/tutorial/modules.html#the-dir-function

This gives you the ability to view a sorted list of all the names a module defines, this should be a good starting place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.