Automatic import of classifications

Hi all,

once the file it’s been updated with the classification, it’s possibile the use It as a default file and continue to modify It? thx

That’s correct Sir.

Cheers

1 Like

Thanks. I’m a noob. In this part of your instruction “ py portfolio-classifier.py <input_file>” . where I have to copy my input Xxx.xml file ? I put it in c:\ …. It seems not working. In the case it is correct to put it in c:\ … is it correct to write “ py portfolio-classifier.py c:\test.xml . Thanks

try a backslash:
py portfolio-classifier.py c:/test.xml

Thanks. It is not working. I receive the same error message.

$ py portfolio-classifier.py C:/Test.xml
C:\Users\samue\OneDrive\GIT\portfolio-classifier.py:331: SyntaxWarning: invalid escape sequence ‘{’
secid = re.search(‘{“i”:“([^”]+)"’, response).group(1)
Traceback (most recent call last):
File “C:\Users\samue\OneDrive\GIT\portfolio-classifier.py”, line 11, in
import requests
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests_init_.py”, line 43, in
import urllib3
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3_init_.py”, line 7, in
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connectionpool.py”, line 11, in
from .exceptions import (
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\exceptions.py”, line 2, in
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
ModuleNotFoundError: No module named ‘urllib3.packages.six.moves’

Can you help me to understand how to solve ?

Thanks

how can I adapt the search pattern ? I see the file portfolio-classifier but i don’t know how to open and adapt it.

It seems that the Morningstar pages look a bit a different now. So you need to adpat the search pattern in line 394 to something like:
token_regex = r"const maasToken \=\s\"(.+)\""

How you’re editing files? Basically right mouse click and edit for instance via wordpad

portfolio-classifier.py is a Python script. The script itself is in text form. So you need to open the file portfolio-classifier.py with any text editor (as explained by Rafa) and substitute line 394 with token_regex = r"const maasToken \=\s\"(.+)\"".

1 Like

thanks for your support but the problem is not solved. Probably i’m doing other mistakes. This is the message that I received . Can you help me to understand where is the error ?

$ py portfolio-classifier.py C:/Test.xml
C:\Users\samue\OneDrive\GIT\portfolio-classifier.py:331: SyntaxWarning: invalid escape sequence ‘{’
secid = re.search(‘{“i”:“([^”]+)"’, response).group(1)
Traceback (most recent call last):
File “C:\Users\samue\OneDrive\GIT\portfolio-classifier.py”, line 11, in
import requests
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests_init_.py”, line 43, in
import urllib3
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3_init_.py”, line 7, in
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\connectionpool.py”, line 11, in
from .exceptions import (
File “C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\urllib3\exceptions.py”, line 2, in
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
ModuleNotFoundError: No module named ‘urllib3.packages.six.moves’

Hi,

I don’t know what text editor you have been using, but it seems that it modified the source code.

Line 291 in the original file looks as follows:
secid = re.search('\{"i":"([^"]+)"', response).group(1)

But your error message shows that it looks different:
secid = re.search(‘{“i”:“([^”]+)"’, response).group(1), i.e. it lost the backslash and maybe also modified the single quotation marks. (And this might not be the only damage that was done.)

Download the original again and modify it with a different text editor which does not spoil the content.

This is the same error i had. It’s the third time we have seen this error and it’s most likely a version of the urllib3 that is not updated when installing the “requirements”. Just an idea, it could be something else.

Back to the solution, you need to uninstall and then reinstall urllib3.
First uninstall

python -m pip uninstall urllib3

Then install again and run the code again. The error should be gone.

python -m pip install urllib3

Hi, thanks this is the result of your suggestion.
But it seems that the problem was not solved … or I made a mistake.
Can you help me ?

PS C:\PROVA> python -m pip uninstall urllib3
Found existing installation: urllib3 1.25.11
Uninstalling urllib3-1.25.11:
Would remove:
c:\users\samue\appdata\local\programs\python\python312\lib\site-packages\urllib3-1.25.11.dist-info*
c:\users\samue\appdata\local\programs\python\python312\lib\site-packages\urllib3*
Proceed (Y/n)? Y
Successfully uninstalled urllib3-1.25.11
PS C:\PROVA> python -m pip install urllib3
Collecting urllib3
Using cached urllib3-2.2.1-py3-none-any.whl.metadata (6.4 kB)
Using cached urllib3-2.2.1-py3-none-any.whl (121 kB)
Installing collected packages: urllib3
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
requests 2.24.0 requires urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1, but you have urllib3 2.2.1 which is incompatible.
Successfully installed urllib3-2.2.1

Then …

PS C:\PROVA> py portfolio-classifier.py C:\PROVA\Test2.xml
C:\PROVA\portfolio-classifier.py:331: SyntaxWarning: invalid escape sequence ‘{’
secid = re.search(‘{“i”:“([^”]+)"’, response).group(1)
C:\Users\samue\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests_init_.py:89: RequestsDependencyWarning: urllib3 (2.2.1) or chardet (3.0.4) doesn’t match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn’t match a supported "
Traceback (most recent call last):
File “C:\PROVA\portfolio-classifier.py”, line 731, in
pp_file.add_taxonomy(taxonomy)
File “C:\PROVA\portfolio-classifier.py”, line 589, in add_taxonomy
securities = self.get_securities()
^^^^^^^^^^^^^^^^^^^^^
File “C:\PROVA\portfolio-classifier.py”, line 691, in get_securities
security_h = security.load_holdings()
^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\PROVA\portfolio-classifier.py”, line 351, in load_holdings
self.holdings.load(isin = self.ISIN, secid = self.secid)
File “C:\PROVA\portfolio-classifier.py”, line 418, in load
bearer_token, secid = self.get_bearer_token(secid, domain)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\PROVA\portfolio-classifier.py”, line 395, in get_bearer_token
resultstringtoken = re.findall(token_regex, response.text)[0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

I have no idea to be honest. The suggestion from Alfons1Qvor12 above looks good but I think you already tried to download the script again and retry again.
Are you able to run the script without errors using the test portfolio provided in the repository?
Assuming you didn’t delete any files inside the folder, you can try that with the command:

py portfolio-classifier.py test/multifaktortest.xml

There’s an open pull request for that issue. In portfolio-classifier.py line 394 must be replaced:
old code: token_regex = r"tokenMaaS:\s"(.+)""
new code: token_regex = r"const maasToken =\s"(.+)""

Hi all,

I tried to update my classifications for my protfolio. I thought it is possible to simply run the program again and the classifications will be updated. But now I have all classifications twice. Is that an expected behaviour or did I something wrong?

Thanks in advance for your help.

@Bastelritius Till now, it’s not updating. See Fizban’s explanation in one of his posts:

I tried to dive into the code, but for me it’s too difficult when it comes to save the data in the save files. I hope PP will get a REST API for taxonomies/classifications with methods to add, edit, delete, list all, list one. Here was a start of an REST API, but it was not continued. [new] REST API for Portfolio Performance · Issue #2085 · portfolio-performance/portfolio · GitHub
With such a Rest API, it would be much better and much more easy (easier to code, and also much, much easier to test it) to write a custom script which adds and also updates taxonomies/classifications. @AndreasB, would be great if this REST API would be continued in the future. Would be perfect to have community add more external custom scripts without breaking any save data or PP logic. Thanks a lot for everything!

1 Like

Thanks for the very quick answer. Now I have to delete all clasifications at first :frowning:
Is there a quick way to delete all classifications for a ETF by one klick?