[Tutor] Regarding Python api script

Avi Gross avigross at verizon.net
Thu Dec 6 21:13:01 EST 2018


I left the "subject" above to be the same, as requested.

The actual subject might have been "Pythonic variable name use"

Alan says he had a challenge evaluating code (below) because the same
variable names were reused and it made me wonder if the python community has
general ideas about name re-use.

I am keeping this short. In languages without garbage collection, reusing
the same name, like "index" repeatedly might save some small amount of
space. With garbage collection, reuse may be one way to hint it can be used
again but if that is important, you can use "del var" to remove var.

For short programs, or in a context like a "normal" function that goes away
when completed, it probably is best to make the code very readable. PLEASE
do not mention the many exceptions as I do not intend much of what I say to
be taken as completely accurate.

But so much code I see in python does not only reuse the same variable names
but in a confusing way.

file =  "some name"
file = open(file, "r")
file = some_wrapper(file)

mylist = [ ... ]
mylist = sorted(mylist)

for index in range(...):
  stuff

for index in open(...)
  more stuff

for index, index2  in enumerate(...)
  yet more stuff

I have often seen something like this done with methods, such as to emulate
decorator functionality where a method is created in an object with a name
and the very next method created has the same name with the same function
name as an argument to replace it.

So is there a guide on when reuse is good and when it just obfuscates? What
is good practice?

-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon.net at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Thursday, December 6, 2018 7:26 PM
To: tutor at python.org
Subject: Re: [Tutor] Regarding Python api script

On 06/12/2018 14:17, Ravi Kumar wrote:

> 1)The for loops that have written I am able to access all the 
> networks,able to loop through  all access points(Devices) in the 
> network,able to loop through and get all clients in each access points 
> but when it comea to client log events I am able to loop through and 
> get only  the last row of accesspoints  list and get those particular 
> and clients and its log events

I don't know the cause, but did take a quick look at the code.
One thing that makes it very difficult to follow is that you use the same
variable names over and over making it hard to keep track of what any given
'item' or 'json_string' or 'r' actually holds at any given time.

Buy using  a different name for each json_string reflecting the expected
data - such as json_networks or json_clients - it would be very much easier
to follow the flow of the code.

> I assume I am going wrong in the last for loop code and output as 
> shown below

Probably but I confess I couldn't spot it from a casual read through.

>     for client in json_string:
> 
>         hostname = client.get("dhcpHostname")
>         description = client.get("description")
>         ipaddress = client.get("ip")
>         macaddress = client.get("mac")
> 
>         usage = client.get("usage")
>         sentbytes = usage.get("sent")
>         recvbytes = usage.get("recv")
> 
>         print ('{0:20}   {1:20}   {2:20}    {3:30}   {4:20}
> {5:20}'.format(hostname, description, ipaddress, macaddress,sentbytes,
> recvbytes))
> 
> 
> 
> for  item in serialnumlist:
>     print ("\nQuerying Meraki for clientlogevents on Devices: (" +
> item.get("mac") + ")")
> 
> for item  in json_string:
>     config_url = "/networks/"+"/N_63***1050/"+"/clients/"+ 
> item.get("mac")
> + "/events?perPage=1000"
>     r = requests.get(base_url + config_url, headers=headers)
> 
>     print ('{}  '.format(r.content))
> **********************************>

The 'item's in the last loop appear to be the same as the 'client's in the
previous loop? Since 'json_string' never changes...

Also the setialnumlist only prints the result it never stores anything.

Then the last loop uses item.get(mac) which will not be the one printed
earlier since item is now read from json_string(back tome earlier confusion
over names!)

I suspect this lack of continuity may be the root of your problem but the
name collisions are twisting my brain and its late at night...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list