Page MenuHomePhabricator

DataPage should get the content once defined
Closed, DeclinedPublic

Description

Originally from: http://sourceforge.net/p/pywikipediabot/bugs/1587/
Reported by: reza1615
Created on: 2013-02-24 19:21:13
Subject: setitem for wikidata doesn't work!
Original description:
I used this code

\#\!/usr/bin/python
\# -\*- coding: utf-8 -\*-
import wikipedia,login
summary=u'آبشار لاتون,آبشار لاتون'

site=wikipedia.getSite\('fa',fam='wikipedia'\)
fapage=wikipedia.Page\(site,u'آبشار\_لاتون'\)
data=wikipedia.DataPage\(fapage\)

list=data.get\(\)
id=list\['entity'\].replace\('q',''\)
data = wikipedia.DataPage\(site.data\_repository\(\), "Q"+id\)

data.setitem\(summary,items=\{'type': u'item', 'label': 'glk', 'value':u'آبشار\_لاتون'\}\)
data.setitem\(summary,items=\{'type': u'sitelink', 'site': 'glk', 'title':u'آبشار\_لاتون\_\(بارزاو\)'\}\)

it shows
Updating page \[\[wikidata:Q5058182\]\] via API
Updating page \[\[wikidata:Q5058182\]\] via API

but it doesn't update the page\!
also please add

def getIdFromPage\(data\)
id=data.get\(\) \['entity'\].replace\('q',''\)
return id

and add

data = DataPage\(site.data\_repository\(\), "Q"+id\)

to first line of setitem\(\)


Version: compat-(1.0)
Severity: enhancement
See Also:
https://sourceforge.net/p/pywikipediabot/bugs/1587

Details

Reference
bz55149

Event Timeline

bzimport raised the priority of this task from to Lowest.Nov 22 2014, 2:22 AM
bzimport set Reference to bz55149.
bzimport added a subscriber: Unknown Object (????).
  • summary: settime for wikidata doesn't work! --> setitem for wikidata doesn't work!

Thank you now it works.
please add these tree lines to setitem\(\)

list=data.get\(\)
id=list\['entity'\].replace\('q',''\)
data = wikipedia.DataPage\(site.data\_repository\(\), "Q"+id\)

now we should write these tree lines for every DataPage\(\)

You don't need to create the DataPage a second time with the 'entity' item. Look at this sample which shows that data == d but they are different objects in this sample:

>>> import wikipedia as wp
>>> s = wp.getSite\(\)
>>> p = wp.Page\(s, 'Helium'\)
>>> d = wp.DataPage\(p\)
>>> i = d.get\(\)
>>> t = i\['entity'\].title\(\)
>>> data = wp.DataPage\(s.data\_repository\(\), t\)
>>> data
DataPage\{\[\[wikidata:Q560\]\]\}
>>> d
DataPage\{\[\[wikidata:Q560\]\]\}
>>> d is data
False
>>> d == data
True
>>>

If I don't make in DataPage a second time it will show this error

Updating page \[\[wikidata:None\]\] via API

This error looks like you haven't made a DataPage.get\(\) before you tried to update the page. In this cas the title is None as shown. If you recreate the datapage with the entity id you make a explicit get call to retrieve the items data. But this will be enough to get the data's id i.e. the DataPage.title\(\).

please run this code and you will see the none title error \!

\#\!/usr/bin/python
\# -\*- coding: utf-8 -\*-
import wikipedia,login
summary=u'test bot'

site=wikipedia.getSite\('fa',fam='wikipedia'\)
fapage=wikipedia.Page\(site,u'آبشار\_لاتون'\)
data=wikipedia.DataPage\(fapage\)

data.setitem\(summary,items=\{'type': u'item', 'label': 'glk', 'value':u'آبشار\_لاتون'\}\)
data.setitem\(summary,items=\{'type': u'sitelink', 'site': 'glk', 'title':u'آبشار\_لاتون\_\(بارزاو\)'\}\)

I tested it and it worked:
http://www.wikidata.org/w/index.php?title=Q5058182&diff=prev&oldid=7790985
We have showing problem. the code works well
I think xqt means if you run this way the showing problem will be solved. something like this notice i just add one line:
\#\!/usr/bin/python
\# -\*- coding: utf-8 -\*-
import wikipedia,login
summary=u'test bot'

site=wikipedia.getSite\('fa',fam='wikipedia'\)
fapage=wikipedia.Page\(site,u'آبشار\_لاتون'\)
data=wikipedia.DataPage\(fapage\)
gett=data.get\(\)
data.setitem\(summary,items=\{'type': u'item', 'label': 'glk',
'value':u'آبشار\_لاتون'\}\)
data.setitem\(summary,items=\{'type': u'sitelink', 'site': 'glk',
'title':u'آبشار\_لاتون\_\(بارزاو\)'\}\)

