Re: [Tutor] Regarding Python api script

2018-12-07 Thread Steven D'Aprano
On Fri, Dec 07, 2018 at 12:30:18PM -0500, Avi Gross wrote:

> #  > 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.
> #  

Then I asked:

> # I don't understand this. Can you show an example? Even if made up?
[...]

And Avi explained:

> class classy(object):
> ...
> # Original function first of this name
> def recreational_fun(args):
> ...
> 
> # New function, second of this name
> recreational _fun = timing_is_everything(recreational _fun, args)

Ah, now I understand!

That's a decorator. The only difference is that it predates the 
introduction of *decorator syntax* using the @ symbol.

The *decorator design pattern* is an idiom which uses a wrapper function 
to add functionality to an existing function. Given a language where 
functions are first-class values, you can pass a function to another 
function as argument, and return yet a third newly constructed function. 
So this gives us the decorate pattern. In pseudo-code:

# Decorator pattern
def decorate(input):
output = wrap input with extra functionality
return output

new_function = decorate(function)

But if you don't need to keep both the old and new versions of the 
function, you can just write this:

function = decorate(function)


https://en.wikipedia.org/wiki/Decorator_pattern

In Java, it is more common to apply the decorator pattern to instances 
rather than functions or classes, but the principle is the same. Even if 
Java makes something conceptually simple incredibly complex :-)

*Technically* speaking, such a design could have been used in Python 
going all the way back to Python 1.4 or older, since functions have 
always been first-class values. But it only got really useful from about 
Python 2.2, with the introduction of closures to the language, and the 
classmethod and staticmethod decorators.

But initially, we didn't have dedicated syntax for applying a decorator, 
and had to write:

def function():
...

function = decorate(function)


which repeats the function name three times. It wasn't until 2.4 that 
the @ syntax was approved:

@decorate
def function():
...


which is literally syntactic sugar for the original version.

See PEP 318 for the background:

https://www.python.org/dev/peps/pep-0318/




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-07 Thread Steven D'Aprano
On Fri, Dec 07, 2018 at 05:59:22PM +, Alan Gauld via Tutor wrote:

[...]
> > ... In languages without garbage collection, reusing
> > the same name, like "index" repeatedly might save some 
> > small amount of space. 
> 
> Garbage collection only helps if the variable loses
> its assignment. If the variable retains its value it
> won't be garbage collected. So the same space saving applies.
> 
> However it is very rare that Python is used in the kind
> of scenarios where that kind of space saving is
> significant (usually embedded controllers with
> tiny memory spaces).

People routinely use Python with seriously large amounts of data, 
hundreds of megabytes or even gigabytes, and freely make copies of it, 
secure in the knowledge that they don't have to do much to manage memory 
because the garbage collector will do so for them.

Consider a simple expression like:

text = '"' + (text.strip().lower().replace(',', ' ')) + '"'

That makes five slightly-modified copies of the original text. And 
that's just from a single short expression. Memory is cheap, but not so 
cheap that we can afford to keep around hundreds of redundant copies of 
large objects.

Generally speaking, we can afford to be profligate with copies of 
objects because we know they won't "leak" and survive for very long: at 
worst, they will be reclaimed when the current function returns. The 
only times we need worry about lifetimes of objects are when we are 
working in the global scope, or when adding attributes to long-lived 
objects. Regardless of whether we write the above as a single 
expression, or split it over many lines:

text = text.strip()
text = text.lower()
text = text.replace(',', ' ')
text = '"' + text
text = text + '"'

the result will be the same. And I don't think the above would be more 
understandable if we invented separate names for each of the 
intermediate results.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-07 Thread Alan Gauld via Tutor
On 07/12/2018 02:13, Avi Gross wrote:
> Alan says he had a challenge evaluating code (below) because the same
> variable names were reused 

It wasn't the reuse per se but the generic nature of
the names combined with reuse. Reusing names like
i,j,k for simple integer indices etc is not a problem.
But when values of domain significance are given
the same generic name (like item or file) then it
gets confusing. Its very easy to miss a significant
reassignment of the variable to a new value.

> ... In languages without garbage collection, reusing
> the same name, like "index" repeatedly might save some 
> small amount of space. 

