-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds -i/--import short import syntax.
- Loading branch information
Showing
11 changed files
with
279 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. highlight:: bash | ||
|
||
|
||
Imports | ||
======= | ||
|
||
``--i``/``--import`` is a shortcut to easily import external libraries. | ||
|
||
Importing a module | ||
------------------ | ||
|
||
The equivalent of ``import xyz`` is ``--import xyz``. It is equivalent to ``--begin import xyz``, just shorter. | ||
|
||
.. code:: bash | ||
py1 --begin 'import math' 'P(math.cos(math.pi))' | ||
py1 --import 'math' 'P(math.cos(math.pi))' | ||
py1 -i 'math' 'P(math.cos(math.pi))' | ||
Importing specific symbols | ||
-------------------------- | ||
|
||
The equivalent of ``from xyz import abc`` is ``--import xyz/abc``. You can import multiple functions with ``--import xyz/abc,def`` | ||
|
||
.. code:: bash | ||
py1 --import 'math/cos,pi' 'P(cos(pi))' | ||
py1 -i 'math/*' 'P(cos(pi))' | ||
Importing with a specific name | ||
------------------------------ | ||
|
||
The equivalent of ``import abc as ABC`` is ``--import abc:ABC``. You can rename specific symbols in the same way like ``--import xyz/abc:ABC`` | ||
|
||
.. code:: bash | ||
py1 --import 'math:M' 'P(M.cos(M.pi))' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,73 @@ | ||
.. highlight:: bash | ||
|
||
|
||
Internals | ||
========= | ||
|
||
This describes the implementation of py1, not its usage. | ||
Unix timestamp | ||
-------------- | ||
|
||
Formats a Unix timestamp (1443214640) as a human-readable string. | ||
|
||
.. code:: bash | ||
py1 -i 'time/ctime' 'P(ctime(1443214640))' | ||
To do so we import ctime from the time module and print it. | ||
|
||
|
||
Count blank lines | ||
----------------- | ||
|
||
Count the number of blank lines in a file. | ||
|
||
.. code:: bash | ||
py1 -b 'c=0' -l 'if not L: c += 1' -e 'P(c)' | ||
Here we define an acumulator variable and increment it when the line satisfies a criteria. | ||
|
||
|
||
Print 2nd and 3rd fields | ||
------------------------ | ||
|
||
Show the second and third fields of ``/etc/passwd``, a file whose fields are separated by "``:``". | ||
|
||
.. code:: bash | ||
cat /etc/passwd | py1 -b 'WS=":"' -l 'P(W[1:2])' | ||
Here we override WS to use the "``:``" separator of ``/etc/passwd``. | ||
|
||
|
||
Show lines matching a regexp | ||
---------------------------- | ||
|
||
Show lines matching the regexp '$a+^'. | ||
|
||
.. code:: bash | ||
py1 -l 'if M("$a+^", L): P(L)' | ||
Here we use the M matching function to match the regexp. | ||
|
||
Count blank lines again | ||
----------------------- | ||
|
||
Count the number of blank lines in a file. | ||
|
||
.. code:: bash | ||
py1 -e 'P(sum(1 if l else 0 for l in F))' | ||
Here we do not set a per-line statement and instead have sum iterate over F. | ||
|
||
Group by | ||
-------- | ||
|
||
Given a file of '$name $value', with name being repeated, sum the values for each name. | ||
|
||
.. autosummary:: | ||
:toctree: _autosummary | ||
.. code:: bash | ||
py1.curly | ||
py1.runner | ||
py1.template_reader | ||
py1.tty | ||
py1 -b 'd=defaultdict(int)' -l 'd[W[0]] += int(W[1])' | ||
-e 'for n, v in d: P(n, v)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright (c) 2013-2015, Brice Arnould <unbrice@vleu.net> | ||
# All rights reserved. | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following condition are met: | ||
# | ||
# 1. Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
"""Parses the import statements.""" | ||
|
||
|
||
class Error(Exception): | ||
|
||
"""Base class for errors from this module.""" | ||
|
||
|
||
class BadShortSyntaxError(Error): | ||
|
||
"""The user-provided code does not decode to valid code.""" | ||
|
||
def __init__(self, short_import, expanded_import): | ||
msg ='Import statement %s means %s which is invalid.' % ( | ||
short_import, expanded_import) | ||
super(BadShortSyntaxError, self).__init__(msg) | ||
self.short_import = short_import | ||
self.expanded_import = expanded_import | ||
|
||
|
||
def expand_short(short_import): | ||
|
||
# from_str: the (optional) part between "from" and "import" | ||
if '/' in short_import: | ||
from_str, imported_str = short_import.split('/', 1) | ||
else: | ||
from_str, imported_str = None, short_import | ||
|
||
expanded_imported_str = imported_str.replace(':', ' as ') | ||
|
||
if from_str: | ||
res = 'from %s import %s' % (from_str, expanded_imported_str) | ||
else: | ||
res = 'import %s' % expanded_imported_str | ||
|
||
try: | ||
code = compile(res, '<py1:imports.expand_short>', 'exec') | ||
except SyntaxError: | ||
code = None | ||
|
||
if not code: | ||
raise BadShortSyntaxError(short_import, res) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) 2013-2015, Brice Arnould <unbrice@vleu.net> | ||
# All rights reserved. | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following condition are met: | ||
# | ||
# 1. Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# | ||
# 2. Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
|
||
"""Tests for the imports module.""" | ||
|
||
import threading | ||
import unittest | ||
|
||
from py1 import imports | ||
|
||
|
||
class TestExpand(unittest.TestCase): | ||
|
||
def testGoodSamples(self): | ||
samples = [ | ||
# The full syntax | ||
('m1.m2.m3/i1:n1,i2:n2,i3', 'from m1.m2.m3 import i1 as n1,i2 as n2,i3'), | ||
# No from | ||
('i1:n1,i2:n2,i3', 'import i1 as n1,i2 as n2,i3'), | ||
# Just one import | ||
('i', 'import i'), | ||
# Import * | ||
('i/*', 'from i import *'), | ||
] | ||
|
||
for short_import, expanded_import in samples: | ||
self.assertEqual(expanded_import, imports.expand_short(short_import)) | ||
|
||
def testInvalidSamples(self): | ||
samples = [ | ||
# Extra "!" | ||
'i!', | ||
# Import star without scope | ||
'*', | ||
# Missing name | ||
'a:', | ||
# Missing module | ||
':b', | ||
] | ||
|
||
for short_import in samples: | ||
self.assertRaises(imports.BadShortSyntaxError, imports.expand_short, short_import) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters