newdir.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » lib-old » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » lib old » newdir.py
# New dir() function


# This should be the new dir(), except that it should still list
# the current local name space by default

def listattrs(x):
  try:
    dictkeys = x.__dict__.keys()
  except (AttributeError, TypeError):
    dictkeys = []
  #
  try:
    methods = x.__methods__
  except (AttributeError, TypeError):
    methods = []
  #
  try:
    members = x.__members__
  except (AttributeError, TypeError):
    members = []
  #
  try:
    the_class = x.__class__
  except (AttributeError, TypeError):
    the_class = None
  #
  try:
    bases = x.__bases__
  except (AttributeError, TypeError):
    bases = ()
  #
  total = dictkeys + methods + members
  if the_class:
    # It's a class instace; add the class's attributes
    # that are functions (methods)...
    class_attrs = listattrs(the_class)
    class_methods = []
    for name in class_attrs:
      if is_function(getattr(the_class, name)):
        class_methods.append(name)
    total = total + class_methods
  elif bases:
    # It's a derived class; add the base class attributes
    for base in bases:
      base_attrs = listattrs(base)
      total = total + base_attrs
  total.sort()
  return total
  i = 0
  while i+1 < len(total):
    if total[i] == total[i+1]:
      del total[i+1]
    else:
      i = i+1
  return total


# Helper to recognize functions

def is_function(x):
  return type(x) == type(is_function)


# Approximation of builtin dir(); but note that this lists the user's
# variables by default, not the current local name space.

def dir(x = None):
  if x is not None:
    return listattrs(x)
  else:
    import __main__
    return listattrs(__main__)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.