标签: conditional-statements

基于 window.location.href 的条件 URL 附加或重定向的 Javascript

我正在尝试制作一个书签,单击该书签时将检查当前选项卡/窗口的 URL,以查看它是否包含“char1”和/或“char2”(给定字符)。如果两个字符都存在,它将重定向到另一个 URL,对于另外两个字符,它将分别附加当前 URL。

我相信一定有一种比下面的更优雅的方式来说明这一点(到目前为止对我来说效果很好),但我对 Javascript 不太了解。我的(笨拙且重复的)工作代码(抱歉):

if (window.location.href.indexOf('char1') != -1 &&
    window.location.href.indexOf('char2') != -1)
{
    window.location="https://website.com/";
}
else if (window.location.href.indexOf('char1') != -1)
{
    window.location.assign(window.location.href += 'append1');
}
else if (window.location.href.indexOf('char2') != -1)
{
    window.location.assign(window.location.href += 'append2');
}
Run Code Online (Sandbox Code Playgroud)

完全符合我的需要,但是,嗯……至少可以说不太优雅。

有没有更简单的方法来做到这一点,也许使用变量或伪对象?或者更好的代码?

javascript url bookmarklet append conditional-statements

5
推荐指数
1
解决办法
1万
查看次数

python 线程 - “condition.wait”和“condition.notifyAll”如何工作

我有以下“消费者”代码:

    ....

    while 1:

        time.sleep(self.sleeptime)

        cond.acquire() #acquire the lock
        print currentThread(), "lock acquired"

        while itemq.isEmpty():
            cond.wait()

        itemq.consume()
        print currentThread(),"Consumed One Item"
        cond.release()
Run Code Online (Sandbox Code Playgroud)

以及以下生产者代码:

....     
while 1 :


           cond.acquire() #acquire the lock
           print currentThread(), "lock acquired"
           print currentThread(),"Produced One Item"
           itemq.produce()
           cond.notifyAll()
           cond.release()

           time.sleep(self.sleeptime)
Run Code Online (Sandbox Code Playgroud)

我正在与 1 个生产者和 2 个消费者一起运行该程序。我不知道会得到什么结果。生产者调用“notifyAll()”,因此我希望两个消费者都能从“等待”中醒来。我看到确实两个消费者都获得了锁,但只有第一个获得锁的消费者真正可以消费该项目。有人可以告诉我“等待”命令是如何工作的吗?如果两个线程都收到“notifyAll”,为什么只有一个线程可以消费?

谢谢,李

python multithreading wait conditional-statements

5
推荐指数
1
解决办法
2万
查看次数

SQL 条件列存在性

我是否可以以某种方式选择视图中存在的列,但如果不存在则忽略该列?

SELECT
    CASE
        WHEN EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyView' AND COLUMN_NAME = 'MyColumn')
            THEN MyView.MyColumn
        ELSE NULL
    END AS [Sometimes]
FROM
    MyView
Run Code Online (Sandbox Code Playgroud)

现在,它会返回“Msg 207 无效的列名”错误。

也许可以选择忽略该错误?

sql exists conditional-statements

5
推荐指数
1
解决办法
5082
查看次数

如何在 Python lambda 中表达条件执行?

我发现了什么:

《Dive in to Python》中,我了解了andand运算符的特殊性质,以及如何通过and-oror技巧使用布尔运算符的短路求值来更简洁地表达条件,该技巧与 C 中的三元运算符非常相似。

C:

result = condition ? a : b
Run Code Online (Sandbox Code Playgroud)

Python:

result = condition and a or b
Run Code Online (Sandbox Code Playgroud)

这似乎很方便,因为 lambda 函数在 Python 中仅限于单行函数,但它使用逻辑语法来表达控制流。

自 Python 2.5 以来,inline-if似乎已经作为一种更易读的语法来拯救 and-or 技巧:

result = a if condition else b
Run Code Online (Sandbox Code Playgroud)

所以我猜想这是可读性较差的 and-or-construct 的 pythonic 替代品。即使我想嵌套多个条件,它看起来仍然相当全面:

result = a if condition1 else b if condition2 else c
Run Code Online (Sandbox Code Playgroud)

但在一个充满不确定性的世界中,我经常发现自己编写一些类似这样的代码来访问 abc :

result = a and hasattr(a, 'b') and hasattr(a.b, 'c') and a.b.c …
Run Code Online (Sandbox Code Playgroud)

python lambda short-circuiting inline-if conditional-statements

5
推荐指数
1
解决办法
4981
查看次数

使用 ruby​​ 的 'case'/'when' 方法和 || 或 && 运算符等

我只是尝试运行一些看起来像这样的代码

def get_proj4(srid, type=nil)
  type.downcase! if type
  case type
  when nil || "epsg"
   open("http://spatialreference.org/ref/epsg/#{srid}/proj4/").read
  when "esri"
   open("http://spatialreference.org/ref/esri/#{srid}/proj4/").read
  end
end
Run Code Online (Sandbox Code Playgroud)

而且它运行不正常,每次都返回nil。将 括nil || "epsg"在括号中也不起作用

事实证明 ruby​​ 不允许我||在此使用运算符

现在我假设 ruby​​ 采用 case/when 方法并最终将其分解为一组看起来像这样的条件

x = type
  if x == (nil || "epsg")
    y = ...runs code...
  elsif x == "esri"
    y = ...
  end
