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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • metaperl

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

    I am looking for something like the caller() routine in Perl:


    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__.__fil e__) # 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.lo gic()
    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.lo gic(__file__)

  • Alex Martelli

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

    metaperl <metaperl@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

    Comment

    • Marc 'BlackJack' Rintsch

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

      In <1158038477.221 439.15260@e63g2 000cwd.googlegr oups.com>, metaperl wrote:
      # Of course I could cheat and pass it, but I don't want to:
      >
      directories = data.storage.lo gic(__file__)
      Why do you consider a plain and simple solution cheating?

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Miki

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

        I am looking for something like the caller() routine in Perl:
        >
        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


        --
        Miki
        If it won't be simple, it simply won't be. [Hire me, source code]


        Comment

        • metaperl

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


          Marc 'BlackJack' Rintsch wrote:
          In <1158038477.221 439.15260@e63g2 000cwd.googlegr oups.com>, metaperl wrote:
          >
          # Of course I could cheat and pass it, but I don't want to:

          directories = data.storage.lo gic(__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 :)

          Comment

          • metaperl

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


            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.

            Comment

            • metaperl

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


              Marc 'BlackJack' Rintsch wrote:
              In <1158038477.221 439.15260@e63g2 000cwd.googlegr oups.com>, metaperl wrote:
              >
              # Of course I could cheat and pass it, but I don't want to:

              directories = data.storage.lo gic(__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.

              Comment

              Working...