Im looking for thoughts on solving what im trying to achieve.
Im looking to do a dns query in python say for instance;
import adns
c=adns.init()
c.synchronous("www.google.com", adns.rr.A)
Im looking to store the output;
(0, 'www.l.google.com', 1167604334, ('216.239.37.99', '216.239.37.104'))
in a list, but the list would need to be associated with teh domain name.
So www.google.com would need to be associated with the common name and ip addresses. So if I had a list of domains they are all printed with their relevant data.
Can you have a list inside another list? Abit like a database i guess?
yep, a dict will do
data = []
domain = "www.google.com"
data[domain] = c.synchronous(domain, adns.rr.A)
print data
Related
I'm trying to retrieve the list of a container content, using python and openstack lib.
after a succesful connection with :
conn = connection.Connection(
...)
and a succesful response to :
for cont in conn.object_store.containers():
print(cont.name)
I'd like to retrieve the objects list of one of the containers. I find this one, but it doesn't work :
for data in conn.get_container('container_name')[1]:
print ('{0}\t{1}\t{2}'.format(data['name'], data['bytes'], data['last_modified']))
Any better approach to advise ?
Thanks
Méric.
I'm brand new here and brand new to Python and programming in general. I wrote a simple script today that I'm pretty proud of as a new beginner. I used BS4 and Requests to scrape some data from a website. I put all of the data in dictionaries inside a list. The same key/value pairs exist for every list item. For simplicity, I'm left with something like this:
[{'country': 'us', 'state':'new york', 'people':50},{'country':'us', 'state':'california','people':30']}
Like I said, pretty simple, but then I can turn it into a Pandas dataframe and everything is organized with a few hundred different dictionaries inside the list. My next step is to do run this scrape every hour for 5 hours--and the only thing that changes is the value of the 'people' key. All of the sudden I'm not sure a list of lists of dictionaries (did I say that right?!) is a great idea. Plus, I really only need to get the updated values of 'people' from the webpage. Is this something I can realistically do with built in Python lists and dictionaries? I don't know much about databases, but I'm thinking that maybe SQLite might be good to use. I really only know about it in concept but haven't worked with it. Thoughts?
Ideally, after several scrapes, I would have easy access to the data to say, see 'people' in 'new york' over time. Or find at what time 'california' had the highest number of people. And then I could plot the data in 1000 different ways! I'd love any guidance or direction here. Thanks a bunch!
You could create a Python class, like this:
class StateStats:
def __init__(self, country, state, people):
self.country = country
self.state = state
self.people = people
def update():
# Do whatever your update script is here
# Except, update the value self.people when it changes
# Like this: self.people = newPeopleValueAsAVariable
And then create instances of it like this:
# For each country you have scraped, make a new instance of this class
# This assumes that the list you gathered is stored in a variable named my_list
state_stats_list = []
for dictionary in my_list:
state_stats_list.append(
StateStats(
dictionary['country'],
dictionary['state'],
dictionary['people']
)
)
# Or, instead, you can just create the class instances
# when you scrape the webpage, instead of creating a
# list and then creating another list of classes from that list
You could also use a database like SQLite, but I think this will be fine for your purpose. Hope this helps!
I'm a novice in programming. Faced with such a problem. Are monitoring servers using Zabbix. It has its own API. The challenge is through a script in Python to connect to the monitor server and get information about printers and their counters and put in the file. The output file should have the format:
name printer \tab counter printer
Like that:
HP1212 124512
I connect and receive data, but cannot record it in two columns using a '\t'.
My code:
`
from pyzabbix import ZabbixAPI
zapi = ZabbixAPI("http://*****/zabbix")
zapi.login("******", "*******")
item_name='Print_counter'
hosts = zapi.host.get( #get printers name
groupids=8,
output=['name'])
items = zapi.item.get( #get printers counter
groupids=8,
output=['lastvalue'],
filter={'name':item_name})`
I understand that the problem is likely trivial, but how to solve I don't know.
I edited my question:
If im use:
for host in hosts:
a = host['name']
print a
.. I get:
tpr001
tpr002
...
tpr020
it my printers.
If i use:
for item in items:
b = host['value']
print b
I get:
12456
34645
...
56468
It counters off my printers.
I want to group the output of my query like this:
tpr001 12456
tpr002 34645
... ...
tpr020 56468
I think you need something like this :
for host in hosts:
a=host['name']
for item in items:
b=item['lastvalue']
print a,'\t',b`
I do not know what exactly is given by your zapi.host.get and zapi.item.get, but your loops dont work, as you expect.
In your first loop a gets a new value in each cycle so you find the last value in it, if the loop ends. And because of your print command after the loop you see exact that value.
Maybe you should put second loop into the first like
for host in hosts:
a=host['name']
for item in items:
b=item['lastvalue']
print a,'\t',b`
But in this case you would combine each row from hosts with each row from items.
Maybe your items.get- command needs the name es filter, something like
for host in hosts:
a=host['name']
items = zapi.item.get( #get printers counter
groupids=8,
output=['lastvalue'],
filter={'name':a})
Maybe you even do not need to ask for hosts, because all your information are inside items
for item in items:
b=item['lastvalue']
a=item['name']
print a,'\t',b
Hope, this helps, but I think you should learn about basics in programming, if you want to go further (and it is easier to understand, if you give speaking names instead of a and b, so not only we better understand, what you expect
It sounds strange to me, that you want to rely on Output order of two different lists. But if so, you could try
for i in range(len(Hosts)):
host = Hosts[i]
item = Items[i]
a=host['name']
b=item['lastvalue']
print a,'\t',b
I'm using Python and "requests" to practice the use of API. I've had success with basic requests and parsing, but having difficulty with list comprehension for a more complex project.
I requested from a server and got a dictionary. From there, I used:
participant_search = (match1_request['participantIdentities'])
To convert the values of the participantIdentities key to get the following data:
[{'player':
{'summonerName': 'Crescent Bladex',
'matchHistoryUri': '/v1/stats/player_history/NA1/226413119',
'summonerId': 63523774,
'profileIcon': 870},
'participantId': 1},
My goal here is to combine the summonerId and participantId to one list. Which is easy normally, but the order of ParticipantIdentities is randomized. So the player I want information on will sometimes be 1st on the list, and other times third.
So I can't use the var = list[0] like how I would normally do.
I have access to summonerId, so I'm thinking I can search the list the summonerId, then somehow collect all the information around it. For instance, if I knew 63523774 then I could find the key for it. From here, is it possible to find the parent list of the key?
Any guidance would be appreciated.
Edit (Clarification):
Here's the data I'm working with: http://pastebin.com/spHk8VP0
At line 1691 is where participant the nested dictionary 'participantIdentities' is. From here, there are 10 dictionaries. These 10 dictionaries include two nested dictionaries, "player" and "participantId".
My goal is to search these 10 dictionaries for the one dictionary that has the summonerId. The summonerId is something I already know before I make this request to the server.
So I'm looking for some sort of "search" method, that goes beyond "true/false". A search method that, if a value is found within an object, the entire dictionary (key:value) is given.
Not sure if I properly understood you, but would this work?
for i in range(len(match1_request['participantIdentities'])):
if(match1_request['participantIdentities'][i]['summonerid'] == '63523774':
# do whatever you want with it.
i becomes the index you're searching for.
ds = match1_request['participantIdentities']
result_ = [d for d in ds if d["player"]["summonerId"] == 12345]
result = result_[0] if result_ else {}
See if it works for you.
You can use a dict comprehension to build a dict wich uses summonerIds as keys:
players_list = response['participantIdentities']
{p['player']['summonerId']: p['participantId'] for p in players_list}
I think what you are asking for is: "How do I get the stats for a given a summoner?"
You'll need a mapping of participantId to summonerId.
For example, would it be helpful to know this?
summoner[1] = 63523774
summoner[2] = 44610089
...
If so, then:
# This is probably what you are asking for:
summoner = {ident['participantId']: ident['player']['summonerId']
for ident in match1_request['participantIdentities']}
# Then you can do this:
summoner_stats = {summoner[p['participantId']]: p['stats']
for p in match1_request['participants']}
# And to lookup a particular summoner's stats:
print summoner_stats[44610089]
(ref: raw data you pasted)
Im working on a program using kivy and python, and i will be saving some data to a json file like say 'items.json'.
The thing is i intend to retrieve the data from the store and use them to form
a list of buttons in my app. here is an example.
store = JsonStore('items.json')
store.put('infinix', name = 'infinix', category = 'gadgets')
store.put('wrist watch', name = 'wrist watch', category = 'outfits')
store.put('t-shirt', name = 't-shirt', category = 'outfits')
this works well. But my problem is in retrieving the data.
i would like to get them in the same order i entered the data into the store.
for example if i do
store.keys()
i would like it to return
['infinix', 'wrist watch', 't-shirt']
which is the same order i entered the data.
currently whenever i try to retrieve the data, the order is mixed up.
is there a way to achieve what i need?
Any help is greatly appreciated.
The simplest option would seem to be just adding an extra storage key containing a list of your items in the correct order. You can then just check this first, and load them in that order.