Wikidata talk:Pywikibot - Python 3 Tutorial

Latest comment: 3 years ago by Premeditated in topic Does someone know if pywikibot support lexemes?

Unknown or Some Value as Target edit

Not sure where to ask questions on pywikibot...

I'm trying to add a claim with a specific property ("decays to") and set the target item to 'unknown' - this is done for example in uranium-235 (Q848497) under the decay mode spontaneous fission (Q146682). I can't figure out a way to do this with pywikibot. If I use the 'unknown value qid' I found somewhere else (Q4294967294) then the API complains that the item doesn't exist. If I try setting the target to None, pywikibot seems to send over a target id value of '-1', and the API responds with "Invalid snak ($idSerialization must match /^Q[1-9]\d*$/i)". I don't see anywhere else in the Claim object to set the target item to 'unknown'. Any hints? Is this not possible with pywikibot? ArthurPSmith (talk) 22:04, 11 November 2015 (UTC)Reply

@ArthurPSmith: Haven't tried that yet, but a quick search in page.py looks like this function might help: --Tobias1984 (talk) 22:16, 11 November 2015 (UTC)Reply
    def changeTarget(self, value=None, snaktype='value', **kwargs):
        """
        Set the target value in the data repository.

        @param value: The new target value.
        @type value: object
        @param snaktype: The new snak type.
        @type snaktype: str ('value', 'somevalue', or 'novalue')
        """
        if value:
            self.setTarget(value)

        data = self.repo.changeClaimTarget(self, snaktype=snaktype,
                                           **kwargs)
        # TODO: Re-create the entire item from JSON, not just id
        self.snak = data['claim']['id']
        self.on_item.latest_revision_id = data['pageinfo']['lastrevid']
That's it, thanks, the snaktype tells! So snaktype = 'somevalue' is what corresponds to 'unknown'. I think I can get this working now. Thanks ArthurPSmith (talk) 19:08, 12 November 2015 (UTC)Reply
@ArthurPSmith: Did setting snaktype to 'unknown' work? Deryck Chan (talk) 22:53, 3 November 2017 (UTC)Reply
@Deryck Chan: Not 'unknown', 'somevalue'. Here's the line of code that does it -
        claim.setSnakType('somevalue')
Source code here: https://github.com/arthurpsmith/wikidata-tools/blob/master/APSbot/APSbot_nuclide_decays.py ArthurPSmith (talk) 16:58, 4 November 2017 (UTC)Reply

Problem with small decimal values edit

@Tobias1984: - I'm continuing to run into that issue we talked about a while ago regarding small decimal values - pywikibot tries to send them using exponential notation, and the Wikidata API fails to accept them. See phabricator T119226. I have a workaround with an edit to __init__.py in pywikibot but there's surely a better solution - any suggestions on how to get this fixed? ArthurPSmith (talk) 15:15, 28 June 2016 (UTC)Reply