Garbage collection only helps if the variable loses
its assignment. If the variable retains its value it
won't be garbage collected. So the same space saving applies.

However it is very rare that Python is used in the kind
of scenarios where that kind of space saving is
significant (usually embedded controllers with
tiny memory spaces).

> if that is important, you can use "del var" to remove var.

Exactly so.

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

Then its probably badly written Python!

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

This is a good example of poor reuse since the objects
are all totally different and only the second it
truly a file.

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

That's not really reusing it, it's just a convoluted way
of modifying the value. (IN a purely functional view
of the world it is reuse but only a functional purist
would argue it that way I suspect)

> for index in range(...):
>   stuff

THis usage is possibly ok if range is used to generate
indexes into some collection. Although probably
iterating over the colection itself would be preferred.

> for index in open(...)
>   more stuff

But this is bad since index is not an index its a line,
and possibly even more specifically a domain object
representation which should get a domain related name.

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

Again the index is OK here but index2 is very wrong
since it holds the value not an index.

But these issues are not Python issues they are
general naming issues in all languages.

It is good practice, regardless of language, to use
problem domain names for variables not generic
names or type related names. And also to not reuse
variables if the name is not applicable to the new
value.

> I have often seen something like this done with methods, such as to emulate
> decorator functionality

It's quite common to decorate a method and assign
the result to the same name but that is akin to the
sorted list example above. The name is being
augmented by the decorator not used for a
different purpose. It is specifically being
assigned to the same name to hide the original
non augmented function object.

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

It's the same in Python as in any language.
Use a name that makes sense in the domain.
If that name is applicable in multiple places
its ok to use the same name.

If a name is non domain specific then it
should only be used in very localised
contexts - such as a loop body - or where
it will be replaced by a domain specific
variable immediately - such as a tuple
from a database being used to instantiate
a domain related object.


-- 
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-07 Thread Avi Gross
[[ Real SUBJECT: emulating decorators ]]

Steven,

I am not suggesting that a particular way of reusing a name is a good idea.
I am asking if some ways are encouraged and others are discouraged in the
python community. 

Just to clarify one point you asked about:

#  > 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.
#  
# I don't understand this. Can you show an example? Even if made up?

The answer is that I saw that mentioned in a Lutz book that I no longer
have. What I wrote above should be clear if I give an example:

Say you have a method in a class definition called do_this() that does
whatever this is.

You may also have other methods in the same or different classes with names
like do_that() and your programs run fine.

At a later point you decide you want to see how long it takes the method to
run. So you make or borrow a function called timing_is_everything() that
looks like this in pseudocode:

def timing_is_everything(fun, args):
save the current time
call fun with args
save the return value of calling fun
get the current time
calculate time elapsed and do whatever with it.
Return saved return value from fun.

The argument "fun" above can be replaced by any function such as "do_this"
or "do_that" and produce the same side-effect while also running the
function unharmed.

Then you decide you want a function that logs when another function is
called and with what arguments:

Again, in PSEUDOCODE, it looks like:

def logistics(fun, args):
with open(logfile):
 write info about the current date/time, name of the function and
arguments
return result of calling the function with those args

Now to the point. You could write each and every module you want
independently. At the top of the body you could insert any similar code to
begin timing it or to log it. Then you place the code needed for that
function followed by anything you want done after such an action and finally
return a value, if needed.

But if you already have written and tested something like the two modules
above, you can do something like this:

class classy(object):
...
# Original function first of this name
def recreational_fun(args):
...

# New function, second of this name
recreational _fun = timing_is_everything(recreational _fun, args)

# New function, third of this name
recreational _fun = logistics(recreational _fun, args)

AGAIN, the above is for illustration only and I am well aware of the proper
ways to do the pseudocode such as using notations like *args, **kwargs and
so on. We are talking about an IDEA that perhaps predated the introduction
of decorators way back when. The point is that you could have used three
names here for the various functions but realistically, only the last was
needed so why carry around extra methods.

With "decorators" as a python feature allowing you to wrap multiple
functions around the one being defined, assuming you built the above helper
functions properly so they can be used as decorators, the above might look
like this:

class classified(object):
...
# Original function first and only of this name 
# and highly decorated bottom to top

@ logistics
@ timing_is_everything
def recreational_fun(args):
...

