I am working on a GUI in pyqt5, that using a database (sqlite3). First, I'm uploading the entire data into a QTableWidget. Then, depending on the user filter parameters (2 comboboxes and 2 free text lineEdits which are signaled by 1 button), the filtering function is called and the data is filtered. The user may do another filter afterwards, and the table will be filtered again according to the new request.
The filter function is called upon one of three conditions:
The first combobox text changed ("comboType")
The second combobox text changed ("comboMaterial")
The button "Filter Ac/AL" clicked ("pbFilter ALAc")
The question: the filtering is always initializes when the "comboType" text is changed only (since it's the first signal). When the user wants to filter according to the second or third conditions, the function can not be called and the filtering can not be conducted. The script is stuck until the first condition will occur (call the comboType.currentTextChanged.connect).
Is there any way to "jump" to the second or third condition when clicked?
### Apply first filter when ComboBox changed###
self.uiCoresDBWin.comboType.currentTextChanged.connect(lambda: updateCoresTable())
self.uiCoresDBWin.comboMaterial.currentTextChanged.connect(lambda: updateCoresTable())
self.uiCoresDBWin.pbFilterALAc.clicked.connect(lambda: updateCoresTable())
`your text`
def updateCoresTable():
db = sqlite3.connect("ferrite_cores_db.db")
cursor = db.cursor()
typeIndex = self.uiCoresDBWin.comboType.currentIndex()+1
typeData = self.uiCoresDBWin.comboType.currentText()
materialIndex = self.uiCoresDBWin.comboMaterial.currentIndex()
materialData = self.uiCoresDBWin.comboMaterial.currentText()
typedAc = self.uiCoresDBWin.leAe.text()
typedAL = self.uiCoresDBWin.leAL.text()
if typeIndex == 0 and materialIndex == 0 and typedAc == '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores")
elif typeIndex == 0 and materialIndex == 0 and typedAc == '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE AL >=?", (typedAL,))
elif typeIndex == 0 and materialIndex == 0 and typedAc != '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Aef >=?", (typedAc,))
elif typeIndex == 0 and materialIndex != 0 and typedAc != '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Aef >=? and AL >=?", (typedAc, typedAL))
elif typeIndex == 0 and materialIndex != 0 and typedAc == '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Material ==?", (materialData,))
elif typeIndex == 0 and materialIndex != 0 and typedAc == '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Material ==? and AL >=?", (materialData, typedAL))
elif typeIndex == 0 and materialIndex != 0 and typedAc != '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Material ==? and Aef >=?", (materialData, typedAc))
elif typeIndex == 0 and materialIndex != 0 and typedAc != '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Material ==? and Aef >=? and AL >=?", (materialData, typedAc, typedAL))
elif typeIndex != 0 and materialIndex == 0 and typedAc == '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type == ?", (typeData,))
elif typeIndex != 0 and materialIndex == 0 and typedAc == '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and AL >=?", (typeData, typedAL))
elif typeIndex != 0 and materialIndex == 0 and typedAc != '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and Aef >=?", (typeData, typedAc))
elif typeIndex != 0 and materialIndex == 0 and typedAc != '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and AL >=? and Aef >=?", (typeData, typedAL, typedAc))
elif typeIndex != 0 and materialIndex != 0 and typedAc == '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and Material ==?", (typeData, materialData))
elif typeIndex != 0 and materialIndex != 0 and typedAc == '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and Material ==? and AL >=?", (typeData, materialData, typedAL))
elif typeIndex != 0 and materialIndex != 0 and typedAc != '' and typedAL == '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and Material ==? and Aef >=?", (typeData, materialData, typedAc))
elif typeIndex != 0 and materialIndex != 0 and typedAc != '' and typedAL != '':
result = cursor.execute("SELECT * from ferrite_cores WHERE Type ==? and Material ==? and Aef >=? and AL >=?", (typeData, materialData, typedAc, typedAL))
self.uiCoresDBWin.CoresTab.setRowCount(0)
for row_number, row_data in enumerate(result):
self.uiCoresDBWin.CoresTab.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.uiCoresDBWin.CoresTab.setItem(row_number, column_number, QtWidgets.Q
Related
I have a table that I am filtering on. There is a four filters status, project, department, KPI
KPI is a boolean value. This filter has three value. True, False or both.
So, when the filter is true, it should return 'true' data, when the filter is 'false', return 'false' data; and when 'all' return both true and false data.
My code attached below, how can I enhance this code ? Can I write this with out if else conditions
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.IsKPI == kpidata) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
//For Non-KPI Tasks
}
else {
result = await _context.TaskMaster.Where(c => c.IsActive && (c.Status == request.Status || request.Status == "All") && (c.ProjectId == request.ProjectId || request.ProjectId == 0) && (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)).Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery().ToListAsync();
}
Revise the code, both queries are almost identical except c.IsKPI == kpidata part.
Few ways to remove the redundant:
Write the IQueryable query and append the .Where() if the condition meets. You are fine to append/extend the query before the query is materialized (via .ToList(), .First() etc.).
var result = new List<TaskMaster>();
bool kpidata;
if (request.IsKPI == 1) { kpidata = true; } else { kpidata = false; }
IQueryable<TaskMaster> query = _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0));
//For KPI Tasks
if (request.IsKPI == 1 || request.IsKPI == 0)
{
query = query.Where(c => c.IsKPI == kpidata);
}
result = await query.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
You can also migrate the if logic to the query too.
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata)
If !(request.IsKPI == 1 || request.IsKPI == 0) returns true, the c.IsKPI == kpidata part will be omitted.
result = await _context.TaskMaster
.Where(c => c.IsActive
&& (c.Status == request.Status || request.Status == "All")
&& (c.ProjectId == request.ProjectId || request.ProjectId == 0)
&& (c.AssignedTo.DepartmentId == request.DepartmentId || request.DepartmentId == 0)
&& (!(request.IsKPI == 1 || request.IsKPI == 0) || c.IsKPI == kpidata))
.Include(y => y.TaskActionLogs)
.Include(y => y.AssignedTo)
.Include(y => y.AssignedBy)
.Include(y => y.Project)
.AsSplitQuery()
.ToListAsync();
This
(request.IsKPI == 1 || request.IsKPI == 0)
can also be revised to:
(new List<int> { 0, 1 }).Contains(request.IsKPI)
I am trying to run the below code and getting the below error -
Error: Syntax: Missing comma or ) in argument list
Below is the complete code, I watned to create.
function tournamentWinner(competetions, results)
teams = Dict()
for i in range(1, length(results))
if (results[i] == 1) && (competetions[i][0] not in teams)
teams[competetions[i][0]] = 1
elseif (results[i] == 1) && (competetions[i][0] in teams)
teams[competetions[i][0]] += 1
elseif (results[i] == 0) && (competetions[i][1] not in teams)
teams[competetions[i][1]] = 1
elseif (results[i] == 0) && (competetions[i][1] in teams)
teams[competetions[i][1]] += 1
end
end
return findmax(teams)[2]
end
competetions = [["HTML", "Java"],["Java", "Python"], ["Python", "HTML"],["C#", "Python"],["Java", "C#"],["C#", "HTML"]]
results = [0, 1, 1, 1, 0, 1]
tournamentWinner(competetions, results)
The below code worked for me, replaced 'in', 'not in' with 'haskey', '!haskey' for dictionary
function tournamentWinner(competetions, results)
teams = Dict()
for i in range(1, length(results))
if (results[i] == 1) && !haskey(teams, competetions[i][1])
teams[competetions[i][1]] = 1
elseif (results[i] == 1) && haskey(teams, competetions[i][1])
teams[competetions[i][1]] += 1
elseif (results[i] == 0) && !haskey(teams, competetions[i][2])
teams[competetions[i][2]] = 1
elseif (results[i] == 0) && haskey(teams, competetions[i][2])
teams[competetions[i][2]] += 1
end
end
return findmax(teams)[2]
end
competetions = [["HTML", "Java"],["Java", "Python"], ["Python", "HTML"],["C#", "Python"],["Java", "C#"],["C#", "HTML"]]
results = [0, 1, 1, 1, 0, 1]
tournamentWinner(competetions, results)
Hi I am trying to do this query:
Select ProjInvoiceJour
where NOT (ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount && ProjInvoiceJour.CountryRegionID != "ES")
But I need to do it whit querybuilder:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('!%1',
strfmt("((%1.%2 == %3) && (%4.%5 != '%6'))",
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
any2int(ProjInvoiceType::OnAccount),
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
queryvalue("ES"))));
But the query has some error:
SELECT * FROM ProjInvoiceJour WHERE ((NOT (ProjInvoiceType = 255)))
Thanks
The law of De Morgan comes to rescue:
select ProjInvoiceJour
where !(ProjInvoiceJour.ProjInvoiceType == ProjInvoiceType::Onaccount &&
ProjInvoiceJour.CountryRegionID != 'ES')
is equivalent to:
select ProjInvoiceJour
where ProjInvoiceJour.ProjInvoiceType != ProjInvoiceType::Onaccount ||
ProjInvoiceJour.CountryRegionID == 'ES'
Or in a query:
qbds.addRange(fieldnum(ProjInvoiceJour, ProjInvoiceType)).value(strfmt('((%1.%2 != %3) || (%4.%5 == "%6"))',
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, ProjInvoiceType),
0+ProjInvoiceType::OnAccount,
tablestr(ProjInvoiceJour),
fieldstr(ProjInvoiceJour, CountryRegionID),
'ES'));
Hi i got question for the entity query. Please see my code
var list = from table in db.USER_BETTINGS
where table.UserID == UserId
&& table.UserID !="admin"
//&& table.WinningAfterRebate != 0m
&& table.BettingTransactionTime >= fromDate &&
table.BettingTransactionTime <= toDAte
//&& table.WinningAfterRebate !=0m
// orderby table.BettingTransactionNumber descending//table.BettingNumber descending//, table.BettingTransactionTime descending//
select table;
if (loteryNumber != 0)
{
list= list.Where(x => x.LotteryNumber == loteryNumber);
}
if (gameNum != 0)
{
list= list.Where(x => x.GameNumber == gameNum);
}
if (periodDate != "")
{
list= list.Where(x => x.PeriodDate == periodDate);
}
if (close.Equals("Y"))
{
list= list.Where(w => w.WinningAfterRebate != 0m);
}
else
{
list= list.Where(x => x.WinningAfterRebate == 0);
}
But the list not filtering , It return all record? Anyone got face this problem before?
You need some makeover in this code. You have unnecessarily used so many if condition, we need to get rid of that first. Change your query with the following.
var list =( from table in db.USER_BETTINGS
where table.UserID == UserId
&& table.UserID !="admin"
&& table.BettingTransactionTime >= fromDate
&& table.BettingTransactionTime <= toDAte
&& table.LotteryNumber == (loteryNumber != 0 ? loteryNumber : table.LotteryNumber)
&& table.GameNumber == (gameNum != 0 ? gameNum : table.GameNumber)
&& table.PeriodDate == (periodDate != string.Empty ? periodDate : table.PeriodDate )
&& (close.Equals("Y") ? table.WinningAfterRebate != 0 : table.WinningAfterRebate == 0)
).ToList();
return (from s in db.StudentMarks
where s.Class == Class && s.Year == Year // this line
orderby s.Year, s.ExamType
select new StudentAddMarks()
{
--Obj
}).ToList();
I am going to return an Object depending on the Class and Year params. I want to remove the where condition when the Class and Year parames are null or zero.
Any ideas please.
Just add a clause handling null or zero values as well:
where ((Class != null && Class != 0) ? s.Class == Class : true) &&
((Year != null && Year != 0) ? s.Year == Year : true)
The above code uses the shorthand if-then-else syntax, that works as follows:
value = (condition ? if_true : if_false);
// ...is equivalent to...
if (condition)
{
value = if_true;
}
else
{
value = if_false;
}
This should work:
where (s.Class == Class && s.Year == Year) || Class == null ||
Class == 0 || Year == null || Year == 0