while tokenization I'm encountering this error - python-3.x

I am a newbie to programming and I tried to do some interesting stuffs and ended up here, while string tokenizing I encountered this error while I can't solve this atmost
code:
import re
def cw(text):
counts=dict()
text=text.lower()
words=re.split(r'[^\w]',text)
for i in words:
print(words)
if i !="":
if i not in words:
counts[i]=1
else:
counts[i]+=1
return counts
def input():
with open("input.txt",'r') as f:
text=f.read()
counts=cw(text)
sort=sorted(counts.items(),key=lambda pair: pair[1],reverse=True)
print(sort)
if __name__ == "__main__":
input()
Error is:
Traceback (most recent call last):
File "C:/Users/surya/AppData/Local/Programs/Python/Python37-32/okay.py", line 35, in <module>
input()
File "C:/Users/surya/AppData/Local/Programs/Python/Python37-32/okay.py", line 27, in input
counts=cw(text)
File "C:/Users/surya/AppData/Local/Programs/Python/Python37-32/okay.py", line 17, in cw
counts[i]+=1
KeyError: 'as'

Related

Executing SPARQL queries with the wikibaseintegrator package on a local Wikibase instance

I'm attempting to run a SPARQL query using the Python package wikibaseintegrator (version 0.10.0).
The program is written as follows:
from wikibaseintegrator.wbi_config import config as wbi_config
from wikibaseintegrator import wbi_login, wbi_core
wbi_config['MEDIAWIKI_API_URL'] = 'http://localhost/database_name/api.php'
wbi_config['SPARQL_ENDPOINT_URL'] = 'http://localhost:8989/database_name/sparql'
wbi_config['WIKIBASE_URL'] = 'http://wikibase.svc'
temp_username = "placeholder_username"
temp_password = "placeholder_password"
def main():
login_instance = login()
sparql_str = """
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P98 wd:Q45.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""
sparql_results = run_sparql_query(sparql_str)
print(sparql_results)
def login(username=temp_username, password=temp_password):
login_instance = wbi_login.Login(user=username, pwd=password)
return login_instance
def run_sparql_query(sparql_str):
sparql_results = wbi_core.ItemsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
return sparql_results
# MAIN
if __name__ == "__main__": main()
When I run this though, the error I get is:
Traceback (most recent call last):
File "database_script.py", line 52, in <module>
if __name__ == "__main__": main()
File "database_script.py", line 40, in main
sparql_results = run_sparql_query(sparql_str)
File "database_script.py", line 48, in run_sparql_query
sparql_results = wbi_core.ItemsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'ItemsEngine'
However, the documentation (https://github.com/LeMyst/WikibaseIntegrator) seems to imply that this is the correct way to format the query. Any help in diagnosing would be much appreciated!
EDIT 1: The documentation says it's in the ItemEngine (The method wbi_core.ItemEngine.execute_sparql_query()), but the program itself seems to show it being in the FuctionsEngine
I have tried all of these variations, with the error being the same:
$ python database_script.py
Traceback (most recent call last):
File "database_script.py", line 52, in <module>
if __name__ == "__main__": main()
File "database_script.py", line 40, in main
sparql_results = run_sparql_query(sparql_str)
File "database_script.py", line 48, in run_sparql_query
sparql_results = wbi_core.FuctionsEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'FuctionsEngine'
$ python database_script.py
Traceback (most recent call last):
File "database_script.py", line 52, in <module>
if __name__ == "__main__": main()
File "database_script.py", line 40, in main
sparql_results = run_sparql_query(sparql_str)
File "database_script.py", line 48, in run_sparql_query
sparql_results = wbi_core.ItemEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: type object 'ItemEngine' has no attribute 'execute_sparql_query'
$ python database_script.py
Traceback (most recent call last):
File "database_script.py", line 52, in <module>
if __name__ == "__main__": main()
File "database_script.py", line 40, in main
sparql_results = run_sparql_query(sparql_str)
File "database_script.py", line 48, in run_sparql_query
sparql_results = wbi_core.FunctionEngine.execute_sparql_query(sparql_str, endpoint=wbi_config['SPARQL_ENDPOINT_URL'])
AttributeError: module 'wikibaseintegrator.wbi_core' has no attribute 'FunctionEngine'
EDIT 2: The larger issue seemed to be the lack of an install for the SPARQL service since I had gotten it running with WAMP64. I installed a Docker instance and it's been a decent amount easier out of the box (except the export of the WAMP64 version and import into the Docker instance).

Python Multiprocessing( TypeError: cannot serialize '_io.BufferedReader' object )

I'm trying to make dictionary attack on zip file using Pool to increase speed.
But I face next error in Python 3.6, while it works in Python 2.7:
Traceback (most recent call last):
File "zip_crack.py", line 42, in <module>
main()
File "zip_crack.py", line 28, in main
for result in results:
File "/usr/lib/python3.6/multiprocessing/pool.py", line 761, in next
raise value
File "/usr/lib/python3.6/multiprocessing/pool.py", line 450, in _ handle_tasks
put(task)
File "/usr/lib/python3.6/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/usr/lib/python3.6/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: cannot serialize '_io.BufferedReader' object
I tried to search for same errors but couldn't find answer that can help here.
Code looks like this
def crack(pwd, f):
try:
key = pwd.strip()
f.extractall(pwd=key)
return True
except:
pass
z_file = zipfile.ZipFile("../folder.zip")
with open('words.dic', 'r') as passes:
start = time.time()
lines = passes.readlines()
pool = Pool(50)
results = pool.imap_unordered(partial(crack, f=z_file), lines)
pool.close()
for result in results:
if result:
pool.terminate()
break
pool.join()
I also tried another approach using map
with contextlib.closing(Pool(50)) as pool:
pool.map(partial(crack, f=z_file), lines)
which worked great and found passwords quickly in Python 2.7 but it throws same exception in python 3.6

EOFError in pickle.load and file not found error

elif(ch==2):
fh=open("emp1.txt","rb+")
fo=open("temp.txt","wb+")
ecode=input("Enter the Ecode :")
rec=(" ")
try:
while True:
emp1= pickle.load(fh)
if (emp1.ecode!=ecode):
pickle.dump(emp1,fh)
except(EOFError):
fh.close()
fo.close()
os.remove("empl.txt")
os.rename("temp.txt","emp1.txt")
print("")
running the following code gives me this error:
Traceback (most recent call last): File
"C:\Users\hello\Desktop\bhavi\python programming\Employ.py", line 78,
in
emp1= pickle.load(fh) EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\hello\Desktop\bhavi\python programming\Employ.py", line 85,
in
os.remove("empl.txt") FileNotFoundError: [WinError 2] The system cannot find the file specified: 'empl.txt'
What should i do now??
You should fix your path. In the first case, you write "emp1.txt"; and in the second, you write "empl.txt". If you look carefully, you should notice that there is a difference in those two strings.
Hint: '1' != 'l'
Your code could probably be refactored as well. While it is not possible for others to test your code since it is very incomplete, the following should work in its place. You will still need to verify it works.
elif ch == 2:
with open('emp1.txt', 'rb+') as fh, open('temp.txt', 'wb+') as fo:
ecode = input('Enter the Ecode: ')
while True:
try:
item = pickle.load(fh)
except EOFError:
break
else:
if item.ecode != ecode:
pickle.dump(item, fo)
os.remove(fh.name)
os.rename(fo.name, fh.name)
print()
I would use shelve, its much easier to use and it doesn't come up with to many errors in my experience. shelve is built on pickle but it just simplify it.
here is a short tutorial
http://python.wikia.com/wiki/Shelve

New line on error message in KeyError - Python 3.3

I am using Python 3.3 through the IDLE. While running a code that looks like:
raise KeyError('This is a \n Line break')
it outputs:
Traceback (most recent call last):
File "test.py", line 4, in <module>
raise KeyError('This is a \n Line break')
KeyError: 'This is a \n Line break'
I would like it to output the message with the line break like this:
This is a
Line Break
I have tried to convert it to a string before or using os.linesep but nothing seems to work. Is there any way I can force the message to be correctly shown on the IDLE?
If I raise an Exception (instead of KeyError) then the output is what I want, but I would like to still raise a KeyError if possible.
You problem has nothing to do with IDLE. The behavior you see is all from Python. Running current repository CPython interactively, from a command line, we see the behavior you reported.
Python 3.7.0a2+ (heads/pr_3947:01eae2f721, Oct 22 2017, 14:06:43)
[MSC v.1900 32 bit (Intel)] on win32
>>> raise KeyError('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'This is a \n Line break'
>>> s = 'This is a \n Line break'
>>> s
'This is a \n Line break'
>>> print(s)
This is a
Line break
>>> raise Exception('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: This is a
Line break
>>> raise IndexError(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: This is a
Line break
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e)
'This is a \n Line break'
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e.args[0])
This is a
Line break
I don't know why KeyError acts differently from even IndexError, but printing e.args[0] should work for all exceptions.
EDIT
The reason for the difference is given in this old tracker issue, which quotes a comment in the KeyError source code:
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
KeyError: ''
rather than the confusing
KeyError
alone. The downside is that if KeyError is raised with an
explanatory
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
This section appears in the KeyError_str object definition in Objects/exceptions.c of the Python source code.
I will mention your issue as another manifestation of this difference.
There is a way to get the behavior you want: Simply subclass str and override __repr__:
class KeyErrorMessage(str):
def __repr__(self): return str(self)
msg = KeyErrorMessage('Newline\nin\nkey\nerror')
raise KeyError(msg)
Prints:
Traceback (most recent call last):
...
File "", line 5, in
raise KeyError(msg)
KeyError: Newline
in
key
error

python: TypeError: expected string or buffer: can't reproduce error with prompt

I am trying to debug this function:
def check_domains(url):
global num_websites,domain_queue,domains,doc_queue,stanford_tagger
the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url)
if the_domain is not None:
if the_domain.groups(0)[1] not in domains.keys():
domains[the_domain.groups(0)[1]] = website(doc_queue,the_domain.groups(0)[1])
domains[the_domain.groups(0)[1]].add_initial_url(url)
domain_queue.append(domains[the_domain.groups(0)[1]])
num_websites = num_websites + 1
else:
domains[the_domain.groups(0)[1]].add_url(url)
error:
File "web_crawler.py", line 178, in getdoc
check_domains(check)
File "web_crawler.py", line 133, in check_domains
the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url)
File "/usr/local/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
I try and be a good boy and test in interactive mode:
>>> def check_domains(url):
... the_domain = re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$',url) #right here
... if the_domain is not None:
... print the_domain.groups(0)[1]
... else:
... print "NOOOO!!!!!"
...
>>>
>>> check_domains("http://www.hulu.com/watch/6704")
hulu.com
>>> check_domains("https://docs.python.org/2/library/datetime.html")
python.org
so this does what I want it to do and I didn't change that line. But why???
The value of url can still be None and that's what gives this error:
>>> re.match(r'^(:?https?:\/\/[^.]*\.)?([^/#?&]+).*$', None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
So you should check whether the object that you're passing for url is indeed a string. It may even be a number or something else but it's not a string which is what the matching function expects.

Resources