SQLAlchemy Error when filtering - python

I'm using using SQLAlchemy to fiter but getting an error:
user = session.query.filter(User.id == 99).one()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'filter'
Does someone know how to filter because on SQLAlchemy Page, I saw this:
query = session.query(User).filter

query is a function, you need to pass in the User class to call it:
user = session.query(User).filter(User.id == 99).one()
^^^^^^
SQLAlchemy cannot divine from the filter alone what type of object you want returned otherwise.

Related

How to get namespace details for ecs

I want to print namespaces for my ecs-client. When I am using print(client.user_info.whoami()) I am getting the output. But when I am executing the below code I am getting attribute error.
from ecsclient.client import Client
from ecsclient.common.multitenancy import namespace
client = Client('3',
username='root',
password='password',
token_endpoint='https://abc.xyz.com:4443/login',
ecs_endpoint='https://abc.xyz.com:4443')
print(client.namespace.get_namespaces())
Error:
Traceback (most recent call last):
File "test.py", line 12, in <module>
print(client.namespace.get_namespaces())
AttributeError: 'Namespace' object has no attribute 'get_namespaces'
instead of using print(client.namespace.get_namespaces()) I used print(client.namespace.list()) and got the list of namespaces

Twitter API user model object not returning all of the fields documented

>>> potus = api.GetUser(screen_name='potus')
>>> potus['created_at']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'User' object has no attribute '__getitem__'
>>> potus
User(ID=822215679726100480, ScreenName=POTUS)
I'm trying to get a basic little script to do analysis of some twitter users, but the api doesn't seem to be returning all of the fields.
This is the python api library I am using:
https://github.com/bear/python-twitter.git

python boto 'NoneType' object has no attribute 'stop_isntances'

i am learning python boto module and i am trying to stop a running instance .
import boto.ec2
conn = boto.ec2.connect_to_region("us-west-2")
conn.stop_instances(instance_ids=['i-0aa5ce441ef7e0e2a'])
but i am getting error which says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'stop_instances'
i gave AWS_access keys for boto.
can anyone please help me to fix this error ?
As #kindall points out, your conn object is not initialized (it's NoneType). I also see that you're using boto in the example, but I thought that I would provide an example of this using boto3:
import boto3
boto3_session = boto3.Session(profile=some_profile_you_configured)
boto3_client = boto3_session.client('ec2', region_name='us-west-2')
response = boto3_client.stop_instances(InstanceIds=['i-0aa5ce441ef7e0e2a'])

Type error in python 2.7

Traceback (most recent call last):
File "C:\Python27\meanshift.py", line 15, in <module>
roi = frame[r:r+h, c:c+w],
TypeError: 'NoneType' object has no attribute '__getitem__'
Please help me to resolve this error, I am trying to make a region of interest on the first frame to track an object.
Usually I get this kind of errors if I forgot to put a return statement at the end of a function:
def create_array(n):
a = np.array((n,5))
b = create_array(10)
print b[5,1]
Without an explicit return a at the end of the function, it will return None.

Django ManyToManyField error when trying to pull values from other models

I'm trying to recreate Groupon using purely django and have having trouble with the below. This is my below idea
A Merchant can start a Campaign(which has a unique CampaignID)
A Customer(who has his own unique ID) can save a Campaign to be redeemed later
I created a model with 3 columns (CampaignID, CustomerID and a booleanfield with a default value of False)
class Customer_save(models.Model):
Customer_ID = models.ManyToManyField(Customer)
Campaign_ID = models.ManyToManyField(Campaigns)
Redeemed = models.BooleanField(default = False) #False denotes that it hasnt been redeemed yet
def __str__(self):
return self.Customer_ID.username,self.Campaign_ID.Campaign_desc
I'm trying to create a fake a row wanting to test if everything properly gets fed into the table. But i'm coming across errors. Could you please tell me where i'm going wrong?
Campaign_ID = '10001'
Customer_ID = 'C12345'
Customer_save.add(Customer_ID = Customer_ID,Campaign_ID = Campaign_ID)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Customer_wallet' has no attribute 'add'
>>> New = Customer_save(Customer_ID = Customer_ID, Campaign_ID `=Campaign_ID)`
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/kj/Desktop/projects/lib/python3.4/site-packages/django/db/models/base.py", line 480, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
>>> new = Customer_save.Customer_ID.create(Customer_ID = Customer_ID)
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'ReverseManyRelatedObjectsDescriptor' object has no attribute 'create'
TypeError: 'Customer_ID' is an invalid keyword argument for this function
You need to create the relevant records before assigning them to a many-to-many field:
customer = Customer(...)
customer.save()
campaign = Campaigns(...)
campaign.save()
customer_save = Customer_save(Redeemed=...)
customer_save.save()
customer_save.Customer_ID.add(customer)
customer_save.Campaign_ID.add(campaign)
More information on Django documentation: Many-to-many relationships.
PS. I would recommend you to choose better names for your fields and models and follow the formal style guide. They are a bit misleading and often such names lead to confusion, never mind that it is difficult for others to follow your code.

Categories