Firestore cannot get path onsnapshot - python

I am listening to changes on the database for collection_group
I cannot access ref (which has path) of a DocumentSnapshot I keep getting the error:
AttributeError: 'DocumentSnapshot' object has no attribute 'ref'
Here is my code:
doc_ref = firestore_db.collection_group(u'collection_name')
doc_ref.on_snapshot(self.__get_snapshot(args))
This is my __get_snapshot method:
def __get_snapshot(self, args):
def on_snapshot(doc_snapshot, changes, read_time):
for doc in doc_snapshot: #crashes
print(u'Received document snapshot: {}'.format(doc.ref))
for change in changes:
if(change.type.name == "MODIFIED"):
print(change.document.ref) #crashes
print(change.document.get("field"))#this works fine
return on_snapshot

The API documentation for DocumentSnapshot says that the reference of the document can be found in its reference property. So you will want to use this: doc.reference.

Related

PyGithub - Can't set attribute error while trying to change default branch

I've written this code to change the default branch from "master" to "release".
from github import Github
g = Github("github token", verify=False, base_url="url to repo")
repo = g.get_repo("repo name")
repo.default_branch = 'release'
I am getting the following error.
repo.default_branch = 'release'
AttributeError: can't set attribute
I am the admin of that repository and I created the branch. I don't think this is an access issue. What am I doing incorrectly?
The default_branch attribute is a read-only attribute; if you want to change the default branch you need to use the edit method:
repo.edit(default_branch='release')

how to delete documents in batch in firestore using python

I've a collection named XYZ in my firestore. And there are 500 documents with different fields in it.
I have to delete multiple documents using a where clause from the collection.
cred = credentials.Certificate('XXXX')
app = firebase_admin.initialize_app(cred)
db = firestore.Client()
batch = db.batch()
doc_ref = db.collection('collection_name').where(u'month', '==', 07).get()
for doc in doc_ref:
batch.delete(doc)
batch.commit()
I tried this but ending up with an error
AttributeError: AttributeError: 'DocumentSnapshot' object has no attribute '_document_path'
Looking for help!
You are passing a DocumentSnapshot object to batch.delete(), which is not allowed. You must pass a DocumentReference object instead, which can be found in a property of a DocumentSnapshot.
batch.delete(doc.reference)

ec2.Instance' object is not iterable

Im trying to copy the specified ec2 tags to their respective volumes. the function is invoked when the instance state changed to 'running'. However I don't want the code to run on every instance--for the first version of the code, when it as invoked my a single instance, it ran on all instances. even those that were already tagged. I want to have it run only for the specific instances that are booting up.
with some changes, Im getting error: ec2.Instance' object is not iterable
im really new to python and not sure how to proceed. Any inputs from you bright minds?
----HERE IS MY LAMBDA CODE----
import boto3
import json
def lambda_handler(event, context):
# is_test = context.function_name == 'test' # this value is injected by SAM local
instance = boto3.resource('ec2').Instance(id=event["detail"]["instance-id"])
tags_to_use = ['Stack-Name', 'StackID']
for instance in instance:
tags = instance.tags
to_tag = [t for t in tags if t['Key'] in tags_to_use]
for vol in instance.volumes.all():
print(f"Tagging volume {vol.id} from instance {instance.id}")
vol.create_tags(Tags=to_tag)

Odoo - How to acess recordsets on web controller

I am using web controller in odoo 8 to make a REST API that will get some data and return values from the database. The problem is that I am not able to get the database from the builtin ORM.
I tried to call osv.pool.get() but gave me the error:
AttributeError: type object 'Model' has no attribute 'pool'
Odoo 8 apparently uses recordsets, but I can't use it too, and couldn't find anything usefull on docs.
How can I browse database data on web controller?
My code:
class TestWebService(http.Controller):
#http.route('/test', type='http', auth='none')
def test(self):
objects = osv.osv.pool.get("some_table")
# I need to get the objects from some_table and search them
return "Hello World"
Try Following
myobj = request.env['some.table']

Obtaining tags from AWS instances with boto

I'm trying to obtain tags from instances in my AWS account using Python's boto library.
While this snippet works correctly bringing all tags:
tags = e.get_all_tags()
for tag in tags:
print tag.name, tag.value
(e is an EC2 connection)
When I request tags from individual instances,
print vm.__dict__['tags']
or
print vm.tags
I'm getting an empty list (vm is actually an instance class).
The following code:
vm.__dict__['tags']['Name']
of course results in:
KeyError: 'Name'
My code was working until yesterday and suddenly I'm not able to get the tags from an instance.
Does anybody know whether there is a problem with AWS API?
You have to be sure that the 'Name' tag exists before accessing it. Try this:
import boto.ec2
conn=boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
for res in reservations:
for inst in res.instances:
if 'Name' in inst.tags:
print "%s (%s) [%s]" % (inst.tags['Name'], inst.id, inst.state)
else:
print "%s [%s]" % (inst.id, inst.state)
will print:
i-4e444444 [stopped]
Amazon Linux (i-4e333333) [running]
Try something like this:
import boto.ec2
conn = boto.ec2.connect_to_region('us-west-2')
# Find a specific instance, returns a list of Reservation objects
reservations = conn.get_all_instances(instance_ids=['i-xxxxxxxx'])
# Find the Instance object inside the reservation
instance = reservations[0].instances[0]
print(instance.tags)
You should see all tags associated with instance i-xxxxxxxx printed out.
For boto3 you will need to do this.
import boto3
ec2 = boto3.resource('ec2')
vpc = ec2.Vpc('<your vpc id goes here>')
instance_iterator = vpc.instances.all()
for instance in instance_iterator:
for tag in instance.tags:
print('Found instance id: ' + instance.id + '\ntag: ' + tag)
It turned out to be an error in my code. I did not consider the case of having one instance without the tag 'Name'.
There was one instance without the tag "Name" and my code was trying to get this tag from every instance.
When I ran this piece of code in an instance without the tag 'Name',
vm.__dict__['tags']['Name']
I got: KeyError: 'Name'. vm is a AWS instance.
With the instances that actually had this tag set, I didn't have any problem.
Thank you for your help and sorry for asking when it was only my own mistake.

Categories