How to shorter code for weather api? - api

How could I shorter this part of the code and instead of putting all options append weather icons for all possible situations?
if (desc == "clear sky")
{
$('div.clear').removeClass('hide');
} else if (desc == "broken clouds")
{
$('div.cloudy').removeClass('hide');
}
else if (desc == "few clouds")
{
$('div.cloudy').removeClass('hide');
}
and so on...
else {
$('#desc').text("now it's ");
}

You can use a switch/case statement which would look like this
switch(desc)
{
case "clear sky":
$('div.clear').removeClass('hide');
break;
case "broken clouds":
$('div.cloudy').removeClass('hide');
break;
case "few clouds":
$('div.cloudy').removeClass('hide');
break;
case default:
$('#desc').text("now it's ");
break;
}
An other option would be to make a dictionary where the key is the case and the value is a function to execute.

Related

Change color With Vuejs

I have this object taken from an API, i want to change the color when status change i tried to do this :
<b-badge :variant="variant">{{ $t(contract.status) }}</b-badge>
script:
computed: {
...mapGetters(["getTeammates", "isCompleted"]),
variant () {
if (status == "pending") {
return "warning";
} else if (status == "confirmed") {
return "success";
} else if (status == "waiting_for_approval"){
return "danger";
} else {
return "dark";
}
},
},
I don't know why it doesn't work,
the color is always dark.
status is not defined in the computed method variant.
Based on your code I guess it should be this.contract.status
variant () {
if (this.contract.status == "pending") {
return "warning";
} else if (this.contract.status == "confirmed") {
return "success";
} else if (this.contract.status == "waiting_for_approval"){
return "danger";
} else {
return "dark";
}
},
Bonus: If you want an advice, I would suggest to replace all those if with a switch statement.

awk variable assignment don't work under switch/case

