小编Red*_*ose的帖子

该函数将从一个表中提取小时值,并在另一表中填充以一小时为增量的“存储桶”

我有以下格式的数据供人们打发时间:

(dat<-data.frame(Date = c("1/1/19", "1/2/19", "1/4/19", "1/2/19"), 
                Person = c("John Doe", "Brian Smith", "Jane Doe", "Alexandra Wakes"), 
                Time_In = c("1:15pm", "1:45am", "11:00pm", "1:00am"), 
                Time_Out = c("2:30pm","3:33pm","3:00am","1:00am")))

    Date          Person Time_In Time_Out
1 1/1/19        John Doe  1:15pm   2:30pm
2 1/2/19     Brian Smith  1:45am   3:33pm
3 1/4/19        Jane Doe  3:00pm   3:00am
4 1/2/19 Alexandra Wakes  1:00am   1:00am
Run Code Online (Sandbox Code Playgroud)

我正在寻找用R或Python编写的函数,该函数将提取每个人在24个不同的存储桶中工作的总小时数,每个存储桶作为自己的列。它看起来像这样:

在此处输入图片说明

因此,在第一种情况下,此人从下午1:15到下午2:30工作,因此他们从下午1点到下午2点(13-14)工作了0.75小时,从下午2点到下午3点(14-15)工作了0.5小时。

我认为某些方法可能会起作用...

  1. 一系列嵌套循环
  2. 一系列的if / then语句
  3. 我尚未想到的Tidyverse或Pandas中的某些功能。

从上面的#1和#2(?)尝试是完全失败的。不确定工作流程是什么,但是任何建议都将不胜感激。

请注意,结果表中的列不必是数字(可以是小时1,小时2等,或者一般来说只要是任何因素,只要它表示24小时即可)。

我过去的尝试包括嵌套的for循环,如下所示:

