Argument of type "str | None" cannot be assigned to parameter "__x" of type "ReadableBuffer | str" - flet

I am getting an error when I access the value of a TextField to perform a sum operation with that number.
My code is as follows:
txt_number = ft.TextField(value="1", text_align=ft.TextAlign(value="center"), width=100)
def minus_click(e):
txt_number.value = str(int(txt_number.value) - 1)
page.update()
the error I get in the console is the following:
Argument of type "str | None" cannot be assigned to parameter "__x" of type "ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc" in function "new"
  Type "str | None" cannot be assigned to type "ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc"
    Type "None" cannot be assigned to type "ReadableBuffer | str | SupportsInt | SupportsIndex | SupportsTrunc"
      Type "None" cannot be assigned to type "str"
      Type "None" cannot be assigned to type "ReadOnlyBuffer"
      Type "None" cannot be assigned to type "bytearray"
      Type "None" cannot be assigned to type "memoryview"
      Type "None" cannot be assigned to type "array[Any]"
      Type "None" cannot be assigned to type "mmap"
...
I tried the example from Flet's counter documentation and got the same error. I even copied and pasted just the example and it still sent that problem in the console.
I have already checked that there was no error when trying to cast between a str and an integer. I think it is a Flet error.

Related

Get constructor by value option in Elm (0.19.1)

I have some types:
type alias Type1 = { name : String }
type alias Type2 = { name : String, age : Int }
type S
= S1 Type1
| S2 Type2
So, when I want to build my info I can do the following:
a = S1 { name = "Name1" }
b = S2 { name = "Name2", age = 100 }
Now, I would like to get its constructor (S1 or S2) based on the info passed as parameter to the constructorByData function. Examples:
constructorByData a -- It should be returned `S1`
constructorByData b -- It should be returned `S2`
This is my aproach:
constructorByData : S -> (a -> S)
constructorByData s =
case s of
S1 _ -> S1
S2 _ -> S2
Please, note that the above code does not work. I get the following error:
-- TYPE MISMATCH ---------------------------------------------------------- REPL
Something is off with the 2nd branch of this `case` expression:
14| S2 _ -> S2
^^
This `S2` value is a:
Type2 -> S
But the type annotation on `constructorByData` says it should be:
a -> S
-- TYPE MISMATCH ---------------------------------------------------------- REPL
Something is off with the 1st branch of this `case` expression:
13| S1 _ -> S1
^^
This `S1` value is a:
Type1 -> S
But the type annotation on `constructorByData` says it should be:
a -> S
The main reason I want to do this is because I want to do the following by generalizing the message:
type Field
= Name
| Age
type Msg
= UpdateFieldString S Field String
| UpdateFieldInt S Field Int
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UpdateFieldString s field value -> updateFieldString model s field value
UpdateFieldInt s field value -> updateFieldInt model s field value
And is for this I need the constructor in updateField{String, Int} functions

What is the most idiomatic way of representing errors in F#

I'm working on F# project and I wonder what is the best practice to return domain error using Result type in F#. There are several ways of doing it which I consider:
Inherited exceptions
type DomainException(message) =
inherit Exception(message)
type ItemNotFoundException(item) =
inherit DomainException(sprintf "Item %s is not found" item)
let findItem item =
match item with
| Some x -> Ok x
| None -> Error(new ItemNotFoundException("someitem"))
Custom record type
type DomainError =
{ Name : string
Message : string }
let findItem item =
match item with
| Some x -> Ok x
| None ->
Error({ Name = "ItemNotFound"
Message = "Item someitem is not found" })
Discriminated union of record type
type DomainErrorTypes =
| ItemNotFoundError of DomainError
| ItemInvalidFormat of DomainError
let findItem item =
match item with
| Some x -> Ok x
| None ->
{ Name = "ItemNotFound"
Message = "Item someitem is not found" }
|> ItemNotFoundError
|> Error
So which way is more idiomatic and convenient to use? I also will be happy to see better options.
Typically it would be a discriminated union. Every error requires different details to accompany the message. For instance:
type DomainErrorTypes =
| ItemNotFound of ItemId
| FileNotFound of string
| InvalidFormat of format
| IncompatibleItems of Item * Item
| SQLError of code:int * message:string
| ...
You can also capture some exceptions (not necessarily all):
| ...
| Exception of exn

