我正在尝试强制用户将 JIRA 票证添加到 git commit。
我使用了一个预接收挂钩,但它只有在推送后才能工作。我希望它在提交后工作,所以如果消息格式不正确,提交将失败,用户将可以选择编辑提交。
这是我的代码示例:
#!/usr/bin/env bash
# set this to your active development branch
#develop_branch="master"
#current_branch="$(git rev-parse --abbrev-ref HEAD)"
# only check commit messages on main development branch
#[ "$current_branch" != "$develop_branch" ] && exit 0
# regex to validate in commit msg
commit_regex='(#-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('#-1111') '"
rm -rf fl.txt
echo $1 >> fl.txt
fil="fl.txt"
if ! grep -iE $commit_regex $fil; then
echo "$error_msg" >&2
exit 1 …Run Code Online (Sandbox Code Playgroud) Django引发了下一个异常:
restframework'tuple'对象没有属性'_meta'
模型
class BDetail(models.Model):
lat = models.FloatField(blank=True, null=True)
lng = models.FloatField(blank=True, null=True)
class Meta:
# managed = False
db_table = 'b_detail'
Run Code Online (Sandbox Code Playgroud)
视图
from .models import BDetail
from .serializers import BDetailSerializer
from rest_framework import viewsets
class BDetailList(viewsets.ModelViewSet):
queryset = BDetail.objects.all()
serializer_class = BDetailSerializer
Run Code Online (Sandbox Code Playgroud)
网址
from django.conf.urls import url, include
from bdetail import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'bdetail', views.BDetailList)
urlpatterns = [
url(r'^', include(router.urls), name='bdetail')
]
Run Code Online (Sandbox Code Playgroud)
串行
from .models import BDetail
from rest_framework import serializers
class BDetailSerializer(serializers.HyperlinkedModelSerializer): …Run Code Online (Sandbox Code Playgroud) python django python-2.7 django-rest-framework django-rest-framework-gis
我有一个字符串,我想在其中替换一些变量,但在不同的步骤中,类似于:
my_string = 'text_with_{var_1}_to_variables_{var_2}'
my_string.format(var_1='10')
### make process 1
my_string.format(var_2='22')
Run Code Online (Sandbox Code Playgroud)
但是当我尝试替换第一个变量时,我得到一个错误:
KeyError: 'var_2'
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
编辑:我想创建一个新列表:
name = 'Luis'
ids = ['12344','553454','dadada']
def create_list(name,ids):
my_string = 'text_with_{var_1}_to_variables_{var_2}'.replace('{var_1}',name)
return [my_string.replace('{var_2}',_id) for _id in ids ]
Run Code Online (Sandbox Code Playgroud)
这是所需的输出:
['text_with_Luis_to_variables_12344',
'text_with_Luis_to_variables_553454',
'text_with_Luis_to_variables_dadada']
Run Code Online (Sandbox Code Playgroud)
但是使用.format而不是.replace.
我正在设置Django项目running tests.但我得到以下错误:
Got an error creating the test database: permission denied to copy database "template_postgis"
Run Code Online (Sandbox Code Playgroud)
注意:我的默认应用程序的数据库工作正常.的issue is happening while running tests.
完整的堆栈跟踪如下:
moin@moin-pc:~/workspace/mozio$ python manage.py test --verbosity=3
nosetests --verbosity=3
nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
Creating test database for alias 'default' ('test_my_db')...
Got an error creating the test database: permission denied to copy database "template_postgis"
Type 'yes' if you would like to try deleting the test database 'test_mozio_db_test', or 'no' to cancel: yes …Run Code Online (Sandbox Code Playgroud) 我试图网上废弃一些网站的内容.但我注意到,对于某些网站,我收到状态代码为200的响应.但是,对于其他一些网站,我收到了404响应状态代码.但是当我在浏览器中打开这些网站(返回404)时,它正好适合我.我在这里错过了什么?
例如:
import requests
url_1 = "https://www.transfermarkt.com/jumplist/startseite/wettbewerb/GB1"
url_2 = "https://stackoverflow.com/questions/36516183/what-should-i-use-instead-of-urlopen-in-urllib3"
page_t = requests.get(url_2)
print(page_t.status_code) #Getting a Not Found page and 404 status
page = requests.get(url_1)
print(page.status_code) #Getting a Valid HTML page and 200 status
Run Code Online (Sandbox Code Playgroud) 使用*iterable解包操作符功能我想保留变量的内容,以便我可以在我的代码中的多个位置使用该变量.这是一个表达我想要的例子:
>>> a = 1
>>> b = None
>>> c = None
>>> args = (x for x in (a, b, c) if x is not None)
>>> print(*args)
>>> 1
>>> print(*args)
>>>
Run Code Online (Sandbox Code Playgroud)
第二个打印返回任何内容,因为args在第一个print语句中已解压缩.
有没有办法通过仍然使用*功能来维护变量的内容?显然,我可以委托(x for x in (a, b, c) if x is not None)给我一直打电话的专用功能.我想知道是否有更简单/更pythonic的方式来处理操作.
我正在尝试编写一个函数,该函数返回 Python 字典中最频繁的值。我不想导入任何东西,只是简单的代码。
有任何想法吗?例如,如果我的字典是:
input_dict = {'A': 1963, 'B': 1963,
'C': 1964, 'D': 1964, 'E': 1964,
'F': 1965, 'G': 1965, 'H': 1966,
'I': 1967, 'J': 1967, 'K': 1968,
'L': 1969 ,'M': 1969,
'N': 1970}
Run Code Online (Sandbox Code Playgroud)
预期结果是1964
(因为它作为dict3 次(最大计数)中的值出现)。
这是我最后一次尝试:
def most_prolific(input_dict):
values = []
for year in input_dict.values():
if year in input_dict.values():
values.append(year)
for most in values:
if most in values:
return max(values.count(most))
Run Code Online (Sandbox Code Playgroud) 我试图从列表中获取所有可能的模式,如:
input_x = ['1', ['2', '2x'], '3', '4', ['5', '5x']]
Run Code Online (Sandbox Code Playgroud)
正如我们所见,它有2个嵌套列表['2', '2x']和['5', '5x']这里.
这意味着所有可能的模式都是4(2个案例x 2个案例),期望输出为:
output1 = ['1','2' , '3', '4', '5']
output2 = ['1','2x', '3', '4', '5']
output3 = ['1','2' , '3', '4', '5x']
output4 = ['1','2x', '3', '4', '5x']
Run Code Online (Sandbox Code Playgroud)
我试图搜索如何,但我找不到任何例子(因为我不知道搜索"关键字")
我认为python有内部库/方法来处理它.
目标是创建一个单词中某些字母的所有可能组合的列表...这很好,除了它现在最终作为具有太多引号和逗号的元组列表.
import itertools
mainword = input(str("Enter a word: "))
n_word = int((len(mainword)))
outp = (list(itertools.permutations(mainword,n_word)))
Run Code Online (Sandbox Code Playgroud)
我想要的是:
[yes, yse, eys, esy, sye, sey]
Run Code Online (Sandbox Code Playgroud)
我得到了什么:
[('y', 'e', 's'), ('y', 's', 'e'), ('e', 'y', 's'), ('e', 's', 'y'), ('s', 'y', 'e'), ('s', 'e', 'y')]
Run Code Online (Sandbox Code Playgroud)
在我看来,我只需要删除所有括号,引号和逗号.
我试过了:
def remove(old_list, val):
new_list = []
for items in old_list:
if items!=val:
new_list.append(items)
return new_list
print(new_list)
Run Code Online (Sandbox Code Playgroud)
我只是运行了几次这个功能.但它不起作用.
我有两个Python列表:组件和签名.我想检查签名中列出的所有类型是否与组件列表中的至少一个元素匹配.
在这里,签名匹配的组件列表,因为那里既是字符串,并以浮动组件:
signature = [float, str]
components = [1.0, [], 'hello', 1]
Run Code Online (Sandbox Code Playgroud)
这里签名 与 组件不匹配,因为没有列表类型.
signature = [float, list]
components = ['apple', 1.0]
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python 3中表达这个条件?
python ×9
list ×4
python-3.x ×4
django ×2
dictionary ×1
generator ×1
geodjango ×1
git ×1
postgis ×1
python-2.7 ×1
string ×1
tuples ×1
types ×1
web-scraping ×1