Both ways of doing this, again if PROPERLY done, should have the same
result, albeit there may be subtle changes such as hidden within the dunder
properties. There will be a single function name visible to any user. When
called, it will log an entry, note what time it is, do whatever the
re-created method is supposed to do, then calculate how much time it took
and return some result.

And, yes, I know there is overhead added as you end up with layered function
calls and so on.

This was an answer to a request. I hope I answered it. I do not wish to now
continue on this topic. The topic was not decorators. But the decorator
functionality used above likely does not confuse anybody as to what the name
of the method is. That variable name is only seen once as compared to my
first example when it refers to three completely different functions and
keeps giving up a connection to what effectively becomes an anonymous
function that can only be remembered and called within the enclosing
function.

I tend to agree with the other points Steve made.


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


Re: [Tutor] Regarding Python api script

2018-12-07 Thread Steven D'Aprano
On Thu, Dec 06, 2018 at 09:13:01PM -0500, Avi Gross wrote:

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

I agree this is confusing: you have the same name, "file", used to mean 
a file name, an open file object, and whatever some_wrapper returns. 
This is not the clearest code I've ever seen.


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

There's nothing wrong with that, except that it ought to be written:

mylist = [ ... ]
mylist.sort()

to sort in place instead of making a sorted copy.



> for index in range(...):
>   stuff
> 
> for index in open(...)
>   more stuff


Using "index" to iterate over the lines in a file is just a crappy 
choice of name.



[...]
> 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.

I don't understand this. Can you show an example? Even if made up?


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

If you pick *meaningful* names, it will be pretty obvious when to reuse 
the same name: is the name still meaningful? Then you MAY reuse. If the 
name is not meaningful, then you MUST NOT reuse it, unless you are 
deliberately writing obfuscated code.

If you have a name "column" which you use for a columns, then you 
shouldn't reuse it for rows, or lines of text read from a file, or the 
length of a list, or the date. But you can reuse it for another column, 
provided there's no confusion over which column is which.

The same applies to any other language.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-07 Thread Avi Gross
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  On Behalf Of
Alan Gauld via Tutor
Sent: Thursday, December 6, 2018 7:26 PM
To: tutor@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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Regarding Python api script

2018-12-06 Thread Steven D'Aprano
On Thu, Dec 06, 2018 at 08:17:23AM -0600, Ravi Kumar wrote:

> I know I am asking a lot 

Yes you are. Please read this:

http://sscce.org/


It is written for Java programmers, but it applies equally to all 
languages, including Python.

Think about how difficult a job you are giving us: we don't have access 
to your network; we don't have the API key (which is understandable); we 
can't run the code; we can't even be confident that the code you give us 
is the same code you are running.

In fact we know that it cannot be the same code, because what you have 
given us has a Syntax Error in line 8:

> organization='4

(missing quote mark). Where there is one change to the code, there could 
be dozens for all we know.


> but I hit a road block on this i am assuming One
> small change in my last for loop I can get this working for all devices

That doesn't seem like a reasonable assumption to me. You might be 
right, or it could take many large changes.


> and
> I have hardcoded for one network how do I use the same code for different
> networks as well

You can read the network from the command line, or a config file, or an 
environment variable, or interactively by asking the user to enter the 
network using the "input" (Python 3) or "raw_input" (Python 2) 
functions.

I expect that you need more than one piece of information to change the 
network, it looks like you probably need at least four:

serveraddress
merakiAPIkey
organization
networkid

so it probably is best to read the details from a config file:

https://docs.python.org/3/library/configparser.html

is probably simplest, but you could use Property Lists as well:

https://docs.python.org/3/library/plistlib.html




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-06 Thread Alan Gauld via Tutor
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-06 Thread Ravi Kumar
Ya i do know SQL thanks I will look into it but before proceeding  I had
couple of concerns about my code to geneeate the client log events my
concerns


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 assume I am going wrong in the last for loop code and output as shown
below

import requests
import meraki
import json


serveraddress= 'meraki.com '
merakiAPIkey='3ce879efeafa5'
organization='4
networkid='N_639512046'

print ("Currently, the following organizations exist:\n")
base_url = "https://; + serveraddress + "/api/v0/organizations"
headers = {'X-Cisco-Meraki-API-Key': merakiAPIkey}

