======================== Perl as a Python package ========================
.. image:: https://travis-ci.org/radiac/python-perl.svg?branch=master :target: https://travis-ci.org/radiac/python-perl
.. image:: https://coveralls.io/repos/radiac/python-perl/badge.svg?branch=master&service=github :target: https://coveralls.io/github/radiac/python-perl?branch=master
Haven't you always dreamed of having the power of Perl at your fingertips when writing Python?
Well, this package is proof that dreams can come true::
>>> import perl
>>> value = "Hello there"
>>> if value =~ /^hello (.+?)$/i:
... print("Found greeting:", $1)
...
Found greeting: there
>>> value =~ s/there/world/
>>> print(value)
Hello world
Note: This is very silly and probably shouldn't go anywhere near production code.
This requires Python 3.7 or later.
Install with pip::
pip install perl
The module needs to be loaded before Python tries to read code which uses these enhancements. There are therefore four different ways to use this module:
Pass it to Python on the command line::
python3.7 -m perl myscript.py
Set it on your script's shebang::
#!/usr/bin/python3.7 -mperl
Import it before importing any of your code which uses its syntax - usually in
your __init__.py
::
import perl
.. note::
You only need to import it once in your project.
However, because Python needs to read the whole file before it can run the
import, you cannot use ``perl``'s functionality in the same file where you
``import perl``.
Use it on the Python interactive shell (REPL)::
$ python3.7
>>> import perl
or::
$ python3.7 -m perl
Syntax::
val =~ /pattern/flags
# or
val =~ m/pattern/flags
where pattern
uses Python's regex syntax
_, and flags
is a subset of the
characters AILMSXG
, which map Python's single character flags, plus g
which
mimics the global flag from Perl.
When run without the global flag, the re.Match
object is returned; any matched
groups will be available as numbered dollar variables, eg $1
, and named groups will
be available on $name
.
When run with the global flag, the list of re.Match
objects will be returned. No
dollar variables will be set.
.. _Python's regex syntax: https://docs.python.org/3/library/re.html#regular-expression-syntax
Examples::
# Case insensitive match
value =~ /^foo (.+?) bar$/i
print(f"Matched {$1}")
# Use in a condition
if value =~ /^foo (.+?) bar$/i:
return $1
# Use as a global
matches = value =~ /foo (.+?) bar/gi;
Syntax::
val =~ s/pattern/replacement/flags
where pattern
uses Python's regex syntax
_, and flags
is a subset of the
characters AILMSXG
, which map Python's single character flags, plus g
which
mimics the global flag from Perl to replace all occurrences of the match.
Examples::
# Case insensitive global replacement
value =~ s/foo/bar/gi
# Backreferences
value =~ s/(.+?) (?<name>.+?)/$1 $name/
Syntax::
$name
$number
Dollar variables act like regular variables - they can be set and used as normal. They are primarily intended for use with regular expressions - each regex will remove all previous dollar variables, to avoid confusion as to whether they matched or not.
During development, install in a virtual environment::
mkdir python-perl
cd python-perl
git clone <path-to-repo> repo
virtualenv --python=python3.7 venv
. venv/bin/activate
cd repo
pip install -r requirements.txt
To run tests::
cd path/to/repo
. ../venv/bin/activate
pytest
To run the example, use one of the following::
$ ./example.py
$ python3.7 -m perl example.py
$ python3.7 example_importer.py
Bumps ipython from 7.7.0 to 7.16.3.
d43c7c7
release 7.16.35fa1e40
Merge pull request from GHSA-pq7m-3gw7-gq5x8df8971
back to dev9f477b7
release 7.16.2138f266
bring back release helper from master branch5aa3634
Merge pull request #13341 from meeseeksmachine/auto-backport-of-pr-13335-on-7...bcae8e0
Backport PR #13335: What's new 7.16.28fcdcd3
Pin Jedi to <0.17.2.2486838
release 7.16.120bdc6f
fix conda buildDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps urllib3 from 1.25.3 to 1.25.8.
Sourced from urllib3's releases.
1.25.8
Release: 1.25.8
1.25.7
No release notes provided.
1.25.6
Release: 1.25.6
1.25.5
Release: 1.25.5
1.25.4
Release: 1.25.4
Sourced from urllib3's changelog.
1.25.8 (2020-01-20)
1.25.7 (2019-11-11)
Preserve
chunked
parameter on retries (Pull #1715, Pull #1734)Allow unset
SERVER_SOFTWARE
in App Engine (Pull #1704, Issue #1470)Fix issue where URL fragment was sent within the request target. (Pull #1732)
Fix issue where an empty query section in a URL would fail to parse. (Pull #1732)
Remove TLS 1.3 support in SecureTransport due to Apple removing support (Pull #1703)
1.25.6 (2019-09-24)
- Fix issue where tilde (
~
) characters were incorrectly percent-encoded in the path. (Pull #1692)1.25.5 (2019-09-19)
- Add mitigation for BPO-37428 affecting Python <3.7.4 and OpenSSL 1.1.1+ which caused certificate verification to be enabled when using
cert_reqs=CERT_NONE
. (Issue #1682)1.25.4 (2019-09-19)
Propagate Retry-After header settings to subsequent retries. (Pull #1607)
Fix edge case where Retry-After header was still respected even when explicitly opted out of. (Pull #1607)
Remove dependency on
rfc3986
for URL parsing.Fix issue where URLs containing invalid characters within
Url.auth
would raise an exception instead of percent-encoding those characters.
... (truncated)
2a57bc5
Release 1.25.8 (#1788)a2697e7
Optimize _encode_invalid_chars (#1787)d2a5a59
Move IPv6 test skips in server fixturesd44f0e5
Factorize test certificates serialization84abc7f
Generate IPV6 certificates using trustme6a15b18
Run IPv6 Tornado server from fixture4903840
Use trustme to generate IP_SAN cert9971e27
Empty responses should have no lines.62ef68e
Use trustme to generate NO_SAN certsfd2666e
Use fixture to configure NO_SAN test certsDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps py from 1.8.0 to 1.10.0.
Sourced from py's changelog.
1.10.0 (2020-12-12)
- Fix a regular expression DoS vulnerability in the py.path.svnwc SVN blame functionality (CVE-2020-29651)
- Update vendored apipkg: 1.4 => 1.5
- Update vendored iniconfig: 1.0.0 => 1.1.1
1.9.0 (2020-06-24)
Add type annotation stubs for the following modules:
py.error
py.iniconfig
py.path
(not including SVN paths)py.io
py.xml
There are no plans to type other modules at this time.
The type annotations are provided in external .pyi files, not inline in the code, and may therefore contain small errors or omissions. If you use
py
in conjunction with a type checker, and encounter any type errors you believe should be accepted, please report it in an issue.1.8.2 (2020-06-15)
- On Windows,
py.path.local
s which differ only in case now have the same Python hash value. Previously, such paths were considered equal but had different hashes, which is not allowed and breaks the assumptions made by dicts, sets and other users of hashes.1.8.1 (2019-12-27)
Handle
FileNotFoundError
when trying to import pathlib inpath.common
on Python 3.4 (#207).
py.path.local.samefile
now works correctly in Python 3 on Windows when dealing with symlinks.
e5ff378
Update CHANGELOG for 1.10.094cf44f
Update vendored libs5e8ded5
testing: comment out an assert which fails on Python 3.9 for nowafdffcc
Rename HOWTORELEASE.rst to RELEASING.rst2de53a6
Merge pull request #266 from nicoddemus/gh-actionsfa1b32e
Merge pull request #264 from hugovk/patch-2887d6b8
Skip test_samefile_symlink on pypy3 on Windowse94e670
Fix test_comments() in test_sourcefef9a32
Adapt test4a694b0
Add GitHub Actions badge to READMEDependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Bumps pygments from 2.4.2 to 2.7.4.
Sourced from pygments's releases.
2.7.4
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
2.7.3
... (truncated)
Sourced from pygments's changelog.
Version 2.7.4
(released January 12, 2021)
Updated lexers:
Fix infinite loop in SML lexer (#1625)
Fix backtracking string regexes in JavaScript/TypeScript, Modula2 and many other lexers (#1637)
Limit recursion with nesting Ruby heredocs (#1638)
Fix a few inefficient regexes for guessing lexers
Fix the raw token lexer handling of Unicode (#1616)
Revert a private API change in the HTML formatter (#1655) -- please note that private APIs remain subject to change!
Fix several exponential/cubic-complexity regexes found by Ben Caller/Doyensec (#1675)
Fix incorrect MATLAB example (#1582)
Thanks to Google's OSS-Fuzz project for finding many of these bugs.
Version 2.7.3
(released December 6, 2020)
... (truncated)
4d555d0
Bump version to 2.7.4.fc3b05d
Update CHANGES.ad21935
Revert "Added dracula theme style (#1636)"e411506
Prepare for 2.7.4 release.275e34d
doc: remove Perl 6 ref2e7e8c4
Fix several exponential/cubic complexity regexes found by Ben Caller/Doyenseceb39c43
xquery: fix pop from empty stack2738778
fix coding style in test_analyzer_lexer02e0f09
Added 'ERROR STOP' to fortran.py keywords. (#1665)c83fe48
support added for css variables (#1633)Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase
.
Hello,
would be great if your module would be able to do get the grouped characters from a substitution regex like this perl script does:
```
$sentence = "This is whatever."; $sentence =~ s/([.,:;]$)//; $remove_punctuation_mark = $1; print "<$sentence><$remove_punctuation_mark>\n"; ```
I didn't find any way to do this with the normal python regex. With your perl module I would expect this to work:
```
sentence = "This is whatever." sentence =~ s/([.,:;])$// remove_punctuation_mark = $1 print(t, remove_punctuation_mark) ```
But this gives 2 errors:
1. Error:
sentence =~ s/([\.,:;])$//
^
SyntaxError: unexpected character after line continuation character
import numpy as np value = "Hello there" value =~ s/there/world/ print(value)
When run this scripts, I get this imformation:
Traceback (most recent call last):
β File "/usr/lib64/python3.7/site-packages/numpy/core/init.py", line 40, in
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
β File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
β "main", mod_spec)
β File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
β exec(code, run_globals)
β File "/home/leon/.local/lib/python3.7/site-packages/perl/main.py", line 25, in
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try git clean -xdf
β (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
β your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
β 1. Check that you are using the Python you expect (you're using /usr/bin/python3.7),
β βand that you have no directories in your PATH or PYTHONPATH that can
β βinterfere with the Python and numpy versions you're trying to use.
β 2. If (1) looks fine, you can open a new issue at
β βhttps://github.com/numpy/numpy/issues. Please include details on:
β β- how you installed Python
β β- how you installed numpy
β β- your operating system
β β- whether or not you have multiple versions of Python installed
β β- if you built from source, your compiler versions and ideally a build log
β βNote: this error has many possible causes, so please don't comment on β βan existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'