Finding index using switch case statement in javascript

I'm using Pentaho(ETL) tool to achieve the output using a javascript component which accepts javascript code to achieve the desired transformation.The following table is imported into pentaho from a .csv file(source file).
For example this is my table structure
+--------+--------+--------+
| RLD | MD | INC |
+--------+--------+--------+
| 0 | 3868 | 302024 |
| 53454 | 7699 | 203719 |
| 154508 | 932 | 47694 |
| 107547 | 36168 | 83592 |
I want to use a script which would give me the max_value and its index number, such that my output would look like
Output Table
+--------+--------+--------+-----------+-----------+
| RQD | MT | IZC | max_value | max_index |
+--------+--------+--------+-----------+-----------+
| 0 | 3868 | 302024 | 302024 | 3 |
| 53454 | 7699 | 203719 | 203719 | 3 |
| 154508 | 932 | 47694 | 154508 | 1 |
| 456 | 107547| 83592 | 107547 | 2 |
To get the max value from rows I have used
var max_value = Math.max(RQD,MT,IZC);
println(max_value);
I tried to get their index using the following script
var max_index = switch (Math.max(RQD,MT,IZC))
{
case "RQD":document.write("1")
case "MT":document.write("2")
case "MT":document.write("3")
default:document.write("0")
}
How can I get the desired result in the form of javascript data structure? Any help would be much appreciated.Thanks
var list = [
{RLD:0,
MD:3868,
INC:302024
},
{RLD:53454,
MD:7699,
INC:203719
},
{RLD:154508,
MD:932,
INC:47694
},
{RLD:107547,
MD:36168,
INC:83592
},
];
list = list.map(function(item){
var keys = Object.keys(item);
item.max_value = item[keys[0]];
item.max_index = '';
for(var i = 1, l = keys.length; i < l; i++) {
var key = keys[i];
var keyValue = item[key];
if (item.max_value < keyValue){
item.max_value = keyValue;
item.max_index = key;
}
}
return item;
})
There are several issues with your code, lets solve them!
Use breaks: you must use breaks in order to avoid the switch moving to cases below its match.
switch cases do not return a value like functions, you cannot use a switch return to define a variable, you need to define the variable inside the switch case.
Math.max does not return the name of its maximum variable, instead it returns the maximum number from its given parameters.
to solve this issue, i would not use a switch case with math.max to be honest, however to answer your question:
var tableArray = [RQD,MT,IZC];
var maxIndex = tableArray.indexOf(Math.max.apply(null, arr));
if(maxIndex > -1) document.write(maxIndex+1);
i used +1 because you have your index in the example table starting from 1 instead of 0.
the way the array is sorted should match the way the table is sorted per raw.
First of all you could not solve this problem with a switch statement.
In a javascript switch you should provide a value that is one of the followed case(s), otherwise the switch will go to the default if defined.
Your problem seems to be to find out the higher value of 3 columns and print out, the colums row by row adding a column with the max value and the index of the column where you found it.
So for example on the row:
1, RLD : 0
2, MD : 3868
3, INC : 302024
In this case the higher value is INC with the column index 3.
If you have just the variables with the number values, you could do nothing more than something like this:
function getMaxValueRow (RLD, MD, INC) {
var max_value = RLD;
var max_index = 1;
if (MD > max_value) {
max_value = MD;
max_index = 2;
}
if (INC > max_value) {
max_value = INC;
max_index = 3;
}
return [RLD, MD, INC, max_value, max_index];
}
You could return an object too like this:
retrun {
'RQD': RLD,
'MT': MD,
'IZC': INC,
'max_value': max_value,
'max_index': max_index
}

