Have error with xgoogle library
When I tried to use this code:
from xgoogle.search import GoogleSearch, SearchError
try:
gs = GoogleSearch("quick")
gs.results_per_page = 50
results = gs.get_results()
for res in results:
print (res.title.encode("utf8"))
print (res.desc.encode("utf8"))
print (res.url.encode("utf8"))
I get this error:
Traceback (most recent call last):
File "C:/Users/s.sorokin/PycharmProjects/hh.ru/xgoogle.py", line 1, in <module>
from xgoogle.search import GoogleSearch, SearchError
File "C:\Users\s.sorokin\PycharmProjects\hh.ru\xgoogle\search.py", line 124
raise SearchError, "Wrong parameter to first_indexed_in_previous: %s" % (str(interval))
^
SyntaxError: invalid syntax
As mentioned on the library's website, xgoogle was only built as a "quick hack". From what you mention, it looks like it's written in Python 2. You therefore will not be able to run it in Python 3, unless you rewrite it.
If you want to use Python 3, you'll likely have to find a library that supports it.
Related
Working with python3, I had a requirement:
Perform some pre-work
Do the core work
Cleanup the pre-work
Taking inspiration from fixtures in pytest I came across this post and wrote some crazy code.
Though this crazy code works, I wish to understand the yield sorcery that makes it working :)
def db_connect_n_clean():
db_connectors = []
def _inner(db_obj):
db_connectors.append(db_obj)
print("Connect : ", db_obj)
yield _inner
for conn in db_connectors:
print("Dispose : ", conn)
This is the driver code:
pre_worker = db_connect_n_clean()
freaky_function = next(pre_worker)
freaky_function("1")
freaky_function("2")
try:
next(pre_worker)
except:
pass
It produces this output:
Connect : 1
Connect : 2
Dispose : 1
Dispose : 2
Traceback (most recent call last):
File "junk.py", line 81, in <module>
next(pre_worker)
StopIteration
What confuses me in this code is, that all the calls to the same generator freaky_func is maintaining a single list of db_connectors
After the first yield, all the objects are disposed and I hit StopIteration
I was thinking that calling freaky_func twice would maintain 2 separate lists and there would be 2 separate yields
Update: The goal of this question is not to understand how to achieve this. As it is evident from the comments, context-manager is the way to go. But my question is to understand how this piece of code is working. Basically, the python side of it.
One of my favorite tools to visualize Python with is PythonTutor.
Basically, you can see that on the first run next(pre_worker) returns the _inner function. Since _inner is inside db_connect_n_clean, it can access all of its variables.
Internally, in Python, _inner contains a reference to db_connectors. You can see the reference under __closure__:
>>> gen = db_connect_n_clean()
>>> inner = next(gen)
>>> inner.__closure__
(<cell at 0x000001B73FE6A3E0: list object at 0x000001B73FE87240>,)
>>> inner.__closure__[0].cell_contents
[]
The name of the reference is the same as the variable:
>>> inner.__code__.co_freevars
('db_connectors',)
Every time this specific function, with this specific __closure__ tries to access the db_connectors, it goes to the same list.
>>> inner(1)
Connect : 1
>>> inner(2)
Connect : 2
>>> inner.__closure__[0].cell_contents
[1, 2]
The original generator gen() is still paused at the first yield:
>>> gen.gi_frame.f_lineno
6 # Gen is stopped at line #6
>>> gen.gi_frame.f_locals["db_connectors"]
[1, 2]
When you advance it again using next() it continues on from the yield and closes everything:
>>> next(gen)
Dispose : 1
Dispose : 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
If you wish to understand how do generators work in general, there are plenty of answers and articles on the subject. I wrote this one for example.
If I didn't fully explain the situation, feel free to ask for clarification in the comments!
I have simple code. Firstly I get an input from user. In target page, there is an editable div. I focus on it and insert the message that I got from user. When I focusing and inserting element I use Javascript so for using Javascript with Python Selenium I use execute_script command.
Code:
message = input()
message = message.replace("'","\\'")
message = message.replace('"','\\"')
browser.execute_script("setTimeout(function() {document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').focus();}, 0); document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').textContent = '{}'".format(message))
Error: Traceback (most recent call last): File "main.py", line 171, in <module> browser.execute_script("setTimeout(function() {document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').focus();}, 0); document.querySelector('._3ee1T ._3uMse ._2FVVk ._3FRCZ').textContent = '{}'".format(message)) KeyError: 'document'
I try to html escape a string
from html import escape
project_name_unescaped = Project.get('name').encode('utf-8')
project_name = escape(project_name_unescaped)
I get the following error
Traceback (most recent call last):
File "hosts.py", line 70, in <module>
project_name = escape(bytes(project_name_unescaped))
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/html/__init__.py", line 19, in escape
s = s.replace("&", "&") # Must be done first!
TypeError: a bytes-like object is required, not 'str'
I also tried it with the following, but still have the same error
project_name = escape(bytes(project_name_unescaped))
The strange thing is, I'm pretty sure this had worked before. Any ideas whats wrong or what I need to change to make it work again? Also I tried it before with
from cgi import escape
The error message is a little confusing. html.escape expects s to be str, not bytes as you would expect from the message.
Remove all encoding-related stuff and it will work
from html import escape
project_name_unescaped = Project.get('name')
project_name = escape(project_name_unescaped)
Why is python shell throwing a NameError where as windows console a ValueError?
def PrintArgs(*arg):
list = ['1','2']
for i in arg:
try:
print(list[int(i)])
except ValueError:
print('Please enter integer value')
except NameError:
print('Name Error')
if __name__ == '__main__':
PrintArgs(*sys.argv[1:])
Providing the following arguments to Windows Console gives this output:
Here is how I call the code in windows console:
C:\>C:\Python34\python C:\Users\User\Documents\PYTest\Test.py 0 a
1
Please enter integer value
Providing the following arguments to Python Shell does not display the cusom error for NameError as mentioned in the code above, but mentions the following error:
PrintArgs(0,a)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
PrintArgs(0,a)
NameError: name 'a' is not defined
In the code example you've provided you define a list i, then you iterate over a collection called list you never initiated, and assign the values in this list to i, thus dropping the original value. I guess you only provided a part of your code, please provide a minimum working example.
If I try to reproduce your problem, I only get a type error, for iterating over a list which is not initialized.
I am getting
SearchBackendError at /forum/search/
No fields were found in any search_indexes. Please correct this before attempting to search.
with search_indexes placed in djangobb app root directory:
from haystack.indexes import *
from haystack import site
import djangobb_forum.models as models
class PostIndex(RealTimeSearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='user')
created = DateTimeField(model_attr='created')
topic = CharField(model_attr='topic')
category = CharField(model_attr='topic__forum__category__name')
forum = IntegerField(model_attr='topic__forum__pk')
site.register(models.Post, PostIndex)
settings.py
# Haystack settings
HAYSTACK_SITECONF = 'search_sites'
HAYSTACK_SEARCH_ENGINE = 'whoosh'
HAYSTACK_WHOOSH_PATH = os.path.join(PROJECT_ROOT, 'djangobb_index')
also i havae haystack and whoosh in my installed apps.
In python interpreter:
>>> import haystack
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/.../lib/python2.7/django_haystack-1.2.5-py2.5.egg/haystack/__init__.py", line 26, in <module>
raise ImproperlyConfigured("You must define the HAYSTACK_SITECONF setting before using the search framework.")
django.core.exceptions.ImproperlyConfigured: You must define the HAYSTACK_SITECONF setting before using the search framework.
Has someone has any ideas? Thanks in advance for any help you might have to offer.
Notice that the value shown in the documentation for HAYSTACK_SITECONF is an example only. The real name should be the module where the SearchIndex-derived classes are defined. So, as in your case the module is search_indexes, then you should have HAYSTACK_SITECONF='search_indexes'
Also, about that error that appears at the interpreter, did you get it using python ./manage.py shell? If not, settings.py wasn't loaded at the interpreter.