我需要创建一个临时文件来发送它,我试过:
# Create a temporary file --> I think it is ok (file not seen)
temporaryfile = NamedTemporaryFile(delete=False, dir=COMPRESSED_ROOT)
# The path to archive --> It's ok
root_dir = "something"
# Create a compressed file --> It bugs
data = open(f.write(make_archive(f.name, 'zip', root_dir))).read()
# Send the file --> Its ok
response = HttpResponse(data, mimetype='application/zip')
response['Content-Disposition'] = 'attachment; filename="%s"' % unicode(downloadedassignment.name + '.zip')
return response
Run Code Online (Sandbox Code Playgroud)
我根本不知道这是不是很好的方法..
如何在OSX上使用MAMP安装php 7.2.1的gmp扩展?
我正在尝试使用带有P-256曲线和SHA-256哈希算法的椭圆曲线数字签名算法(ECDSA)使用php和此库(lcobucci/jwt)加密令牌.
运行php脚本时出错:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'Mdanter\Ecc\EccFactory' not found
Run Code Online (Sandbox Code Playgroud)
我想我错过了mdanter/ecc
依赖包,所以我尝试运行以下命令.
命令运行:
composer require mdanter/ecc
Run Code Online (Sandbox Code Playgroud)
输出:
mdanter/ecc v0.5.0 requires ext-gmp * -> the requested PHP extension gmp is missing from your system
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一切我能找到的研究这个错误并与MAMP相关的内容.但我无法解决这个问题.这里有没有人有专门为MAMP添加gmp php扩展的经验,并愿意帮助我吗?
注意:我不是一个非常有经验的Web开发人员,我主要创建iOS应用程序,我想在php中生成令牌,因此我可以访问Apple Music Kit API资源.
大部分信息在网上查到说,这可能是与完成unzip(1)
,但遗憾的是它不一样了,.ipa文件格式已经改变,unzip -v xyz.ipa
:
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Stored 0 0% 09-18-2018 15:38 00000000 META-INF/
379 Unk:099 367 3% 09-19-2018 08:44 bf0c5de5 META-INF/com.apple.ZipMetadata.plist
23 Stored 23 0% 09-19-2018 08:44 132aa79c META-INF/com.apple.FixedZipMetadata.bin
0 Stored 0 0% 09-18-2018 15:36 00000000 Payload/
0 Stored 0 0% 09-19-2018 23:44 00000000 Payload/xyz.app/
0 Stored 0 0% 09-18-2018 15:36 00000000 Payload/xyz.app/_CodeSignature/
358128 Unk:099 84505 76% 09-19-2018 23:44 7f51c7bf Payload/xyz.app/_CodeSignature/CodeResources
10131 …
Run Code Online (Sandbox Code Playgroud) 有没有办法duplicate-code
只为测试文件禁用Pylint的消息?我们项目中的所有测试都是DAMP,因此重复的代码是设计的.我知道我们可以在# pylint: disable=duplicate-code
整个测试过程中添加,但宁可添加一些规则,说明文件test/
夹下的所有文件都会禁用此规则.有没有办法做到这一点?
更具体地说,我正在寻找与"运行两次"解决方案不同的东西(这是我已经重新开始的).
我有一个叫做约会的模型,它的列datetime
是一个DateTime
字段,duration
它是一个Integer
字段,代表持续时间,以分钟为单位.现在我要检查,如果func.now()
是与datetime
任命和的总和datetime
和duration
我目前正试图这样做,但我需要一个适用于PostgreSQL和SQLite的解决方案.
current_appointment = Appointment.query.filter(
Appointment.datetime.between(
func.now(),
func.timestampadd(
'MINUTES', Appointment.duration, func.now()
)
)
).limit(1).one_or_none()
Run Code Online (Sandbox Code Playgroud) 我喜欢typing.NamedTuple
Python 3.6中的。但是通常情况下,namedtuple
包含非哈希属性,而我想将其用作dict
键或set
成员。如果一个namedtuple
类使用对象标识(id()
for __eq__
和__hash__
)有意义,那么将这些方法添加到该类中就可以正常工作。
但是,现在我的代码在多个地方都有这个模式,我想摆脱样板__eq__
和__hash__
方法定义。我知道namedtuple
'不是普通班,我还无法弄清楚如何使它正常工作。
这是我尝试过的:
from typing import NamedTuple
class ObjectIdentityMixin:
def __eq__(self, other):
return self is other
def __hash__(self):
return id(self)
class TestMixinFirst(ObjectIdentityMixin, NamedTuple):
a: int
print(TestMixinFirst(1) == TestMixinFirst(1)) # Prints True, so not using my __eq__
class TestMixinSecond(NamedTuple, ObjectIdentityMixin):
b: int
print(TestMixinSecond(2) == TestMixinSecond(2)) # Prints True as well
class ObjectIdentityNamedTuple(NamedTuple):
def __eq__(self, other):
return self is …
Run Code Online (Sandbox Code Playgroud) 为什么以下使用该concurrent.futures
模块的Python代码会永远挂起?
import concurrent.futures
class A:
def f(self):
print("called")
class B(A):
def f(self):
executor = concurrent.futures.ProcessPoolExecutor(max_workers=2)
executor.submit(super().f)
if __name__ == "__main__":
B().f()
Run Code Online (Sandbox Code Playgroud)
这个调用产生了一个无形的例外[Errno 24] Too many open files
(见它,更换线路executor.submit(super().f)
用print(executor.submit(super().f).exception())
)。
但是,按预期将打印替换ProcessPoolExecutor
为ThreadPoolExecutor
“被调用”。
为什么以下使用该multiprocessing.pool
模块的Python代码引发异常AssertionError: daemonic processes are not allowed to have children
?
import multiprocessing.pool
class A:
def f(self):
print("called")
class B(A):
def f(self):
pool = multiprocessing.pool.Pool(2)
pool.apply(super().f)
if __name__ == "__main__":
B().f()
Run Code Online (Sandbox Code Playgroud)
但是,按预期将打印替换Pool
为ThreadPool
“被调用”。
环境:CPython 3.7,MacOS …
基本上,我意识到我正在test_update_with_only_1_field
为多个模型的相似网址编写相同的测试用例()
from django.test import RequestFactory, TestCase
class BaseApiTest(TestCase):
def setUp(self):
superuser = User.objects.create_superuser('test', 'test@api.com', 'testpassword')
self.factory = RequestFactory()
self.user = superuser
self.client.login(username=superuser.username, password='testpassword')
class SomeModelApiTests(base_tests.BaseApiTest):
def test_update_with_only_1_field(self):
"""
Tests for update only 1 field
GIVEN the following shape and related are valid
WHEN we update only with just 1 field
THEN we expect the update to be successful
"""
shape_data = {
'name': 'test shape',
'name_en': 'test shape en',
'name_zh_hans': 'test shape zh hans',
'serial_number': 'test shape serial …
Run Code Online (Sandbox Code Playgroud) 我有一个启动子send_signal(SIGTERM)
进程的服务器,我可以设法执行一个会终止该进程的操作。但并不优雅。如果我从 shell 调用我的子进程(即作为单个进程),则定义的信号处理程序将正常启动并退出。
server.py:(所以..从我第一次调用的另一个脚本start_app()
,然后exit_app()
def start_app():
app = subprocess.Popen("python app.py")
def exit_app():
p = app.poll()
if p==None:
print("Subprocess is alive") # debug
app.send_signal(signal.SIGTERM)
Run Code Online (Sandbox Code Playgroud)
应用程序
def exit_signal_handler(signal, frame):
print("Terminate signal received")
app.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
signal.signal(signal.SIGTERM, exit_signal_handler)
signal.signal(signal.SIGINT, exit_signal_handler)
sys.exit(app.exec())
Run Code Online (Sandbox Code Playgroud)
同样,如果我从 shell 调用 app.py 并发送一个SIGTERM
信号,我会得到一个跟踪Terminate signal received
并关闭应用程序。但是当 app.py 由服务器启动并且我exit_app
在服务器中调用时,我得到一个跟踪Subprocess is alive
(来自 server.py)并且应用程序被杀死但信号没有被应用程序的信号处理程序捕获exit_signal_handler
编辑:这似乎send_signal()
不发送信号,以在该子捕获信号意义上的子进程。它发送信号以在子进程上发生一个动作:
def send_signal(self, sig):
"""Send a signal …
Run Code Online (Sandbox Code Playgroud) 我有一个使用 http.server 的小型演示页面。我尝试与同事共享,但发现 http.server 在任何打开的连接上仍然被阻止,因此无法为并发用户提供服务。有没有办法运行 http.server 来处理并发连接?我在这里没有发现任何有用的东西:https ://docs.python.org/3/library/http.server.html
我的任务是构建一个API的使用者,该API需要一个加密的令牌,其种子值是UNIX时间.我展示的示例是使用我不熟悉的Java实现的,在阅读完文档和其他堆栈文章之后,我们无法找到解决方案.
使用javax.crypto.SecretKey
,javax.crypto.SecretKeyFactory
,javax.crypto.spec.PBEKeySpec
,和javax.crypto.spec.SecretKeySpec
协议,我需要生成令牌类似如下:
public class EncryptionTokenDemo {
public static void main(String args[]) {
long millis = System.currentTimeMillis();
String time = String.valueOf(millis);
String secretKey = "somekeyvalue";
int iterations = 12345;
String iters = String.valueOf(iterations);
String strToEncrypt_acctnum = "somevalue|" + time + "|" + iterations;
try {
byte[] input = strToEncrypt_acctnum.toString().getBytes("utf-8");
byte[] salt = secretKey.getBytes("utf-8");
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey tmp = factory.generateSecret(new PBEKeySpec(secretKey.toCharArray(), salt, iterations, 256));
SecretKeySpec skc = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher …
Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×2
composer-php ×1
django ×1
hashlib ×1
hmacsha1 ×1
http ×1
httpserver ×1
ios ×1
ipa ×1
java ×1
mamp ×1
namedtuple ×1
pbkdf2 ×1
php ×1
php-gmp ×1
pickle ×1
postgresql ×1
process-pool ×1
pylint ×1
python-3.6 ×1
signals ×1
sqlalchemy ×1
sqlite ×1
subprocess ×1
unit-testing ×1
unzip ×1
windows ×1
zip ×1