Tweepy pagination KeyError: 0 - pagination

I tried using tweepy's pagination based on the code provided in it's documentation:
```
import tweepy
auth = tweepy.AppAuthHandler("Consumer Key here", "Consumer Secret here")
api = tweepy.API(auth)
for status in tweepy.Cursor(api.search_tweets, "Tweepy",
count=100).items(250):
print(status.id)
```
However, I get the following error:
```
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16136/3940301818.py in <module>
----> 1 for status in tweepy.Cursor(api.search_tweets, "Tweepy",
2 count=100).items(250):
3 print(status.id)
C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py in __next__(self)
84
85 def __next__(self):
---> 86 return self.next()
87
88 def next(self):
C:\ProgramData\Anaconda3\lib\site-packages\tweepy\cursor.py in next(self)
290 self.page_index += 1
291 self.num_tweets += 1
--> 292 return self.current_page[self.page_index]
293
294 def prev(self):
KeyError: 0
```
Can someone explain and rectify the error please?

With the current version of Tweepy 4.8.0, the AuthHandler syntax has changed.
Update Tweepy:
pip install Tweepy -U
and the following should work:
import tweepy
auth = tweepy.OAuth2AppHandler("Consumer Key here", "Consumer Secret here")
api = tweepy.API(auth)
for status in tweepy.Cursor(api.search_tweets, "Tweepy",
count=100).items(250):
print(status.id)

Related

"TypeError: float() argument must be a string or a number, not 'FreeRV' " in pymc3

I am stuck in the following error in pymc3 and at a loss.
TypeError: float() argument must be a string or a number, not 'FreeRV'
The below is my code. I pick up this code from here. (Sorry, this is Japanese). However it does not work in my environment:
Google Colab, Python: 3.7.13, pymc3: 3.11.4
import numpy as np
import matplotlib
import matplotlib.pylab as plt
%matplotlib inline
from tqdm import tqdm
import pymc3 as pm
# generate time-series data
np.random.seed(0)
y = np.cumsum(np.random.normal(size=100))
# Infer parameters in time-series data
N = len(y)
T = 1000
with pm.Model() as model:
muZero = pm.Normal(name='muZero', mu=0.0, tau=1.0)
sigmaW = pm.InverseGamma(name='sigmaW', alpha=1.0, beta=1.0)
mu = [0]*N
mu[0] = pm.Normal(name='mu0', mu=muZero, tau=1/sigmaW)
for n in range(1, N):
mu[n] = pm.Normal(name='mu'+str(n), mu=mu[n-1], tau=1/sigmaW)
sigmaV = pm.InverseGamma(name='sigmaV', alpha=1.0, beta=1.0)
y_pre = pm.Normal('y_pre', mu=mu, tau=1/sigmaV, observed=y) # I got error here
start = pm.find_MAP()
step = pm.NUTS()
trace = pm.sample(T, step, start=start)
Here is the full discription of my error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pymc3/theanof.py in floatX(X)
82 try:
---> 83 return X.astype(theano.config.floatX)
84 except AttributeError:
AttributeError: 'list' object has no attribute 'astype'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'FreeRV'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
4 frames
<ipython-input-30-c3709f545993> in <module>()
26
27 sigmaV = pm.InverseGamma(name='sigmaV', alpha=1.0, beta=1.0)
---> 28 y_pre = pm.Normal('y_pre', mu=mu, tau=1/sigmaV, observed=y) # I got error here
29 start = pm.find_MAP()
30 step = pm.NUTS()
/usr/local/lib/python3.7/dist-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
119 dist = cls.dist(*args, **kwargs, shape=shape)
120 else:
--> 121 dist = cls.dist(*args, **kwargs)
122 return model.Var(name, dist, data, total_size, dims=dims)
123
/usr/local/lib/python3.7/dist-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)
128 def dist(cls, *args, **kwargs):
129 dist = object.__new__(cls)
--> 130 dist.__init__(*args, **kwargs)
131 return dist
132
/usr/local/lib/python3.7/dist-packages/pymc3/distributions/continuous.py in __init__(self, mu, sigma, tau, sd, **kwargs)
485 self.tau = tt.as_tensor_variable(tau)
486
--> 487 self.mean = self.median = self.mode = self.mu = mu = tt.as_tensor_variable(floatX(mu))
488 self.variance = 1.0 / self.tau
489
/usr/local/lib/python3.7/dist-packages/pymc3/theanof.py in floatX(X)
84 except AttributeError:
85 # Scalar passed
---> 86 return np.asarray(X, dtype=theano.config.floatX)
87
88
ValueError: setting an array element with a sequence.

Can't call with tpqoa

