How to obtain the typed field instances from a typed org.jooq.Table instance - jooq

I have a function that returns an org.jooq.Table<RecordN<.....>> is there a way to obtain typed field instances without having to specify the types again?
final Table<Record11<String, Long, String, String, String, String, String, Timestamp,
String, String, Timestamp>> upcoming =
tableUpcomingFor(i_app, i_req, sport_idName, minDateName);
At the moment I have to do something like:
final Field<Timestamp> minDate = upcoming.field(name("minDate"), Timestamp.class);
This seems redundant, I should be able to get the typed fields by position. Is there a way to do that?
I require multiple fields for use in an outer query that uses the table instance.
Any ideas how to do that?

Related

If i store index number fetched from db in variable & using in select from list by index, m getting err as expected string, int found-Robot Framework

enter image description here
select from list by index ${locator_var} ${inp_msge_type}
--getting error as expected string, int found
select from list by index ${locator_var} 7
-----not getting any error
${inp_msge_type}----contains 7 from DB query the result is stored in this variable, to avoid hard coding we need to do this
Is there any way to write
Do not add links to screenshots of code, or error messages, and format the code pieces accordingly - use the ` (tick) symbol to surround them.
The rant now behind us, your issue is that the keyword Select From List By Index expects the type of the index argument to be a string.
When you called it
Select From List By Index ${locator_var} 7
, that "7" is actually a string (though it looks like a number), because this is what the framework defaults to on any typed text. And so it works.
When you get the value from the DB, it is of the type that the DB stores it with; and probably the table schema says it is int. So now you pass an int to the keyword - and it fails.
The fix is simple - just cast (convert) the variable to a string type:
${inp_msge_type}= Convert To String ${inp_msge_type}
, and now you can call the keyword as you did before.

How to convert object series to string series?

df = pd.read_csv("06.Master.csv")
df["column(str)"]= df["column"].astype('str')
df["column(str)"].head()
'64382' in df["column(str)"]
64382 in df["column(str)"]
I try to convert this object series to string, but it doesn't work.
As you see, after applying #.astype('str'), all values are caught as an integer, not a string.
Even though I make a simple function that converts integer to string and use #.apply(function), the same as above.
What should I do?

Ampscript BuildRowsetFromString() fails on single item

I've been tasked with an ExactTarget task, which uses Ampscript. Trying to learn on the go here. See code snippet below:
%%[
Var #testString, #testOutput
Set #testString = Qwerty
Set #testOutput = BuildRowsetFromString(#testString,"~")
]%%
TestOutput:%%= v(#testOutput) =%%
The code works if the testString contains a ~, but when there is no ~ character in the string, the ouput is blank. Is this correct by design? Do I need to add a conditional to check for the presence of the ~ character?
That's the expected behavior. The BuildRowsetFromString() function alone isn't going to return any value when displayed, you're going to need to use Row() and Field() in order to pull the value out.
Using your example:
%%[
Var #testString, #testOutput
Set #testString = "Qwerty"
Set #testOutput = BuildRowsetFromString(#testString,"~")
]%%
RowCount: %%=RowCount(#testOutput)=%%
TestOutput: %%=v(#testOutput)=%%
The RowCount() function returns a value of 1, essentially saying it knows there's at least one 'row' in there. To display that one value, you'll need to wrap that value with Field() and Row():
TestOutput: %%=Field(Row(#testOutput,1),1)=%%
If you want to display other values in the string, say you were passing "Qwerty~Second~Third", you'll need to either change the number at the Row() function or perform a loop.
References
Using Loops
BuildRowsetFromString() Function

Get handles as a number in order to apply function and operate mathematicaly

The program asks for input in gui editbox as a value, then it takes this value and applies the equation to get the pressure. I haven't been able to do so and I heard from some classmates that matlab takes the input as a string and doesn't operate strings.
get(handles.spl,'String') this is how I get the value, I tried get(handles.spl,'Double') instead but it didn't work, also tried str2double.
I don't know what else to try, I'm also pretty new in programming.
I'd appreciate the help, thanks.
You are correct that the uicontrol String property returns...a string. So you'll need to convert it to a number using str2double.
u = uicontrol('style', 'edit', 'String', '42');
strvalue = get(u, 'String');
numvalue = str2double(strvalue);
% 42

how should i store a price in mongoose?

I'm using mongoose schemas for node.js along with express-validator (which has node-validator santiziations and validators).
What's a good way to store price for an item?
I currently have
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, price : Number
});
Price is optional, so I have:
if ( req.body.price ) {
req.sanitize('price').toFloat();
req.assert('price', 'Enter a price (number only)').isFloat();
}
express-validator gives me isNumeric (allows 0 padding), isDecimal, and isInt...I'd rather just convert to decimal and strip all characters, so I'm always inserting 42.00 into db.
I want to allow them to enter $42.00, $42, 42, 42.00 and just store 42.00. How can I accomplish this? and still validate that I'm seeing something resembling a number, for example if they enter 'abc' I want to throw an error back to the form using req.assert.
Also, I suppose currency will eventually become an issue...
Update, I found this post which says to store price as integer in cents, so 4200
https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use
I just need a way to convert 4200 to $42.00 when I call item.price and also sanitize and convert the input into 4200.
This is what I ended up doing...
I stored price as cents in database, so it is 4999 for 49.99 as described here: https://dba.stackexchange.com/questions/15729/storing-prices-in-sqlite-what-data-type-to-use
the getPrice will convert it back to readable format, so I can use item.price in my views w/o modifying it.
the setPrice converts it to cents.
model:
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, price : {type: Number, get: getPrice, set: setPrice }
});
function getPrice(num){
return (num/100).toFixed(2);
}
function setPrice(num){
return num*100;
}
I opted to only allow digits and decimal in price field, without $.
So they can enter 49, 49.99, 49.00, but not 49.0 or $49
validation using regex:
if ( req.body.price ) {
req.assert('price', 'Enter a price (numbers only)').regex(/^\d+(\.\d{2})?$/);
}
I wish there was a way to allow the $ because I think its a usability issue, just let the user enter it, but strip it off. I'm not sure how to do that and still validate that we have a price and not a bunch of letters for example.
Hint: The method described here is basically just another implementation of chovy's answer.
Workaround for Mongoose 3 & 4:
If you have trouble to define getters and setters directly in the schema, you could also use the schema.path() function to make this work:
var ItemSchema = new Schema({
name: String,
price: Number
});
// Getter
ItemSchema.path('price').get(function(num) {
return (num / 100).toFixed(2);
});
// Setter
ItemSchema.path('price').set(function(num) {
return num * 100;
});
A bit late but...
The answer of chovy almost worked for me – I just needed to add
{ toJSON: { getters: true }} as an options parameter in the schema declaration.
Example:
import mongoose from 'mongoose'
const productosSchema = new mongoose.Schema(
{
name: String,
price: {
type: Number,
get: v => (v/100).toFixed(2),
set: v => v*100
}
},
{
toJSON: { getters: true } //this right here
}
);
export default mongoose.model('productos', productosSchema)
This works on Mongoose 6.0.14.
References: https://mongoosejs.com/docs/api.html#document_Document-toJSON
Adds schema type "Currency" to mongoose for handling money. Strips out common characters automatically (",", "$" and alphabet chars)
https://github.com/paulcsmith/mongoose-currency
What it does:
Saves a String as an integer (by stripping non digits and multiplying by 100) to prevent rounding errors when performing calculations (See gotchas for details)
Strips out symbols from the beginning of strings (sometimes users include the currency symbol)
Strips out commas (sometimes users add in commas or copy paste values into forms, e.g. "1,000.50)
Only save from two digits past the decimal point ("$500.559" is converted to 50055 and doesn't round)
Strips [a-zA-Z] from strings
Pass in a string or a number. Numbers will be stored AS IS.
Assumes that if you set the value to an integer you have already done the conversion (e.g. 50000 = $500.00)
If a floating point number is passed in it will round it. (500.55 -> 501). Just pass in integers to be safe.
Hope it helps some1.
I've been researching for a while on this topic, because I want to store not only price, but version, which both may have trailing 0s that get chopped off when stored as a number. As far as I know, Mongoose/MongoDB can't save a number with trailing zeroes.
Unless you save the number as a string.
Aside from storing numbers in tens or thousands and dividing or parsing, you can also store it as a string. This means, you can always just print it out when you need to show "1.0" or "1.00" by just using the variable without any conversion. Due to JavaScript being untyped, you can still compare it to numbers (make sure it's on the left hand side). Var < 10, for example, will return the right evaluation, even when var is a string. If you're comparing two variables, you'd need to make sure that they're both numbers, though. When you need a number, you can multiply the string by one (var * 1 < var2 * 1), which will ensure that JavaScript treats the var as a number, although it will lose the trailing zeros.
On the one hand, storing it as a string means you need to do a conversion every time you want to use the variable as a number. On the other hand, you would presumably be doing a numeric conversion anyway (var / 100) every time you want to use a cents number as a dollar amount. This option would depend on how frequently you need to your value as a number. Also it may cause bigger bugs if you forget that your variable is a string than if you forget that your variable is in cents.
(However, it's a great fit for version numbers that would only ever be used for display and comparison.)
The numeral module will accomplish that:
http://numeraljs.com/
https://www.npmjs.com/package/numeral

Resources