[Python 3.1]
我正在尝试为容器创建一个哈希,其中可能有嵌套容器,深度未知.在层次结构的所有级别,只有内置类型.有什么好办法呢?
为什么我需要它:
我在一个pickle对象(在磁盘上)缓存一些计算的结果.如果使用不同的参数调用该函数,我将需要使该缓存的文件无效(这种情况很少发生,因此我不打算将多个文件保存到磁盘).哈希将用于比较参数.
第一次python用户在这里,温柔.... ;-)
OSX上的Python 2.6
有一个类只有一些围绕sqlite包装...这里是
from pysqlite2 import dbapi2 as sqlite
class SqliteDB:
connection = ''
curser = ''
def connect(self):
try:
self.connection = sqlite.connect("pagespeed.sqlite")
self.curser = self.connection.cursor()
except sqlite.Error, e:
print "Ooops: ", e.args[0]
def find_or_create(self, table, column, value):
self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))
records = self.curser.fetchall()
if records.count() == false:
self.curser.execute("INSERT into ? SET ?=?", (table, column, value))
self.curser.execute("SELECT id FROM ? WHERE ?=? LIMIT 1", (table, column, value))
print records
Run Code Online (Sandbox Code Playgroud)
我在一个单独的文件中这样称呼它 …
我在使用 Numpy 的 nan_to_num 函数时遇到了一些问题:我有一个数组,其中最后一列包含的元素比其他列少,当我将它导入 Python 时,它会放置 'nan's 来填充数组。那很好,直到我需要做一些被'nan's绊倒的其他事情。
我正在尝试使用“nan_to_num”但没有成功。这可能是我遗漏的一件小事,但我无法弄清楚。
下面是一些简单的输入和输出:
输入:
a = numpy.array([[1, nan, 3]])
print a
numpy.nan_to_num(a)
print a
Run Code Online (Sandbox Code Playgroud)
输出
[[ 1. nan 3.]]
[[ 1. nan 3.]]
Run Code Online (Sandbox Code Playgroud)
第二个“nan”应该是零... http://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html
提前致谢。
我想在python中开发一个代码,它将在localhost中打开一个端口,并将日志发送到该端口.日志只是python文件的命令输出.
喜欢 :
hello.py
i = 0
while True:
print "hello printed %s times." % i
i+=1
Run Code Online (Sandbox Code Playgroud)
这将不断打印声明.我想要这个续.输出发送到打开的端口.
任何人都可以告诉我如何做同样的事情?
提前致谢
如果我编写一个python脚本,任何人都可以简单地将编辑器指向它并阅读它.但对于用C编写的编程,必须使用反编译器和十六进制表等.这是为什么?我的意思是我根本无法打开Safari Web浏览器并查看其代码.
请帮我解决这个错误
C:\Python26\Lib\site-packages\pyqrcode\encoder>python setup.py install
running install
running bdist_egg
running egg_info
writing qrcode.egg-info\PKG-INFO
writing top-level names to qrcode.egg-info\top_level.txt
writing dependency_links to qrcode.egg-info\dependency_links.txt
package init file 'qrcode\__init__.py' not found (or not a regular file)
writing manifest file 'qrcode.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
running build_ext
building 'qrcode.Encoder' extension
error: Unable to find vcvarsall.bat
Run Code Online (Sandbox Code Playgroud)
谢谢,
马努
如何搜索文件中的行并提取搜索行的上方和下方的行.
我的意见就像
Tue Jun 26 14:59:46 2012
Warning ffffffff act_msg_ctms_remove_from_pending_queue: deleting message 44817201 from the queue.
Tue Jun 26 14:59:46 2012
Warning ffffffff Finishing processing record number 44817201
Tue Jun 26 14:59:46 2012
Warning 5000000 activity_queue_manager_finish_cb: unknown activity 120.
Tue Jun 26 14:59:46 2012
Warning ffffffff Activity State Machine priority (2) finished
Tue Jun 26 14:59:46 2012
Warning ffffffff
====================================================
Processing database file "INCOMING_MESSAGES" record number 47810234 from user "(unknown)"
Tue Jun 26 14:59:46 2012
Warning ffffffff ACTIVITY data: rec_num …Run Code Online (Sandbox Code Playgroud) 我有一个长度为N的列表,此列表的每个元素都是0或1.我需要获得此列表的所有可能组合.这是我的代码:
def some(lst):
result = []
for element in lst:
c1 = copy.copy(element)
c2 = copy.copy(element)
c1.append(0)
c2.append(1)
result.append(c1)
result.append(c2)
return result
def generate(n):
if(n == 1):
return [[0], [1]]
else:
return some(generate(n - 1))
print generate(4)
Run Code Online (Sandbox Code Playgroud)
我认为这个任务有更多的pythonic解决方案.提前致谢.
在尝试编译以下代码时,我不断收到"写入已关闭的文件错误":
fout = open('markov_output.txt', 'w')
for i in range( MAXGEN ) :
# get our hands on the list
key = (w1,w2)
sufList = table[key]
# choose a suffix from the list
suf = random.choice( sufList )
if suf == NONWORD : # caught our "end story" marker. Get out
if len( line ) > 0 :
fout.write(line)
break
if len( line ) + len( suf ) > MAX_LINE_LEN :
fout.write(line)
line = ""
line = line + " " …Run Code Online (Sandbox Code Playgroud) 如何在厨师食谱中转换以下shell命令?
安装
sudo easy_install pygments
sudo echo 'export RAILS_ENV=production' >> ~/.bash_profile
sudo adduser --system --shell /bin/sh --gecos 'git version control' --group --disabled-password --home /home/git git
ssh-keygen -t rsa
sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub
sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
echo "gem: --no-rdoc --no-ri" > ~/.gemrc
rvmsudo gem install passenger
rvmsudo passenger-install-nginx-module
Run Code Online (Sandbox Code Playgroud)
为nginx创建Upstart文件
sudo vim /etc/init/nginx.conf
description "Nginx HTTP Server"
start on filesystem
stop on runlevel [!2345]
respawn
exec /opt/nginx/sbin/nginx -g "daemon off;"
Run Code Online (Sandbox Code Playgroud)
重启虚拟机
sudo shutdown -r now
Run Code Online (Sandbox Code Playgroud)
验证nginx正在运行
sudo initctl …Run Code Online (Sandbox Code Playgroud) 好吧,如果你在Python中运行下面的代码它只打印问题变量的第一个字母,但是它打印其余的就好了.只有在我的Python脚本中有for循环函数时才会发生这种情况.有关如何修复它的任何想法,所以我得到整个问题变量打印?
import random
global nouns
global verbs
global question
nouns =["website","browser","server","printer","computer","disc","software","desktop","a internet connection","the internet","site","forum","smf forum","phpbb forum","money making website","money making blog","firefox","chrome","opera","",""]
verbs = ["cosntruct","build","create","design","update","reconstruct","clean","fix","repair","browse","discover","formualte","form","plan"]
question = ["How do I","How would I", "how do i", "how would i", "what is a", "what is the", "how would i", "how should i", "when does a", "When does a", "How should I"]
def q_gen():
global nouns
global verbs
global question
noun_pick = random.choice(nouns)
verb_pick = random.choice(verbs)
question = random.choice(question)
create = question+" "+verb_pick+" "+noun_pick+"?"
print …Run Code Online (Sandbox Code Playgroud) 使用simpleJSON的JSON序列化Python
如何创建对象以便我们可以优化对象的序列化
我正在使用simpleJSON
1,2是固定变量
3是类别和分数的固定字典
图4是长度固定的dicts阵列(4),该阵列是在运行时特定的长度.
这个过程需要尽可能快,所以我不确定最好的解决方案.
{
"always-include": true,
"geo": null,
"category-score" : [
{
"Arts-Entertainment": 0.72,
"Business": 0.03,
"Computers-Internet": 0.08,
"Gaming": 0.02,
"Health": 0.02,
}
],
"discovered-entities" : [
{
'relevance': '0.410652',
'count': '2',
'type': 'TelevisionStation',
'text': 'Fox News'
},
{
'relevance': '0.396494',
'count': '2',
'type': 'Organization',
'text': 'NBA'
}
]
],
}
Run Code Online (Sandbox Code Playgroud) python ×10
algorithm ×1
c ×1
chef-infra ×1
chef-recipe ×1
command-line ×1
containers ×1
decompiling ×1
function ×1
grep ×1
hash ×1
installation ×1
nested ×1
numpy ×1
python-3.x ×1
qr-code ×1
send ×1
simplejson ×1
sqlite ×1
tcp ×1
unix ×1
vagrant ×1
variables ×1