Where is my syntax error? - python

I'm trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:
(reading_threshold =
int(request.POST['reading_threshold']))
I do not see anything that I can notice as wrong in that statement or the syntax in custom.py.
What is my mistake here?
A slightly sanitized version of the file reads:
from django.http import HttpResponse
def threshold_check(user, item, text = None):
if user.issuperuser():
if text == None:
return True
else:
return text
else:
if (user.status >= item.threshold and user.reading_threshold <=
item.threshold):
if text == None:
return True
else:
return text
else:
if text == None:
return False
else:
return ''
def threshold_check_required(item):
def outer_wrap(view_function):
def inner_wrap(request, *arguments, **keywords):
if request.user.issuperuser():
return view_function(request, *arguments, **keywords)
else:
if (request.user.status >= item.threshold and
request.user.reading_threshold <= item.threshold):
return view_function(request, *arguments, **keywords)
else:
return HttpResponse('')
return inner_wrap
return outer_wrap
def threshold_controls(request):
user = request.user
if user and user.status != None:
if request.method == 'POST':
try:
(reading_threshold =
int(request.POST['reading_threshold']))
except ValueError:
reading_threshold = user.reading_threshold
try:
(writing_threshold =
int(request.POST['writing_threshold']))
except ValueError:
writing_threshold = user.writing_threshold
writing_threshold = min(user.status, writing_threshold)
reading_threshold = min(writing_threshold,
reading_threshold)
user.reading_threshold = reading_threshhold
user.writing_threshold = writing_threshold
user.save()
return render_to_response('threshold_controls.html',
{
'user': user
})
else:
return render_to_response('threshold_controls_blank.html')

You're looking right at the error. Python isn't C; assignment is a statement, rather than an expression, so you can't parenthesize it.

Related

Unable to import Excel to database

I have a system whereby it allows to import the student attendance file into the system. However, it did not import it successfully. I tried to debug and i found out that when it goes to the importer.py function, it does not go to the "def _do_save(self, row)", hence that is the problem why it was not saved into database.
importer.py
class AttendanceImporter(CsvImporter):
field_names=["username", "mark"]
#it does not go to the method.
def _handle_row(self,row):
print("this")
if (not self._is_row_valid(row)):
return self._FAILED
username=row["username"]
if (self._is_username_exist(username)):
if (self._is_mark_exist(username)):
if (self._do_update(row)):
return self._UPDATED
else:
return self._FAILED
else:
if (self._do_save(row)):
return self._CREATED
else:
return self._FAILED
else:
return self._FAILED
def _is_row_valid(self, row):
for item in self.field_names:
if (len(row[item])==0):
return False
return True
def _is_username_exist(self, username):
return len(User.objects.filter(username=username))>0
print(username)
def _is_mark_exist(self, username):
user = User.objects.get(username=username)
return len(Attendance.objects.filter(user=user))>0
print(username)
def _do_save(self, row):
# create attendace mark
try:
attendance=Attendance()
user=User.objects.get(username=row["username"])
attendance=Attendance.objects.create(user=user, mark=row["mark"])
print("save?")
attendance.save()
except:
return False
return True
def _do_update(self, row):
# update attendance mark
try:
user=User.objects.get(username=row["username"])
attendance=Attendance.objects.get(user=user)
attendance.mark = row["mark"]
attendance.save()
except Exception as e:
print(e)
return False
return True
Views.py
#transaction.atomic
#csrf_exempt
def data_import(request, file_type):
# TODO: currently only support importing users, later can support importing groups
fields_required = None
if(file_type == "user"):
fields_required = "username, password, email, matric_number, fullname, groups"
elif (file_type == "attendance"):
fields_required = "username, mark"
if request.FILES:
successful = False
try:
im = None
if(file_type == "user"):
upload_file = request.FILES['user_file']
file_path = save_uploaded_file(request.FILES['file'], filename=generate_unique_file_name(extension="csv"),
filedir=USER_DATA_UPLOAD_PATH)
im = StudentImporter(source=open(file_path))
elif (file_type == "attendance"):
upload_file = request.FILES['attendance_file']
file_path = save_uploaded_file(upload_file, filename=generate_unique_file_name(extension="csv"),
filedir=USER_DATA_UPLOAD_PATH)
im = AttendanceImporter(source=open(file_path))
successful, result = im.import_to_database()
except Exception:
pass
if successful:
messages.info(request, "The import is successful!\n" + result)
else:
messages.warning(request, "The import is NOT successful, no data is imported!")
return HttpResponseRedirect(reverse("student_user_profile_list"))
return render(request,
"app-appglobal/import-data.html",
{'type': file_type, 'fields_required': fields_required})
Model.py:
class Attendance(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE)
mark=models.IntegerField(default=0)
class Meta:
ordering=['user']
def get_mark(self):
return self.mark
Urls
url(r'^student/attendance/$' ,views.data_import,{'file_type':'attendance'},name='attendance_import'),
CSVIMPORTER
class CsvImporter(object, metaclass=ABCMeta):
def __init__(self, source):
"""
:param source: a file object
:return:
"""
self.source=source
def import_to_database(self):
"""
:return: (successful:boolean,result:string)
"""
pass
example:

