小编Sar*_*els的帖子

使用JavaScript动态更改页面中的LESS变量

我正在尝试使用less.js使用JavaScript动态修改LESS变量.不过,我似乎无法得到它一起工作modifyVars,watchrefresh.

我在加载less.js之前尝试使用样式表链接标记less.modifyVars({'@bg': '#00ff00'}),但是后台没有发生任何变化.我使用的文件越少越简单:

@bg: #000;
body { background-color: @bg; }
Run Code Online (Sandbox Code Playgroud)

我还尝试将样式表链接标记放在less.js之后,如本问题所述,然后使用less.sheets.push($('#less-link')[0])调用modifyVars然后调用refresh.背景颜色没有变化.

我也尝试@bg: #000;在LESS文件中注释掉,然后设置它modifyVars.没有运气:我最终得到了一个Less::ParseError: variable @bg is undefined.

如何动态更改LESS变量并更新页面外观以反映这些更改?

编辑:嗯,这可能是一些Rails魔术的发生.当我加载我的less文件时,变量定义消失了,我看到的只是body { background-color: #000; }.当我切换到使用<style type="text/less">页面中嵌入的LESS块并使用此覆盖LESS JavaScript时,我能够更改变量less.Override('@bg', '#00ff00')并立即更改页面的背景颜色.感谢这个答案.现在我将使用<style>标签而不是使用less.js来尝试它<link>.

编辑:看起来更少 - 1.3.3.min.js想要加载link标签 - TypeError: Cannot read property 'href' of undefined我尝试使用时得到 …

javascript css less

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

使用goto与运行时代码评估

最近对于编程类,我们被赋予了用任何语言编写程序的赋值,给定n,将产生大小为n的数组p的所有可能的紊乱,使得对于所有i:0的p [i]!= i <= i <n.我们必须使用迭代器,例如.yield

示例:n = 3,[0,1,2]不是紊乱,但[2,0,1]和[1,2,0]一样.

我提出了一个可以工作的伪代码解决方案,但问题是它需要电源循环(即n个嵌套循环,其中n仅在运行时已知).为此,我在一个字符串中生成了Ruby代码中的n个嵌套循环,然后eval是字符串.我的解决方案有效,但是我的教授认为使用少量gotos比动态代码生成更好的解决方案(至少更容易阅读).

我的印象goto总是一个糟糕的选择.为什么运行时评估动态生成的代码比选择更糟糕goto呢?生成的代码简洁明了,对于给定的问题似乎相当有效.代码生成所依赖的唯一用户输入是n,检查它以确保它是预先的整数值.这应该yield是唯一的紊乱.

我不是要求我的编程任务的解决方案,我只是想知道使用goto过度动态代码评估的原因,反之亦然.

编辑: 澄清一下,赋值包括使用迭代器编写程序和使用递归编写另一个程序,因此迭代版本不一定意味着高效.

algorithm iterator loops goto

4
推荐指数
1
解决办法
436
查看次数

使用Linq生成要从另一个集合中删除的东西的集合

我熟悉修改集合的问题,同时用循环遍历它foreach(即"System.InvalidOperationException:Collection was modified").但是,当我使用Linq创建要从字典中删除的键列表,然后遍历我的新List时,我得到相同的异常对我没有意义.

之前的代码,抛出异常:

IEnumerable<Guid> keysToDelete = _outConnections.Where(
    pair => pair.Value < timeoutPoint
).Select(pair => pair.Key);

foreach (Guid key in keysToDelete)
{
    ...some stuff not dealing with keysToDelete...
    _outConnections.Remove(key);
}
Run Code Online (Sandbox Code Playgroud)

代码之后,工作:

List<Guid> keysToDelete = _outConnections.Where(
    pair => pair.Value < timeoutPoint
).Select(pair => pair.Key).ToList();

for (int i=keysToDelete.Count-1; i>=0; i--)
{
    Guid key = keysToDelete[i];
    ...some stuff not dealing with keysToDelete...
    _outConnections.Remove(key);
}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我觉得可能我的Linq查询并没有真正返回一个新的集合,而是原始集合的一些子集,因此它指责我keysToDelete在删除元素时修改集合_outConnections.

更新: 以下修复也有效,感谢Adam Robinson:

List<Guid> keysToDelete = _outConnections.Where(
    pair => pair.Value < timeoutPoint …
Run Code Online (Sandbox Code Playgroud)

c# linq collections loops

4
推荐指数
1
解决办法
896
查看次数

C#Type-casting oddity - 接口作为泛型类型

我刚刚遇到了我认为类型转换的奇怪之处.我有类似以下代码:

interface IMyClass { }

class MyClass: IMyClass { }

class Main
{
  void DoSomething(ICollection<IMyClass> theParameter) { }

  HashSet<MyClass> FillMyClassSet()
  {
    //Do stuff
  }

  void Main()
  {
    HashSet<MyClass> classSet = FillMyClassSet();
    DoSomething(classSet);
  }
}
Run Code Online (Sandbox Code Playgroud)

当它到达DoSomething(classSet)时,编译器会抱怨它无法将HashSet <MyClass>强制转换为ICollection <IMyClass>.这是为什么?HashSet实现了ICollection,MyClass实现了IMyClass,那么为什么不能使用强制转换?

顺便说一句,这并不难解决,认为它有点尴尬.

void Main()
{
  HashSet<MyClass> classSet = FillMyClassSet();
  HashSet<IMyClass> interfaceSet = new HashSet<IMyClass>();
  foreach(IMyClass item in classSet)
  {
    interfaceSet.Add(item);
  }
  DoSomething(interfaceSet);
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这个作品的事实使得无法施展更加神秘.

.net c# casting interface

4
推荐指数
1
解决办法
1656
查看次数

C#继承自Dictionary,迭代KeyValuePairs

我有一个继承自的类Dictionary<string, string>.在一个实例方法中,我想迭代所有KeyValuePair<string, string>的.我尝试过以下方法:

foreach (KeyValuePair<string, string> pair in base)
Run Code Online (Sandbox Code Playgroud)

但是这失败了以下错误:

在此上下文中使用关键字"base"无效

如何KeyValuePair<string, string>在派生自的类中的实例方法中迭代Dictionary<string, string>

编辑: 我发现我可以执行以下操作:

var enumerator = base.GetEnumerator();
while (enumerator.MoveNext())
{
    KeyValuePair<string, string> pair = enumerator.Current;
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然想知道是否有办法通过foreach循环来做到这一点.

编辑: 感谢有关不继承的建议Dictionary<string, string>.我正在实施System.Collections.IEnumerable, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, IDictionary<string, string>.

c# foreach dictionary

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

ReSharper中修改了闭包警告

我希望有人可以向我解释在这段代码中会发生什么坏事,这会导致ReSharper给出"访问修改后的封闭"警告:

bool result = true;

foreach (string key in keys.TakeWhile(key => result))
{
    result = result && ContainsKey(key);
}

return result;
Run Code Online (Sandbox Code Playgroud)

即使上面的代码看起来很安全,在其他"修改后的闭包"实例中会发生什么坏事?我经常在使用LINQ查询时看到这个警告,我倾向于忽略它,因为我不知道会出现什么问题.ReSharper尝试通过创建对我来说毫无意义的第二个变量来解决问题,例如它将foreach上面的行更改为:

bool result1 = result;
foreach (string key in keys.TakeWhile(key => result1))
Run Code Online (Sandbox Code Playgroud)

更新:在附注中,显然整个代码块可以转换为以下语句,这不会导致修改后的闭包警告:

return keys.Aggregate(
    true,
    (current, key) => current && ContainsKey(key)
);
Run Code Online (Sandbox Code Playgroud)

.net c# resharper closures

4
推荐指数
2
解决办法
2264
查看次数

C# - 将属性值从一个实例复制到另一个实例,不同的类

我有两个C#类,它们具有许多相同的属性(按名称和类型).我希望能够将实例中的所有非空值复制Defect到实例中DefectViewModel.我希望用反射来做,使用GetType().GetProperties().我尝试了以下方法:

var defect = new Defect();
var defectViewModel = new DefectViewModel();

PropertyInfo[] defectProperties = defect.GetType().GetProperties();
IEnumerable<string> viewModelPropertyNames =
    defectViewModel.GetType().GetProperties().Select(property => property.Name);

IEnumerable<PropertyInfo> propertiesToCopy =
    defectProperties.Where(defectProperty =>
        viewModelPropertyNames.Contains(defectProperty.Name)
    );

foreach (PropertyInfo defectProperty in propertiesToCopy)
{
    var defectValue = defectProperty.GetValue(defect, null) as string;
    if (null == defectValue)
    {
        continue;
    }
    // "System.Reflection.TargetException: Object does not match target type":
    defectProperty.SetValue(viewModel, defectValue, null);
}
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?我应该维护单独的Defect属性和DefectViewModel属性列表,以便我可以做到viewModelProperty.SetValue(viewModel, defectValue, null)吗?

编辑: 多亏了JordãoDave的 …

c# mapping reflection properties

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

用于用户身份验证的CherryPy自定义工具

我正在尝试在CherryPy控制器类中设置一种简单的方法来装饰方法,这样如果用户尚未进行身份验证,则会将用户重定向到登录页面.我打算做一个基本的Python装饰器,但这里的答案建议我使用CherryPy自定义工具代替.所以我试图这样做,但我不能让它工作.这就是我所拥有的:

def authenticate():
    user = cherrypy.session.get('user', None)
    if not user:
        raise cherrypy.HTTPRedirect('/?errMsg=Please%20log%20in%20first')

cherrypy.tools.authenticate = cherrypy.Tool('on_start_resource', authenticate)
Run Code Online (Sandbox Code Playgroud)

/home页面是一个应限制为经过身份验证的用户的页面,因此我有:

@cherrypy.expose
@cherrypy.tools.authenticate
def home(self, **kwargs):
    tmpl = TemplateDir.get_template('home.mako')
    return tmpl.render()
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试启动我的网站时出现此错误:

Traceback (most recent call last):
  File ".\example.py", line 3, in <module>
    from controller.main import Root
  File "C:\...\controller\main.py", line 9, in <module>
    class Root(BaseModule):
  File "C:\...\controller\main.py", line 19, in Root
    @cherrypy.tools.authenticate
  File "C:\Python26\lib\site-packages\cherrypy\_cptools.py", line 119, in
   __call__ % self._name)
TypeError: The 'authenticate' Tool does not accept positional arguments; you …
Run Code Online (Sandbox Code Playgroud)

python authentication session cherrypy custom-tools

4
推荐指数
1
解决办法
6405
查看次数

使用LEPL解析布尔搜索查询

我正在尝试编写LEPL语法来描述布尔搜索语言.这是我到目前为止:

from lepl import *
text = String() | Word()
tail = ~Lookahead('AND') & ~Lookahead('OR') & text
with DroppedSpace():
    andClause = (tail & Lookahead('AND') & Drop('AND') & tail)[1:] > tuple
    orClause = (tail & Lookahead('OR') & Drop('OR') & tail)[1:] > list
    andOrText = (andClause & Lookahead('OR') & Drop('OR') & tail)[1:] > list
    orAndText = (orClause & Lookahead('AND') & Drop('AND') & tail)[1:] > tuple
    oredAnds = (andClause & Lookahead('OR') & Drop('OR') & andClause)[1:] > list
    andedOrs = (orClause & …
Run Code Online (Sandbox Code Playgroud)

python grammar parsing boolean lepl

4
推荐指数
1
解决办法
1086
查看次数

SQLAlchemy - 当count()表示还有更多时,只返回一个结果

我遇到一个非常大的结果集只有返回一行的问题.

Session.query(TestSet).join(Instance).count()
>> 4283878
Session.query(TestSet).join(Instance).offset(0).limit(100).count()
>> 100
Session.query(TestSet).join(Instance).offset(0).limit(100).all()
>> [<model.testset.TestSet object at 0x043EC2F0>]
Run Code Online (Sandbox Code Playgroud)

也就是说,all只返回我的模型的一个实例,而不是100.现在,对于更奇怪的东西:

len(Session.query(TestSet).join(Instance).offset(0).limit(100).distinct().all())
>> 100
Run Code Online (Sandbox Code Playgroud)

因此,如果我distinct之前添加all,我会收到所有100个结果.这里发生了什么?

python database select sqlalchemy

4
推荐指数
1
解决办法
2433
查看次数