for (i in 1:nrow(data)){

  if((int_overlaps(createinterval(data$PunchDate[i],0,1), workinterval[i]))){ `0-1`[i]=1} else{ `0-1`[i]=0}
  if((int_overlaps(createinterval(data$PunchDate[i],1,2), workinterval[i]))){ `1-2`[i]=1} else{ `1-2`[i]=0}
  if((int_overlaps(createinterval(data$PunchDate[i],2,3), …
Run Code Online (Sandbox Code Playgroud)

python r dataframe pandas

10
推荐指数
2
解决办法
366
查看次数

使用 2 个非默认构造函数反序列化类

我有一个类,它尝试序列化和反序列化给它的对象,使用 Json.net

public class JsonSerializer
{
    public string Serialize(object toSerialaze)
    {
        return JsonConvert.SerializeObject(toSerialaze);
    }

    public T Deserialize<T>(string toDeserialaze)
    {
        return JsonConvert.DeserializeObject<T>(toDeserialaze);
    }
}
Run Code Online (Sandbox Code Playgroud)

并给它一个这样的类的对象

public class Isbn
{
    private readonly int _groupCode;
    private readonly int _publisherCode;
    private readonly int _titleCode;
    private readonly int _checkCode;
    private static readonly Regex Regex = new Regex(@"^\s*\d*\s*-\s*\d*\s*-\s*\d*\s*-\s*\d*\s*$");

    public Isbn(int groupCode, int publisherCode, int titleCode, int checkCode)
    {
        _groupCode = groupCode;
        _publisherCode = publisherCode;
        _titleCode = titleCode;
        _checkCode = checkCode;
    }

    public Isbn(string isbn)
    {
        if …
Run Code Online (Sandbox Code Playgroud)

c# json

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

Docker 和 AzureKeyVault:无法加载共享库“libsecret-1.so.0”

我有 Asp.net core Xunit 集成测试,连接到 MongoDb 以测试集合上的基本存储库。测试是在 AKS 的容器中构建和运行的。我已经设置了测试装置来连接 Azure Key Vault 以检索到 MongoDb 的连接字符串。

        var pathToSetting= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        var configuration = new ConfigurationBuilder()
            .SetBasePath(pathToSetting)
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();

        var secretClient = new SecretClient(
            new Uri("url_to_Azure_keyVault"),
            new DefaultAzureCredential(),
            new SecretClientOptions()
            {
                Retry =
                {
                    Delay = TimeSpan.FromSeconds(2),
                    MaxDelay = TimeSpan.FromSeconds(4),
                    MaxRetries = 2,
                    Mode = RetryMode.Exponential
                }
            });

        configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
Run Code Online (Sandbox Code Playgroud)

我使用以下 Docker 文件进行集成测试:

#Grab an OS image made to run the .Net Core SDK
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build

#copy files for build …
Run Code Online (Sandbox Code Playgroud)

azure docker azure-keyvault asp.net-core azure-aks

6
推荐指数
2
解决办法
7838
查看次数

StackExchange.Redis按UTC日期进行密钥除外

我正在使用StackExchange.Redis并构建一些redis客户端界面RedisClientManager.在我的界面中,我有2个密钥设置器(按时间跨度和日期时间到期):

按时间跨度:

public void Set(string key, object value, TimeSpan timeout)
{
    _cache.StringSet(key, Serialize(value), timeout);
}
Run Code Online (Sandbox Code Playgroud)

截止日期:

public void Set(string key, object value, DateTime expires)
{
    _cache.StringSet(key, Serialize(value));
    _cache.KeyExpire(key, expires);
}
Run Code Online (Sandbox Code Playgroud)

用法:

按时间跨度:

RedisClientManager.Set(o.Key, o, new TimeSpan(0, 0, 5, 0));
Run Code Online (Sandbox Code Playgroud)

截止日期:

RedisClientManager.Set(o.Key, o, DateTime.UtcNow.AddMinutes(5));
Run Code Online (Sandbox Code Playgroud)

如果我使用Timespan(第一种方法)添加新密钥,则该对象位于Redis缓存中,并在5分钟后过期.如果使用Date(第二种方法)添加新密钥,则该对象不会添加到Redis.此问题仅在服务器上发生.在localhost上一切正常.可能是redis使用本地服务器时间键吗?我该如何解决这个问题?

使用密钥设置绝对到期的正确方法是什么StackExchange.Redis

c# asp.net stackexchange.redis

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

如何为存储为数组 [ticks,offset] 的 DatetimeOffset 创建 MongoDB TTL 索引?

我正在尝试在包含带有 DateTimeOffset 的文档的集合上创建 MongoDB TTL 索引。但是日期作为刻度和偏移量的数组持久化,例如[刻度,偏移量]。例如 :

TimeStamp:Array [
    0:636803424000000000, 
    1:-360]
Run Code Online (Sandbox Code Playgroud)

我的模型如下所示:

public class Log
{
    ....
    DateTimeOffset? TimeStamp{get; set;}
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 MongoDb.Driver v2.7.2 并且我一直在尝试以这种方式创建索引

....
var indexKeysDefinition = Builders<Log>.IndexKeys.Descending(l => l.TimeStamp);
var indexOptions = new CreateIndexOptions 
{ 
    ExpireAfter = TimeSpan..FromTicks(TimeSpan.TicksPerMinute), 
    Name = "LogsTimeStamPIndex", 
    Background = true 
};
var model = new CreateIndexModel<Log>(indexKeysDefinition, indexOptions);
await _database.GetCollection<Log>("Log").Indexes.CreateOneAsync(model);
....
Run Code Online (Sandbox Code Playgroud)

这是创建索引,但文档不会在 1 分钟后过期。如何为以这种方式存储的 DateTimeOffset 创建 TTL?或者我在这里错过了什么?

c# mongodb mongodb-.net-driver

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

Resharper 建议将 Using 语句转换为单个“等待使用”?

Resharper 建议将带有 await 关键字的 using 语句改进为单行 await using 语句。例如,以下代码片段:

    private async Task<string> ReadResponseAsync(HttpContext context)
    {
        var originalBody = context.Response.Body;

        try
        {
            using (var responseStream = new MemoryStream())
            {
                context.Response.Body = responseStream;
                await _next(context).ConfigureAwait(false);

                var response = context.Response;
                response.Body.Seek(0, SeekOrigin.Begin);

                using (var responseReader = new StreamReader(response.Body))
                { 
                    var responseContent = await responseReader
                    .ReadToEndAsync()
                    .ConfigureAwait(false);

                    response.Body.Seek(0, SeekOrigin.Begin);

                    await responseStream.CopyToAsync(originalBody)
                    .ConfigureAwait(false);
                    return responseContent;
                }
            }
        }
        finally
        {
            context.Response.Body = originalBody;
        }
    }
Run Code Online (Sandbox Code Playgroud)

应用连续的 Resharper 建议后,代码变为以下内容:

    private async Task<string> ReadResponseAsync(HttpContext context)
    {

        var …
Run Code Online (Sandbox Code Playgroud)

resharper using async-await c#-8.0

3
推荐指数
1
解决办法
1003
查看次数

尝试为 FormFile 实例设置 ContentType 会引发异常

我有以下简单的方法来创建一个简单的 FormFile 进行测试。

    private static IFormFile CreateTemporaryFile(string fileName, string contentType)
    {

        byte[] bytes = Encoding.UTF8.GetBytes("Test content");

        var formFile = new FormFile(
            baseStream: new MemoryStream(bytes),
            baseStreamOffset: 0,
            length: bytes.Length,
            name: "Data",
            fileName: fileName);

        formFile.ContentType = contentType;
        return formFile;
    }
Run Code Online (Sandbox Code Playgroud)

它在这一行不断抛出 NullReference 异常formFile.ContentType = contentType;。我们有 FormFile 的实例,ContentType 是一个带有 getter 和 setter 的公共字符串属性?我错过了什么吗?

在此输入图像描述

c# asp.net-core

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