Django serializer's is_valid returning True even if validate returns False

I have a POST function like this:
def post(self, request):
try:
serializer = CCTDSPostSerializer(data=request.data)
print("serializer", serializer)
print("is valid", serializer.is_valid())
The Serializer is as follows, its not a model serializer for specific reasons.
class CCTDSPostSerializer(serializers.Serializer):
status = serializers.CharField()
transaction = serializers.CharField(allow_blank=True, allow_null=True)
comment = serializers.CharField(allow_blank=True, allow_null=True)
tds_id = serializers.ListField(child=serializers.IntegerField())
def check_tds_eligibility(self, data):
tds_ids = data.get('tds_id', None)
if tds_ids is not None:
tds_obj = TDS.objects.filter(id__in=tds_ids, status='open')
if tds_obj.count() == len(tds_ids):
return tds_ids
return None
def validate_status_transaction(self, obj):
status = obj.get('status', None)
transaction = obj.get('transaction', None)
if status == 'closed' and transaction is not None:
return True
elif status == 'rejected' and transaction is None:
return True
return False
def validate(self, obj):
validate_status_transaction = self.validate_status_transaction(obj)
tds_ids = self.check_tds_eligibility(obj)
if validate_status_transaction and tds_ids:
print("returning obj")
return obj
print("returning false")
return False
The data that I am passing it is:
{
"tds_id":[1],
"status":"closed",
"transaction":"ABC",
"comment":"Boom"
}
Now based on the conditions on the data present in the database, it comes to the statement print("returning false") i.e. it is returning False, but on the view side, the statement serializer.is_valid() gives the output as True
How come the validate function returns False and the is_valid returns True?
validate method returns validated date or raise error. Since your validate method doesnt raise any error is_valid() returns True. Change method like this to fix:
def validate(self, obj):
validate_status_transaction = self.validate_status_transaction(obj)
tds_ids = self.check_tds_eligibility(obj)
if validate_status_transaction and tds_ids:
print("returning obj")
return obj
print("returning false")
raise serializers.ValidationError("Some error")

Good way to writing unit test in python

