问题列表 - 第27167页

我怎样才能使这个简单的C#泛型工厂工作?

我有这个设计:

public interface IFactory<T> {
  T Create();
  T CreateWithSensibleDefaults();
}

public class AppleFactory : IFactory<Apple> { ... }
public class BananaFactory : IFactory<Banana> { ... }
// ...
Run Code Online (Sandbox Code Playgroud)

虚构的Apple,Banana这里不一定共享任何常见类型(object当然除此之外).

我不希望客户必须依赖特定的工厂,所以相反,你可以问FactoryManager一个新的类型.它有一个FactoryForType方法:

IFactory<T> FactoryForType<T>();
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用类似的方法调用适当的接口方法FactoryForType<Apple>().Create().到现在为止还挺好.

但是在实现级别存在问题:如何将这种映射从类型存储到IFactory<T>s?天真的答案是一个IDictionary<Type, IFactory<T>>,但这不起作用,因为没有类型协方差T(我使用C#3.5).我只是坚持IDictionary<Type, object>并且手动进行铸造?

c# generics factories

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

Django中是否有内置的登录模板?

我想让用户在看到页面之前登录.是否有用户登录的内置模板,以便我不必编写自己的登录页面?

django templates login

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

如何从FTP服务器删除超过7天的Python脚本文件?

我想写一个Python脚本,它允许我在达到一定年龄后从FTP服务器中删除文件.我准备了下面的scipt但它抛出了错误消息:WindowsError: [Error 3] The system cannot find the path specified: '/test123/*.*'

有人知道如何解决这个问题吗?先感谢您!

import os, time
from ftplib import FTP

ftp = FTP('127.0.0.1')
print "Automated FTP Maintainance"
print 'Logging in.'
ftp.login('admin', 'admin')

# This is the directory that we want to go to
path = 'test123'
print 'Changing to:' + path
ftp.cwd(path)
files = ftp.retrlines('LIST')
print 'List of Files:' + files 
#--everything works fine until here!...

#--The Logic which shall delete the files after the are 7 days old--
now …
Run Code Online (Sandbox Code Playgroud)

python ftp file delete-file

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

OCUnit测试嵌入式框架

更新:我最终放弃并将GHUnit添加到我的项目中.我在几分钟内就开始使用GHUnit.

更新:您可以在此处下载Xcode项目:http://github.com/d11wtq/Cioccolata

我已经为我的Xcode项目添加了一个单元测试目标,但它在构建时找不到我的框架,说:

Test.octest could not be loaded because a link error occurred. It is likely that dyld cannot locate a framework framework or library that the the test bundle was linked against, possibly because the framework or library had an incorrect install path at link time.

我的框架(主项目目标)旨在嵌入,因此具有安装路径@executable_path/../Frameworks.

我已将框架标记为测试目标的直接依赖关系,并将其添加到"Link Binary with Libraries"构建阶段.

另外,我添加了"复制文件"的第一步(在它构建了依赖项之后),它只是将框架复制到单元测试包的Frameworks目录中.

有人有这方面的经验吗?我不确定我错过了什么.

编辑| 我很确定我不应该,因为框架不可执行,但我没有设置"测试主机"和"捆绑加载器".这应该(据我的理解)一切正常,因为测试包与框架链接并将像任何其他包一样加载它.

编辑| 我想我差不多了.我阅读了以下文章,该文章规定使用@rpath而不是@executable_path.

http://www.dribin.org/dave/blog/archives/2009/11/15/rpath/

在这种情况下,它非常有意义,因为OCUnit测试包不是可执行文件,它是一个普通的旧包,所以@executable_path不兼容.所以现在我的框架将其安装目录设置为@rpath,并且Test目标将其运行时搜索路径(rpath)定义为构建目录.这节省了我不得不将框架复制到测试包中,这意味着整个生成的框架本质上更灵活,因为它可以在任何地方生活.

现在,我也意识到我应该在Test目标上设置Bundle Loader,所以现在将其设置为框架二进制文件的路径.

我可以构建测试目标,我可以从框架中#import类,没有错误.但是一旦我尝试从框架中实例化一个类,我就会收到以下错误:

/Developer/Tools/RunPlatformUnitTests.include:412: note: Started tests for architectures 'i386' /Developer/Tools/RunPlatformUnitTests.include:419: …

xcode cocoa objective-c ocunit gh-unit

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

PHP:删除所有未按预期运行的fcn,Code Inside

我做了这个简单的功能(从$ array中删除所有$ elem):

function remall($array, $elem) {
    for($i=0; $i < count($array); $i++)
        if($array[$i] == $elem)
            unset($array[$i]);
    $newarray = array_values($array);
    return $newarray;
}
Run Code Online (Sandbox Code Playgroud)

但它并不完美,这里有一些输入和输出

$u = array(1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7);
$r = remall($u, 7);
Output of $r: 12345767

$n = array(7, 7, 1, 7, 3, 4, 6, 7, 2, 3, 1, -3, 10, 11, 7, 7, 7, 2, 7);
$r = remall($n, 7);
Output of $r: 1346231-30117727
Run Code Online (Sandbox Code Playgroud)

请注意我的输出中仍然有7个.此外,我的功能只会从数组中删除数字.如果你发现了什么,请告诉我,谢谢.

解决方案:嘿伙计这对我有用(感谢Flavius Stef)

function remall($array, $elem) …
Run Code Online (Sandbox Code Playgroud)

php removeall

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

当程序集丢失时,.NET应用程序无法启动

我有一个.NET C#winforms应用程序,它在我的机器上运行良好,但如果我尝试在另一台机器上运行它,它就无法启动.没有错误消息,没有崩溃消息,没有窗口 - 没有.

我发现这总是在缺少引用的程序集时发生.我想这是.NET的一般行为而不是我的应用程序特有的,是吗?

有没有办法配置.NET或我的应用程序,以便它在这种情况下吐出"引用的程序集丢失",如错误消息?

.net c# reference winforms

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

我想将Google Map API用于我的桌面应用程序

我想在我的桌面应用程序中使用谷歌地图API.该应用程序将完全连接到互联网.

我正在搜索关于这种实现的一些研究笔记.我找到了一个理想的配置网站,但它有一些java文件要下载,但当我尝试该网站时,它没有加载.这是swinglabs.org

http://today.java.net/pub/a/today/2007/10/30/building-maps-into-swing-app-with-jxmapviewer.html

在我的桌面应用程序中执行此api实现的任何其他选项?还有一件事.我试着下载google api.即使它问一个网址.我们必须提供一个网址然后只有我们得到一个键下载它.并且api应该在该特定URL中运行.否则,它不起作用.这对于桌面应用程序如何显示

任何想法欢迎.

java google-maps

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

为什么这个Java代码不使用所有CPU内核?

附加的简单Java代码应该在使用正确的参数启动时加载所有可用的cpu核心.例如,你开始使用它

java VMTest 8 int 0

它将启动8个线程,除了循环并将2添加到整数之外什么都不做.在寄存器中运行的东西,甚至不分配新的内存.

我们现在面临的问题是,当运行这个简单的程序(当然有24个线程)时,我们没有加载24核机器(AMD 2个插槽,每个12个核心).类似的事情发生在每12个线程或更小的机器的2个程序.

所以我们怀疑JVM(Linux x64上的Sun JDK 6u20)不能很好地扩展.

有没有人看到类似的东西或有能力运行它并报告它是否在他/她的机器上运行良好(> =仅8个核心)?想法?

我在带有8个内核的Amazon EC2上尝试过这个,但是虚拟机似乎与真正的盒子不同,因此加载的行为非常奇怪.

package com.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class VMTest
{
    public class IntTask implements Runnable 
    {
        @Override
        public void run()
        {
            int i = 0;

            while (true)
            {
                i = i + 2;
            }
        }
    }
    public class StringTask implements Runnable 
    {
        @Override
        public void run()
        {
            int i = 0;

            String s;
            while (true)
            {
                i++;
                s = …
Run Code Online (Sandbox Code Playgroud)

java cpu scaling scalability multicore

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

使用Ruby进行任意精度算术

Ruby怎么做到这一点?Jörg或其他任何人都知道幕后发生了什么吗?

不幸的是我不太了解C,所以bignum.c对我没什么帮助.我有点好奇,有人可以用简单的英语解释它使用的奇迹算法背后的理论.

irb(main):001:0> 999**999
Run Code Online (Sandbox Code Playgroud)

368063488259223267894700840060521865838338232037353204655959621437025609300472231530103873614505175218691345257589896391130393189447969771645832382192366076536631132001776175977932178658703660778465765811830827876982014124022948671975678131724958064427949902810498973271030787716781467419524180040734398996952930832508934116945966120176735120823151959779536852290090377452502236990839453416790640456116471139751546750048602189291028640970574762600185950226138244530187489211615864021135312077912018844630780307462205252807737757672094320692373101032517459518497524015120165166724189816766397247824175394802028228160027100623998873667435799073054618906855460488351426611310634023489044291860510352301912426608488807462312126590206830413782664554260411266378866626653755763627796569082931785645600816236891168141774993267488171702172191072731069216881668294625679492696148976999868715671440874206427212056717373099639711168901197440416590226524192782842896415414611688187391232048327738965820265934093108172054875188246591760877131657895633586576611857277011782497943522945011248430439201297015119468730712364007639373910811953430309476832453230123996750235710787086641070310288725389595138936784715274150426495416196669832679980253436807864187160054589045664027158817958549374490512399055448819148487049363674611664609890030088549591992466360050042566270348330911795487647045949301286614658650071299695652245266080672989921799342509291635330827874264789587306974472327718704306352445925996155619153783913237212716010410294999877569745287353422903443387562746452522860420416689019732913798073773281533570910205207767157128174184873357050830752777900041943256738499067821488421053870869022738698816059810579221002560882999884763252161747566893835178558961142349304466506402373556318707175710866983035313122068321102457824112014969387225476259342872866363550383840720010832906695360553556647545295849966279980830561242960013654529514995113584909050813015198928283202189194615501403435553060147713139766323195743324848047347575473228198492343231496580885057330510949058490527738662697480293583612233134502078182014347192522391449087738579081585795613547198599661273567662441490401862839817822686573112998663038868314974259766039340894024308383451039874674061160538242392803580758232755749310843694194787991556647907091849600704712003371103926967137408125713631396699343733288014254084819379380555174777020843568689927348949484201042595271932630685747613835385434424807024615161848223715989797178155169951121052285149157137697718850449708843330475301440373094611119631361702936342263219382793996895988331701890693689862459020775599439506870005130750427949747071390095256759203426671803377068109744629909769176319526837824364926844730545524646494321826241925107158040561607706364484910978348669388142016838792902926158979355432483611517588605967745393958061959024834251565197963477521095821435651996730128376734574843289089682710350244222290017891280419782767803785277960834729869249991658417000499998999

ruby math bignum arbitrary-precision

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

Authlogic OpenID集成

我在使用Authlogic进行OpenId身份验证时遇到了困难.似乎问题出现在open_id_authentication插件的更改中.从我到目前为止所读到的,人们需要从使用宝石切换到使用插件.

以下是我为使Authlogic-OpenID集成工作所做的工作:

  1. 删除了相关的宝石:
    • authlogic
    • authlogic-OID
    • 机架的OpenID
    • ruby-openid*
    • 安装,配置并启动了authlogic示例应用程序(http://github.com/binarylogic/authlogic_example)--works如预期.这需要:
    • 安装authlogic(2.1.3)gem($ sudo gem install authlogic)
    • 将依赖项(config.gem"authlogic")添加到environment.rb文件中.
    • 添加迁移以向User模型添加open-id支持; 跑迁; 列按预期添加
    • 对UsersController和UserSessionsController进行了更改,以使用块来保存每个.
    • 对新的用户会话视图进行了更改以支持open id(f.text_field:openid_identifier)
    • 安装open_id_authentication插件($ script/plugin install git://github.com/rails/open_id_authentication.git)
    • 安装了authlogic-oid插件($ script/plugin install git://github.com/binarylogic/authlogic_openid.git)
    • 安装了插件($ script/plugin install git://github.com/glebm/ruby-openid.git)
    • 重启mongrel(CTRL-C; $ script/server)

Mogrel未能启动,返回以下错误:

/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rack/openid (MissingSourceFile)
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/plugins/open_id_authentication/lib/open_id_authentication.rb:3
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /Users/craibuc/NetBeansProjects/authlogic_example/vendor/plugins/open_id_authentication/init.rb:5:in `evaluate_init_rb' …
Run Code Online (Sandbox Code Playgroud)

openid ruby-on-rails authlogic

9
推荐指数
1
解决办法
1445
查看次数