Deutsch   English   Français   Italiano  
<mailman.14.1724890041.2917.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!npeer.as286.net!npeer-ng0.as286.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: dn <PythonList@DancesWithMice.info>
Newsgroups: comp.lang.python
Subject: Re: Script stops running with no error
Date: Thu, 29 Aug 2024 12:07:07 +1200
Organization: DWM
Lines: 136
Message-ID: <mailman.14.1724890041.2917.python-list@python.org>
References: <87r0a8xskb.fsf@rpi3>
 <bb82f035-45dc-4c6f-aaec-b1e59ce825f7@tompassin.net>
 <0fec5175-e2a2-407a-9e09-c6901617b75c@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de EjzuFUrnAxYe6IofivItSQPkxvnV39+arnYx0R0eEqmg==
Cancel-Lock: sha1:85XVYQws5nLDRmkuXwNI6MAmGmo= sha256:Fq1f7XMbcD0QD2DxHt7yPNiua53olQINvUcWQOdH/sA=
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=danceswithmice.info header.i=@danceswithmice.info
 header.b=CK1BByx4; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(which': 0.04;
 'containing': 0.05; '(to': 0.07; 'matches': 0.07; 'subject:error':
 0.07; '=dn': 0.09; 'compute': 0.09;
 'from:addr:danceswithmice.info': 0.09; 'from:addr:pythonlist':
 0.09; 'json': 0.09; 'ok,': 0.09; 'parse': 0.09; 'question:': 0.09;
 'received:192.168.1.64': 0.09; 'requests.': 0.09; 'steps': 0.11;
 '(more': 0.16; 'are.\xc2\xa0': 0.16; 'computation': 0.16;
 'computers': 0.16; 'constant': 0.16; 'dictionary.': 0.16; 'hint':
 0.16; 'intro': 0.16; 'message-id:@DancesWithMice.info': 0.16;
 'outputting': 0.16; 'pertinent': 0.16; 'printer': 0.16; 'pytest,':
 0.16; 'received:51.254': 0.16; 'received:51.254.211': 0.16;
 'received:51.254.211.219': 0.16; 'received:cloud': 0.16;
 'received:rangi.cloud': 0.16; 'script.': 0.16; 'splitting': 0.16;
 'subject:running': 0.16; 'wikimedia': 0.16; 'wrote:': 0.16;
 'problem': 0.16; 'python': 0.16; 'api': 0.17; 'pull': 0.17;
 'probably': 0.17; "aren't": 0.19; 'pm,': 0.19; 'to:addr:python-
 list': 0.20; 'input': 0.21; 'code': 0.23; 'lines': 0.23;
 'url:wiki': 0.23; 'idea': 0.24; '(and': 0.25; 'discussion': 0.25;
 'seems': 0.26; 'again,': 0.26; 'library': 0.26; "isn't": 0.27;
 'leave': 0.27; 'function': 0.27; 'expect': 0.28; 'output': 0.28;
 'settings': 0.28; 'example,': 0.28; 'ideas': 0.28; 'header:User-
 Agent:1': 0.30; 'seem': 0.31; 'header:Organization:1': 0.31;
 'think': 0.32; 'execution': 0.32; 'python-list': 0.32;
 'structure': 0.32; 'received:192.168.1': 0.32; 'but': 0.32; "i'm":
 0.33; 'there': 0.33; 'able': 0.34; 'header:In-Reply-To:1': 0.34;
 'complex': 0.35; 'functions': 0.36; 'those': 0.36; "skip:' 10":
 0.37; 'main': 0.37; 'using': 0.37; 'received:192.168': 0.37;
 'way': 0.38; 'could': 0.38; 'means': 0.38; 'put': 0.38; 'read':
 0.38; 'two': 0.39; 'text': 0.39; 'enough': 0.39; 'use': 0.39;
 '(with': 0.39; 'on.': 0.39; 'seeing': 0.39; 'skip:u 20': 0.39;
 'table': 0.39; 'advantage': 0.40; 'data.': 0.40; 'learn': 0.40;
 'provide': 0.60; 'likely': 0.61; 'sample': 0.61; 'seen': 0.62;
 'come': 0.62; 'once': 0.63; 'our': 0.64; 'research': 0.64; 'skip:r
 20': 0.64; 'definition': 0.64; 'received:51': 0.64; 'your': 0.64;
 'top': 0.65; 'produce': 0.65; 'look': 0.65; 'improve': 0.66;
 'earlier': 0.67; 'now,': 0.67; 'back': 0.67; 'outside': 0.67;
 'time,': 0.67; 'that,': 0.67; 'items': 0.68; 'further': 0.69;
 'functional': 0.69; 'repeatedly': 0.69; 'times': 0.69; 'above,':
 0.70; 'url-ip:208.80.154/24': 0.70; 'url-ip:208.80/16': 0.70;
 'url-ip:208/8': 0.71; 'process,': 0.75; 'finds': 0.76; 'discuss':
 0.78; 'returned': 0.81; 'url:api': 0.84; 'ask,': 0.84; 'bid':
 0.84; 'big,': 0.84; 'expand,': 0.84; 'inherent': 0.84;
 'organised': 0.84; 'thus,': 0.84; "wikimedia's": 0.84; 'you?':
 0.88; 'sub': 0.91; 'url:wikimedia': 0.91; 'word.': 0.91
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 8346976A
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
 s=staff; t=1724890033;
 bh=//F2FXiRd529WWMhyePr8TMbdF2BYckQbEsTzKP/pvs=;
 h=Date:From:Subject:To:References:In-Reply-To:From;
 b=CK1BByx4uc1B1MPbs7oQcRAZqbFnItZwrHxioPXXGtEQBrRFQ58eFiIDqmYtUH1SS
 MaLY7yclWdZFlcAb+o3fYtDj7C4bdmWOnEMmyyEKjD2wsUx+4k5W3vntQqgJTeif0f
 R+5clzz8sK4Yz4AQDPwglYcLGrPyPD8j2782UhRz07JD6/xTimx7Jds+3coTm8tM0z
 97q1802bkEwP8xkr31LNApRkeD2/NTFf2QvY2neiNnu9DXVG1rKqLcLSv5H5AKK7Vv
 2wAwIdY8Quk1TWg5orYPQfqSeIrYxyL8wfEB16artFxd4EK/0dr7WV8uH2jTrKp1zW
 X84HbcIoaobPA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <bb82f035-45dc-4c6f-aaec-b1e59ce825f7@tompassin.net>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0fec5175-e2a2-407a-9e09-c6901617b75c@DancesWithMice.info>
X-Mailman-Original-References: <87r0a8xskb.fsf@rpi3>
 <bb82f035-45dc-4c6f-aaec-b1e59ce825f7@tompassin.net>
Bytes: 10901

On 29/08/24 10:32, Thomas Passin via Python-list wrote:
> On 8/28/2024 5:09 PM, Daniel via Python-list wrote:
>> As you all have seen on my intro post, I am in a project using Python
>> (which I'm learning as I go) using the wikimedia API to pull data from
>> wiktionary.org. I want to parse the json and output, for now, just the
>> definition of the word.
>>
>> Wiktionary is wikimedia's dictionary.
>>
>> My requirements for v1
>>
>> Query the api for the definition for table (in the python script).
>> Pull the proper json
>> Parse the json
>> output the definition only


> You need to check at each part of the code to see if you are getting or 
> producing what you think you are.  You also should create a text 
> constant containing the JSON input you expect to get.  Make sure you can 
> process that.  Start simple - one main item.  Then two main items.  Then 
> two main items with one sub item.  And so on.
> 
> I'm not sure what you want to produce in the end but this seems awfully 
> complex to be starting with.  Also you aren't taking advantage of the 
> structure inherent in the JSON.  If the data response isn't too big, you 
> can probably take it as is and use the Python JSON reader to produce a 
> Python data structure.  It should be much easier (and faster) to process 
> the data structure than to repeatedly scan all those lines of data with 
> regexes.


Good effort so far!


Further to @Thomas: the code does seem to be taking the long way around! 
How can we illustrate that, and improve life?


The Wiktionary docs at https://developer.wikimedia.org/use-content/ 
discuss how to use their "Developer Portal". Worth reading!

As part of the above, we find the "API:Data formats" page 
(https://www.mediawiki.org/wiki/API:Data_formats) which offers a simple 
example (more simple than your objectives):

api.php?action=query&titles=Main%20page&format=json

which produces:

{
   "query": {
     "pages": {
       "217225": {
         "pageid": 217225,
         "ns": 0,
         "title": "Main page"
       }
     }
   }
}

Does this look like a Python dict[ionary's] output to you?

It is, (more discussion at the web.ref)
- but it is wrapped into a JSON payload.

There are various ways of dealing with JSON-formatted data. You're 
already using requests. Perhaps leave such research until later.


So, as soon as "page_data" is realised from "response", print() it (per 
above: make sure you're actually seeing what you're expecting to see). 
Computers have this literal habit of doing what we ask, not what we want!

PS the pprint/pretty printer library offers a neater way of outputting a 
"nested" data-structure (https://docs.python.org/3/library/pprint.html).


Thereafter, make as much use of the returned dict/list structure as can. 
At each stage of the 'drilling-down' process, again, print() it (to make 
sure ...)


In this way the code will step-through the various 'layers' of 
data-organisation. That observation and stepping-through of 'layers' is 
a hint that the code should (probably) also be organised by 'layer'! For 
example, the first for-loop finds a page which matches the search-key. 
This could be abstracted into a (well-named) function.

Thus, you can write a test-harness which provides the function with some 
sample input (which you know from earlier print-outs!) and can ensure 
(with yet another print()) that the returned-result is as-expected!

NB the test-data and check-print() should be outside the function. 
Please take these steps as-read or as 'rules'. Once your skills expand, 
you will likely become ready to learn about unit-testing, pytest, etc. 
At which time, such ideas will 'fall into place'.


BTW/whilst that 'unit' is in-focus: how many times will the current code 
========== REMAINDER OF ARTICLE TRUNCATED ==========