472,374 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

I wish I could add docstrings to vars.


I build a lot of elaborate dictionaries in my interpreter, and then I
forget exactly how they work. It would be really nice to be able to add
notes to the dictionary.

Is there some way to do this now?

Matt
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #1
4 909
On 2006-09-12, Matthew Wilson <ma**@tplus1.comwrote:
I build a lot of elaborate dictionaries in my interpreter, and
then I forget exactly how they work. It would be really nice
to be able to add notes to the dictionary.

Is there some way to do this now?
Writing a thin wrapper around the dictionary might be beneficial,
and would also furnish a place for the docstrings. Actually, the
wrapper would probably prevent you from needing the docstring
very often. ;)

--
Neil Cerutti
Eddie Robinson is about one word: winning and losing. --Eddie
Robinson's agent Paul Collier
Sep 12 '06 #2
On Tue 12 Sep 2006 10:06:27 AM EDT, Neil Cerutti wrote:
Writing a thin wrapper around the dictionary might be beneficial,
and would also furnish a place for the docstrings.
I wrote a function that hopefully does just that. I'm not very savvy at
doing this class-factory stuff, so any advice would be welcome.

def vd(C):

"""

Return a subclass of class C that has a instance-level attribute _vardoc.

"""

class VDC(C):

def __init__(self, *args, **kwargs):

vardoc = kwargs.get('vardoc')
if vardoc:
assert isinstance(vardoc, str), "vardoc must be a
string!"
kwargs.pop('vardoc')
self._vardoc = vardoc

C.__init__(self, *args, **kwargs)

def __repr__(self):

if self._vardoc:
return self._vardoc + "\n" + C.__repr__(self)

else:
return C.__repr__(self)

return VDC
def test_vd():

i = vd(int)(6)
i._vardoc = "integer!"
assert isinstance(i, int)

d = vd(dict)(a=1, b=2, c=i, vardoc="dict!")
assert d['a'] == 1
assert d['c'] == 6
assert isinstance(d, dict)

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #3
On 2006-09-12, Matthew Wilson <ma**@tplus1.comwrote:
On Tue 12 Sep 2006 10:06:27 AM EDT, Neil Cerutti wrote:
>Writing a thin wrapper around the dictionary might be
beneficial, and would also furnish a place for the docstrings.

I wrote a function that hopefully does just that. I'm not very
savvy at doing this class-factory stuff, so any advice would be
welcome.
I should have chosen my words more carefully. I meant to suggest
writing small interfaces for your dictionaries. This sort of
trick is often used in Scheme, where lists are used to represent
many different data types. Designing and writing a good interface
makes the code much easier to understand.

Here's a simple-minded example of a dictionary of identifiers for
use in an interpreter, which nevertheless demonstrates the
advantages of the technique.

class IdentError():
pass

idents = {}

def new_ident(id, initial_value=None):
"""
Create a new identifier, id, (possibly with an initial_value)
and add it to the table of identifiers. Raises IdentError if
the identifier already exists.
"""
if id in idents:
raise IdentError
else:
idents[id] = initial_value

def ident_value(id):
"""
Return the current value of id. Raises IdentError if the
identifier is undefined.

"""
if key not in idents:
raise IdentError
else:
return idents[id]

Then the rest of the code uses this tiny interface.

--
Neil Cerutti
If only faces could talk. --Pat Summerall
Sep 12 '06 #4
It seems that you are getting some complex answers with confusing
examples. So, here is hopefully a less confusing example:

class MyDict(dict):
"""MyDict doc-string!"""

#then to use it

d = MyDict()
d['something'] = whatever you want

This solution leaves it open to do whatever you want with the class,
but it's better than nothing.

Matthew Wilson wrote:
I build a lot of elaborate dictionaries in my interpreter, and then I
forget exactly how they work. It would be really nice to be able to add
notes to the dictionary.

Is there some way to do this now?

Matt
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #5

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

Similar topics

2
by: Sridhar R | last post by:
When writing a python library, we can use docstrings for methods and functions that are part of API. But what about comments for non-API objects or python application code? For applications,...
0
by: Craig Ringer | last post by:
Hi folks I'm currently working on a fairly well internationalised app that embeds a Python intepreter. I'd like to make the docstrings translatable, but am running into the issue that the...
2
by: Steven Bethard | last post by:
I have two classes that implement the same interface, e.g. something like: class C(object): def foo(self): """Foo things""" ... def bar(self): """Bar things""" ... def baz(self):
0
by: Michael Muller | last post by:
I've observed something strange about property docstrings and I'm wondering if anyone here can clarify what's going on: if I create a class derived from property, the docstrings of the instances...
12
by: jelle | last post by:
That's basically the idea... Often i find myself annotating what variables actually stand for, so to refer back to the code in half a year or so. # check if ID's or coords self.pointIDs = ptIDs...
4
by: Lawrence D'Oliveiro | last post by:
The Python docs recommend the use of triple-quoted string literals for docstrings, e.g. def Meet(Alice, Bob) : """arranges a meeting between Alice and Bob. Returns a reference to the meeting...
1
bartonc
by: bartonc | last post by:
You will be rewarded sooner than you think when you use docstrings to document your classes and functions. Whether you ever intend to publish your work or not, docstrings will help you in several...
0
by: tjhnson | last post by:
The topic of docstrings for variables has come up many times before. In fact, a PEP was proposed and rejected on this very topic. http://www.python.org/dev/peps/pep-0224/ When creating...
6
by: Steve Brown | last post by:
I've got a series of modules which look like this: #************ # # Temperature Sense Test # #************ class Test3(ar_test.AR_TEST): """Temperature Sense Test"""
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
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 required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines 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 technical details, Gmail likely implements measures...
1
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 server and have made sure to enable curl. I get a...
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 synthesis of my design into a bitstream, not the C++...
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 has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.