Skip to content

Commit

Permalink
Merge pull request #859 from rocky/add-system-byteordering-and-black
Browse files Browse the repository at this point in the history
Add System`Byteordering System`Environemnt and blacken system.py
  • Loading branch information
GarkGarcia authored Sep 11, 2020
2 parents 69054db + 21c3d2e commit 27df55c
Showing 1 changed file with 124 additions and 80 deletions.
204 changes: 124 additions & 80 deletions mathics/builtin/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,78 @@
import platform
import sys

from mathics.core.expression import Expression, Integer, String, strip_context
from mathics.core.expression import Expression, Integer, String, Symbol, strip_context
from mathics.builtin.base import Builtin, Predefined
from mathics import version_string


class Version(Predefined):
class Aborted(Predefined):
"""
<dl>
<dt>'$Version'
<dd>returns a string with the current Mathics version and the versions of relevant libraries.
<dt>'$Aborted'
<dd>is returned by a calculation that has been aborted.
</dl>
>> $Version
= Mathics ...
"""

name = '$Version'

def evaluate(self, evaluation):
return String(version_string.replace('\n', ' '))
name = "$Aborted"


class Names(Builtin):
class ByteOrdering(Predefined):
"""
<dl>
<dt>'Names["$pattern$"]'
<dd>returns the list of names matching $pattern$.
<dt>'$ByteOrdering'
<dd>returns the native ordering of bytes in binary data on your computer system.
</dl>
>> Names["List"]
= {List}
The wildcard '*' matches any character:
>> Names["List*"]
= {List, ListLinePlot, ListPlot, ListQ, Listable}
>> $ByteOrdering == -1 || $ByteOrdering == 1
= True
"""

The wildcard '@' matches only lowercase characters:
>> Names["List@"]
= {Listable}
name = "$ByteOrdering"

>> x = 5;
>> Names["Global`*"]
= {x}
def evaluate(self, evaluation) -> Integer:
return Integer(1 if sys.byteorder == "big" else -1)

The number of built-in symbols:
>> Length[Names["System`*"]]
= ...

#> Length[Names["System`*"]] > 350
= True
class CommandLine(Predefined):
"""
<dl>
<dt>'$CommandLine'
<dd>is a list of strings passed on the command line to launch the Mathics session.
</dl>
>> $CommandLine
= {...}
"""

def apply(self, pattern, evaluation):
'Names[pattern_]'

pattern = pattern.get_string_value()
if pattern is None:
return

names = set([])
for full_name in evaluation.definitions.get_matching_names(pattern):
short_name = strip_context(full_name)
names.add(short_name if short_name not in names else full_name)
name = "$CommandLine"

# TODO: Mathematica ignores contexts when it sorts the list of
# names.
return Expression('List', *[String(name) for name in sorted(names)])
def evaluate(self, evaluation) -> Expression:
return Expression("List", *(String(arg) for arg in sys.argv))


class Aborted(Predefined):
class Environment(Builtin):
"""
<dl>
<dt>'$Aborted'
<dd>is returned by a calculation that has been aborted.
<dt>'Environment[$var$]'
<dd>returns the value of an operating system environment variable.
</dl>
Example:
<pre>
In[1] = Environment["HOME"]
Out[1] = rocky
</pre>
"""

name = '$Aborted'
def apply(self, var, evaluation):
"Environment[var_]"
if not isinstance(var, String):
return
env_var = var.get_string_value()
if env_var not in os.environ:
return Symbol("$Failed")
else:
return String(os.environ[env_var])


class Failed(Predefined):
Expand All @@ -102,23 +96,7 @@ class Failed(Predefined):
= $Failed
"""

name = '$Failed'


class CommandLine(Predefined):
'''
<dl>
<dt>'$CommandLine'
<dd>is a list of strings passed on the command line to launch the Mathics session.
</dl>
>> $CommandLine
= {...}
'''

name = '$CommandLine'

def evaluate(self, evaluation):
return Expression('List', *(String(arg) for arg in sys.argv))
name = "$Failed"


class Machine(Predefined):
Expand All @@ -135,9 +113,9 @@ class Machine(Predefined):
</pre>
"""

