Issue with python version 2.7.3 importing maxrepeat module - python

I am trying to import maxrepeat module in python 2.7.3 and couldn’t get much information in google can some one please help.
What is the module that helps maxrepeat module to work ?
I am able to import maxrepeat module using “from _sre import maxrepeat” but still fails while runnninv automation .

MAXREPEAT is used internally by the re module as an upper limit for the minimum, maximum, or exact number of repetitions that can be specified in a pattern. For example:
>>> import re
>>> re.compile(r'a{100}') # exactly 100 "a"s
<_sre.SRE_Pattern object at 0x7fa68be10780>
>>> re.compile(r'a{100, 200}') # between 100 and 200 "a"s
Equalling or exceeding MAXREPEAT in a repetition value causes an exception to be raised by the regular expression parser in module sre_parse:
>>> from sre_constants import MAXREPEAT
>>> MAXREPEAT
4294967295L
>>> re.compile(r'a{{{}}}'.format(MAXREPEAT-1))
<_sre.SRE_Pattern object at 0x7f0ec959f660>
>>> re.compile(r'a{{{}}}'.format(MAXREPEAT))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/re.py", line 194, in compile
return _compile(pattern, flags)
File "/usr/lib64/python2.7/re.py", line 249, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib64/python2.7/sre_compile.py", line 572, in compile
p = sre_parse.parse(p, flags)
File "/usr/lib64/python2.7/sre_parse.py", line 716, in parse
p = _parse_sub(source, pattern, 0)
File "/usr/lib64/python2.7/sre_parse.py", line 324, in _parse_sub
itemsappend(_parse(source, state))
File "/usr/lib64/python2.7/sre_parse.py", line 518, in _parse
raise OverflowError("the repetition number is too large")
OverflowError: the repetition number is too large
There should not be any reason to care about MAXREPEAT with normal use of the re module. If you need to handle errors then use the exception:
try:
re.compile(r'a{{{}}}'.format(MAXREPEAT))
except OverflowError as exc:
print 'Failed to compile pattern: {}'.format(exc.message)

Related

How do I fix WinError 126 for Pyenchant?

I am relatively new to Python. I already know what most things are and do so now I am trying to work on as many projects as I can to get familiar with Python coding.
But here I've run into a speed bump. I'm trying to create something that takes in letters from a user to generate words ranging from 3 letters to the length of the word.
My code is okay, I think, but when I run it I get a WinError 126 and I'm not trying to import any dll files. I reckon it has something to do with Pyenchant.
Please, may someone help me??
CODE & ERROR:
from itertools import permutations
import enchant
d = enchant.Dict("en_UK")
WORDS = set()
LETTERS = input("ENTER LETTERS: ")
PERMS = [i.upper() for i in LETTERS]
for n in range(2, len(LETTERS)):
for x in list(permutations(PERMS, n)):
ANAGRAMS = "".join(x)
if d.check(ANAGRAMS):
WORDS.add(ANAGRAMS)
print(WORDS)
_________________________________________________________
Traceback (most recent call last):
File "C:\Users\JAVA MANU\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\WordScapes cheat tool v2.py", line 2, in <module>
import enchant
File "C:\Users\JAVA MANU\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\enchant\__init__.py", line 81, in <module>
from enchant import _enchant as _e
File "C:\Users\JAVA MANU\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\enchant\_enchant.py", line 161, in <module>
e = ctypes.cdll.LoadLibrary(enchant_lib_path)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 442, in LoadLibrary
return self._dlltype(name)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

Avoid error when trying crawling the web

I am trying ' crawling the web in python '. the problem is that I get an error with the re module. Here is my code:
#Python27
import re
import urllib
htm = urllib.urlopen('http://www.4shared.com/')
wp = htm.read()
begin = []
for start_tag in wp:
x = re.search('<a', wp)
begin.append(x.end())
Here the error message:
Traceback (most recent call last):
File "E:/Python Essen/Python27/crawling_web_pratice.py", line 23, in <module>
a = re.search(start_tag, wp)
File "C:\Python27\lib\re.py", line 146, in search
return _compile(pattern, flags).search(string)
File "C:\Python27\lib\re.py", line 251, in _compile
raise error, v # invalid expression
error: nothing to repeat
Please How to avoid the error ?