@ArthurPSmith: I personally would prefer if we could also send the numeric values as a dictionary of strings: {"value": "123", "upper_bound": "125", "lower_bound": "120"}. This would be closer to what the web-interface does and would avoid converting Python floats (which seems to have differences for 2.x and 3.x). What do you think about that? --Tobias1984 (talk) 15:22, 28 June 2016 (UTC)Reply
they ARE being sent as strings - that's what the conversion is doing in __init__.py, when _fromdecimal(value) is called. Obviously any http request can only contain strings, anything else has to be encoded somehow as a string. Are you suggesting that pywikibot itself only accept strings as values even for numeric quantities? Either way I think that just moves the problem elsewhere - the users' code needs to then do the conversion of float values to strings, rather than pywikibot. And we still have the problem that the web API for setting quantity values does not understand exponential notation so users will run directly into that if they try to use those as string values. ArthurPSmith (talk) 15:41, 28 June 2016 (UTC)Reply
@ArthurPSmith: Sorry, I did not word that ideally. I think that it would be better to give pywikibot the value, upper, and lower bound as strings without any Python internal math operations that could potentially change the data. And pywikibot should send them straight to the API and see if it gets accepted (As users of the GUI do). Most of the bad stuff happens when pywikibot gets the numbers and the standard deviations, converts them to floats and calculates the upper and lower bound. - In any case we need some tests for the largest and smallest values and that they really arrive the way they should. I just checked the ranges and added them here: Help:Statements#Quantitative_values. - I am also not sure how the data is preserved in the handling of exponential numbers. So the first thing would probably be to write an exhaustive list of tests. --Tobias1984 (talk) 16:07, 28 June 2016 (UTC)Reply
@Tobias1984: well, I guess that's fine - but the real problem here is that the wikidata API does not accept exponential notation for quantity values - wikidata also does not display them with exponential notation. I really don't care a whole lot if something is entered or display as 1.0e-123 or 1e-123 but I really do not want to have to read something that looks like "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" - that is just not helpful. And with current wikidata it truncates it and displays it as "0.000000" anyway, which is even worse. So I think we need to somehow push for additional support for numeric value representation somewhere, but I'm not sure what the right place to look is. ArthurPSmith (talk) 17:45, 28 June 2016 (UTC)Reply
Oh, are you suggesting writing explicitly some tests for pywikibot? Something to add to tests/wikibase_tests.py for instance? Are there any tests of inserting data values there? I'll take a look... ArthurPSmith (talk) 17:50, 28 June 2016 (UTC)Reply
Ok, it looks like relevant tests are in wikibase_edit_tests.py - and there's nothing there yet concerning setting Quantity values so we should certainly look to add that. It's not clear to me how to run the tests first though - I get an error "skipped 'TestWikibaseMakeClaim: Not able to login to bot as wikidata:test'" - do you need to set up something else to run these tests? ArthurPSmith (talk) 18:06, 28 June 2016 (UTC)Reply
@ArthurPSmith: When I run $ tox I also get these errors. I usually use pytest: $ python3 -m pytest tests/wikibase_edit_tests.py. But that of course does not test all the versions of Python. The documentation for running the tests could use a few lines of documentation. --Tobias1984 (talk) 18:55, 28 June 2016 (UTC)Reply
@Tobias1984: ok, trying with pytest but now it's just "skipping" all the tests in that test file:
python -m pytest tests/wikibase_edit_tests.py 
============================= test session starts ==============================
platform darwin -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/apsmith/projects/Institutions/wikidata/core, inifile: tox.ini
collected 12 items 

tests/wikibase_edit_tests.py ssssssssssss

========================== 12 skipped in 1.73 seconds ==========================

ArthurPSmith (talk) 19:06, 28 June 2016 (UTC)Reply
- and now it hangs without running any tests at all. Weird. I'm sure I have something broken with regard to configuration. Though - these are supposed to run against test.wikidata.org right? And affect item Q68? But that hasn't been touched by anybody, according to the history link there, since February 18 ??? So maybe nobody's been running these tests? ArthurPSmith (talk) 20:27, 28 June 2016 (UTC)Reply
@ArthurPSmith: I checked the CI service on gerrit.wikimedia.org now. Apparently it runs the tests using: $ tox -v -e nose34. That works for me for the tests in tests/wikibase_edit_tests.py, but some others fail. Too bad that the tests are not compatible with pytest apparently. --Tobias1984 (talk) 21:15, 28 June 2016 (UTC)Reply

Edit summaries edit

Hey Tobias1984 - Thanks for your encouragement re my update.

I see that edit summaries seem to have dropped off the radar by the second learning block. Is there any way to append an edit summary when one uses deep-level methods like claim.changeTarget(target) and claim.addQualifier()?

Also, what is your recommended way of creating a claim or qualifier from scratch? Deryck Chan (talk) 22:47, 3 November 2017 (UTC)Reply

AFAIK, all of those methods support summary as a kwarg. Matěj Suchánek (talk) 08:42, 4 November 2017 (UTC)Reply
Matěj Suchánek, can you give me a code example? The tutorial hasn't been using kwarg to do edit summaries. Deryck Chan (talk) 17:32, 4 November 2017 (UTC)Reply
[1] Matěj Suchánek (talk) 17:40, 4 November 2017 (UTC)Reply

Data harvest edit

Wikidata talk:Pywikibot - Python 3 Tutorial/Data Harvest#How to print selected language's labels--Info-farmer (talk) 01:29, 11 February 2020 (UTC)Reply

Output of two cells has changed with respect to the reference notebook for Data Harvest edit

See Wikidata talk:Pywikibot - Python 3 Tutorial/Data Harvest#Output of two cells has changed with respect to the reference notebook. --Daniel Mietchen (talk) 19:25, 9 December 2020 (UTC)Reply

Does someone know if pywikibot support lexemes? edit

I would like to watch a lexeme using pywikibot. Is that possible?--So9q (talk) 12:32, 15 December 2020 (UTC)Reply

With Pywikibot, not yet. You could use LexData in the mean time. - Premeditated (talk) 14:12, 15 December 2020 (UTC)Reply
Return to the project page "Pywikibot - Python 3 Tutorial".