473,385 Members | 1,958 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,385 software developers and data experts.

Module access from inside itself

I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

I've tried searching for an answer, but haven't found anything. Please
excuse me if I'm missing something simple, I'm a newbie to Python.

I'm doing something like this:

#!/usr/bin/python
"""Module doc string goes here.
"""

import getopt, sys

def MyFunction(args):
* * pass

if __name__ == "__main__":
* * opts, args = getop.getopt(sys.argv[1:], [], ["help"])
* * for opt in opts:
* * * * if opt == "--help":
* * * * * * print MY_MODULE.__doc__ * *# How do I get this?
* * * * else:
* * * * * * MyFunction(args)

How do I get a reference to the module from inside the module? Is this
the Pythonic way of generating help strings for CL scripts?
Thanks,
--
Steven D'Aprano
Jul 18 '05 #1
4 1914
On Fri, Aug 15, 2003 at 04:20:43PM +0000, Steven wrote:
I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.


You don't need the module object for this. MY_MODULE.__doc__ is the
same as accessing __doc__ directly.

If you really need the current module object use sys.modules[__name__]

Oren

Jul 18 '05 #2
Steven wrote:
I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.


How about this:

# demo of accessing the docstring:

"""
This is a test.

Had this been a real document, it would have said something.
"""
if __name__ == '__main__':
import __main__
print __main__.__doc__

Jul 18 '05 #3
On Fri, 15 Aug 2003 16:20:43 GMT, Steven <di***@mikka.net.au> wrote:
I'm writing a Python script which can be called from the command-line, and
I want to use my module doc string as the CL help text, but I don't know
how to access my module object from inside the module.

I've tried searching for an answer, but haven't found anything. Please
excuse me if I'm missing something simple, I'm a newbie to Python.

I'm doing something like this:

#!/usr/bin/python
"""Module doc string goes here.
"""

import getopt, sys

def MyFunction(args):
* * pass

if __name__ == "__main__":
* * opts, args = getop.getopt(sys.argv[1:], [], ["help"])
* * for opt in opts:
* * * * if opt == "--help":
* * * * * * print MY_MODULE.__doc__ * *# How do I get this? * * * * * * print __doc__ * *# should get it
* * * * else:
* * * * * * MyFunction(args)

How do I get a reference to the module from inside the module? Is this
the Pythonic way of generating help strings for CL scripts?

You are already inside the module, so an unqualified name will refer
to module globals unless it's being used in some local scope that has
a binding shadowing the global name.

Regards,
Bengt Richter
Jul 18 '05 #4
In article <pa**********************************@mikka.net.au >,
Steven <di***@mikka.net.au> wrote:

if __name__ == "__main__":
* * opts, args = getop.getopt(sys.argv[1:], [], ["help"])
* * for opt in opts:
* * * * if opt == "--help":
* * * * * * print MY_MODULE.__doc__ * *# How do I get this?
* * * * else:
* * * * * * MyFunction(args)


import __main__
print __main__.__doc__
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andreas Neudecker | last post by:
Hi. I know you can read the filename of a program as sys.argv. But what about modules? Is there a similar way to find out the file name of a module (called by some other module or program) from...
2
by: Martin Drautzburg | last post by:
Withing a module I can assign a value to a global var by assigning to it in the outermost scope. Fine. But how can I do this if the attribute name itself is kept in a variable. Once the module...
8
by: Irmen de Jong | last post by:
What would be the best way, if any, to obtain the bytecode for a given loaded module? I can get the source: import inspect import os src = inspect.getsource(os) but there is no...
5
by: kramb64 | last post by:
I'm trying to use setattr inside a module. From outside a module it's easy: import spam name="hello" value=1 setattr(spam, name, value) But if I want to do this inside the module spam...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
3
by: johnny | last post by:
Can a class inside a module, access a method, outside of class, but inside of the module? Eg. Can instance of class a access main, if so how? What is the scope of "def main()" interms of class...
0
by: Jorgen Bodde | last post by:
Hi All, I am wrestling with some architecture inside my app. Let's say I have a tunings collection, which contains e.g. 23 types of guitar tunings. In my song object I want to restore a relation...
13
by: Kirk | last post by:
I have been reading Scott Allen's article on Master Pages (http:// odetocode.com/Articles/450.aspx) but I am having problems understanding a concept. Specifically, I have created a property...
6
by: Samuel | last post by:
Hi, Given the following directory structure: --------- |-- Obj.py |-- __init__.py |-- foo | |-- FooTest.py | `-- __init__.py
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.