我想在Visual Basic中定义一个函数,它在给定的括号中计算所得税.输入应该是收入,边际税率,下括号边界,以及 - 可选 - 上括号边界.(对于顶部支架,没有上边界).
这是我如何去做的.首先,我定义了一个"ramp"函数,如下所示:
Public Function ramp(x)
ramp = (x + Abs(x)) / 2
End Function
Run Code Online (Sandbox Code Playgroud)
这基本上与IF(x <0,0,x)相同.然后我将税收函数(荷兰语)定义为
Public Function schijfbelasting(inkomen, ondergrens, bovengrens, tarief)
schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
End Function
Run Code Online (Sandbox Code Playgroud)
这里"inkomen"=收入,"ondergrens"=下括号边界,"bovengrens"=上括号边界,"tarief"=边际税率,输出"schijfbelasting"=指定括号中的税.
这一切都很好,除了我想使用"bovengrens"(上括号边界)可选
Optional bovengrens
Run Code Online (Sandbox Code Playgroud)
在Matlab中,我会使用"nargin"(参数个数)函数来执行以下操作:
Public Function schijfbelasting(inkomen, ondergrens, Optional bovengrens, tarief)
If nargin==4
schijfbelasting = ramp(tarief * (inkomen - ondergrens)) - ramp(tarief * (inkomen - bovengrens))
Elseif nargin==3
schijfbelasting = ramp(tarief*(inkomen-ondergrens))
End If
End Function
Run Code Online (Sandbox Code Playgroud)
但是,我不知道在Visual Basic中类似于"nargin"的函数.它也可能是"如果论证"bovengrens"被定义".有人知道如何解决这个问题吗?提前致谢. …
假设我像这样连接两个 DataFrame:
import numpy as np
import pandas as pd
array1 = np.random.randn(3,3)
array2 = np.random.randn(3,3)
df1 = pd.DataFrame(array1, columns=list('ABC'))
df2 = pd.DataFrame(array2, columns=list('ABC'))
df = pd.concat([df1, df2])
Run Code Online (Sandbox Code Playgroud)
生成的 DataFramedf如下所示:
A B C
0 1.297362 0.745510 -0.206756
1 -0.056807 -1.875149 -0.210556
2 0.310837 -1.068873 2.054006
0 1.163739 -0.678165 2.626052
1 -0.557625 -1.448195 -1.391434
2 0.222607 -0.334348 0.672643
Run Code Online (Sandbox Code Playgroud)
请注意,索引与原始 DataFrame 中的索引相同。我想重新索引df,以便索引简单地从0到运行5。我怎样才能做到这一点?
(我试过,df = df.reindex(index = range(df.shape[0]))但这给出了ValueError: cannot reindex from a …
我正在尝试理解一个包含.h文件的C程序
#define random ((float) rand() / (float)((1 << 31) - 1))
Run Code Online (Sandbox Code Playgroud)
C程序还包括<math.h>.
我的猜测是,这只是在区间[0,1]上从均匀分布产生一个随机数; 它是否正确?
我编写了一个Python程序,确保在收到Unix SIGTERM信号后正常停止其线程.我知道docker stop会向容器内的主进程发送SIGTERM请求,并在宽限期后发送SIGKILL.docker-compose down的文档指出它"停止容器",但没有明确说明如何.我假设它调用docker stop每个容器?
我正在编写一个类RecurringInterval- 基于dateutil.rrule对象 - 代表一个反复出现的时间间隔.我__str__为它定义了一个自定义的,人类可读的方法,并且还想定义一个parse方法(类似于rrulestr()函数)将字符串解析回一个对象.
以下是parse方法和一些测试用例:
import re
from dateutil.rrule import FREQNAMES
import pytest
class RecurringInterval(object):
freq_fmt = "{freq}"
start_fmt = "from {start}"
end_fmt = "till {end}"
byweekday_fmt = "by weekday {byweekday}"
bymonth_fmt = "by month {bymonth}"
@classmethod
def match_pattern(cls, string):
SPACES = r'\s*'
freq_names = [freq.lower() for freq in FREQNAMES] + [freq.title() for freq in FREQNAMES] # The frequencies may be either lowercase or start with a capital …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行一个刮刀,其输出日志结束如下:
2017-04-25 20:22:22 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <429 http://www.apkmirror.com/apk/instagram/instagram-instagram/instagram-instagram-9-0-0-34920-release/instagram-9-0-0-4-android-apk-download/>: HTTP status code is not handled or not allowed
2017-04-25 20:22:22 [scrapy.core.engine] INFO: Closing spider (finished)
2017-04-25 20:22:22 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 16048410,
'downloader/request_count': 32902,
'downloader/request_method_count/GET': 32902,
'downloader/response_bytes': 117633316,
'downloader/response_count': 32902,
'downloader/response_status_count/200': 121,
'downloader/response_status_count/429': 32781,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2017, 4, 25, 18, 22, 22, 710446),
'log_count/DEBUG': 32903,
'log_count/INFO': 32815,
'request_depth_max': 2,
'response_received_count': 32902,
'scheduler/dequeued': 32902,
'scheduler/dequeued/memory': 32902,
'scheduler/enqueued': 32902,
'scheduler/enqueued/memory': 32902,
'start_time': datetime.datetime(2017, 4, 25, 17, 54, 36, 621481)} …Run Code Online (Sandbox Code Playgroud) 以Django教程中的(部分)民意调查应用为例,我试图让pytest-django运行。
使用命令django-admin startproject mysite2,我创建了一个具有以下结构的项目目录:
.
??? db.sqlite3
??? manage.py
??? mysite2
? ??? __init__.py
? ??? settings.py
? ??? urls.py
? ??? wsgi.py
??? polls
? ??? __init__.py
? ??? admin.py
? ??? apps.py
? ??? migrations
? ? ??? 0001_initial.py
? ? ??? __init__.py
? ??? models.py
? ??? tests.py
? ??? urls.py
? ??? views.py
??? pytest.ini
Run Code Online (Sandbox Code Playgroud)
我的pytest.ini样子
[pytest]
DJANGO_SETTINGS_MODULE = mysite2.settings
python_files = tests.py test_*.py *_tests.py
Run Code Online (Sandbox Code Playgroud)
按照该教程,polls/models.py我已经创建 …
作为简化示例,我UpdateView为Book模型编写了一个,并ListView在成功时重定向到:
from django.urls import reverse
from django.views.generic import ListView
from django.views.generic.edit import UpdateView
from .models import Book
class BookUpdate(UpdateView):
model = Book
fields = ['title', 'author']
class BookList(ListView):
model = Book
Run Code Online (Sandbox Code Playgroud)
该Book模型定义为
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100, blank=True)
def get_absolute_url(self):
return reverse('books-list')
Run Code Online (Sandbox Code Playgroud)
这里urls.py是
from django.urls import path
from books.views import BookUpdate, BookList
urlpatterns = [
path('books/', BookList.as_view(), name='books-list'),
path('book/<int:pk>/', BookUpdate.as_view(), name='book-update')
]
Run Code Online (Sandbox Code Playgroud)
在books/tests.py我尝试编写以下测试:
class BookUpdateTest(TestCase): …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用动态填充的字段创建Django表单:也就是说,当checkin_type从下拉菜单中选择一个字段()时,其他字段会自动填充相应的数据。为此,一旦选择了下拉选项,我想向服务器发送POST请求。
到目前为止,我已经尝试了以下模板(遵循https://docs.djangoproject.com/en/2.0/ref/csrf/):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script>
$(document).ready(function(){
var csrftoken = Cookies.get('csrftoken');
$(".auto-submit").change(function() {
$.post({
url: "{% url 'get-checkin-type' %}",
data: $(".auto-submit option:selected").val(),
headers: {
X-CSRFToken: csrftoken
}
})
});
});
</script>
<form action="" method="post">{% csrf_token %}
{% for field in form %}
<div class="{% if field.name == 'checkin_type' %}auto-submit{% endif %}">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
</div>
{% endfor %}
<input type="submit" value="Send message" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,当我选择一个下拉选项时,
新增:17个未捕获的SyntaxError:意外令牌-
从该X-CSRFToken: csrftoken …
我正在尝试使用其 API 从 Python 程序填充 Google 表格,使用快速入门指南 ( https://developers.google.com/sheets/api/quickstart/python ) 作为起点:
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
# Setup the Sheets API
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
Run Code Online (Sandbox Code Playgroud)
但是,我只能使用该flow_from_clientsecrets()方法(默认情况下)在浏览器中打开身份验证页面才能使其工作。这似乎不适合我想在生产服务器上定期运行的东西。
在任何情况下,根据https://pypi.org/project/oauth2client/,oauth2client已弃用,建议开发人员google-auth改用。
因此,我尝试按如下方式调整此示例(遵循https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files):
from google.oauth2 import service_account …Run Code Online (Sandbox Code Playgroud) python google-api google-authentication google-oauth google-sheets-api
python ×5
django ×3
c ×1
django-csrf ×1
docker ×1
excel ×1
excel-vba ×1
google-api ×1
google-oauth ×1
javascript ×1
jquery ×1
pandas ×1
parsing ×1
pytest ×1
random ×1
regex ×1
scrapy ×1
vba ×1
web-scraping ×1