# Send the query to the Prime Infrastructure Server
r = requests.get(base_url,headers=headers)
# Retrieve the results in JSON format
json_string = r.json()


for org in json_string:

  print ("Organization: {}, ID: {}".format(org.get("name"), org.get("id")))
  print ("Meraki Query Engine Starting...\n")



 Section 2 Getting the networkID'S
#Construct the url for querying Prime Infrastructure
base_url=("https://"+serveraddress+"/api/v0;)
config_url=("/organizations/"+organization+"/networks")
headers = {'X-Cisco-Meraki-API-Key':merakiAPIkey}


r = requests.get(base_url+config_url,headers=headers)
json_string = r.json()

for network in json_string:
print("Network:{}, ID:  {}".format(network.get("name")
,network.get("id")))


for item in json_string:

if item['id'] == "N_630" :
network = item['id']
found_network = True

if not found_network:
print ("No Valid Network Found...")
exit()

print ("\n>>Querying Meraki for Access Points(Devices) on
Network:"+network)

 Section 2 Getting the Accesspoints(Devices)
config_url = "/networks/"+network+"/devices"
r = requests.get(base_url+config_url,headers=headers)
json_string = r.json()

print ('{0:20}   {1:20}   {2:20}   {3:30}   {4:20}'.format("Switch Name",
"MAC Address", "Serial Number", "IP Address","Device Type"))


serialnumlist=[]


for item in json_string:


# Extract all the appropriate fields
devicename = item.get("name")
macAddress = item.get("mac")
serialnum = item.get("serial")

serialnumlist.append(item)

devicetype = item.get("model")
ipaddress = item.get("lanIp")

# Print the resulting data to the screen
print ('{0:20}   {1:20}   {2:20}{3:30}   {4:20}'.format(devicename,
macAddress, serialnum,ipaddress, devicetype))

for item in serialnumlist:
print ("\n*>>Querying Meraki for clients on Access
Points:" + item.get("name")+ " (" + item.get("serial") + ")")
print ('{0:20}{1:30}{2:16}{3:18}  {4:15}
{5:20}'.format("Client Name", "Description", "IP Address", "MAC Address",
"Sent KBytes", "Recv KBytes"))


config_url = "/devices/" + item.get("serial") +
"/clients?timespan=86400"
r = requests.get(base_url + config_url, headers=headers)
json_string = r.json()

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") + ")")
   # print ('{0:20}   {1:30}   {2:16}   {3:18}   {4:10}
{5:11}'.format("Hostname", "Description", "IP Address", "MAC Address","Sent
KBytes", "Recv KBytes"))

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

OUTPUT


Network:Room, ID:  N_631140260
Network:Ofce, ID:  N_639540739

***>>Querying Meraki for Access Points(Devices) on Network:N_639511050

Switch NameMAC Address

Serial Number

HA e0:cb:b:3e:95  Q2XD-USN6  172.16.172.

AB.  e0:cb:7:01:f4  Q2XD-G8Q8  172.16.172.


***>>Querying Meraki for clients on Access Points:HR (Q2XD-USN6)

Client NameMAC Address
Kiosk55:77:6a:5
Chphone.   44:91:60:a2:0

*>>Querying Meraki for clients on Access Points:DerbyRoom
(Q2XD--GQ8)

Client Name   MAC Address
iPhone  f0:98:9d:2c:   arphone
c8:21:58:79:b
it-min 

Re: [Tutor] Regarding Python api script

2018-12-05 Thread Alan Gauld via Tutor
CCing the list, please use Reply All when responding to the tutor list.


On 05/12/2018 03:44, Ravi Kumar wrote:
> Yes  thats right I want to extract the xml and store into database(SQL
> Server) and I will have to cteate a new table
>
> Here is the sample output I am getting similarly there bulk data which
> I want to store in database format is as shown below
>
>
> b'[{"deviceSerial":"Q2XD-333X-G8Q8","occurredAt":1537565.640085,"type":"802.11
> association","details":{"radio":"1","vap":"0","clientMac":"C8:21:6679:B6:16","channel":"44","rssi":"57","aid":"31645681"}},{"deviceSerial":"Q2XD-97gX-G8Q8","occurredAt":153765.700095,"type":"WPA
> deauthentication","details":{"radio":"1","vap":"0","clientMac":"C621:58:79:B6:16","aid":"316681"}},{"deviceSerial":"Q2XD-97gX-G8Q8","occurredAt":1563369.780085,"type":"WPA
> deauthentication","details":{"radio":"1","vap":"0","clientMac":"C8:21:58:9:B6:16","aid":"31645681"}},{"deviceSerial":"Q297JX-G8Q8"
>
>
> Please let me whether this can be formatted into json in the code

It looks to me like it is already in JSON format.

It is a list of objects.

The json module will let you extract the objects into Python data objects.


> and also any other tips or links to refer if available  to store in
> database

Do you know SQL?

If so using the Python DBAPI is very easy.

If not you will need to learn some basic SQL.

You can try reading the database topic in my Python tutorial(see below)
which is based on SQLite rather than SQL Server but should be 90%
compatible.


-- 
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-04 Thread Alan Gauld via Tutor
On 04/12/2018 01:31, Ravi Kumar wrote:
> My output api calls in python is in xml so I want the output to be in the
> database

I'm still not 100% clear but I think you are saying
that your API currently returns XML when you call it
from Python. And you want to extract the data from
the XML and store it in a SQL Server database.
Is that correct?

If so the sequence of events is:

1) Connect to web site and retrieve the data (It
   sounds like you can already do this bit)