I am writing a script that can create proxy rule from a csv file.
#!/usr/bin/env bash
awk -F', ' '
function head (soft, keyword) {
if (soft == "clash") {
switch (keyword) {
case "HOST":
rule_head = "- DOMAIN";
break;
case "KEYWORD":
rule_head = "- DOMAIN-KEYWORD";
break;
case "SUFFIX":
rule_head = "- DOMAIN-SUFFIX";
break;
case "IP-CIDR":
rule_head = "- IP-CIDR";
break;
}
} else
if (soft == "quant") {
switch (keyword) {
case "HOST":
rule_head = "HOST";
break;
case "KEYWORD":
rule_head = "HOST-KEYWORD";
break;
case "SUFFIX":
rule_head = "HOST-SUFFIX";
break;
case "IP-CIDR":
rule_head = "IP-CIDR";
break;
}
}
ruturn rule_head;
}
function tail (soft, keyword) {
if (soft == "clash") {
switch (keyword) {
case "REJECT":
rule_tail = "REJECT";
break;
case "DIRECT":
rule_tail = "DIRECT";
break;
case "jp_proxy":
rule_tail = "Japan_Tokyo";
break;
case "us_proxy":
rule_tail = "USA_Oregon";
break;
case "kr_proxy":
rule_tail = "Korea_Seoul";
break;
}
} else
if (soft == "quant") {
switch (keyword) {
case "REJECT":
rule_tail = "REJECT";
break;
case "DIRECT":
rule_tail = "DIRECT,no-resolve";
break;
case "jp_proxy":
rule_tail = "Japan - Tokyo";
break;
case "us_proxy":
rule_tail = "USA - Oregon";
break;
case "kr_proxy":
rule_tail = "Korea - Seoul";
break;
}
}
return rule_tail;
}
{
if (NR>2) {
printf "%s,%s,%s\n", head(clash, $2), $3 ,tail(clash, $4);
}
}
' Resources/exception.csv
and the file Resources/exception.csv
# group, type, basis, action, comment
china_list, KEYWORD, 360buy, DIRECT, No_comment
china_list, KEYWORD, baidu, DIRECT, No_comment
china_list, KEYWORD, bdstatic, DIRECT, No_comment
china_list, KEYWORD, bcebos, DIRECT, No_comment
proxy_list, KEYWORD, fbcdn, us_proxy, No_comment
proxy_list, KEYWORD, twitter, us_proxy, No_comment
proxy_list, KEYWORD, twimg, us_proxy, No_comment
and the shell output is
,360buy,
,baidu,
,bdstatic,
,bcebos,
,fbcdn,
,twitter,
,twimg,
The rule_head and rule_tail seems not working at all, I already tried to print the keyword inside function head or tail > if before switch/case, it can print the keyword correctly.
But when I tried to print the rule_head or rule_tail after the switch/case, it output noting.
Could you please try changing either(I am driving so haven't test it but should work if your code is not having any syntax issues)
head(clash, $2), $3 ,tail(clash, $4); ----> head("clash", $2), $3 ,tail("clash", $4);.
OR you could create a variable with awk -v value="clash" to keep it in variable format only. Then mention:
head(value, $2), $3 ,tail(value, $4);
#JoshuaLee. mentioning anything inside " double quotes means we are mentioniong value, if you want to have a variable and assign value to it use var="bla" or you want to pass directly anything without variable any value then use print "bla" for example I am mentioning here.

YADCF range_number - is it possible to add a preset select list to to/from range?

I want to add a select list to to/from fields of range_number so user can pick from set amounts to set range.
best option would be to use the custom function filtering filter_type: 'custom_func'
see showcase, first column , code sample and everything can be found on that page
{
column_number: 0,
filter_type: 'custom_func',
custom_func: myCustomFilterFunction,
...
(possible) custom func implementation
function myCustomFilterFunction(filterVal, columnVal) {
var found;
if (columnVal === '') {
return true;
}
switch (filterVal) {
case 'happy':
found = columnVal.search(/:-\]|:\)|Happy|JOY|:D/g);
break;
case 'sad':
found = columnVal.search(/:\(|Sad|:'\(/g);
break;
case 'angry':
found = columnVal.search(/!!!|Arr\.\.\./g);
break;
case 'lucky':
found = columnVal.search(/777|Bingo/g);
break;
case 'january':
found = columnVal.search(/01|Jan/g);
break;
default:
found = 1;
break;
}
if (found !== -1) {
return true;
}
return false;
}

GStreamer GstBusSyncHandler Stop, Pause and Play States

I use Gstreamer as base for playing videos and audio and so far its working fine. Now I want to know when media is stopped, paused, loaded or started playing and notify the GUI. Now the Messages keep repeating itself (see the attached image). Now in that situation, it will be flooding my GUI. I want to receive event only once (for example when media is stopped or pause or loaded).
The only Message that I get consistently (once) is End of Stream.
Is there a way to get the said message only once, when they happen?
Here is my current code
extern "C"
{
static GstBusSyncReply
bus_sync(GstBus* bus, GstMessage* message, gpointer user_data)
{
CustomData* data = reinterpret_cast<CustomData*>(user_data);
GstState old_state, new_state, pending;
gst_message_parse_state_changed(message, &old_state, &new_state, &pending);
g_print ("State set from %s to %s --- %s\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state), gst_element_state_get_name (pending));
switch(GST_MESSAGE_TYPE(message))
{
case GST_MESSAGE_STATE_CHANGED:
{
wxGStreamer* win = dynamic_cast<wxGStreamer*>(data->parent);
if(win)
{
win->SendMessage(wxMEDIASTATE_CHANGED);
}
if(data->state!=new_state)
{
//state changed, save it
data->state = new_state;
if(old_state==GST_STATE_NULL && new_state == GST_STATE_READY && pending== GST_STATE_PLAYING)
{
g_print ("***Loaded***\n");
}
else if(old_state==GST_STATE_PLAYING && new_state == GST_STATE_PAUSED && pending== GST_STATE_VOID_PENDING)
{
g_print ("***Paused***\n");
}
else if(old_state==GST_STATE_READY && new_state == GST_STATE_NULL && pending== GST_STATE_VOID_PENDING)
{
g_print ("***Stopped***\n");
}
else if(new_state == GST_STATE_PLAYING &&(old_state==GST_STATE_READY||old_state==GST_STATE_PAUSED) )
{
g_print ("***Playing***\n");
}
}
break;
}
}
}
}
Simply answered: you get more messages from GstBus other than play/stop states.
Implement a switch in your code and your if/else statements are called only once:
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
break;
case GST_MESSAGE_WARNING:
break;
case GST_MESSAGE_INFO:
break;
case GST_MESSAGE_EOS:
break;
case GST_MESSAGE_BUFFERING:
break;
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state,
&new_state, &pending_state);
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (__YOUR_PIPELINE_POINTER__)) {
// your code here
}
}
break;
}
// DON'T FORGET TO RETURN
return GST_BUS_DROP;
Hope, that helps ;-)

adding error codes in yii login

I am trying to add new custom error in my login, if user_status is '0' then it should give error, I am proceeding as follow :
UserIdentity.php
const ERROR_USERNAME_INACTIVE=68;
public function authenticate()
{
$user = Users::model()->findByAttributes(array('email'=>$this->username));
if ($user===null)
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if ($user->password !== md5($this->password))
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else if($user->user_status !='1')
{
$this->errorCode=self::ERROR_USERNAME_INACTIVE;
}
else
{
$this->errorCode=self::ERROR_NONE;
$this->_id = $user->id;
}
return !$this->errorCode;
}
In login.php
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username','No Such user is associated with us .');
break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
Now I am getting problem that when the user_Status is !=1 then it is giving the error correctly but also giving, password incorrect while the password is correct
You forgot to add a break to the last case statement. Without a break the default will be executed.
...
break;
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
...
Sorry, I was making a silly mistake, after switch case, i forget to put break statement so the next default statement was also executing.
case UserIdentity::ERROR_USERNAME_INACTIVE:
$this->addError('user_status','Sorry, this user is not activated yet');
**break;**
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;