can someone help me with this error?
Is there an error in my pyalgo file?
What type of other address I have to put into tpqoa call?
Thanks
CODE
pyalgo.cfg
[oanda]
account_id = "101-012-21240417-001"
access_token = 'hidden'
account_type = practice
CODE
import oandapyV20
import pandas as pd
import tpqoa
api = tpqoa.tpqoa('../pyalgo.cfg')
api.get.instruments()[:15]
Output
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5064/836215391.py in <module>
2 import pandas as pd
3 import tpqoa
----> 4 api = tpqoa.tpqoa('../pyalgo.cfg')
5 from oandapyV20 import API
6 import oandapyV20.endpoints.pricing as pricing
~\anaconda3\envs\PythonPC\lib\site-packages\tpqoa\tpqoa.py in __init__(self, conf_file)
111 self.config = configparser.ConfigParser()
112 self.config.read(conf_file)
--> 113 self.access_token = self.config['oanda']['access_token']
114 self.account_id = self.config['oanda']['account_id']
115 self.account_type = self.config['oanda']['account_type']
~\anaconda3\envs\PythonPC\lib\configparser.py in __getitem__(self, key)
958 def __getitem__(self, key):
959 if key != self.default_section and not self.has_section(key):
--> 960 raise KeyError(key)
961 return self._proxies[key]
962
KeyError: 'oanda'
you have indented the account_id, access_token and account_type.
unindent them then run it.

Cannot list blobs in Azure container

I would like to list the blobs (the files) in an Azure container. To do so, I reproduced exactly the code snippet given as an example in the official documentation (see here). Here is what my code looks like:
from azure.storage.blob import BlobServiceClient, ContainerClient
from azure.identity import ClientSecretCredential
token_credential = ClientSecretCredential(tenant_id='WWW',
client_id='XXX',
client_secret='YYY')
service = BlobServiceClient("ZZZ", credential=token_credential)
container_client = service.get_container_client(container='AAA')
print(container_client.container_name)
blob_list = container_client.list_blobs()
for blob in blob_list:
print(blob.name + '\n')
All the lines in this example run fine except the last one, which throws the following error:
---------------------------------------------------------------------------
StorageErrorException Traceback (most recent call last)
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/storage/blob/_list_blobs_helper.py in _get_next_cb(self, continuation_token)
75 cls=return_context_and_deserialized,
---> 76 use_location=self.location_mode)
77 except StorageErrorException as error:
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/storage/blob/_generated/operations/_container_operations.py in list_blob_flat_segment(self, prefix, marker, maxresults, include, timeout, request_id, cls, **kwargs)
1215 map_error(status_code=response.status_code, response=response, error_map=error_map)
-> 1216 raise models.StorageErrorException(response, self._deserialize)
1217
StorageErrorException: (InvalidQueryParameterValue) Value for one of the query parameters specified in the request URI is invalid.
RequestId:39f9e5c3-201f-0114-551d-efab6d000000
Time:2021-01-20T11:13:03.6566856Z
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-6-a064d97987b5> in <module>
----> 1 for blob in blob_list:
2 print(blob.name + '\n')
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/core/paging.py in __next__(self)
127 if self._page_iterator is None:
128 self._page_iterator = itertools.chain.from_iterable(self.by_page())
--> 129 return next(self._page_iterator)
130
131 next = __next__ # Python 2 compatibility.
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/core/paging.py in __next__(self)
74 raise StopIteration("End of paging")
75 try:
---> 76 self._response = self._get_next(self.continuation_token)
77 except AzureError as error:
78 if not error.continuation_token:
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/storage/blob/_list_blobs_helper.py in _get_next_cb(self, continuation_token)
76 use_location=self.location_mode)
77 except StorageErrorException as error:
---> 78 process_storage_error(error)
79
80 def _extract_data_cb(self, get_next_return):
/anaconda/envs/azureml_py36_automl/lib/python3.6/site-packages/azure/storage/blob/_shared/response_handlers.py in process_storage_error(storage_error)
92 error_body = ContentDecodePolicy.deserialize_from_http_generics(storage_error.response)
93 if error_body:
---> 94 for info in error_body.iter():
95 if info.tag.lower() == 'code':
96 error_code = info.text
AttributeError: 'dict' object has no attribute 'iter'
What am I doing wrong?
For info, I'm using Python 3.6.9 with azure-storage-blob version==12.5.0 and azure-identity==1.4.1.
The package azure.storage.blob is used to to access Azure blob. Account URL that we use in script should be like https://{StorageAccountName}.blob.core.windows.net. The URL https://{StorageAccountName}.dfs.core.windows.net is the URL of Azure Data Lake Storage Gen2. If you want to list files stored in Azure Data Lake Storage Gen2, you need to use the package azure-storage-file-datalake. Besides regarding how to use the package, please refer to the sample

how to remove nameerror Traceback (most recent call last)

