我正在尝试使用less.js使用JavaScript动态修改LESS变量.不过,我似乎无法得到它一起工作modifyVars
,watch
或refresh
.
我在加载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
我尝试使用时得到 …
最近对于编程类,我们被赋予了用任何语言编写程序的赋值,给定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
是字符串.我的解决方案有效,但是我的教授认为使用少量goto
s比动态代码生成更好的解决方案(至少更容易阅读).
我的印象goto
总是一个糟糕的选择.为什么运行时评估动态生成的代码比选择更糟糕goto
呢?生成的代码简洁明了,对于给定的问题似乎相当有效.代码生成所依赖的唯一用户输入是n,检查它以确保它是预先的整数值.这应该yield
是唯一的紊乱.
我不是要求我的编程任务的解决方案,我只是想知道使用goto
过度动态代码评估的原因,反之亦然.
编辑: 澄清一下,赋值包括使用迭代器编写程序和使用递归编写另一个程序,因此迭代版本不一定意味着高效.
我熟悉修改集合的问题,同时用循环遍历它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) 我刚刚遇到了我认为类型转换的奇怪之处.我有类似以下代码:
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)
对我来说,这个作品的事实使得无法施展更加神秘.
我有一个继承自的类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>
.
我希望有人可以向我解释在这段代码中会发生什么坏事,这会导致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) 我有两个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)
吗?
我正在尝试在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) 我正在尝试编写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) 我遇到一个非常大的结果集只有返回一行的问题.
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个结果.这里发生了什么?
c# ×5
python ×3
.net ×2
loops ×2
algorithm ×1
boolean ×1
casting ×1
cherrypy ×1
closures ×1
collections ×1
css ×1
custom-tools ×1
database ×1
dictionary ×1
foreach ×1
goto ×1
grammar ×1
interface ×1
iterator ×1
javascript ×1
lepl ×1
less ×1
linq ×1
mapping ×1
parsing ×1
properties ×1
reflection ×1
resharper ×1
select ×1
session ×1
sqlalchemy ×1