Create a Python Program to get User Input - input

I am to write a python code to the following:
Ask the user to enter the first name and store it in the first_name variable.
Ask the user to enter the second name and store it in the last_name variable.
Print the full name.
I have tried it repeatedly but it kept on giving errors.
first_name = input("Enter your first name: Ade ")
last_name = input("Enter your last name:Olu ")
full_name = first_name + last_name
print(full_name)
And the expected outcome is:
Ade Olu

Related

Conditional Doobie query with Option field

I have following
case class Request(name:Option[String], age: Option[Int], address: Option[List[String]])
And I want to construct a query like this the conditions should apply if and only if the field is defined:
val req = Request(Some("abc"), Some(27), Some(List["add1", "add2"])
select name, age, email from user where name = "abc" AND age = 27 AND address in("add1", "add2");
I went through doobies documentation and found about fragments which allow me to do the following.
val baseSql: Fragment = sql"select name, age, email from user";
val nameFilter: Option[Fragment] = name.map(x => fr" name = $x")
val ageFilter: Option[Fragment] = age.map(x => fr" age = $x")
val addressFilter: Option[Fragment] = address.map(x => fr " address IN ( " ++ x.map(y => fr "$y").intercalate(fr",") ++ fr" )"
val q = baseSql ++ whereAndOpt(nameFilter, ageFilter, addressFilter)
from my understanding the query should look like this if all the fields are defined:
select name, age, email from user where name = "abc" AND age = 27 AND address in("add1","add2");
but the query looks like this:
select name, age, email from user where name = ? AND age = ? AND address in(?);
What is wrong here I am not able to find that.
Thanks in advance !!!!
Everything is fine.
Doobie prevents SQL injections by SQL functionality where you use ? in your query (parametrized query), and then pass the values that database should put into the consecutive ? arguments.
Think like this: if someone posted name = "''; DROP table users; SELECT 1". Then you'd end up with
select name, age, email from user where name = ''; DROP table users; SELECT 1
which could be a problem.
Since the database is inserting arguments for you, it can do it after the parsing of a raw text, when such injection is impossible. This functionality is used not only by Doobie but by virtually every modern library or framework that let you talk to database at level higher than plain driver.
So what you see is a parametrized query in the way that database will see it, you just don't see the parameters that will be passed to it.

ActiveRecord (SQL) query multiple columns only if search string not empty

Using a PG database filled with registered voters.
Trying to set it up so I can search by first name, last name, zip or city. I want to be able to find all voters that match all of the entered params, but having trouble dealing with empty search fields.
where("zip LIKE ? OR city LIKE ? OR last_name LIKE ? OR first_name LIKE ?",
"#{params[:zip]}","#{params[:city]}","#{params[:last_name]}","#{params[:first_name]}")
Is there a better way to build it out so that it matches ALL entered parameters, but ignores empty string parameters? Right now if I enter a first and last name 'John Smith' I will get 'John Jones' and 'Jane Smith'.
This can do the trick:
attrs_name_to_search = %w( zip city last_name first_name )
fitlered_params = params.slice(*attrs_name_to_search).select { |_,v| v.present? }
sql_cond = filtered_params.map { |k,_| "#{k} LIKE :#{k}" }.join(' OR ')
YourModel.where(sql_cond, filtered_params)
But it should return all the records if no zip/city/last_name/first_name is given.

Rails with Devise: Set Unique Username from First Name

In my app I'm trying to automatically populate a username column from the first_name field that I already have tied in to my Devise login system. In theory, it should just be the user's first_name if they are the only one with that name, but it should be something like "Mal the 4th" or "Jayne the 3rd" if there are other users with that first name already.
So far, in googling and consulting other SO posts (like this one) I have this basic structure in my registrations_controller:
before_create :set_username
private
def set_username
#users = User.where(first_name == self.first_name)
same_first_name_array = []
#users.each do |u|
same_first_name_array << u.first_name
end
if same_first_name_array.size = 0
self.username = first_name
else
self.username = first_name + " the " + ordinalize(same_first_name_array.size + 1)
end
end
But I'm struggling to fill in the blanks. So far it looks like the best way to do it is to do an if statement that checks if that first_name is unique and, if it's not to ordinalize some kind of count, but please let me know if there's a better or "more Ruby" way. Any help getting this to work would be appreciated!
I think your way is correct, however can be optmized definately, you can avoid the step in which you create a array and populate all the same names.
Also, try to see if you can create a index on the first name column, this will optimise the query.
self.username already points to name user has entered, you need to change it only if it has multiple occurrences so no need of if / else. change it only if there are multiple occurrence.
You can rewrite it like below
before_create :set_username
private
def set_username
#users = User.where(first_name == self.first_name)
self.username = #users.count.eql?(0) ? self.first_name : first_name + " the " + ordinalize(#users.count + 1)
end
Note: having user names likes this will create performance issues when your application scales and has large number of users.

concatening rows in a rails style

Suppose I want to check if some string appears as name-surname in the concatenation of two rows name and surname of a table. How do I write valid this sql in a rails style ? And is these syntaxes correct ?
SELECT (name + '-'+ surname) FROM table1 where (name + '-'+ surname = string)
table.select(:name+'-'+:surname).where((:name+'-'+:surname) == string)
I am not sure if I am understanding your question correctly, but I think this is what you are wanting. For the following string variable,
string = "John - Doe"
you want to pull a record like this from the User table
id | name | sur_name
1 | John | Doe
If this is what you want, you can actually massage your string variable like this
parsed_string = string.split('-')
name = parsed_string[0].strip # strip to remove white spaces
sur_name = parse_string[1].strip
Then, you can run the following code to get what you want:
users = User.where(:name => name, :surname => sur_name)
Hope this answers your question.

Why does the javascript snippet below not assign to LHS?

This is my first attempt at using Pentaho's Spoon.
Input file: CSV as follows
last_name, first_name
, Nod
zanie, Rod
anie, Slob
, Vod
meanie, Dod
Transformation: Where last_name is empty, set it to first_name
Hence the transformation script is
if ( last_name == null ){
last_name = first_name;
}
Output file: Coalescent.xml
The trouble is that above replacement simply does not happen. Why?
you need to specify the field in the grid at the bottom and specify replace=Y if you want to replace the last_name field back into the stream.
Did you use a Modified JavaScript Value for your transformation? Try to add
return last_name