we can load or get data inside setitem\(\)

it is not normal that we type a code that it's variable \( gett=data.get\(\) \) doesn't need\! \(visually\)

it is logic that users only type this code

site=wikipedia.getSite\('fa',fam='wikipedia'\)
fapage=wikipedia.Page\(site,u'آبشار\_لاتون'\)
data=wikipedia.DataPage\(fapage\)
data.setitem\(summary,items=\{'type': u'item', 'label': 'glk','value':u'آبشار\_لاتون'\}\)

Library should be let user to type minimal code.

The line is not useless. If the item doesn't exist you will have an error instead of doing nothing

No\! it is not correct \!

now user should write

try:
gett=data.get\(\)
except:
some thing to do \(may be saying error\)

Instead of these four lines library should tell user that item doesn't exist so code should switch to create a new item.

There are two ways doing that task creating an item; i'll explain it with a pseudocode:

1st:
repo = repository\(localpage\)
try: repo.createitem\(\)
except itemExistError: print repo.title\(\), 'already exist'

2st:
repo = repository\(localpage\)
if not repo.exists\(\): repo.createitem\(\)
else: print repo.title\(\), 'allready exits'

I prefer the last one. Look at this sample:
>>> import wikipedia as wp
>>> p = wp.Page\('de', 'Helium'\)
>>> d = wp.DataPage\(p\)
>>> d
DataPage\{\[\[wikidata:None\]\]\}
>>> d.exists\(\)
True
>>> d
DataPage\{\[\[wikidata:Q560\]\]\}
>>>

This means you does not need to call DataPage.get\(\) you may also use DataPage.exists\(\) for this test.

in this report I don't want to make an item\!
I know about making an item amir solved it in last report
\(https://www.mediawiki.org/wiki/Special:Code/pywikipedia/11103\)

Now I want update an item with wikipedia.py
for updating an item we should call data object with .get\(\) or .exists\(\) or other functions to let wikipedia.py load that data object and it is not good because user should write 4 lines in every part of his code
you can add these lines to library to not repeating.

for setitem\(\) you can add

if d.exists\(\):
pass \# pass or doing next lines
else:
print 'page not exist do you want creat it? '\# or it can call creatitem\(\) function if user allowed bot to create not existed item

for createitem\(\) you can add

if d.exists\(\):
print 'page exists so creating process is aborted?'\# or we can ask user do you want overwrite?
else:
pass \# pass or doing next lines

it should be done by library not users. because minimal coding is much better than writing repeated lines\!
if I want to create or edit an item I should add these 4 lines to my code but if they are inside wikipedia.py we don't need to write them.

it makes easy for coding . now calling .get\(\) or .exists\(\) is tricky and every user don't know and they will have problem with this\!

How could you update an item without knowing its content?

when we write
my\_data = wp.DataPage\(p\)
my\_data should contained item\!
now for loading data to my\_data we shod type .get\(\) or .exist\(\) which are not necessary

What does you meen with .get is not necessary? For getting a content, create an object and assign it to an alias:

\# rewrite branch
myPage = wikipedia.Page\(wikipedia.getSite\(\), 'Helium'\)
content = myPage.text
myPage.text = u'This is a new content'
myPage.put\(\) \# put it to the web

\# trunk release
myPage = wikipedia.Page\(wikipedia.getSite\(\), 'Helium'\)
content = myPage.get\(\)
content = u'This is a new content'
myPage.put\(content\) \# put it to the web

\# wikidata
myData = wikipedia.DataPage\(1234\)
entity = myData.get\(\)
content = \{...\}
myData.setitem\('changing an item', content\) \# put it to the web

In all three samples you do not need to call the getter. But for that case you derived a DataPage from a Page object you have explicit to retrieve the data from the repository site before you can put any data back. Ok I guess you expect something like this should be possible:

myPage = wikipedia.Page\(wikipedia.getSite\(\), 'Helium'\)
myData = wikipedia.DataPage\(myPage\)
content = \{...\}
myData.setitem\('changing an item', content\) \# put it to the web

Am I right?

Yes
in my opinion code should be like below \(as you said in last comment\) and before DataPage\(\) library should load myData by itself without other lines\! \(minimal code\) and this loading could be done in setitem\(\)

myPage = wikipedia.Page\(wikipedia.getSite\(\), 'Helium'\)
myData = wikipedia.DataPage\(myPage\)
content = \{...\}
myData.setitem\('changing an item', content\) \# put it to the web

Ok I agree. Sorry for misunderstanding you.

After lots of discussions people agreed to add .get() once a person defines a DataPage. I strongly disagree and (we don't run get() once we define a Page).

Honestly I think we need to close this as WONTFIX

Per consistency with Page (also, to avoid loading data when not needed)