我一直在玩startswith()
,我发现了一些有趣的东西:
>>> tup = ('1', '2', '3')
>>> lis = ['1', '2', '3', '4']
>>> '1'.startswith(tup)
True
>>> '1'.startswith(lis)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be str or a tuple of str, not list
Run Code Online (Sandbox Code Playgroud)
现在,错误是显而易见的,并将列表转换为元组将正常工作,就像它在第一时间做的那样:
>>> '1'.startswith(tuple(lis))
True
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是:为什么第一个参数必须是str或str前缀的元组,而不是 str前缀列表?
AFAIK,Python代码startswith()
可能如下所示:
def startswith(src, prefix):
return src[:len(prefix)] == prefix
Run Code Online (Sandbox Code Playgroud)
但这让我更加困惑,因为即使考虑到它,无论是列表还是元组,它仍然不应该有任何区别.我错过了什么?
这个问题专门针对Django 2.0的答案,因为该registration
模块尚不可用.
更多,这看起来似乎很广泛,但我经常发现自己处于不能使用任何第三方模块的情况,因为......哦,好吧..政策.我相信很多人都这样做了.而且我知道,从这里查看和汇总从django docs获取的信息令人头疼.
我们假设我们需要以下流程:
first_name
,last_name
并email
(将电子邮件作为用户名).额外信息:用户稍后将使用他的电子邮件(实际上是他的用户名)和密码登录.
我试图从这个网站获得所有产品,但不知何故我不认为我选择了最好的方法,因为其中一些缺失了,我无法弄清楚为什么.这不是我第一次遇到困难时.
我现在这样做的方式是这样的:
现在,下面的代码可以工作,但它没有得到所有的产品,我没有看到为什么它跳过一些的任何原因.也许我接近一切的方式是错误的.
from lxml import html
from random import randint
from string import ascii_uppercase
from time import sleep
from requests import Session
INDEX_PAGE = 'https://www.richelieu.com/us/en/index'
session_ = Session()
def retry(link):
wait = randint(0, 10)
try:
return session_.get(link).text
except Exception as e:
print('Retrying product page in {} seconds because: {}'.format(wait, e))
sleep(wait)
return retry(link)
def get_category_sections():
au = list(ascii_uppercase)
au.remove('Q')
au.remove('Y')
au.append('0-9')
return au
def get_categories():
html_ = retry(INDEX_PAGE)
page = html.fromstring(html_) …
Run Code Online (Sandbox Code Playgroud) 首先,我们给出了以下代码:
from validate_email import validate_email
import time
import os
def verify_emails(email_path, good_filepath, bad_filepath):
good_emails = open(good_filepath, 'w+')
bad_emails = open(bad_filepath, 'w+')
emails = set()
with open(email_path) as f:
for email in f:
email = email.strip()
if email in emails:
continue
emails.add(email)
if validate_email(email, verify=True):
good_emails.write(email + '\n')
else:
bad_emails.write(email + '\n')
if __name__ == "__main__":
os.system('cls')
verify_emails("emails.txt", "good_emails.txt", "bad_emails.txt")
Run Code Online (Sandbox Code Playgroud)
我希望在emails.txt
包含大量行(> 1k)时,联系SMTP服务器是我程序中最昂贵的部分.使用某种形式的并行或异步I/O应该可以加快速度,因为我可以等待多个服务器响应而不是顺序等待.
据我所知:
异步I/O通过将对I/O的请求排队到文件描述符来进行操作,独立于调用进程进行跟踪.对于支持异步I/O(通常是原始磁盘)的文件描述符,进程可以调用aio_read()(例如)来请求从文件描述符中读取多个字节.无论I/O是否完成,系统调用都会立即返回.一段时间之后,该过程然后轮询操作系统以完成I/O(即,缓冲区充满数据).
为了真诚,我不太明白如何在我的程序上实现异步I/O. 任何人都可以花一点时间向我解释整个过程吗?
根据PArakleta的编辑建议:
from validate_email import validate_email
import time
import os
from multiprocessing …
Run Code Online (Sandbox Code Playgroud) 我想在javascript中使用一个函数作为参数获取url并返回该URL的端口,如下所示:
http
或https
(端口80/443),它将不会显示在url结构中,但我还是希望它们返回.例:
function myFunction(url){
something here
...
return port
}
Run Code Online (Sandbox Code Playgroud)
我已经看到使用一些额外的库可以很容易地做到这一点,但我不想使用它.从现在开始我没有和js一起工作,如果有人能够解释他的解决方案,我真的很感激.
我正在尝试使用Nginx + uwsgi运行Django应用程序,但是504 Gateway Time-out
加载一分钟后我收到了。
我的应用在搜索多个网站上的特定内容时需要花费时间来做所需的事情。
我的nginx conf是下一个:
upstream uwsgi {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name server_ip;
root /opt/emails/subscriptions;
index index.html index.htm index.php;
location /emailsproject/ {
root /opt/emails/subscriptions/;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://uwsgi;
proxy_set_header Host $http_host;
uwsgi_read_timeout 18000;
}
}
Run Code Online (Sandbox Code Playgroud)
我的uwsgi脚本:
description "uWSGI server"
env PYTHONPATH=/opt/emails/subscriptions
env DJANGO_SETTINGS_MODULE=emailsproject.settings
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec uwsgi_python --http-socket 127.0.0.1:8000 -p 4 --wsgi-file /opt/emails/subscriptions/emailsproject/wsgi.py
Run Code Online (Sandbox Code Playgroud)
我的nginx在error.log中给我以下错误消息:
2015/09/28 02:15:57 …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法,该方法允许我在午夜旋转日志,而不是使用maxBytes
参数旋转它们。
到目前为止,当文件达到 10MB 时就会轮换日志。
LOG_PATH = os.path.join(APPLICATION_PATH, "log\\My_log.log")
my_handler = RotatingFileHandler(LOG_PATH, mode='a', maxBytes=10000000, backupCount=20)
logger.addHandler(my_handler)
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以传递date
参数,RotatingFileHandler()
以便在文件达到 10MB 后不再旋转,而是在午夜旋转文件?
我最近遇到了一个问题,我必须在C++项目中处理一些逻辑,不幸的是我无法正确使用它goto
.
问题是什么?
我有一个函数,命名ProcessRunning(const char *name)
它来验证进程是否存在(它返回true
)或不存在(它返回false
).
我还有一个4层的功能,int fun1(const char name[])
,int fun2(const char name[])
,int fun3(const char name[])
,int fun4(const char name[])
. - 这些功能中的每一个都是这样的:
int fun1(const char name[]){
//open a new .bat file
//start of what I write in .bat
//taskkill process.exe
//start again process.exe
//delete the created file
//end of what I write in the file
return system(name);
}
Run Code Online (Sandbox Code Playgroud)
其他3个功能完全相同.
现在,在我的main()
程序中,我尝试执行以下操作:
int main(){
//...
start_again: …
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些项目的列表.我想按如下方式重新排列它们:
product_name
应该是第一个元素items
从图像开始应该是最后一个(也应该排序)杂项:product_name
我的列表中只有一个(并且总是一个),里面的项目没有特定的顺序排列(也就是说,项目的顺序是随机的 - 我只举了一个例子).此外,该列表包含唯一items
.
以下是它的工作,但似乎有点冗长和低效.我确信有一种方法可以在单个for循环中执行此操作,但我现在有一个脑筋.是否有更简单的方法来实现以下目标?
def rearrange(header):
final = ['product_name']
images = sorted([item for item in header if item.startswith('image')])
for item in header:
if item != 'product_name' and not item.startswith('image'):
final.append(item)
final += images
return final
header = [
'word2',
'image_4',
'word1',
'product_name',
'image_3',
'image_1',
'image_5',
'word3',
'image_6',
'image_2',
]
print(rearrange(header))
Run Code Online (Sandbox Code Playgroud)
结果:
Run Code Online (Sandbox Code Playgroud)['product_name', 'word2', 'word1', 'word3', 'image_1', 'image_2', 'image_3', 'image_4', 'image_5', 'image_6']
我有一个程序,可以创建一个从文本文件中命名的给定数量的文件夹.我有以下算法:
private void button2_Click(object sender, EventArgs e)
{
if (path != null && Directory.Exists(path))
{
Random rnd = new Random();
for (int i = 0; i < value; i++)
{
var lines = File.ReadAllLines(path1);
var randomLineNumber = rnd.Next(0, lines.Length);
var line = lines[randomLineNumber];
StringBuilder b = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c = line[j];
if (rnd.Next(2) == 0)
{
c = Char.ToUpper(c);
}
b.Append(c);
if (j % 2 == 1)
{
b.Append(rnd.Next(10)); …
Run Code Online (Sandbox Code Playgroud) python ×6
python-3.x ×3
django ×2
algorithm ×1
asynchronous ×1
c ×1
c# ×1
c++ ×1
io ×1
javascript ×1
list ×1
logging ×1
lxml ×1
nginx ×1
regex ×1
string ×1
tuples ×1
uwsgi ×1
web-scraping ×1
winforms ×1