I would like to ask you because I am writing a unittest for a function. The problem is the function contains for loop and in for there is sth like:
test = s.mod.Something
q = s.query(test).filter(test.id == t.id).first()
When I use in my test sth like this s.query().filter().first().test_part = 'Z' then it works but I need to change my value in the loop and I am stuck. Do you have any suggestion how I can work with this kind of query in for loop? When I iterate sth in the loop I would like to see for:
first iteration s.query().filter().first().test_part = 'Z'
second iteratin s.query().filter().first().test_part = 'X'
Now I always see s.query().filter().first().test_part = 'Z'
my unittest looks like:
def test():
m = mock.Mock()
m.query().filter().first().test_part_x.side_effect = ['Z', 'X']
m.query().filter().first().test_part = m.query().filter().first().test_part_x()
...
Related
I got this class, and I want to test the call method to see if the class is working properly. I'm implementing a test for testing that class Cooling
works. The test function should verify that the call method returns the
correct results for given values of the arguments T and t (the input into the call method is T and t) How would you solve it? With test function i mean with def test_cooling(): My code is under:
class Cooling:
def __init__(self,h,Ts):
self.h=h
self.Ts=Ts
def __call__(self, T, t):
self.T = T
self.t = t
h = -self.h*(self.T - self.Ts)
self.T = (self.T) + self.t*h
return self.T, self.t
I have tried with
def test_Cooling(self):
c=Cooling()
expected=c.__call__(T,t)
self.assertTrue(expected, msg=None)
To test something, you need to understand what it does.
Cooling.__init__ takes two arguments. c = Cooling(h, Ts).
__call__ should not be called explicitly. It instead lets you call an object like a function: c(T, t). It returns the result of a calculation.
With that in mind, we can write a test to check that it returns what we expect.
You'd also need to test that h, Ts, T, and t are stored correctly. They're all just copies of the input except self.T.
class SampleTest(unittest.TestCase):
def test_Cooling(self):
h = 1
Ts = 2
T = 3
t = 4
newT = -1
expected = (newT,t)
cooling = Cooling(h, Ts)
self.assertEqual( cooling(T,t), expected )
self.assertEqual( cooling.h, h )
self.assertEqual( cooling.Ts, Ts )
self.assertEqual( cooling.t, t )
self.assertEqual( cooling.T, newT )
You'll have to pick a set of values for h, Ts, T, and t, and do the calculation for newT by hand. Or ask who wrote the function for some expected inputs and outputs.
Note that it returns t back unaltered, why is it returned at ll?
I'm new to LUA and I still haven't gotten the hang of how classes work in LUA, so my question
probably has a very simple answer. I'm trying to make a function that takes a CSV file and turns it into a lua table.
The input file would be something like this
PropertyKey1,Propertykey2,Propertykey3
object1property1,object1property2,object1property3
object2property1,object2property2,object2property3
object3property1,object3property2,object3property3
and I want the resulting lua table to look something like this
objects = {
{
PropertyKey1 = object1property1
PropertyKey2 = object1property2
PropertyKey3 = object1property3
}
{
PropertyKey1 = object2property1
PropertyKey2 = object2property2
PropertyKey3 = object2property3
}
{
PropertyKey1 = object3property1
PropertyKey2 = object3property2
PropertyKey3 = object3property3
}
}
this is what I have thus far
function loadcsv(path)
local OutTable = {}
local file = io.open(path, "r")
local linecount = 0
for line in file:lines() do
local data = {}
local headers = {}
local headerkey = 1
if linecount < 1 then
for val in line:gmatch("([^,]+),?") do
table.insert(headers, val)
end
else
for word in line:gmatch("([^,]+),?") do
key = headers[headerkey]
data[headerkey] = word
headerkey = headerkey + 1
table.insert(OutTable, data)
end
end
linecount = linecount + 1
end
file:close()
return OutTable
end
The above code does not run. When I try to print any of the values, they come as nil.
The problem is this bit
key = headers[headerkey]
data[headerkey] = word
I wanted to use the values I stored in one table as keys on the second table, but it looks like since LUA only passes the references, that doesn't work.
I did a quick experiment to confirm it. I first set up 2 tables.
test = {}
test2 = {}
test[1]={"index"}
key = test[1]
key2 = "index"
First I tried assigning the value directly form the table
test2[test[1]] = "text"
print(test2.index) --This did not work
then I tried going trough another variable
test2[key] = "texto"
print(test2.index) --This did not work
I even tried using tostring()
key = tostring(test[1])
test2[key] = "texto"
print(test2.index) --This did not work
I wrote the string directly in the variable "key2" to confirm that I was using the right notation.
test2[key2] = "text"
print(test2.index) --This one worked
I read a bit on metatables, but I'm not fully clear on those. Would that be the simplest way to do what I'm trying to do, or is my approach flawed in some other way?
key = headers[headerkey]
key is not used so why assign a value to it?
data[headerkey] = word
headerkey is a numeric key. You start at 1 for each line and add 1 for each word in a line. So you end up with
data = {
[1] = "object1property1",
[2] = "object1property2",
[3] = "object1property3"
}
Instead of the intended
data = {
PropertyKey1 = "object1property1",
PropertyKey2 = "object1property2",
PropertyKey3 = "object1property3"
}
So you probably meant to write
local key = headers[headerkey]
data[key] = word
But you have to move headers out of the loop. Otherwise you'll end up with an empty table for line 1 resulting in key being nil which would cause Lua errors for using a nil table index.
The following line is called for every word
table.insert(OutTable, data)
You need to do this for every line!
Your code basically produces this output:
local tableA = {"object1property1", "object1property2", "object1property3"}
local tableB = {"object2property1", "object2property2", "object2property3"}
local tableC = {"object3property1", "object3property2", "object3property3"}
OutTable = {
tableA, tableA, tableA, tableB, tableB, tableB, tableC, tableC, tableC
}
I suggest you formulate your program in your first language and then translate it into Lua. This helps to avoid such errors.
Your problem is not related to metatables, classes or anything else mentioned. You simply used the wrong variable and messed up your inner loop.
THIS IS WHAT I coded :
def statement(ticker,statement):
if statement =='IS':
temp='income-statement'
elif statement =='BS':
temp='balance-sheet-statement'
else:
temp='cash-flow-statement'
df = tickers.temp(frequency='q')
return df
tickers=['AAPL','GOOG','TSLA']
I want the temp from the function to replace in the df = tickers.temp(frequency='q') and finally giving me the correct financial statement when i replace the tickers with the stock code. If anyone has similar suitable code , Please do post it below
I am not really sure what you want to achieve.
So i assume you have something like
tickers.income-statement()
tickers.balance-sheet-statement()
...
and you want to call the method according to the temp string in that function.
so
myfunc = getattr(tickers, temp)
should give you the desired function out of the string you achieved.
I stack on this error for a long time, I try to find some records by passing the where condition with variable. for some reason the query code that I wrote in python those not get the variable and return this error :
self._connection.handle_unread_result().
raise errors.InternalError("Unread result found").
mysql.connector.errors.InternalError: Unread result found.
and here is the code I am using to function to execute!
def unfollow_user(username, update):
if update == True:
get_list_of_not_following_back()
sql.cur.execute("SELECT user_id FROM not_following_back WHERE username = (%s) AND of_user =
(%s)",(username, current_user[0]))
unfollow_user_id = sql.cur.fetchone()
def unfollow_number_of_follower():
sql.cur.execute("SELECT username,user_id FROM not_following_back WHERE of_user =(%s)",
(current_user[0],))
list_of_user_to_unfollow = sql.cur.fetchmany(number_of_user_to_unfollow)
for each_user_to_unfollow in list_of_user_to_unfollow:
unfollow_user(each_user_to_unfollow[0], False)
I figured out the result to this problem all you need to do is defined your cur as
cur = conn.cursor(buffered=True)
I am not sure why but it work
I have a function defined as below to query the database table
def query_from_DB(obj, **filter):
DBSession = sessionmaker(bind=engine)
session = DBSession()
res = session.query(obj).filter_by(**filter)
session.close()
return [x for x in res]
I query the table using the request as below
query_from_DB(Router, sp_id="sp-10.1.10.149", connectivity="NO")
the above result returns the response from the DB correctly, but when I make a query using
query_from_DB(Router, sp_id!="sp-10.1.10.149", connectivity="NO")
i got an error
SyntaxError: non-keyword arg after keyword arg
What could be the possible changes I can make to get the result?
I don't believe you can use != on a keyword argument.
You can do connectivity="YES" or use the filter sqlalchemy function so you can pass == or !=, but then you won't be able to use keyword args. You'd have to pass a SQL expression like so...
res = session.query(obj).filter_by(connectivity != "NO")
This question may be helpful...
flask sqlalchemy querying a column with not equals
Did you simply try res = session.query(obj).filter_by(connectivity <> "NO") ?