Issues with mypy
I am trying to use mypy for static type checking in my python project. However, I am seeing the following errors when running mypy,
utils.py:15: error: Name 'google' is not defined
The annotation is something like this
# type: () -> google.protobuf.timestamp_pb2.Timestamp
See also questions close to this topic
What are the speed benchmarks in Watson-NLU?
I am trying to process tweets stored in a text file. My code reads the tweets(one by one), processes them and then save the results of Watson in a csv file. The speed is around 28 tweets per minute only. Is data file handling causing this delay?
while 1: where = file.tell() line = file.readline() if not line: print "no line found, waiting for a 1 seconds" time.sleep(1) file.seek(where) else: if (re.search('[a-zA-Z]', line)): print "-----------------------------" print "the line is: " print line print "-----------------------------" response = natural_language_understanding.analyze( text=line, features=Features( entities=EntitiesOptions( emotion=True, sentiment=True, limit=2), keywords=KeywordsOptions( emotion=True, sentiment=True, limit=2)), language='en' ) response["tweet"] = line print(json.dumps(response, indent=2)) with open('#DDvKXIP.csv', 'a') as csv_file: writer = csv.writer(csv_file) for key, value in response.items(): writer.writerow([key, value]) else: print "--------------------------------" print "found a line without any alphabet in it, hence not considering." print line print "--------------------------------"
Looping through DF column to remove rows that have spanish text
Here is the code I'm using to filter out any spanish text:
from langdetect import detect #detects what language is written from tqdm import tqdm #timing package # 'summary_processed' is a list of sentence strings that had general text preprocessing done (lemmetization, regex removal, lowercasing, etc) summary_processed_en = [i for i in tqdm(summary_processed) if detect(i) == 'en']
Now, this isn't a typical conditional statement so I can't do my normal
df[df == "X"]format.
I'm not entire sure how to approach this. Any help would be much appreciated.
TextBlob: Ranges to define sentiment groups
I'm working on a research project that uses TextBlob for sentiment analysis. On their website, the developers of TextBlob explain that the sentiment polarity ranges from -1.0 to +1.0, where -1.0 denotes complete negativity while +1.0 should represent complete positivity.
What are common ways, or how did the developers intend, that the numbers are converted to the classification: positive, neutral, negative?
In other words, can we assume that the following classification exists:
Sentiment: Score: Positive +1.0 > x > 0 Neutral x = 0 (precisely) Negative 0 < x < -1.0
Is there perhaps documentation about this? I haven't been able to find detailed documents yet.
Class cannot subclass 'QObject' (has type 'Any') using mypy
I have a class that subclasses QObject. Everyting works fine but when I run mypy on it I get the error:
"error: Class cannot subclass 'QObject' (has type 'Any')"
At the moment I am totally stuck. I Have been reading the mypy docs but couldn't find where the error was.
Here the code:
from PyQt5.QtCore import QObject class ServiceLocator(QObject): def __init__(self) -> None: super().__init__() ...
Python type checking on a function that returns str or Sequence
I have a Python function that processes a sequence, and returns the same sort of sequence. E.g. if it is fed a list of integers, it will return a list of integers, and if it is fed a string, it will return a string.
How do I add type hints for this function?
The following works fine, but is not a very strict type check:
from typing import Any, TypeVar, Sequence _S = Any def process_sequence(s: _S) -> _S: return s def print_str(word: str): print(word) def print_sequence_of_ints(ints: Sequence[int]): for i in ints: print(i) a = process_sequence("spam") print_str(a) a = process_sequence([1,2,3,42]) print_sequence_of_ints(a)
However, when I try to narrow down
_S = Sequence
_S = TypeVar('_S', Sequence, str)
mypy (a type checking code validator) yields the following error:
error: Argument 1 to "print_str" has incompatible type "Sequence[Any]"; expected "str"
How can I add a type hint to my function that says the input must be a sequence, and the output has the same type as the input, and make mypy happy at the same time?