I have simply 'to do' app in python 2.7 and I wrote for this some unit test. This is my first time with python unit test and I just wanna to know idea of unit test.
Someone can tell me whether I'm going in the right direction?
How to improve this tests?
How to check message in IndexError is correct? for this ("IndexError('Note doesn\'t exist')" or IndexError('Returned more then one entry') )
App:
# coding: utf-8
from __future__ import unicode_literals
from shutil import copyfile
import json
import os
DATABASE = 'notes_data/notes.json'
BOARDS = ['to do', 'in progress', 'done']
class NotesManagerMixin(object):
def count(self):
return len(self.notes)
def filter(self, *args, **kwargs):
result = self.notes
for key, value in kwargs.iteritems():
result = [
note for note in result
if getattr(note, key, None) == value or
note.message.startswith(str(value)) or
note.message.endswith(str(value))
]
return NotesQueryset(result)
def get(self, *args, **kwargs):
notes = self.filter(*args,**kwargs)
if notes.count() == 0:
raise IndexError('Note doesn\'t exist')
elif notes.count() == 1:
return notes[0]
else:
raise IndexError('Returned more then one entry')
def first(self):
return self.notes[0]
def last(self):
return self.notes[-1]
class NotesQueryset(NotesManagerMixin):
def __init__(self, notes):
self.notes = [note for note in notes]
def update(self, *args, **kwargs):
for note in self.notes:
for key, value in kwargs.items():
setattr(note, key, value)
note.save()
return self
def delete(self):
for note in self.notes:
note.delete()
return self
def __getitem__(self, idx):
return self.notes[idx]
def __str__(self):
return str(self.notes)
def __repr__(self):
return self.__str__()
class NotesManager(NotesManagerMixin):
def __init__(self):
self.notes = []
def __iter__(self):
return self.next()
def __generate_id(self):
"""
Funkcja pomocnicza do pobrania pewnej wolnej wartoĊ›ci indexu.
"""
try:
return max(note.id for note in self.notes) + 1
except ValueError:
return 1
def all(self):
return NotesQueryset(self.notes)
def add(self, idx, board, message):
self.notes.append(Note(idx=idx, board=board, message=message))
def create(self, board, message):
note = Note(
idx=self.__generate_id(),
board=board,
message=message
)
note.clean()
self.notes.append(note)
note.save()
return note
def next(self):
for note in self.notes:
yield note
def to_dict(self):
return [note.to_dict() for note in self.notes]
class Note(object):
objects = NotesManager()
def __init__(self, idx, board, message):
self.id = idx
self.board = board
self.message = message
def __str__(self):
return 'ID: {}, Board: {}, Message: {}'.format(
self.id,
self.board,
self.message
)
def __repr__(self):
return self.__str__()
def clean(self):
if not self.message:
raise ValueError('Message is required')
if self.board not in BOARDS:
raise ValueError('Board "{}" doesn\'t exists'.format(self.board))
if type(self.id) != int:
raise ValueError('Note id "{}" is invalid'.format(self.id))
def save(self):
for key, note in enumerate(self.objects):
if note.id == self.id:
self.objects.notes[key] = self
break
with open(DATABASE, 'w') as database_file:
json.dump(self.objects.to_dict(), database_file, indent=4)
return True
def delete(self):
for key, note in enumerate(self.objects.notes):
if note.id == self.id:
self.objects.notes.pop(key)
with open(DATABASE, 'w') as database_file:
json.dump(self.objects.to_dict(), database_file, indent=4)
def to_dict(self):
return {
'id': self.id,
'message': self.message,
'board': self.board
}
def load_initial_data():
with open(DATABASE, 'r') as database_file:
json_data = json.load(database_file, encoding='utf-8')
for item in json_data:
Note.objects.add(
idx=item['id'],
board=item['board'],
message=item['message'],
)
load_initial_data()
unit tests:
import unittest
from notes_manager_v2 import NotesQueryset, Note, load_initial_data, NotesManagerMixin
class TestNotesQueryset(unittest.TestCase):
def test_filter(self):
actual = Note.objects.all().filter(board='in progress') # get all notes with board 'in progress'
expected = []
for note in Note.objects.all():
if note.board == 'in progress':
expected.append(note)
self.assertItemsEqual(actual, expected)
def test_get(self):
actual = [Note.objects.all().get(id=1)] # get note with method get
expected = []
for note in Note.objects.all(): # search note with index 1
if note.id == 1:
expected.append(note)
self.assertEqual(actual, expected)
def test_get_fail_1(self):
self.assertRaises(IndexError, lambda:Note.objects.all().get(id=9868976)) # thos note dont exist should raise IndexError
def test_update(self):
from_board = 'to do'
to_board = 'done'
before_change = Note.objects.filter(board=from_board) # use filter method to get all notes with board 'to do'
actual = Note.objects.filter(board=from_board).update(board=to_board) # update all board
self.assertNotEqual(before_change, actual) # check for difference
notes = Note.objects.all()
for note in actual:
self.assertIn(note, notes) # check notes are updated
def test_delete(self):
to_delete = Note.objects.filter(id=2).delete() # find note with filter method and delete it
notes = Note.objects.all()
self.assertNotIn(to_delete, notes)
def test_create(self):
new_note = Note.objects.create(message='lorem ipsum', board='in progress') # create new note
notes = Note.objects.all()
self.assertIn(new_note, notes) #
if __name__ == '__main__':
unittest.main()
Have you looked at the documentation? See doctest. They are an easy way to integrate unit tests into python code. Another option is the unittest framework.

