Aaaahh, and I'm partly missing your point!

So, query the singleton system and then add Q-COSINE...


Updated code:

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

def get_reserved_symbols(ignore_numbers=True):
    rawnames = filter( lambda name: re.match("__", name) is None, 
dir(sy.S) )
    objs = map( lambda name: eval("sy.S.%s" % name),  rawnames )
    if ignore_numbers:
        objs = filter( lambda obj: not obj.is_Number,  objs )
    strs = map( lambda obj: str(obj),  objs )

    descs = {}
    for obj,name in zip(objs,strs):
        descs[name] = str(type(obj))

    # 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.
    #
    for s in "QOSINE":
        obj = sy.sympify(s)
        if obj not in objs:  # I and E are likely to end up in twice, so
check first
            objs.append( obj )
            strs.append( s )
            descs[s] = str(obj)  # for most of these this is ok
    descs["S"] = "the SymPy singleton system"  # str(obj) == "S", not
helpful
    descs["N"] = "the SymPy numerical evaluator"  # str(obj) ==
"<function N at...>"

    # Now:
    #    - 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[].
    #
    return (strs,descs)

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


This still leaves open the possibility of the user overwriting the
default singleton atoms, but I'm not sure how to handle that.

That depends on what is meant by overwriting - if the updated symbol is
stored into the singleton system, overwriting the old one, then this
approach already takes that into account. The whole idea here is to
dynamically query the state of the singleton system at that point of
program execution where the list is needed.

But of course stuff like

pi = sy.sympify("3")
expr = sy.sin(2*pi)  # actually, sin(6)

(which does not involve sympification for expr) is outside the scope of
this solution.


 -J


On 29/08/12 14:56, Juha Jeronen wrote:
> Hi,
>
> On 29/08/12 12:51, Chris Smith wrote:
>> On Wed, Aug 29, 2012 at 2:27 PM, Juha Jeronen <[email protected]> wrote:
>>> Hi all,
>>>
>>> This time a question: is there a preferred way of getting a complete
>>> list of symbols reserved by SymPy, i.e. atoms which have a default
>>> meaning (E, pi, EulerGamma, oo, etc.)?
>> These are the singletoms (below, ignore methods with `__`). But they
>> can all be overwritten. The only reserved symbols that don't become
>> symbols upon sympification are Q-COSINE (the C will actually parse
>> like a symbol so in that sense it's not reserved; the "-" could help
>> you remember that).
> Thanks for the quick reply.
>
> Ok.
>
>
>>>>> dir(S)
>> ['Catalan', 'ComplexInfinity', 'EmptySet', 'EulerGamma', 'Exp1',
>> 'GoldenRatio', 'Half', 'IdentityFunction', 'ImaginaryUnit',
>> 'Infinity', 'Integers', 'NaN', 'Naturals', 'NegativeInfinity',
>> 'NegativeOne', 'NumberSymbol', 'One', 'Pi', 'Reals', 'UniversalSet',
>> 'Zero', '__call__', '__class__', '__delattr__', '__doc__',
>> '__format__', '__getattribute__', '__hash__', '__init__',
>> '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
>> '__setattr__', '__sizeof__', '__slots__', '__str__',
>> '__subclasshook__']
> Thanks!
>
> So, dir(S) is the preferred way of getting a complete list :)
>
> It also works in both old and new versions, so that's good. I think the
> only remaining question is if this is likely to stay working for the
> foreseeable future?
>
> (The point being, at least my mind would be more at ease if there was a
> dedicated API method.)
>
>
> Proceeding from this point, it seems that in order to get the
> corresponding strings that sympify() converts into these objects, the
> additional map(...) step is needed. What I mean is:
>
> import re
> import sympy as sy
> L = dir(sy.S)
> objnames = filter( lambda name: re.match("__", name) is None,  L )
> strs = map( lambda name: str(eval("sy.S.%s" % name)),  objnames )
> print objnames
> print strs
>
> For example, running this on 0.6.7 (sorry for the old version; running
> on Debian Stable at the moment) we get
>
> objnames = ['Catalan', 'ComplexInfinity', 'EulerGamma', 'Exp1',
> 'GoldenRatio', 'ImaginaryUnit', 'Infinity', 'NaN', 'NegativeInfinity',
> 'NegativeOne', 'One', 'Pi', 'Zero']
>
> strs = ['Catalan', 'zoo', 'EulerGamma', 'E', 'GoldenRatio', 'I', 'oo',
> 'nan', '-oo', '-1', '1', 'pi', '0']
>
> with "strs" being what I was after. (In real-world use, I would further
> filter out anything that matches is_Number, in order to drop the obvious
> 0, -1, 1.)
>
>
> This further conversion is important because:
>
> import sympy as sy
> pi = sy.sympify("pi")
> print type(pi)  # => sympy.core.numbers.Pi
> #  but:
> pi = sy.sympify("Pi")
> print type(pi)  # => sympy.core.symbol.Symbol   (<--- !!!)
>
> ...so the "reserved symbol" (in the sense I intended) should be
> lowercase "pi".
>
>
>>> And finally, a related question: is there a way to get a human-readable
>>> description for a reserved name (e.g. "zoo" -> "complex infinity")? I
>>> didn't find anything in the API for this.
>> Not sure how to go further than
>>
>>>>> type(oo)
>> <class 'sympy.core.numbers.Infinity'>
> Aaaahh, the type! Nice.
>
> Thanks!
>
>
> The final code that I ended up using (modulo variable naming and
> comments) is:
>
> ---8<---8<---8<---
>
> rawnames = filter( lambda name: re.match("__", name) is None,  dir(sy.S) )
> objs = map( lambda name: eval("sy.S.%s" % name),  rawnames )
> objs = filter( lambda obj: not obj.is_Number,  objs )
> strs = map( lambda obj: str(obj),  objs )
>
> descs = {}
> for obj,name in zip(objs,strs):
>     descs[name] = str(type(obj))
>
> # now:
> #    - strs[] contains all reserved atoms, except literal numbers
> #    - descs{} contains a human-readable description of each item.
>
> ---8<---8<---8<---
>
> I would like to suggest adding an API method for this (e.g. with the
> is_Number check made optional), unless my use case is too specific for
> general use :)
>
>
>  -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.

Reply via email to