小编Emm*_*een的帖子

如何在本地正确存储密码

我一直在MSDN上阅读这篇关于Rfc2898DeriveBytes的文章.以下是它们提供的示例加密代码.

string pwd1 = passwordargs[0];
// Create a byte array to hold the random value. 
byte[] salt1 = new byte[8];
using (RNGCryptoServiceProvider rngCsp = ne RNGCryptoServiceProvider())
{
    // Fill the array with a random value.
    rngCsp.GetBytes(salt1);
}

//data1 can be a string or contents of a file.
string data1 = "Some test data";
//The default iteration count is 1000 so the two methods use the same iteration count.
int myIterations = 1000;
try
{
    Rfc2898DeriveBytes k1 = new …
Run Code Online (Sandbox Code Playgroud)

c# encryption

13
推荐指数
2
解决办法
3714
查看次数

在Python中正确实现Shutil.Error

我正在学习Python 3,并尝试编写一个将复制目录的脚本。我正在使用shutil.copytree。从Python文档中说:

如果发生异常,则会引发错误并列出原因。

此异常收集在多文件操作期间引发的异常。对于copytree(),exception参数是一个三元组的列表(srcname,dstname,exception)。

示例中,他们这样做:

 except Error as err:
            errors.extend(err.args[0])
Run Code Online (Sandbox Code Playgroud)

这是我的脚本:

def copyDirectory(src, dest):

    errors = []

    try:
        shutil.copytree(src, dest)
     except Error as err:
            errors.extend(err.args[0])

source="C:/Users/MrRobot/Desktop/Copy"      
destination="C:/Users/MrRobot/Desktop/Destination"

copyDirectory(source, destination)
moveDirectory(destination,"I:/")
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 您如何正确捕获使用时可能发生的异常shutil.copytree(假设以上脚本不正确)?

  2. 那您如何查看发生的错误,我将遍历整个errors阵列?

directory exception-handling python-3.x

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

在For循环中放置Try-Catch是不好的做法吗?

我正在创建一个删除子目录/文件的命令行实用程序.如果正在使用文件,System.IO.IOException则抛出该文件.我在for循环中使用了try-catch块.

题:

1.在for循环中有一个try-catch是不好的做法吗?

2.如果是,什么是更好的选择?

我的代码:

  System.IO.DirectoryInfo di = new DirectoryInfo(path);

    foreach (FileInfo file in di.GetFiles())
    {
         try
         {
             file.Delete(); 
         }
         catch(System.IO.IOException)
         {
           Console.WriteLine("Please Close the following File {0}", file.Name);
         }

    }
Run Code Online (Sandbox Code Playgroud)

c# for-loop try-catch

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

在Perl中创建一个类

package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n";
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self, $class;
    return $self;
}
Run Code Online (Sandbox Code Playgroud)

我从这个网站得到了上面的代码.

从Classes官方Perl站点:

Perl没有为类定义提供任何特殊语法.包只是一个包含变量和子例程的命名空间.

  1. 考虑到这一点,上面的代码来自Tutorial Point,这是在Perl中创建类时的最佳实践吗?

  2. 在其他OO语言中,当没有设置值时,你可以从构造函数内部抛出异常,在Perl中也可以这样做吗?

  3. 哪个是正确的祝福方式?

教程点中的代码执行此操作:

bless $self, $class;
Run Code Online (Sandbox Code Playgroud)

但是从perl文档:

  my $self = bless {
      path => $path,
      data => $data,
  }, $class;
  return $self;
Run Code Online (Sandbox Code Playgroud)

oop perl

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