x = nil
y
Run Code Online (Sandbox Code Playgroud)

但显然事实并非如此。这里发生了什么?

谢谢

ruby case andand conditional-statements or-operator

5
推荐指数
1
解决办法
931
查看次数

jinja2 模板中变量的范围

我正在编写基于 jinja2 模板的应用程序。我正在尝试编写一个逻辑来设置变量。

{% set last_item = none %}
{% for u in users %}
  {% if not u.username == user.username%}
    {% if  g.user.is_bestfriend(u) %}
      {% set last_item = 'true' %}
    {% endif %}
  {% endif %}
{% endfor %}

{{last_item}}
Run Code Online (Sandbox Code Playgroud)

但之后{% endfor %}last_item值再次设置为 none,而不是 true。有什么方法可以在 jinja2 模板中将其设置为 true 吗?

scope jinja2 conditional-statements

5
推荐指数
2
解决办法
3904
查看次数

快速条件函数返回

我想知道如何快速管理有条件回报。例如,我返回一个自定义 UICollectionViewCell,具体取决于调用哪个 collectionview 委托:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
      if (collectionView.isEqual(collectionView1)) {
         var cell = self.epgCollectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as Cell1
         return cell
      }
      else if (collectionView.isEqual(collectionView2)) {
        var cell = self.epgCollectionView.dequeueReusableCellWithReuseIdentifier("Cell2", forIndexPath: indexPath) as Cell2
        return cell
      }
}
Run Code Online (Sandbox Code Playgroud)

编译器说“函数中缺少 return 语句期望返回 UICollectionViewCell”,即使在这两种情况下我都返回一个单元格。

我解决了它添加

return UICollectionViewCell()
Run Code Online (Sandbox Code Playgroud)

在函数的底部,但我认为这不是正确的方法。

我知道我可以在第一个“if”上方声明单元格,修改它并在“if”之外的函数末尾返回它,但随后“dequeueReusableCellWithIdentifier”调用会挂起。

谢谢你们。

return function conditional-statements ios swift

5
推荐指数
1
解决办法
2万
查看次数

Cassandra CQL3 条件插入/更新

我有一个无序事件列表,我的任务是存储它们的第一次和最后一次出现。

我在 Cassandra 中有以下列族:

CREATE TABLE events (
   event_name TEXT,
   first_occurrence BIGINT,
   last_occurrence BIGINT,
   PRIMARY KEY (event_name)
);
Run Code Online (Sandbox Code Playgroud)

因此,如果我有一个名为“some_event”的事件,并且出现次数为 123456,那么我想要做的事情在 MySQL 术语中看起来像这样:

INSERT INTO events (event_name, first_occurence, last_occurence)
VALUES ('some_event', 123456, 123456)
ON DUPLICATE KEY UPDATE 
     first_occurrence = LEAST(first_occurrence, 12345), 
     last_occurrence = GREATEST(last_occurrence, 123456)
Run Code Online (Sandbox Code Playgroud)

我打算在 Cassandra 中使用轻量级事务来完成它,如下所示:

INSERT INTO events(event_name, first_occurrence, last_occurrence) VALUES ('some_event', 12345, 12345) IF NOT EXISTS;
UPDATE events SET first_occurrence = 123456 WHERE event_name='some_event' IF first_occurrence > 123456;
UPDATE events SET last_occurrence = 123456 WHERE event_name='some_event' IF …
Run Code Online (Sandbox Code Playgroud)

updates cassandra nosql conditional-statements cql3

5
推荐指数
1
解决办法
7185
查看次数

如何将 Python 列表理解与条件表达式结合使用

有一个字典,

dlist = [{'Bilbo' : 'Ian', 'Frodo' : 'Elijah'}, {'Bilbo' : 'Martin', 'Thorin' : 'Richard'}]
Run Code Online (Sandbox Code Playgroud)

然后让k = 'Frodo'

我想提取k对应的值(当它存在时)并制作一个列表。

我写

value_list = [dlist[i][k] for i in range(len(dlist)) if k in dlist[i] else "NOT PRESENT"]. 
Run Code Online (Sandbox Code Playgroud)

但电脑说else是错误的。我不知道为什么。

python list-comprehension conditional-statements

5
推荐指数
2
解决办法
2149
查看次数

null 的算术运算

LedgerId    AccountId   EntryType   Debit       Credit      
2               2           D       50000.00    NULL        
3               2           D       10000.00    NULL        
4               2           C       NULL        25000.00    
6               2           C       NULL        10000.00    
7               2           D       89000.00    NULL        
8               2           D       89000.00    NULL        
10              3           D       715871.00   NULL        
Run Code Online (Sandbox Code Playgroud)

以下查询计算Balance

Select Accounts.ID [AccountID],Name,AccountType [AccountType], SUM(Debit) - SUM(Credit) [Balance] FROM Accounts 
    join Dealers on Accounts.DealerId = Dealers.ID 
    join Ledger on Accounts.ID = Ledger.AccountId
    GROUP BY Accounts.ID, Name, AccountType
Run Code Online (Sandbox Code Playgroud)

它返回:

AccountID   Name    AccountType     Balance
2           Mateen  P               203000.00
3 …
Run Code Online (Sandbox Code Playgroud)

sql sql-server conditional-statements

5
推荐指数
1
解决办法
6464
查看次数