Django-haystack with whoosh - django-haystack

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.

Related

Why can't get price pair (USDT/KGS, USDT/KZT) by ticker in BINANCE API?

`So, this is my code
# Import libraries
import json
import requests
# defining key/request url
key = "https://api.binance.com/api/v3/ticker/price?symbol=USDTKGS"
# requesting data from url
data = requests.get(key)
data = data.json()
print(f"{data['symbol']} price is {data['price']}")
But for some reason I get this error:
Traceback (most recent call last):
File "rate.py", line 11, in <module>
print(f"{data['symbol']} price is {data['price']}")
KeyError: 'symbol'
Probably, this pair doesn't exist, but what to do in such situation?
I need to get the pair by API, but don't see any other ways to do so...
Please, help me!
I tried to use usual pairs like USDT/UAH, EUR/USDT - they work
But USDT/KGS, USDT/KZT doesn't work - they print error, but I need to get it
There is no such pair in Binance API currently (12/10/2022)

Python3 Trace back issue

First, let me disclaim that I am extremely new to the coding world and work requires me to use Python going forward. My experience is limited to having just completed SANS 573.
I'm trying to obtain the Date and Time from image files in their various formats. Excuse the example, I'm just using this to try and get it working.
This what I currently have:
from pathlib import Path
from PIL import Image
L88 = Path("L88.jpg")
def corvette(L88):
imgobj=Image.open(L88)
info=imgobj._getexif()
return info[36867]
>>> corvette(L88)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in corvette
KeyError: 36867
>>>
I am running the function from the desktop which is where I have the image currently saved.
I don't understand the error messages.
KeyError: 36867 also has me confused too as when I looked up the tag, that is what I found and what worked when I just did my course.
The _getexif() method returns a Python datastructury called a dictionary.
The values in a dictionary are accessed by their key.
In this case, the keys are numbers.
Not all keys are mandatory. You can use the keys() method to see which ones exist. Try:
print(info.keys())
to see which keys exist.
You can also test if a key exists:
if 36867 in info:
return info[36867]
else:
return None
Note that there is a mapping between these numbers and their exif tags names available.
You can use that to create a readable dictionary.
Note that not all JPEG images have EXIF information. In that case, the _getexif() method returns None, so you should take that into account and test for that:
from PIL import Image, ExifTags
def image_info(path):
imgobj = Image.open(path)
info = imgobj._getexif()
if info is None:
return None
rv = {
ExifTags.TAGS[key]: info[key]
for key in info.keys()
}
return rv

Have error when try to use xgoogle library in code

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.

pymodm can't find an object, while pymongo successfully finds it

I have a problem getting an object from the mongodb instance. If I search for this object with pymongo interface, everything is fine - object can be found. If try to do the very same thing with pymodm - it fails with error.
Here is what I'm doing:
from pymodm import connect, MongoModel, fields
from pymongo import MongoClient
class detection_object(MongoModel):
legacy_id = fields.IntegerField()
client = MongoClient(MONGODB_URI)
db = client[MONGODB_DEFAULT_SCHEME]
collection = db['detection_object']
do = collection.find_one({'legacy_id': 1437424})
print(do)
connect(MONGODB_URI)
do = detection_object.objects.raw({'legacy_id': 1437424}).first()
print(do)
The first print outputs this: {'_id': ObjectId('5c4099dcffa4fb11494d983d'), 'legacy_id': 1437424}. However, during the execution of this command: do = detection_object.objects.raw({'legacy_id': 1437424}).first() interpreter fails with the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pymodm/queryset.py", line 127, in first
return next(iter(self.limit(-1)))
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/konsof01/PycharmProjects/testthisfuckingshit/settings.py", line 29, in <module>
do = detection_object.objects.raw({'legacy_id': 1437424}).first()
File "/usr/local/lib/python3.7/site-packages/pymodm/queryset.py", line 129, in first
raise self._model.DoesNotExist()
__main__.DoesNotExist
How can this be? I'm trying to query the very same object, with the same connection and collection. Any ideas, please?
you could try it as follows:
detection_object.objects.raw({'legacy_id': "1437424"} ).first()
probably the legacy_id is stored as string.
Othewise, make sure the db name is present at the end of the MONGO_URI as it is underlined in the docs.
Each document in your 'detection_object' collection requires to have '_cls' attribute. The string value stored in this attribute should be
__main__.classname
(class name according to your code is detection_object).
For example a document in your database needs to look like this:
{'_id': ObjectId('5c4099dcffa4fb11494d983d'), 'legacy_id': 1437424, '_cls': '__ main __.detection_object'}

Error with HTMLParser and feed in Python

Hello I am using Python3 to create an application which from a given URL it returns the text of the website without the HTML tags, just clean and plain text.
Here is my code is supposed to work but is not:
import urllib, formatter, sys
from urllib.request import urlopen
from html.parser import HTMLParser
website = urlopen("http://www.google.com")
data = website.read()
website.close()
format = formatter.AbstractFormatter(formatter.DumbWriter(sys.stdout))
ptext = HTMLParser(format)
ptext.feed(data)
ptext.close()
The error:
File "app.py", line 11, in <module>
ptext.feed(data)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/html/parser.py", line 144, in feed
self.rawdata = self.rawdata + data
TypeError: Can't convert 'bytes' object to str implicitly
I do find a solution which overcome the error but I don't get the appropriate result. The solution was to change the following line:
ptext.feed(data)
to:
ptext.feed(data.decode("utf-8"))
The problem now is that there is no outcome to the terminal the program is run but there is no outcome, the code is tested on a tutorial I saw and it works.
Thanks.

Resources