Hi all (again),

And here's a fixed version. The code I just posted returned some nonsensical results, because it didn't filter out modules and classes.

Apart from that, it seems that sympy.core.core.ClassRegistry must be filtered out separately, and sympy.ntheory.generate.Sieve (a classobj with no __name__ or __class__, neither str() nor repr() matching the sympifiable string) must be handled as a special case.

I updated the version in my repo. This has been tested on SymPy 0.7.1.

For convenience, the relevant part is included below (in case someone else needs this). An API function for retrieving the reserved strings would be nice, but I guess this hack will do for now :)

Thanks for the help on this, everyone!

---8<--8<---8<---

import sympy as sy

def method2(ignore_numbers=True):
    """Get reserved names in SymPy.

    Method 2: dir(sympy), and add "Q" and "O".

    Return value:  (strs, descs), where
        - strs[] contains names of all reserved symbols
          (except literal numbers if ignore_numbers is True)
        - descs{} contains a human-readable description of each item,
          keyed by the items in strs[].

    Parameters:
ignore_numbers = bool. If True, ignore literal numbers such as -1, 0, 1/2, 1.
                         Default True.

    """
objs = [getattr(sy, name) for name in dir(sy) if not name.startswith("__")]
    if ignore_numbers:
objs = [obj for obj in objs if (not hasattr(obj, "is_Number") or not obj.is_Number) ]

    # ignore module and class objects
    from types import ModuleType
    from types import ClassType
objs = [obj for obj in objs if not isinstance(obj, ModuleType) and not isinstance(obj, ClassType)]

    # ignore sympy.C
    from sympy.core.core import ClassRegistry
    objs = [obj for obj in objs if not isinstance(obj, ClassRegistry) ]

# For most objects, we can extract the string recognized by sympify() with str(obj).
    #
    # However, for functions and classobjs we must do something else.
    #
    def get_name(obj):
        s = str(obj)
        if not s.startswith('<'):
            return s
        else:
            if hasattr(obj, "__name__"):
                return obj.__name__
            else:
# XXX HACK: sympy.ntheory.generate.Sieve is of type classobj
                # and has no __name__ or __class_.
                # Its repr() is "<Sieve with X primes sieved: ...>";
                # we trigger on that.
                #
                if repr(obj).find("Sieve"):
                    return "Sieve"

raise NotImplementedError("get_name(): cannot handle (%s, %s, %s)" % (obj, type(obj), s))
    strs = map( get_name, objs )

    # Handle the set "Q-COSINE".
    #
    # C parses to Symbol, so strictly speaking it is not reserved
    # in the sense meant here. Hence, we leave it out.
    #
    # dir(sy) misses "Q" and "O", so we need to handle only those.
    #
    for s in "QO":
        objs.append( sy.sympify(s) )
        strs.append( s )

    # Generate human-readable descriptions, indicating the module
    # where the symbol comes from, and its name.
    #
    descs = {}
    for obj,name in zip(objs,strs):
        m = "%s." % obj.__module__ if hasattr(obj, "__module__") else ""
        n = obj.__name__ if hasattr(obj, "__name__") else str(obj)
        descs[name] = "%s%s" % (m,n)

    return (sorted(strs),descs)


---8<--8<---8<---


 -J


On 31.08.2012 11:38, Juha Jeronen wrote:
Hi all,

Here is a new version based on dir(sympy). This approach still misses "Q" and "O", so those are added in manually.

These get-reserved-symbols experiments are archived here:

https://yousource.it.jyu.fi/jjrandom2/freya/blobs/master/docs/ressym.py

Included below is the relevant part.

This seems to work, but probably it's still not completely robust.

--
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to