2) Parse the XML into usable data - you need
   etree (or another parser) to do that
3) Store the data in the database. You need
   a DBAPI module to do that. You will need
   to know some SQL.

Also, for step 3 you will need to either create
a new database or use an existing one. Do you
already have a target database? Or do you need
to design and create the table(s)? That may
require more SQL.

> So is there a way where I can alter my code to get
> api responses in json format in python and then
> later move the output to  the database

The answer to that lies in the API.
Assuming it is not under your control then you need
to read the API documentation to see if there is an
option to change the output to JSON. If not you are
stuck with whatever the API gives you.

Before we can give you any more help you will need
to share some code with us. What have you got
so far?

-- 
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-04 Thread Ravi Kumar
My output api calls in python is in xml so I want the output to be in the
database

So is there a way where I can alter my code to get api responses in json
format in python and then later   move the output   to  the database

Basically at the end of it I want the all the data to be in database

Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-04 Thread Alan Gauld via Tutor
CCd the list, please use Reply All when responding to the tutor list.

On 04/12/2018 00:52, Ravi Kumar wrote:
> Thanks a lot! I was wondering is it easier to access JSON format into
> Sql Server from python rather than XML If so how do I format my output
> from xml to Json 
>
JSON is generally easier than XML using the json module.


However, I confess I don't know what you mean by

"...access JSON format into Sql Server from python..."

Is the JSON/XML put into the database before you access it?

Or are you trying to access data from JSON/XML and then
store it in the database?

Or are you just trying to store JSON/XML directly into the database?

I really don't understand what it is you are trying to do.


-- 

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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-03 Thread Alan Gauld via Tutor
On 03/12/2018 22:15, Ravi Kumar wrote:

> I have developed a python script to get api calls for meraki
> clientlogevents I am wanting the output of apicalls which is in xml format
> integrated to sql server management  studio how do i do that?

XML is such a flexible format that without seeing the
specific schema and data its near impossible to give
specific answers.

However, the etree module that comes with Python is
designed for parsing XML so if you take a look at
its documentation (and search for ElementTree
tutorials) you should get something.

There are other third party modules that are slightly
easier to use but provided its straightforward,
well formed XML, etree should be fine.

If you need to access the SQL Server database to
fetch the XML then the Python DBAPI has modules
that work with SQLserver. The ODBC one if nothing
else works!

-- 
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-03 Thread Steven D'Aprano
On Mon, Dec 03, 2018 at 04:15:51PM -0600, Ravi Kumar wrote:
> Hi,
> 
> I have developed a python script to get api calls for meraki
> clientlogevents I am wanting the output of apicalls which is in xml format
> integrated to sql server management  studio how do i do that?

What have you tried?


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Regarding Python api script

2018-12-03 Thread Ravi Kumar
Hi,

I have developed a python script to get api calls for meraki
clientlogevents I am wanting the output of apicalls which is in xml format
integrated to sql server management  studio how do i do that?

Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor