Something Like os.environ['HTTP_REFERER']

Dave Angel davea at ieee.org
Sun Dec 20 16:22:02 EST 2009



Victor Subervi wrote:
> Inelegant. This will be elegant:
>
> ourFile = string.split(__file__, "/")
> p = ourFile[len(ourFile) - 1]
> p = p[: - 3]
> site = ourFile[4][:-10]
> if site != '':
>   site = site[:-1]
>
> from this import that(site)
>
> Now it's automated.
> V
>
>   
Amazing.  When trying to split a path string, consider using 
os.path.split().  When trying to get rid of the file extension, consider 
using os.path.splitext().  When trying to get the last item of a list, 
consider using subscript of -1.  And when you want all but the last 10 
characters of a string, use [:-11] instead of a two-step.

Since you've hardcoded the directory structure (also a bad idea), this 
may not be quite right.  But something like:
  site = os.path.splitext(os.path.split(__file__)[1])[0][:-8]

will extract the beginning of the basename, whether it's a URL, or 
Linux, or Windows.

But all of this has nothing to do with your original question, which said:

 >>>>If I have a script that is imported by another script, how can I have
 >>>>the script that is being imported determine which script imported it?

We were all willing to overlook the misuse of the word script -- a 
python source file that's imported isn't a script, it's a module --  but 
somehow we assumed you were asking a serious question.  Nothing about 
the code you now post has anything to do with the script that imported it.

DaveA




More information about the Python-list mailing list