Recursion and return statements in python

I am trying to implement a 2-3 tree but I am having trouble with the find method.
This method given an int as parameter should return the node that contains the int.
The problem is that sometimes it works, sometimes it does't and I don't know why.
I have added a test print. For a particular int that I know for sure that is part of the tree, the code executes the print statement, meaning that it has found the node, but does not return this node. Instead it return False which is at the end of the code.
Can you help me solving this ?
def find(self,data,node=0): #return problem ???
if node==0:
a=self.root
else:
a=node
if a.data2==None:
if data==a.data: ### here is the problem
print("qwertyuiop") ### it does not execute the return statement
return a
elif data < a.data:
if a.left!=None:
return self.find(data,a.left)
elif data > a.data:
if a.right!=None:
return self.find(data,a.right)
else:
if a.data2!=None:
if (data==a.data or data==a.data2):
return a
elif data<a.data:
if a.left!=None:
return self.find(data,a.left)
elif (data>a.data and data<a.data2):
if a.middle!=None:
return self.find(data,a.middle)
elif data>a.data2:
if a.right!=None:
return self.find(data,a.right)
print("Not Found") ### function executes this print
return False
self.root is the root of the tree and is an object of the following class
class Node:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.data2 = None
self.data3 = None
self.left = left
self.right = right
self.middle = None
self.middle2 = None
Binary Search Tree:
class Nodeee:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
class BST:
def __init__(self, root=None):
self.c=[]
self.total=0
self.root = None
def parent(self,data,node=5):
def search(nodee,cc,data):
if data==cc.data:
return nodee
else:
if data<cc.data:
nodee=cc
return search(nodee,cc.left,data)
elif data>cc.data:
nodee=cc
return search(nodee,cc.right,data)
print("Parent Error")
return False
if node==self.root:
print("Root has no parent")
else:
a=self.root
c=self.root
return search(a,c,data)
def lookup(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data < a.data:
if a.left==None:
return a
else:
return self.lookup(data,a.left)
elif data > a.data:
if a.right==None:
return a
else:
return self.lookup(data,a.right)
def find(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data==a.data:
print("WTF")
return a
elif data < a.data:
if a.left!=None:
return self.find(data,a.left)
elif data > a.data:
if a.right!=None:
return self.find(data,a.right)
print("Not Found")
return False
def find2(self,data,node=0):
if node==0:
a=self.root
else:
a=node
if data==a.data:
return True
elif data < a.data:
return self.find2(data,a.left)
elif data > a.data:
return self.find2(data,a.right)
return False
def is_empty(self):
if self.root==None:
return True
def is_leaf(self,n):
if (n.left==None and n.right==None):
return True
return False
def delete(self):
self.root=None
def insert(self, data):
if self.root==None:
self.root=Nodeee(data)
self.total+=1
return True
else:
b=self.lookup(data)
if data < b.data:
b.left=Nodeee(data)
self.total+=1
return True
elif data > b.data:
b.right=Nodeee(data)
self.total+=1
return True
print("Insert Error !")
return False
def inorder_swap(self,data):
a=self.find(data)
b=a.right
while self.is_leaf(b)!=True:
if b.left!=None:
b=b.left
elif b.left==None:
b=b.right
temp=a.data
a.data=b.data
b.data=temp
def remove(self,data):
a=self.find(data)
if self.is_leaf(a)==True:
b=self.parent(data)
if b.left==a:
b.left=None
elif b.right==a:
b.right=None
elif self.is_leaf(a)==False:
if a.left==None:
b=self.parent(data)
if b.left==a:
b.left=b.left.right
elif b.right==a:
b.right=b.right.right
elif a.right==None:
b=self.parent(data)
if b.left==a:
b.left=b.left.left
elif b.right==a:
b.right=b.right.left
elif (a.left!=None and a.right!=None):
self.inorder_swap(data)
self.remove(data)
def inorder(self,node):
if node!=None:
self.inorder(node.left)
self.c.append(node.data)
self.inorder(node.right)
def inorder_print(self):
self.c=[]
self.inorder(self.root)
print("\nStart")
for x in range(len(self.c)):
print(self.c[x], end=",")
print("\nFinish\n")
a=BST()
print(a.insert(234)==True)
print(a.insert(13)==True)
print(a.insert(65)==True)
print(a.insert(658)==True)
print(a.insert(324)==True)
print(a.insert(86)==True)
print(a.insert(5)==True)
print(a.insert(76)==True)
print(a.insert(144)==True)
print(a.insert(546)==True)
print(a.insert(2344)==True)
print(a.insert(1213)==True)
print(a.insert(6345)==True)
print(a.insert(653348)==True)
print(a.insert(35324)==True)
print(a.insert(8463)==True)
print(a.insert(5555)==True)
print(a.insert(76539)==True)
print(a.insert(14499)==True)
print(a.insert(59999946)==True)
a.inorder_print()
a.remove(35324)
a.remove(1213)
a.remove(2344)
a.remove(144)
a.remove(5555)
a.remove(6345)
a.remove(59999946)
a.remove(76)
print(a.root.data)
a.inorder_print()
def inorder_swap(self,data):
a=self.find(data)
b=a.right
while self.is_leaf(b)!=True:
if b.left!=None:
b=b.left
elif b.left==None:
b=b.right
temp=a.data
a.data=b.data
b.data=temp
a here is the node containing the passed data. This method does nothing else than swapping a's data with some leaf's data (first one while finds), thereby distorting the tree order. A follow-up find on the same data therefore fails and returns False. Since your code has no error checks, this results in an AttributeError.
You probably want to move nodes around in inorder_swap. However you only assign to the local name b. If you want to change nodes, then you need to use b.left = or b.right =.
It might be that there are more problems, that I don't see right now.
Also your code has several style problems, some of them:
You have four functions doing the same: find, find2, lookup and search in parent.
Most of the naming is not informative or even confusing.
Lines like if a.right==None: should be written as if not a.right: (or maybe if a.right is None:).
Check the return value of functions and don't just assume they return a valid node if they might not (i.e. find might return False instead of a node). Or alternatively use exception handling.
If you have an if ... elif ... elif block you don't have to check the last possibility if it is sure to be true, for example:
if b.left!=None:
# something
elif b.left==None:
# something else
should be
if b.left:
# something
else:
# something else

AttributeError: 'bool' object has no attribute 'keys'

i am a newbie on this site and am also a newbie to programming, i'm trying to learn python using a book for beginners on python 3.1. I have come across an example that just won't work whatever I try, I have searched for faults in writing about 10 times still it seems exactly what I see in the book. This is the example:
the fridge class
#!/usr/bin/env
class Fridge:
"""methods:
has(food_name [, quantity])-chk if string is in the fridge
has_various(foods)-chk if enough food is in the fridge
add_one(food_name) -adds 1 food
add_many(food_dict)- adds dict to fridge
get_one(food_name)- take out 1 food
get_many(food_dict) - a dict out of fridge
get_ingred(food)- if passed an obj get the list of __ingredients__
"""
def __init__(self, items={}) :
if type(items) != type({}):
raise typeError("Fridge req a dict but was given %s" % type(items))
self.items=items
return
def __add_multi(self, food_name, quantity):
if (not food_name in self.items):
self.items[food_name]=0
self.items[food_name]=self.items[food_name]+quantity
def add_one(self, food_name):
if type(food_name) != type(""):
raise TypeError ("add_one requires a string givem a %s " % type(food_name))
else:
self.__add_multi(food_name, 1)
return True
def add_many(self, food_dict):
if type(food_dict) != type({}):
raise TypeError ("add_many requires a dict, got a %s" % type(food_dict))
for item in food_dict.keys() :
self.__add_multi(item, food_dict[item])
return
def has(self, food_name, quantity=1):
return self.has_varoius({food_name:quantity})
def has_various(self, foods):
try:
for food in foods.keys():
if self.items[food] < foods[food]:
return False
return True
except KeyError:
return Fasle
def __get_multi(self, food_name, quantity):
try:
if (self.items[food_name] is None) :
return False
if (quantity > self.items[food_name]):
return False
self.items[food_name] = self.items[food_name] - quantity
except KeyError:
return False
return quantity
def get_one(self, food_name):
if type(food_name) !=type(""):
raise TypeError("get_one requires a string and was given a %s" % type(food_name))
else:
result=self.__get_multi(food_name, 1)
return result
def get_many(self, food_dict):
if self.has_various(food_dict):
foods_removed={}
for item in food_dict.keys():
foods_removed[item]=self.__get_multi(item, food_dict[item])
return foods_removed
def get_ingredients(self, food):
try:
ingredients=self.get_many(food.__ingredients())
except AttributeError:
return False
if ingredients!=False:
return ingredients
the omelet class
#!/usr/bin/env python3.3
class Omelet:
def __init__(self, kind="cheese"):
self.set_kind(kind)
return
def __ingredients__(self):
return self.needed_ingredients
def get_kind(self):
return self.kind
def set_kind(self, kind):
possible_ingredients=self.__known_kinds(kind)
if possible_ingredients == False :
return False
else:
self.kind=kind
self.needed_ingredients= possible_ingredients
def set_new_kind(self, name, ingredients):
self.kind=name
self.needed_ingredients= ingredients
return
def __known_kinds(self, kind):
if kind == "cheese":
return {"eggs":2, "milk":1, "cheese":1}
elif kind == "mushroom":
return {"eggs":2, "milk":1, "cheese":1, "mushroom":2}
elif kind == "onion":
return {"eggs":2, "milk":1, "cheese":1, "onion":1}
else:
return False
def get_ingredients(self, fridge):
self.from_fridge= fridge.get_ingredients(self)
def mix(self):
for ingredient in self.from_fridge.keys():
print("mixing %d %s for the %s omelet" % (self.from_fridge["ingredient"], ingredient, self.kind))
self.mixed=True
def make(self):
if self.mixed == True:
print("Cooking the %s omelet!" % self.kind)
self.cooked = True
this is how i invoke the classes and what i do to use the classes and the error i get
>>> exec(open("/home/knoppix/test/fridge.py").read())
>>> exec(open("/home/knoppix/test/omelet.py").read())
>>> o=Omelet("cheese")
>>> f=Fridge({"cheese":5, "milk":4, "eggs":12})
>>> o.get_ingredients(f)
>>> o.mix()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 41, in mix
AttributeError: 'bool' object has no attribute 'keys'
Please excuse me if there is a typing fault in the code , it`s exactly what I found in the book!
def get_ingredients(self, fridge):
self.from_fridge= fridge.get_ingredients(self)
In this function, your fridge.get_ingredients() might be returning False.
So self.from_fridge has Boolean value which does not have keys() method.
You may want to add appropriate check in mix() method.
The function "__known_kinds(kind)" should preferably return {} to be consistent, instead of different object types although "None" is better than "False".
if kind == "cheese":
return {"eggs":2, "milk":1, "cheese":1}
elif kind == "mushroom":
return {"eggs":2, "milk":1, "cheese":1, "mushroom":2}
elif kind == "onion":
return {"eggs":2, "milk":1, "cheese":1, "onion":1}
else:
return {}
Then you only have to deal with dictionary type in mix() method. The else can also be dropped from mix() as exception will be raised if dict == {}.
def mix(self):
if self.from_fridge == {}:
raise IndexError("self.from_fridge returns Nothing")
for ingredient in self.from_fridge.keys():
....

Categories

Resources