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.
---8<---8<---8<---
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) ]
# For most objects, we can extract the string recognized by
sympify() with str(obj).
#
# However, for functions and classes 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__
elif hasattr(obj, "__class__"):
return str(obj.__class__) # e.g. sympy.C
else:
raise NotImplementedError(s) # TODO: reasonable fallback
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 (strs,descs)
---8<---8<---8<---
-J
On 31.08.2012 10:23, Juha Jeronen wrote:
On 30/08/12 20:16, Aaron Meurer wrote:
On Thu, Aug 30, 2012 at 12:41 AM, Juha Jeronen <[email protected]> wrote:
It seems, though, that just the string is not enough information. For
example, in 0.7.x there is the LT() function (to get the leading term),
but e.g. the following is nevertheless valid:
expr = sy.sympify("LT*a") # no error, expr will have Symbols "LT" and "a"
# artificial example: let's replace the symbol "LT" by sin(x)
my_LT = sy.symbols("LT") # refer to the Symbol "LT", not function LT()
expr.subs( {my_LT : sy.sympify("sin(x)")} )
This works in the latest version. The reason is that LT was not added
until 0.7.0.
Ah, I was originally running on 0.7.1, which does have LT(), but it
turns out there was a mistake in the above artificial example.
What my original code actually did in the case I tested, was only the
last two lines. The my_LT = ... line does work on 0.7.1, but as you say,
it's not possible to sympify() strings containing "LT" as a custom
symbol; instead, the string "LT" will always be interpreted as referring
to the function LT().
The catch is that while I did use sympify() in the original code, the
expression did not contain any "LT". If it did, the TypeError would have
been triggered... so there is a bug in my code. Thanks for discovering it ;)
(The using code has later been optimized so that it won't do unnecessary
subs(); the artificial example was based on how I recalled to have
discovered this.)
So, please disregard my previous comment - correcting, it seems the
string alone *is* enough information to decide whether the name is
reserved or not.
Thus, dir(sympy) and some processing should give a proper list...
-J
--
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.