name = '$Machine'
name = "$Machine"

def evaluate(self, evaluation):
def evaluate(self, evaluation) -> String:
return String(sys.platform)


Expand All @@ -155,12 +133,59 @@ class MachineName(Predefined):
</pre>
"""

name = '$MachineName'
name = "$MachineName"

def evaluate(self, evaluation):
return String(os.uname().nodename)


class Names(Builtin):
"""
<dl>
<dt>'Names["$pattern$"]'
<dd>returns the list of names matching $pattern$.
</dl>
>> Names["List"]
= {List}
The wildcard '*' matches any character:
>> Names["List*"]
= {List, ListLinePlot, ListPlot, ListQ, Listable}
The wildcard '@' matches only lowercase characters:
>> Names["List@"]
= {Listable}
>> x = 5;
>> Names["Global`*"]
= {x}
The number of built-in symbols:
>> Length[Names["System`*"]]
= ...
#> Length[Names["System`*"]] > 350
= True
"""

def apply(self, pattern, evaluation):
"Names[pattern_]"

pattern = pattern.get_string_value()
if pattern is None:
return

names = set([])
for full_name in evaluation.definitions.get_matching_names(pattern):
short_name = strip_context(full_name)
names.add(short_name if short_name not in names else full_name)

# TODO: Mathematica ignores contexts when it sorts the list of
# names.
return Expression("List", *[String(name) for name in sorted(names)])


class ProcessorType(Predefined):
"""
<dl>
Expand All @@ -175,32 +200,33 @@ class ProcessorType(Predefined):
</pre>
"""

name = '$ProcessorType'
name = "$ProcessorType"

def evaluate(self, evaluation):
return String(platform.machine())


class ScriptCommandLine(Predefined):
'''
"""
<dl>
<dt>'$ScriptCommandLine'
<dd>is a list of string arguments when running the kernel is script mode.
</dl>
>> $ScriptCommandLine
= {...}
'''
"""

name = '$ScriptCommandLine'
name = "$ScriptCommandLine"

def evaluate(self, evaluation):
try:
dash_index = sys.argv.index('--')
dash_index = sys.argv.index("--")
except ValueError:
# not run in script mode
return Expression('List')
return Expression("List")

return Expression("List", *(String(arg) for arg in sys.argv[dash_index + 1 :]))

return Expression('List', *(String(arg) for arg in sys.argv[dash_index + 1:]))

class SystemID(Predefined):
"""
Expand All @@ -216,11 +242,12 @@ class SystemID(Predefined):
</pre>
"""

name = '$SystemID'
name = "$SystemID"

def evaluate(self, evaluation):
def evaluate(self, evaluation) -> String:
return String(sys.platform)


class SystemWordLength(Predefined):
"""
<dl>
Expand All @@ -235,13 +262,30 @@ class SystemWordLength(Predefined):
</pre>
"""

name = '$SystemWordLength'
name = "$SystemWordLength"

def evaluate(self, evaluation):
# https://docs.python.org/3/library/platform.html#module-platform
# says it is more reliable to get bits using sys.maxsize
# than platform.architecture()[0]
size = 128
while not sys.maxsize > 2**size:
while not sys.maxsize > 2 ** size:
size >>= 1
return Integer(size << 1)


class Version(Predefined):
"""
<dl>
<dt>'$Version'
<dd>returns a string with the current Mathics version and the versions of relevant libraries.
</dl>
>> $Version
= Mathics ...
"""

name = "$Version"

def evaluate(self, evaluation) -> String:
return String(version_string.replace("\n", " "))

0 comments on commit 27df55c

Please sign in to comment.