我有以下查询:
profiles = session.query(profile.name).filter(and_(profile.email == email, profile.password == password_hash))
Run Code Online (Sandbox Code Playgroud)
如何检查是否有一行,如何返回第一行(如果匹配则应该只有一行)?
我正在尝试使用Python 3内置的JSON模块进行一些简单的JSON解析,并且通过阅读关于SO和谷歌搜索的一堆其他问题,看起来这应该是非常简单的.但是,我想我得到一个字符串而不是预期的字典.
首先,这是我试图从中获取值的JSON.这只是Twitter API的一些输出
[{'in_reply_to_status_id_str': None, 'in_reply_to_screen_name': None, 'retweeted': False, 'in_reply_to_status_id': None, 'contributors': None, 'favorite_count': 0, 'in_reply_to_user_id': None, 'coordinates': None, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'geo': None, 'retweet_count': 0, 'text': 'Tweeting a url \nhttp://t.co/QDVYv6bV90', 'created_at': 'Mon Sep 01 19:36:25 +0000 2014', 'entities': {'symbols': [], 'user_mentions': [], 'urls': [{'expanded_url': 'http://www.isthereanappthat.com', 'display_url': 'isthereanappthat.com', 'url': 'http://t.co/QDVYv6bV90', 'indices': [16, 38]}], 'hashtags': []}, 'id_str': '506526005943865344', 'in_reply_to_user_id_str': None, 'truncated': False, 'favorited': False, 'lang': 'en', 'possibly_sensitive': False, 'id': 506526005943865344, 'user': {'profile_text_color': '333333', 'time_zone': None, 'entities': {'description': …
Run Code Online (Sandbox Code Playgroud) 我需要使用Python 3捕获192 kHz音频以进行一些生物声学实验.我有硬件,声音设备USBPre 2声卡,具有高达100 kHz的良好频率响应曲线的麦克风,我已启用我的操作系统(ubuntu 13.04)从这张卡以192 kHz采样.
我试过用PyAudio录音.它似乎工作,并将给我一个采样率为192 kHz的wav文件.然而,当我观察光谱时,没有超过24 kHz的功率,这表明PyAudio不是真正捕获192 kHz,而是48 kHz.但是,当我使用Audacity录制来自JACK的输入时,我得到了一个很好的录音,功率高达96kHz.所以,我的印象是PyAudio实际上并没有以192 kHz采样声音,即使它应该能够.如何解决这个问题?
我没有错误地启动JACK:
/usr/bin/jackd -R -dalsa -Chw:1,0 -n3 -o1 -p2048 -r192000
jackd 0.122.0
Copyright 2001-2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and others.
jackd comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see the file COPYING for details
JACK compiled with System V SHM support.
loading driver ..
apparent rate = 192000
creating alsa driver ... …
Run Code Online (Sandbox Code Playgroud) 我想以类似的方式分割一个字符串.split()
(因此产生一个列表),但是以一种更聪明的方式:我希望它将它拆分成最多15个字符的块,但不会拆分中间字所以:
string = 'A string with words'
[splitting process takes place]
list = ('A string with','words')
Run Code Online (Sandbox Code Playgroud)
此示例中的字符串分为"with"和"words",因为这是您可以拆分它的最后一个位置,第一个位是15个字符或更少.
一方面,我们鼓励我们只创建字段,而不是使用以其他语言为模型的额外存取函数来包含我们的python类.
我正在做一些时区转换,我得到了非常奇怪的结果.基本上只在整个小时不同的时区之间进行转换,我仍然得到非完整的结果.例如:
from datetime import datetime
from pytz import timezone
datetime(2013, 12, 27, 20, 0, 0, tzinfo=timezone('Europe/Bucharest'))\
.astimezone(timezone('Europe/Berlin')).replace(tzinfo=None)
Run Code Online (Sandbox Code Playgroud)
给我
datetime.datetime(2013, 12, 27, 19, 16)
Run Code Online (Sandbox Code Playgroud)
(布加勒斯特和柏林之间的时差是1小时,所以我应该得到19:00 - 而不是19:16)
我可能错过了一些非常明显的东西,但我无法弄清楚.我究竟做错了什么?
[Windows 7 64 位;蟒蛇2.7]
如果我尝试使用 Urllib2,则会收到此错误
Traceback (most recent call last):
File "C:\Users\cYanide\Documents\Python Challenge\1\1.py", line 7, in <module>
response = urllib2.urlopen('http://python.org/')
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 438, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, …
Run Code Online (Sandbox Code Playgroud) 自Unix时代开始以来,我有几秒钟的时间.我想用24小时制作它们.到目前为止我的努力是
from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
angles = 2*np.pi*np.random.randint(0,864000,100)/86400
ax = plt.subplot(111, polar=True)
ax.scatter(angles, np.ones(100)*1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这给出了以下内容
但是,这并不是我想要的.
0.2, 0.4, ...
?使用Python,是否可以在本地主机和某个端口上发送UDP数据,然后在同一程序内同时监听本地主机上的不同端口?我一直在使用错误48'地址已经在使用'并尝试使用python的重用地址,虽然我很确定它无论如何都不适用于这个应用程序.
背景:我对软件开发一无所知,更不用说Python,这只是某人在工作中要求的东西.
我感谢任何帮助.
from threading import Thread
import time
import socket
HOST = 'localhost'
PORT = 5455
PORT1 = 5457
data1 = "1"
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST,PORT1))
a = 0
def myfunction(string, *args):
while 1:
cmd = int( raw_input("send message: ") )
if (cmd == 1):
s.sendto(data1, (HOST,PORT))
time.sleep(1)
def myfunction2(string, *args):
while 1:
print s.recv(30)
time.sleep(.5)
if __name__=='__main__':
try:
Thread(target=myfunction, args=(a, 1)).start()
Thread(target=myfunction2, args=(a, 1)).start()
except Exception, errtxt:
print errtxt
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现最小化函数的惩罚函数方法。我需要找到Rosenbrok's function的最小值。
我正在使用这个惩罚函数:
首先,我使用scipy.optimize.minimize
以下方法找到了最小值:
from scipy.optimize import minimize, rosen
rz = lambda x: (1-x[0])**2 + 100*(x[1] - x[0]**2)**2;
h_1 = lambda x: (x[0] - 2 * x[1] + 2);
h_2 = lambda x: (-x[0] - 2 * x[1] + 6);
h_3 = lambda x: (-x[0] + 2 * x[1] + 2);
x0 = [2.3, 5];
cons = ({'type': 'ineq', 'fun': h_1},
{'type': 'ineq', 'fun': h_2},
{'type': 'ineq', 'fun': h_3})
minimize(rz, x0, constraints=cons)
Run Code Online (Sandbox Code Playgroud)
答案是x
:array([ …