Selenium Date comparison example

I am trying to validate dates using Selenium. So my scenario is;
Scenario: Date validation
When I set field <field> to value <value>
And I save the form
Then I should see error message <message>
Examples:
| field | value | message |
| startDate | 01/01/2014 | End date should be greater than start date |
| endDate | 01/01/2014 | End date should be greater than start date |
My step method to populate field is generic as below;
#When("I set field <field> to value <value>")
public void populateField(#Named("field") String fieldName, #Named("value") String value) {
populateFieldValue(fieldName, value);
}
My question is since the step method is generic, how do I set the start date, then end date and display the error message in a single example line.
You can use a StringListConverter to pass lists of values to parameters
Simple example:
Scenario: Date validation
When I set field <field> to value <value>
Then I should see error message <message>
Examples:
| field | value | message |
| startDate | 01/01/2014 | End date should be greater than start date |
| endDate | 01/01/2014 | End date should be greater than start date |
| startDate,endDate | 01/01/2014, 01/01/2014 | display the error message in a single example line. |
And a test code:
#When("I set field <field> to value <value>")
public void xxxx(#Named("field") List<String> fields,
#Named("value") List<String> values){
System.out.println(">>>>>------------->>>>----------->>>>>");
for( int i = 0; i < fields.size(); i++ ){
String field = fields.get(i);
String value = values.get(i);
System.out.println(" --> Set field: " + field + " to value: " + value);
}
}
#Then("I should see error message <message>")
public void when(#Named("message") String message){
System.out.println(" -------> message = " + message);
}
The above test generates this results:
Running story main/resources/test.story
>>>>>------------->>>>----------->>>>>
--> Set field: startDate to value: 01/01/2014
-------> message = End date should be greater than start date
>>>>>------------->>>>----------->>>>>
--> Set field: endDate to value: 01/01/2014
-------> message = End date should be greater than start date
>>>>>------------->>>>----------->>>>>
--> Set field: startDate to value: 01/01/2014
--> Set field: endDate to value: 01/01/2014
-------> message = display the error message in a single example line.

org.springframework.jdbc.BadSqlGrammarException: bad SQL grammar

I am getting the following error in my code:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad
SQL grammar [insert into bulletins (date, name, subject, note, approved) values
(?, ?, ?, ?, ?)]; nested exception is com.mysql.jdbc.exceptions.MySQLSyntaxError
Exception: Unknown column 'date' in 'field list'
This line is in my Spring controller.
bulletinDAO.writeBulletin(bulletin);
The actual place in my DAO class where I'm trying to write using Hibernate.
public void writeBulletin(Bulletin bulletin) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(bulletin);
tx.commit();
} catch (Exception e) {
System.out.println(e.toString());
}
}
Here is my model class.
#Entity
#Table(name="login")
public class Bulletin {
#Id
#Column(name="id")
#GeneratedValue
private int id;
#Column(name="bulletin_date")
private String date;
#Column(name="name")
private String name;
#Column(name="subject")
private String subject;
#Column(name="note")
private String note;
#Column(name="approved")
private boolean approved;
// Getters and setters follow
}
Finally, here is the layout of the table.
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| bulletin_date | varchar(10) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| subject | varchar(50) | YES | | NULL | |
| note | varchar(2500) | YES | | NULL | |
| approved | tinyint(1) | YES | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
There must be something wrong with your getters and setters.
I would recommend changing the property name from date to bulletinDate. And then set & get it correctly...
#Column(name="bulletin_date")
private String bulletinDate;
public String getBulletinDate() {
return bulletinDate;
}
public void setBulletinDate(String bulletin_date) {
this.bulletinDate = bulletin_date;
}
Your issue is here
[insert into bulletins (date, name, subject, note, approved)]
Whereas you need bullletin_date.
From experience i needed to rebuild the project completely to ensure that right column name is referenced.
Clear your project cache and rebuild it.
Let me know how you go and i'll help further if it doesn't help.