I am making a series using three dictionaries, in dictionary, there is no keyword or values "name"/ "null".but it is showing NameError: name 'null' is not defined
rerun the code in different jupyter notebook
import pandas as pd
p1=pd.Series({'team':'england','keyplayer':'joe root','bowler':'jofra'})
p2=pd.Series({'team':'india','keyplayer':'virat kohli','bowler':'bumhra'})
p3=pd.Series({'team':'australia','keyplayer':'steve smith','bowler':'starc'})
df=pd.DataFrame([p1,p2,p3],index=['1','2','3'])
df.head()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
400 if cls is not object \
401 and callable(cls.__dict__.get('__repr__')):
--> 402 return _repr_pprint(obj, self, cycle)
403
404 return _default_pprint(obj, self, cycle)
~\Anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
695 """A pprint that just redirects to the normal repr function."""
696 # Find newlines and replace them with p.break_()
--> 697 output = repr(obj)
698 for idx,output_line in enumerate(output.splitlines()):
699 if idx:
~\Anaconda3\lib\site-packages\pandas\core\base.py in __repr__(self)
80 Yields Bytestring in Py2, Unicode String in py3.
81 """
---> 82 return str(self)
83
84
~\Anaconda3\lib\site-packages\pandas\core\base.py in __str__(self)
59
60 if compat.PY3:
---> 61 return self.__unicode__()
62 return self.__bytes__()
63
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __unicode__(self)
661 width = None
662 self.to_string(buf=buf, max_rows=max_rows, max_cols=max_cols,
--> 663 line_width=width, show_dimensions=show_dimensions)
664
665 return buf.getvalue()
NameError: name 'null' is not defined
----------------after this table is showing
I tried to run your code in an isolated environment and I managed!
IPython:
In [1]: import pandas as pd
In [2]: p1=pd.Series({'team':'england','keyplayer':'joe root','bowler':'jofra'})
: p2=pd.Series({'team':'india','keyplayer':'virat kohli','bowler':'bumhra'})
: p3=pd.Series({'team':'australia','keyplayer':'steve smith','bowler':'starc'})
: df=pd.DataFrame([p1,p2,p3],index=['1','2','3'])
In [3]: df.head()
Out[3]:
team keyplayer bowler
1 england joe root jofra
2 india virat kohli bumhra
3 australia steve smith starc
I installed the minimum required to run: numpy==1.16.4, pandas==0.24.2 e jupyter==1.0.0
Maybe there is some problem with your libs.
I recommend you try to run your code using virtualenv and install the required libraries.
To learn more, nothing better than the Python documentation itself: https://docs.python.org/3/tutorial/venv.html

InvalidConfigError: Invalid client secrets file while saving PyDrive credentials

I use a Colaboratory Notebook and tried to automate the GoogleAuth process while using PyDrive librairy.
I tried the way dano proposed here: https://stackoverflow.com/a/24542604/10131744
Nevertheless, I get an error, linked to client secret.
Here is my code:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
#gauth.credentials = GoogleCredentials.get_application_default()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
And here is the message I get:
/usr/local/lib/python3.6/dist-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access mycreds.txt: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
120 try:
--> 121 with open(filename, 'r') as fp:
122 obj = json.load(fp)
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'
During handling of the above exception, another exception occurred:
InvalidClientSecretsError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
385 try:
--> 386 client_type, client_info = clientsecrets.loadfile(client_config_file)
387 except clientsecrets.InvalidClientSecretsError as error:
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in loadfile(filename, cache)
164 if not cache:
--> 165 return _loadfile(filename)
166
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
124 raise InvalidClientSecretsError('Error opening file', exc.filename,
--> 125 exc.strerror, exc.errno)
126 return _validate_clientsecrets(obj)
InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
During handling of the above exception, another exception occurred:
InvalidConfigError Traceback (most recent call last)
<ipython-input-9-370983bd3c5e> in <module>()
13 if gauth.credentials is None:
14 # Authenticate if they're not there
---> 15 gauth.LocalWebserverAuth()
16 elif gauth.access_token_expired:
17 # Refresh them if expired
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in _decorated(self, *args, **kwargs)
111 self.LoadCredentials()
112 if self.flow is None:
--> 113 self.GetFlow()
114 if self.credentials is None:
115 code = decoratee(self, *args, **kwargs)
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in GetFlow(self)
441 if not all(config in self.client_config \
442 for config in self.CLIENT_CONFIGS_LIST):
--> 443 self.LoadClientConfig()
444 constructor_kwargs = {
445 'redirect_uri': self.client_config['redirect_uri'],
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfig(self, backend)
364 raise InvalidConfigError('Please specify client config backend')
365 if backend == 'file':
--> 366 self.LoadClientConfigFile()
367 elif backend == 'settings':
368 self.LoadClientConfigSettings()
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
386 client_type, client_info = clientsecrets.loadfile(client_config_file)
387 except clientsecrets.InvalidClientSecretsError as error:
--> 388 raise InvalidConfigError('Invalid client secrets file %s' % error)
389 if not client_type in (clientsecrets.TYPE_WEB,
390 clientsecrets.TYPE_INSTALLED):
InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
I've tried to add a client_secrets.json file according to this answer: https://stackoverflow.com/a/33426759/10131744
But either I did something wrong, or the .json file is not in the right place, but it doesn't work.
Thanks a lot for your help.

Resources