why Object is not callable at the second time of execution? - python

I am trying to use run the following code. The code generates output for the first time, however during the 2nd execution returns an error -- TypeError: 'YahooSecurity' object is not callable
I am not sure why this is happening. I am using the following python package https://pypi.org/project/pypf/
My code is -
import os
import time
from pypf.chart import PFChart
from pypf.instrument import YahooSecurity
symbol = "TATAPOWER","TATAMOTORS"
for i in symbol:
print(i)
YahooSecurity = YahooSecurity(i, True, True, '1d',1)
print(type(YahooSecurity))
chart = PFChart(YahooSecurity,0.01,1,'C',3,True,True,True)
chart.create_chart(dump=True)
time.sleep(20)
"TATAPOWER","TATAMOTORS" are csv file with data
Error -
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [38], line 9
7 for i in symbol:
8 print(i)
----> 9 YahooSecurity = YahooSecurity(i, True, True, '1d',1)
10 print(type(YahooSecurity))
11 chart = PFChart(YahooSecurity,0.01,1,'C',3,True,True,True)
TypeError: 'YahooSecurity' object is not callable
I am trying to get multiple charts in one go.

because YahooSecurity = YahooSecurity(i, True, True, '1d',1) replaces the object with the result of calling the object (or, more likely, the class). So, the second time you use it, it's not a class anymore. Just make it somethin like yahoo_security = YahooSecurity(i, True, True, '1d',1)

Related

How to get a list of all tokens from Lucene 8.6.1 index using PyLucene?

I have got some direction from this question. I first make the index like below.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator
index_path = "./index"
lucene.initVM()
analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)
doc = Document()
doc.add(Field("docid", "1", TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)
writer.close()
store.close()
I then try to get all the terms in the index for one field like below.
store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)
Attempt 1: trying to use the next() as used in this question which seems to be a method of BytesRefIterator implemented by TermsEnum.
for lrc in reader.leaves():
terms = lrc.reader().terms('title')
terms_enum = terms.iterator()
while terms_enum.next():
term = terms_enum.term()
print(term.utf8ToString())
However, I can't seem to be able to access that next() method.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while terms_enum.next():
5 term = terms_enum.term()
6 print(term.utf8ToString())
AttributeError: 'TermsEnum' object has no attribute 'next'
Attempt 2: trying to change the while loop as suggested in the comments of this question.
while next(terms_enum):
term = terms_enum.term()
print(term.utf8ToString())
However, it seems TermsEnum is not understood to be an iterator by Python.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while next(terms_enum):
5 term = terms_enum.term()
6 print(term.utf8ToString())
TypeError: 'TermsEnum' object is not an iterator
I am aware that my question can be answered as suggested in this question. Then I guess my question really is, how do I get all the terms in TermsEnum?
I found that the below works from here and from test_FieldEnumeration() in the test_Pylucene.py file which is in pylucene-8.6.1/test3/.
for term in BytesRefIterator.cast_(terms_enum):
print(term.utf8ToString())
Happy to accept an answer that has more explanation than this.

name 'FillMissing' is not defined

When I run following code:
df = pd.read_csv('../input/marketingrar/marketing.csv')
df.head()
dep_var = 'Revenue'
cat_names = ['Day_Name','Promo']
cont_names = ['Date','Week','Month','Month_ID','Year','Visitors','Marketing Spend']
procs = [FillMissing, Categorify, Normalize]
I got this error bellow:
NameError Traceback (most recent call
last) in
----> 1 procs = [FillMissing, Categorify, Normalize]
NameError: name 'FillMissing' is not defined
P.S. I'm using Kaggle notebook. Why this error occurs and how to solve it?
from fastai.tabular.all import *
is the only working solution for me
With this code, you are trying to initiate a list named procs with the 3 references to FillMissing, Categorify and Normalise, but you never created those references before.
Did you maybe want to create a list of 3 strings? Then you forgot the '', compare the other lists like cat_names or cont_names
Maybe it could also help to include
from fastai import *
from fastai.tabular import *

Python 'int' object is not iterable when using len on a list from beautifulsoup

So far I have the following code, I'm including all of it in case it helps
import requests
from bs4 import BeautifulSoup
URL = 'https://projects.fivethirtyeight.com/2020-nba-predictions/games/'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
todays_games = soup.find('div', class_="games-section extra-space-0")
stats = []
for games in len(list(todays_games.children)):
game = list(todays_games.children)[games]
and the error I am getting is
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-39f21f5da0b0> in <module>
1 #This is the main new feature of this version. A for loop to perform the operation for all the games of that day
2 stats = []
----> 3 for games in len(list(todays_games.children)):
4 game = list(todays_games.children)[games]
5 game_body = game.body
TypeError: 'int' object is not iterable
If I do type() on list(todays_games.children) and Len(list(todays_games.children)) I get "list" and the length, which happens to be 6 for this particular case on this particular day, So I don't understand why I am getting this error. Any Ideas?
len(list(todays_games.children)) evaluates to an integer - 5, 6, etc. You can't iterate directly over an integer with a for loop.
You can use the built-in function range to loop a set number of times if you'd need, but you should iterator directly over todays_games.children.
A big part of Python is that it simplifies iterator usage. Instead of using indices to access things like an array, (asking for the 5th element, etc) you can directly access the element with the for loop.
for game in todays_games.children:
curr_game = game.body
do_something_else(curr_game)
compare this to
for i in range(len(todays_games.children)):
curr_game = todays_games.children[i].body
do_something_else(curr_game)
```.

Python Error :'numpy.float64' object is not callable

I have written a code in python to generate a sequence of ARIMA model's and determine their AIC values to compare them.The code is as below,
p=0
q=0
d=0
for p in range(5):
for d in range(1):
for q in range(4):
arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
print(arima_mod.params)
print arima_mod.aic()
I am getting a error message as below,
TypeError Traceback (most recent call last)
<ipython-input-60-b662b0c42796> in <module>()
8 arima_mod=sm.tsa.ARIMA(df,(p,d,q)).fit()
9 print(arima_mod.params)
---> 10 print arima_mod.aic()
global arima_mod.aic = 1262.2449736558815
11
**TypeError: 'numpy.float64' object is not callable**
Remove the brackets after print arima_mod.aic(). As I read it, arima_mod.aic is 1262.2449736558815, and thus a float. The brackets make python think it is a function, and tries to call it. You do not want that (because it breaks), you just want that value. So remove the brackets, and you'll be fine.

Why can't I call a Kivy.core.image function?

I did a program using python and pyGTK. Now, I'm translating it to Kivy, for use it with a tablet (Android). One archive.py is a custom widget with graphic information about a person (an image of her/his, a frame with color if him/his is selected, a little candle if is your birthday,..)
The question is, to make the custom widget I need to know the image size, but when I write:
wi = self._imagen.width()
I have this error:
TypeError: 'int' object is not callable
Before, in code, I check self._imagen is an kivy.core.image.Image object, that has a width function:
if self._imagen != None:
print self._imagen #terminal print <kivy.core.image.Image object at 0xb2f2db9c>
wi = self._imagen.width() #ERROR
hi = self._imagen.height()
I search for any instruction in my code like: self._imagen = 1 (or other int number) (there is not).
I don't know why. Thanks and sorry for my english
Use wi = self._imagen.width using wi = self._imagen.width() is trying to call the int object as a method, that is why you are getting the error.
In [1]: width = 3
In [2]: print width
3
In [3]: print width()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-33019bd92b32> in <module>()
----> 1 print width()
TypeError: 'int' object is not callable

Categories