472,352 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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 1071
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Myers | last post by:
Hello folks: (This message is also posted on the help forum at the pexpect sourceforge page, but all indentation in the code got stripped away...
2
by: Theodore Feldman | last post by:
If connection properties and global variables are set from an INI file, how can a DTS package be made truly portable? If the INI file pathname is...
4
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't...
2
by: kkrizl | last post by:
I have an Access database that has a few huge tables. It was taking about 20 minutes per table to import them from another application. I used...
3
by: Rob Cowie | last post by:
I have searched this group and the wider net to find an answer to this, but I haven't been successful. Pydoc seems to be capable of writing...
14
by: Richard Harter | last post by:
Apologies for the length - this post is best viewed with fixed font and a line width >= 72. Below is the source code for a C header file that...
5
by: Stuart D. Gathman | last post by:
The pyspf package http://cheeseshop.python.org/pypi/pyspf/] can use either pydns, or dnspython. The pyspf module has a simple driver function,...
0
snowdonkey
by: snowdonkey | last post by:
I'm trying to use the Mail package from PEAR in a script that sends an e-mail over SMTP. The problem is that the Mail class in Mail.php provided by...
7
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.