Perl as a Python module

radiac, updated πŸ•₯ 2022-01-21 20:01:34

======================== 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.

  • Project site: http://radiac.net/projects/python-perl/
  • Source code: https://github.com/radiac/python-perl

Installation

This requires Python 3.7 or later.

Install with pip::

pip install perl

Usage

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:

  1. Pass it to Python on the command line::

    python3.7 -m perl myscript.py
    
  2. Set it on your script's shebang::

    #!/usr/bin/python3.7 -mperl
    
  3. 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``.
    
  4. Use it on the Python interactive shell (REPL)::

    $ python3.7
    >>> import perl
    

    or::

    $ python3.7 -m perl
    

Features

Regular expression matching

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;

Regular expression replacement

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/

Dollar variables

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.

Contributing

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

Issues

Bump ipython from 7.7.0 to 7.16.3

opened on 2022-01-21 20:01:33 by dependabot[bot]

Bumps ipython from 7.7.0 to 7.16.3.

Commits


Dependabot compatibility score

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.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/radiac/python-perl/network/alerts).

Bump urllib3 from 1.25.3 to 1.25.8

opened on 2021-04-30 21:19:48 by dependabot[bot]

Bumps urllib3 from 1.25.3 to 1.25.8.

Release notes

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

Changelog

Sourced from urllib3's changelog.

1.25.8 (2020-01-20)

  • Drop support for EOL Python 3.4 (Pull #1774)

  • Optimize _encode_invalid_chars (Pull #1787)

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)

Commits
  • 2a57bc5 Release 1.25.8 (#1788)
  • a2697e7 Optimize _encode_invalid_chars (#1787)
  • d2a5a59 Move IPv6 test skips in server fixtures
  • d44f0e5 Factorize test certificates serialization
  • 84abc7f Generate IPV6 certificates using trustme
  • 6a15b18 Run IPv6 Tornado server from fixture
  • 4903840 Use trustme to generate IP_SAN cert
  • 9971e27 Empty responses should have no lines.
  • 62ef68e Use trustme to generate NO_SAN certs
  • fd2666e Use fixture to configure NO_SAN test certs
  • Additional commits viewable in compare view


Dependabot compatibility score

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.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/radiac/python-perl/network/alerts).

Bump py from 1.8.0 to 1.10.0

opened on 2021-04-20 18:34:36 by dependabot[bot]

Bumps py from 1.8.0 to 1.10.0.

Changelog

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.locals 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 in path.common on Python 3.4 (#207).

  • py.path.local.samefile now works correctly in Python 3 on Windows when dealing with symlinks.

Commits
  • e5ff378 Update CHANGELOG for 1.10.0
  • 94cf44f Update vendored libs
  • 5e8ded5 testing: comment out an assert which fails on Python 3.9 for now
  • afdffcc Rename HOWTORELEASE.rst to RELEASING.rst
  • 2de53a6 Merge pull request #266 from nicoddemus/gh-actions
  • fa1b32e Merge pull request #264 from hugovk/patch-2
  • 887d6b8 Skip test_samefile_symlink on pypy3 on Windows
  • e94e670 Fix test_comments() in test_source
  • fef9a32 Adapt test
  • 4a694b0 Add GitHub Actions badge to README
  • Additional commits viewable in compare view


Dependabot compatibility score

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.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/radiac/python-perl/network/alerts).

Bump pygments from 2.4.2 to 2.7.4

opened on 2021-03-29 20:27:33 by dependabot[bot]

Bumps pygments from 2.4.2 to 2.7.4.

Release notes

Sourced from pygments's releases.

2.7.4

  • Updated lexers:

    • Apache configurations: Improve handling of malformed tags (#1656)

    • CSS: Add support for variables (#1633, #1666)

    • Crystal (#1650, #1670)

    • Coq (#1648)

    • Fortran: Add missing keywords (#1635, #1665)

    • Ini (#1624)

    • JavaScript and variants (#1647 -- missing regex flags, #1651)

    • Markdown (#1623, #1617)

    • Shell

      • Lex trailing whitespace as part of the prompt (#1645)
      • Add missing in keyword (#1652)
    • SQL - Fix keywords (#1668)

    • Typescript: Fix incorrect punctuation handling (#1510, #1511)

  • 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)

Changelog

Sourced from pygments's changelog.

Version 2.7.4

(released January 12, 2021)

  • Updated lexers:

    • Apache configurations: Improve handling of malformed tags (#1656)

    • CSS: Add support for variables (#1633, #1666)

    • Crystal (#1650, #1670)

    • Coq (#1648)

    • Fortran: Add missing keywords (#1635, #1665)

    • Ini (#1624)

    • JavaScript and variants (#1647 -- missing regex flags, #1651)

    • Markdown (#1623, #1617)

    • Shell

      • Lex trailing whitespace as part of the prompt (#1645)
      • Add missing in keyword (#1652)
    • SQL - Fix keywords (#1668)

    • Typescript: Fix incorrect punctuation handling (#1510, #1511)

  • 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)

Commits
  • 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 ref
  • 2e7e8c4 Fix several exponential/cubic complexity regexes found by Ben Caller/Doyensec
  • eb39c43 xquery: fix pop from empty stack
  • 2738778 fix coding style in test_analyzer_lexer
  • 02e0f09 Added 'ERROR STOP' to fortran.py keywords. (#1665)
  • c83fe48 support added for css variables (#1633)
  • Additional commits viewable in compare view


Dependabot compatibility score

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.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/radiac/python-perl/network/alerts).

$1 after s/// not working

opened on 2020-02-25 21:39:40 by ruppde

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:

```

!/usr/bin/perl

$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:

```

!/usr/bin/python3.8 -mperl

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

  1. If the regex is changed to e.g. s/([.,:;])$/X/ it throws the error: NameError: name '__perl__var__1' is not defined

the numpy can not import when using this package

opened on 2020-02-18 05:58:34 by toothedsword

!/usr/bin/python3.7 -mperl

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 ▏ from . import multiarray ▏ File "/usr/lib64/python3.7/site-packages/numpy/core/multiarray.py", line 12, in ▏ from . import overrides ▏ File "/usr/lib64/python3.7/site-packages/numpy/core/overrides.py", line 6, in ▏ from numpy.core._multiarray_umath import ( ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

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 ▏ load("main", args.filename) ▏ File "/home/leon/.local/lib/python3.7/site-packages/perl/loader.py", line 56, in load ▏ loader.exec_module(module) ▏ File "", line 728, in exec_module ▏ File "", line 219, in _call_with_frames_removed ▏ File "./test3.1.py", line 6, in ▏ import numpy as np ▏ File "/usr/lib64/python3.7/site-packages/numpy/init.py", line 142, in ▏ from . import core ▏ File "/usr/lib64/python3.7/site-packages/numpy/core/init.py", line 71, in ▏ raise ImportError(msg) ImportError:

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'

Richard Terry
GitHub Repository