User talk:Ivanhercaz

Jump to navigation Jump to search

About this board

Previous discussion was archived at User talk:Ivanhercaz/Archive 1 on 2017-01-02.

Summary by Ivanhercaz

How change labels and descriptions with a Python script made by Edoderoo.

Edoderoo (talkcontribs)
Ivanhercaz (talkcontribs)

Hi Edoderoo! It sounds great, it is a way to begin with a easy task and learn how to work Wikidata with a bot. I have moved your script to my user page and changed all the "nl" to "es" (I have thought I could make the same for Portuguese).

CanaryBot is running in PAWS when it works in the es-wiki. I could use the script in a Python notebook, don't you think? Sorry, but haven't worked with Python notebook deeply yet, so I would like to know if there is some way to test it with logfiles without writing the items.

Tonight I will request the bot permission for "Set Spanish and Portuguese label and description" and I will explain how I am going to work and that I will begin to set the Spanish task for all Indonesian villages.

If you answer my doubt I would try to begin with test edits that night. I want to be sure before to run the task because with less errors, better.

Thanks in advance!

Regards, Ivanhercaz

P. S. Well, I could use site = pywikibot.Site('test', 'wikidata') to test this script, no? What I need if I want to test it in this test-space is to be sure that exists "Indonesian villages" where I could make the change, no?

Edoderoo (talkcontribs)

You can indeed use test.wikidata to test your edits. When I wrote this script yesterday evening, I replaced the village.editEntity() with a print() as a start, until it showed me well enough results... After replacing the print() with .editEntity() I often add a print(0/0) temporarily after my editEntity(), this will cause a runtime error just after the edit, so it will be limited to one edit... Then I can check my edit in the browser, and when all is fine, I remove the "divide by zero" Obviously, when using test.wikidata you can take more risks and less precautions.

Ivanhercaz (talkcontribs)

I have test the code in Test-Wikidata but it didn't works, I think that it could be because there isn't the query service, no? I created items with "instance of" : "indonesian village" and "country" : "Indonesia" and change in your query code the P and Q to the new created (Q40354 and Q40355).

Could it be happens because Test-Wikidata hasn't a query service available and therefore it doesn't execute the main query to search the items that the script have to set?

Edoderoo (talkcontribs)

That might be possible, I never used test.wikidata (or maybe once in my early days). Then my suggestion is to put a # remark before the editEntry to comment it out, and see how the script will work on the real Wikidata... When that runs fine, you can put the editEntry back...

Edoderoo (talkcontribs)

Another solution can be: Instead of using wd_sparql_query, which is just a generator to list items, you can manually pick a few items that exist on and put them in your own generator....

def generate_a_few:

 for wd in myitemlist:
   if (wd.exists()):
     yield wd

It seems that is pretty empty after all. For Q2225692 there is no record, and I've seen just now that the two records above were created by yourself...

Ivanhercaz (talkcontribs)

First, reply to your last comment about how empty is yes, there isn't so much properties nor items, but I created the necessary to execute the query; I created, as you have said, the items that I shared with you, the "instance of", because it didn't exist, and the Q40353 (Indonesian village).

I am going to test first with the second solution: create an array of items to modify. I will notify you in this post if it works fine in

Then, if it doesn't work, I would put a # remark before the editEntry.

What I don't want is make any edit in Wikidata yet because I haven't got the task approved or the permission to execute it. I will request the task to test it in Wikidata as soon as possible.

When I will have some news about the script or the task, I will notify you!

Regards, Ivanhercaz

Edoderoo (talkcontribs)

First of all, good luck. Secondly, you are allowed to make some test edits with your bot, as it is part of requesting a bot-flag. So I would not wait till you get aproval, but just create like 50 edits to be a good example of what you want to achieve. You can double check these edits manually, and keep in mind: your bot is allowed to make any edit that you would have done manually with your own account too, only a bot is allowed to make edits faster, like 1 edit per second. Standard the pywikibot-throttle is set to 10 seconds, so I don't expect people will complain.

Ivanhercaz (talkcontribs)

All right Edoderoo! Thank you for your help and the information. When I request the task, I will begin to made it, but only 50 edits to check that it works fine. I have thought that when I have the permission, I am going to config user-config to execute it with a maxlag = 5.

Finally I have decided to comment the editEntry because the other way didn't run. What I get in the Python notebook is:


I understand that if there is a Indonesian label, the script set the Spanish label. But if the result, like Q14684990-0-0-90-[]-<> hasn't anything at the right, after the <>, I understand that it doesn't exist a Indonesian label to set the Spanish label. I have checked some items ―Q14635224, Q14633921 and Q14684990― and I didn't see the Indonesian label, but there is in Dutch and Germany, I thought it could be useful to use the nl tag to set the Spanish, but when I replace the "id" for "nl" it checks items where the "id" is set and not the "nl".

I think there is something in the code that I am not understanding. Excuse me, the code isn't hard, but there is something that I am not seeing.

Edoderoo (talkcontribs)

just before the try: line, put something like: print('[%s]-<%s>' % (nl,id))

this will print the Spanish label (nl) within <> and the Indonesian label between [] ... The first one/Spanish will be blank, and the Indonesian one will be filled ... Something else you can do is use print(data) that will print the variable data, that will look something like '{labels : {es:Indonesian village name}}' and this is exactly the "command" for editEntity() to change the es-label ...

Later on you can also add: data.update('{descriptions:{%s:%s}}' % (lang,'pueblo en Indonesia')) This will set the description ...

Ivanhercaz (talkcontribs)

Thank you so much Edoderoo! Now it seems to work fine and I understand it. About description, could it make at the same time? Something like:

      es_blank += 1  
      data.update('{descriptions':'{%s:%s}'}' % (lang,'pueblo en Indonesia'))
      print('[%s]-<%s>' % (es,id))

Or, do I need to make another function and execute in another instance to add descriptions?

I am reading Pywikibot API Docs., so I hope to clarify more how to develop with it.

Edoderoo (talkcontribs)

Yes, you can do the label and description in one editEntity(). You build the data part in two steps, like you show above, and send that to editEntity(data,summary='Edit summary')

Ivanhercaz (talkcontribs)

If I use:

      es_blank += 1  
      data.update('{descriptions:{%s:%s}}' % (lang,'pueblo en Indonesia'))
      print('[%s]-<%s>' % (es,id))

I get a ValueError:

ValueError                                Traceback (most recent call last)
<ipython-input-20-cf0e0cf7c5ee> in <module>()
     29       data={}
     30       data.update({'labels':{lang:id}})
---> 31       data.update('{descriptions:{%s:%s}}' % (lang,'pueblo en Indonesia'))
     32       print('[%s]-<%s>' % (es,id))
     33       print(data)

ValueError: dictionary update sequence element #0 has length 1; 2 is required

I am trying to solve it, any idea?

Edoderoo (talkcontribs)

I made a mistake, the format for data.update is : data.update({ : }) ... within the brackets you get two values, so: data.update({'labels':{'es':'pueblo en Indonesia'}}) (the second one is nested, to make it a bit harder ;-)) line 31 has a slightly different (and therefor wrong) format in the end... sorry for that!

data.update({'descriptions':'{%s:%s}' % ('es','pueblo en Indonesia')})

Ivanhercaz (talkcontribs)

Don't worry Edoderoo! Now it works perfectly, thank you. I suppose that the error was something with :, but I didn't find how fix it. I am going to request the task tonight, I will mention you because this script is yours and I am very grateful with you.

Edoderoo (talkcontribs)

You're welcome, I'm glad to help to make free knowledge better ;-)

There are no older topics