473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 1107
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 when I submitted the post.) For some time I've...
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 hard-coded in the package, it must be edited...
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 even know if it's possible Thanks - Chuck...
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 the upsize wizard to put them in a SQL server...
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 documentation for all modules within a package by...
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 provides a suite of storage management macros. I am...
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, DNSLookup(), that defaults to the pydns version. It...
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 the package isn't found by the script. I'm using:...
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 wrapper that imports that module. Everything I try...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.