Open main menu

Wikidata talk:Pywikibot - Python 3 Tutorial

Unknown or Some Value as TargetEdit

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)

@ArthurPSmith: Haven't tried that yet, but a quick search in looks like this function might help: --Tobias1984 (talk) 22:16, 11 November 2015 (UTC)
    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:

        data = self.repo.changeClaimTarget(self, snaktype=snaktype,
        # 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)
@ArthurPSmith: Did setting snaktype to 'unknown' work? Deryck Chan (talk) 22:53, 3 November 2017 (UTC)
@Deryck Chan: Not 'unknown', 'somevalue'. Here's the line of code that does it -
Source code here: ArthurPSmith (talk) 16:58, 4 November 2017 (UTC)

Problem with small decimal valuesEdit

@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 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)

@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)
they ARE being sent as strings - that's what the conversion is doing in, 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)
@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)
@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)
Oh, are you suggesting writing explicitly some tests for pywikibot? Something to add to tests/ for instance? Are there any tests of inserting data values there? I'll take a look... ArthurPSmith (talk) 17:50, 28 June 2016 (UTC)
Ok, it looks like relevant tests are in - 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)
@ArthurPSmith: When I run $ tox I also get these errors. I usually use pytest: $ python3 -m pytest tests/ 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)
@Tobias1984: ok, trying with pytest but now it's just "skipping" all the tests in that test file:
python -m pytest tests/ 
============================= 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/ ssssssssssss

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

ArthurPSmith (talk) 19:06, 28 June 2016 (UTC)
- 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 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)
@ArthurPSmith: I checked the CI service on now. Apparently it runs the tests using: $ tox -v -e nose34. That works for me for the tests in tests/, but some others fail. Too bad that the tests are not compatible with pytest apparently. --Tobias1984 (talk) 21:15, 28 June 2016 (UTC)

Edit summariesEdit

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)

AFAIK, all of those methods support summary as a kwarg. Matěj Suchánek (talk) 08:42, 4 November 2017 (UTC)
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)
[1] Matěj Suchánek (talk) 17:40, 4 November 2017 (UTC)
Return to the project page "Pywikibot - Python 3 Tutorial".