469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How to get the package, file, and line of a method/function invocant?

I am looking for something like the caller() routine in Perl:
http://perldoc.perl.org/functions/caller.html

I am writing a script which needs to be allocated an object containing
a set of paths that it will use for reading and writing during it's
execution:

import os.path

class logic:
def __init__(self):
std_dirs = "in out zip arc".split()
self.root = os.path.dirname(__main__.__file__) # doesnt work
for d in std_dirs:
mkdir = "%s/%s" % (self.root, d)
os.mkdir(mkdir)
setattr(self, d, mkdir)
# Then the script can do this:

directories = data.storage.logic()
infile = "%s/%s" % (directories.in, "infile")
f = open (infile, 'r')

# Of course I could cheat and pass it, but I don't want to:

directories = data.storage.logic(__file__)

Sep 12 '06 #1
6 1026
metaperl <me******@gmail.comwrote:
I am looking for something like the caller() routine in Perl:
http://perldoc.perl.org/functions/caller.html
Look at the inspect module in Python's standard library.
Alex
Sep 12 '06 #2
In <11*********************@e63g2000cwd.googlegroups. com>, metaperl wrote:
# Of course I could cheat and pass it, but I don't want to:

directories = data.storage.logic(__file__)
Why do you consider a plain and simple solution cheating?

Ciao,
Marc 'BlackJack' Rintsch
Sep 12 '06 #3
I am looking for something like the caller() routine in Perl:
http://perldoc.perl.org/functions/caller.html

Look at the inspect module in Python's standard library.
Or is you're feeling lazy, have a look at the "here" function found in
http://www.unixreview.com/documents/...e_listing1.htm

--
Miki
http://pythonwise.blogspot.com

Sep 12 '06 #4

Marc 'BlackJack' Rintsch wrote:
In <11*********************@e63g2000cwd.googlegroups. com>, metaperl wrote:
# Of course I could cheat and pass it, but I don't want to:

directories = data.storage.logic(__file__)

Why do you consider a plain and simple solution cheating?
Hmm, I dont know the proper software engineering term, but just on the
basis of instinct, it seems wrong for a function to use something that
it can get on its own.

It's kind of like being at a 5-star hotel. It might be very possible
for you to bring your own pop tarts and toast them, but they go to
great lengths to have a continental buffet ready for you without you
doing anything.

Good question, airheaded answer :)

Sep 12 '06 #5

Miki wrote:
I am looking for something like the caller() routine in Perl:
http://perldoc.perl.org/functions/caller.html
Look at the inspect module in Python's standard library.
Or is you're feeling lazy, have a look at the "here" function found in
http://www.unixreview.com/documents/...e_listing1.htm
Thanks for the link. I would've never figured out how to use inspect on
my own.

Sep 12 '06 #6

Marc 'BlackJack' Rintsch wrote:
In <11*********************@e63g2000cwd.googlegroups. com>, metaperl wrote:
# Of course I could cheat and pass it, but I don't want to:

directories = data.storage.logic(__file__)

Why do you consider a plain and simple solution cheating?
Ok now I figured it out. The reason is that this function will be used
by many "client" classes. If we use the plain and simple solution, our
codebase increases in size, because each client class will supply the
function. Not only that, but they might make an error in what they
send.

By having the "server" class do the work, the code base decreases in
size and the amount of errors drops.

Sep 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Eric Myers | last post: by
2 posts views Thread by Theodore Feldman | last post: by
3 posts views Thread by Rob Cowie | last post: by
14 posts views Thread by Richard Harter | last post: by
5 posts views Thread by Stuart D. Gathman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.