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

M

metaperl

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__)
 
M

Marc 'BlackJack' Rintsch

# 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
 
M

metaperl

Marc said:
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 :)
 
M

metaperl

Marc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top