Not able to Import in NLTK - Python

When I run this command in a file or in a shell
import nltk
I get the following error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/nltk/__init__.py", line 83, in <module>
from collocations import *
File "/usr/lib/python2.7/dist-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/usr/lib/python2.7/dist-packages/nltk/probability.py", line 45, in <module>
import random
File "random.py", line 2, in <module>
T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''
Not able to comprehend what is going wrong.
You have a local random module, which masks the random module from standard library.
If you try to import nltk from a different working directory, it should succeed. But in general it's not a good idea name your modules after standard modules, so please rename your random.py file to something else.
You for completeness, let me say that the error was obvious from the last lines of you traceback:
File "random.py", line 2, in <module>
T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''
From the path, random.py, you can tell that the error is in a local file named random.py. And from the exception, you know that something passed an empty string, '', from raw_input to int function, which failed to be converted to int.
Rule of thumb number 2: Always guard you executable code, in a module, in a if __name__ == '__main__': block.

ImportError: No module named _multiprocessing on python 2.7.3

I'm trying to do:
import multiprocessing
but get the error:
ERROR 2015-01-21 18:40:49,457 wsgi.py:263]
Traceback (most recent call last):
File "/home/nir/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/nir/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/nir/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/root/workspace/spring-nova-827/Main.py", line 2, in <module>
import multiprocessing
File "/usr/lib/python2.7/multiprocessing/__init__.py", line 84, in <module>
import _multiprocessing
File "/home/nir/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 898, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named _multiprocessing
I looked this problem up, but could not find a solution
You are running in a sand-boxed environment that will not allow you to import certain modules in the standard library (multiprocessing) in this case.
Why do you need multiprocessing? It may be that you can get away with using threading, though that may come with a performance hit depending on what you are trying to achieve. I've also noticed that concurrent.futures is also recommended for use with App Engine.
See the sections on "The Sandbox" and "Pure Python". https://cloud.google.com/appengine/docs/python/

What's the maximum number of repetitions allowed in a Python regex?

In Python 2.7 and 3, the following works:
>>> re.search(r"a{1,9999}", 'aaa')
<_sre.SRE_Match object at 0x1f5d100>
but this gives an error:
>>> re.search(r"a{1,99999}", 'aaa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/usr/lib/python2.7/re.py", line 240, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib/python2.7/sre_compile.py", line 523, in compile
groupindex, indexgroup
RuntimeError: invalid SRE code
It seems like there is an upper limit on the number of repetitions allowed. Is this part of the regular expression specification, or a Python-specific limitation? If Python-specific, is the actual number documented somewhere, and does it vary between implementations?
A quick manual binary search revealed the answer, specifically 65535:
>>> re.search(r"a{1,65535}", 'aaa')
<_sre.SRE_Match object at 0x2a9a68>
>>>
>>> re.search(r"a{1,65536}", 'aaa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/re.py", line 240, in _compile
p = sre_compile.compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/sre_compile.py", line 523, in compile
groupindex, indexgroup
OverflowError: regular expression code size limit exceeded
This is discussed here:
The limit is an implementation detail. The pattern is compiled into codes which are then interpreted, and it just happens that the codes are (usually) 16 bits, giving a range of 0..65535, but it uses 65535 to represent no limit and doesn't warn if you actually write 65535.
and
The quantifiers use 65535 to represent no upper limit, so ".{0,65535}" is equivalent to ".*".
Thanks to the authors of the comments below for pointing a few more things out:
CPython implements this limitation in _sre.c. (#LukasGraf)
There is a constant MAXREPEAT in sre_constants.py that holds this max repetition value:
>>> import sre_constants
>>>
>>> sre_constants.MAXREPEAT
65535
(#MarkkuK. and #hcwhsa)

Categories