RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-397399

Namerek's questions

Martin Hope
Namerek
Asked: 2022-09-10 15:44:47 +0000 UTC

在 Python 中优化 JSON 解析

  • 0

下面的代码功能齐全并执行其任务。
对在不使用第三方库的情况下对其进行优化或实施的可能性感兴趣。
提前感谢您的建设性建议。

编码:


incoming.json

{
  "questions": [
    {
      "key": "what_type_of_business_do_you_operate?", 
      "label": "What type of business do you operate?", 
      "options": [
        {
          "key": "reseller", 
          "value": "Reseller"
        }, 
        {
          "key": "logistics_company", 
          "value": "Logistics company"
        }, 
        {
          "key": "other", 
          "value": "Other"
        }
      ], 
      "type": "CUSTOM", 
      "id": "****************"
    }, 
    {
      "key": "conditional_question_1", 
      "label": "Please choose the truck brand your are interested in", 
      "options": [
        {
          "key": "volvo", 
          "value": "Volvo"
        }, 
        {
          "key": "mercedes-benz_actros", 
          "value": "Mercedes-Benz Actros"
        }, 
        {
          "key": "daf", 
          "value": "DAF"
        }
      ], 
      "type": "CUSTOM", 
      "dependent_conditional_questions": [
        {
          "name": "Please choose age of the vehicle", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_2"
        }, 
        {
          "name": "Quantity you want to purchase", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_3"
        }, 
        {
          "name": "When do you plan to purchase", 
          "input_type": "inline_select", 
          "field_key": "conditional_question_4"
        }
      ], 
      "conditional_questions_choices": [
        {
          "value": "Volvo", 
          "customized_token": "volvo", 
          "next_question_choices": [
            {
              "value": "1-3 years", 
              "customized_token": "1-3_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }, 
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }, 
        {
          "value": "Mercedes-Benz Actros", 
          "customized_token": "mercedes-benz_actros", 
          "next_question_choices": [
            {
              "value": "1-3 years", 
              "customized_token": "1-3_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }, 
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }, 
        {
          "value": "DAF", 
          "customized_token": "daf", 
          "next_question_choices": [
            {
              "value": "3-5 years", 
              "customized_token": "3-5_years", 
              "next_question_choices": [
                {
                  "value": "1", 
                  "customized_token": "1", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "2", 
                  "customized_token": "2", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "3-5", 
                  "customized_token": "3-5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }, 
                {
                  "value": "more than 5", 
                  "customized_token": "more_than_5", 
                  "next_question_choices": [
                    {
                      "value": "In upcoming days or weeks", 
                      "customized_token": "in_upcoming_days_or_weeks"
                    }, 
                    {
                      "value": "Within 3 months", 
                      "customized_token": "within_3_months"
                    }, 
                    {
                      "value": "This year", 
                      "customized_token": "this_year"
                    }, 
                    {
                      "value": "Next year", 
                      "customized_token": "next_year"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ], 
      "conditional_questions_group_id": "1781381718904816", 
      "id": "****************"
    }, 
    {
      "key": "company_name", 
      "label": "Company name", 
      "type": "COMPANY_NAME", 
      "id": "****************"
    }, 
    {
      "key": "full_name", 
      "label": "Full name", 
      "type": "FULL_NAME", 
      "id": "****************"
    }, 
    {
      "key": "phone_number", 
      "label": "Phone number", 
      "type": "PHONE", 
      "id": "****************"
    }, 
    {
      "key": "email", 
      "label": "Email", 
      "type": "EMAIL", 
      "id": "****************"
    }
  ], 
  "locale": "en_US", 
  "name": "Resellers - Retail lead form test 220908 multiple sales people", 
  "created_time": "2022-09-08T11:32:03+0000", 
  "context_card": {
    "title": "Testing multiple sales people", 
    "content": [
      "aekejskal;fjkldsfjdklsfj"
    ], 
    "style": "PARAGRAPH_STYLE", 
    "id": "****************"
  }, 
  "id": "****************"
}

处理程序:
pip install jmespath

import jmespath as jp
import json

data: dict = json.load(
    open('incoming.json', 'r', encoding='utf-8'),
)

form_questions = dict()

for item in jp.search(
        'questions[?dependent_conditional_questions!=null]',
        data
):
    item: dict
    questions = {
        item.get('key'): {
            'label': item.get('label'),
            'type': 'CONDITIONAL'
        }
    }
    for elem in jp.search(
            'dependent_conditional_questions[*].[field_key,{label:name}]',
            item
    ):
        elem[1]['type'] = 'CONDITIONAL'
        questions.update(
            (elem,)
        )

    for i, key in enumerate(questions):
        pat = f'conditional_questions_choices' + ('[0].next_question_choices' * i) + '[*].[customized_token,value]'
        asd = questions[key]['values'] = dict(jp.search(pat, item))

    form_questions.update(
        questions
    )

for item in jp.search(
        "questions[?dependent_conditional_questions==null && type=='CUSTOM']",
        data
):
    item: dict

    form_questions.update(
        {
            item.get('key'):
                {
                    'label': item.get('label'),
                    'values': {
                        elem.get('key'): elem.get('value')
                        for elem in item.get('options', [])
                    },
                    'type': 'CUSTOM'
                }
        }
    )

for item in jp.search(
        "questions[?type=='COMPANY_NAME' || type=='PHONE' || type=='FULL_NAME' || type=='EMAIL']",
        data
):
    item: dict
    form_questions.update(
        {
            item.get('key'): {
                'label': item.get('label'),
                'type': item.get('type'),
                'values': dict()
            }
        }
    )

结果:

{
  "conditional_question_1": {
    "label": "Please choose the truck brand your are interested in", 
    "type": "CONDITIONAL", 
    "values": {
      "volvo": "Volvo", 
      "mercedes-benz_actros": "Mercedes-Benz Actros", 
      "daf": "DAF"
    }
  }, 
  "conditional_question_2": {
    "label": "Please choose age of the vehicle", 
    "type": "CONDITIONAL", 
    "values": {
      "1-3_years": "1-3 years", 
      "3-5_years": "3-5 years"
    }
  }, 
  "conditional_question_3": {
    "label": "Quantity you want to purchase", 
    "type": "CONDITIONAL", 
    "values": {
      "1": "1", 
      "2": "2", 
      "3-5": "3-5", 
      "more_than_5": "more than 5"
    }
  }, 
  "conditional_question_4": {
    "label": "When do you plan to purchase", 
    "type": "CONDITIONAL", 
    "values": {
      "in_upcoming_days_or_weeks": "In upcoming days or weeks", 
      "within_3_months": "Within 3 months", 
      "this_year": "This year", 
      "next_year": "Next year"
    }
  }, 
  "what_type_of_business_do_you_operate?": {
    "label": "What type of business do you operate?", 
    "values": {
      "reseller": "Reseller", 
      "logistics_company": "Logistics company", 
      "other": "Other"
    }, 
    "type": "CUSTOM"
  }, 
  "company_name": {
    "label": "Company name", 
    "type": "COMPANY_NAME", 
    "values": {}
  }, 
  "full_name": {
    "label": "Full name", 
    "type": "FULL_NAME", 
    "values": {}
  }, 
  "phone_number": {
    "label": "Phone number", 
    "type": "PHONE", 
    "values": {}
  }, 
  "email": {
    "label": "Email", 
    "type": "EMAIL", 
    "values": {}
  }
}
python
  • 0 个回答
  • 0 Views
Martin Hope
Namerek
Asked: 2022-07-18 04:10:18 +0000 UTC

python-dateutil 计算工作时间

  • -1

今天,有人问了我一个有趣的问题,即以分钟为单位计算工作时间。
我根据使用库写了一个答案python-dateutil

from datetime import datetime
from dateutil.rrule import *
from dateutil.parser import parse


def worktime_count(start: datetime, end: datetime, unit=MINUTELY):
    return len(
        [
            *rrule(
                unit,
                start,
                until=end,
                byweekday=range(0, 5),
                byhour=range(9, 18),
                wkst=MO
            )
        ]
    )


print(
    worktime_count(
        parse('2022-05-13 17:50:00'),
        parse('2022-05-16 09:10:00')
    )
)
# 21
print(
    worktime_count(
        parse('2022-05-13 17:50:00'),
        parse('2022-05-13 18:00:00')
    )
)
# 10
# Поэтому просто -1 к результату не катит

在我看来,答案应该是 20(我想你会同意我的)。
这就是为什么我删除了我的答案

如果我们使每秒生成时间点的频率然后列表的长度//60,
那么答案将是正确的。

from datetime import datetime
from dateutil.rrule import *
from dateutil.parser import parse


def worktime_count(start: datetime, end: datetime, unit=SECONDLY):
    return len(
        [
            *rrule(
                unit,
                start,
                until=end,
                byweekday=range(0, 5),
                byhour=range(9, 18),
                wkst=MO
            )
        ]
    ) // 60


print(
    worktime_count(
        parse('2022-05-13 17:50:00'),
        parse('2022-05-16 09:10:00')
    )
)
# 20

print(
    worktime_count(
        parse('2022-05-13 17:50:00'),
        parse('2022-05-13 18:00:00')
    )
)
# 10

也许有人知道如何以每分钟的频率获得正确的结果?

以防万一链接到文档

python
  • 1 个回答
  • 45 Views
Martin Hope
Namerek
Asked: 2021-12-24 21:37:53 +0000 UTC

MySQL时间戳写入时区信息失败

  • 0

到目前为止,我认为它TIMESTAMP应该MySQL包含有关timezone 但在实践中面对该主题时,我收到以下信息

mysql> SELECT TIMESTAMPDIFF(HOUR , '2020-12-22T04:10:45+00:00', '2020-12-22T04:10:45+03:00');
+--------------------------------------------------------------------------------+
| TIMESTAMPDIFF(HOUR , '2020-12-22T04:10:45+00:00', '2020-12-22T04:10:45+03:00') |
+--------------------------------------------------------------------------------+
|                                                                             -3 |
+--------------------------------------------------------------------------------+
1 row in set (0.00 sec)

除了。在尝试从 python 写入数据时,我还遇到一种情况,表明我不明白它应该如何工作(很可能)或者我配置了错误:

from common.mysql_connection import db_connection
    from datetime import datetime
    import pytz

    call_datetime: datetime = pytz.utc.localize(datetime.fromisoformat('2020-12-22 04:10:45'))

    in_server = call_datetime.astimezone(pytz.timezone('Europe/Moscow'))

    print(call_datetime, in_server)

    db_connection.cursor.executemany(
        "insert into infinity.calls (row_id, trigger_datetime) values (%s, %s);", [
            [
                126356,
                in_server
            ],
            [
                5858585,
                call_datetime
            ]
        ]
    )
# 2020-12-22 04:10:45+00:00 2020-12-22 07:10:45+03:00

在此处输入图像描述

其实是一个问题。是否有可能(并且应该)以这样的方式完成
2020-12-22 04:10:45+00:00 == 2020-12-22 07:10:45+03:00

将代码中的所有时间值转换为服务器的时区是没有问题的。我只是不明白它应该如何在 MySQL 中正常工作

UPD

insert into test (
                  ts, # timestamp
                  dt  # datetime
                  )
values ('2020-12-22 04:10:45+03:00', '2020-12-22 04:10:45+00:00'),
       ('2020-12-22 04:10:45+03:00', '2020-12-22 04:10:45+00:00');

在此处输入图像描述

python
  • 2 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5