假设我有这样的简单 ActiveRecord 模型:
class Post < ActiveRecord::Base
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :posts
has_many :published_posts, -> { where(:published => true) }
end
Run Code Online (Sandbox Code Playgroud)
我想创建一个模块Reindexable,它将添加一个调用reindex到基类中的方法。我希望能够通过以下 3 种方式调用此方法:
Place.reindex
Place.reindex(Place.where(:published => true))
Place.where(:published => true).reindex
Category.first.places.reindex
Run Code Online (Sandbox Code Playgroud)
在这个方法里面,我应该能够做这样的事情:
Reindexer.new(relation).reindex # how can I get relation here?
Run Code Online (Sandbox Code Playgroud)
在 Ruby-on-Rails 中执行此操作的正确方法是什么?在所有这种情况下,我如何访问当前关系?
如果有的话,我想在 Python 中访问某种导入依赖项跟踪。
我决定在我的模块中添加一个__dependencies__描述模块导入的所有模块版本的字典。
我想要一种自动获取模块导入的模块列表的方法。最好在模块的最后一行。
ModuleFinder(如如何在 Python 模块中列出导入所建议的那样?)将不起作用,因为应对已加载的模块执行检查。
另一个问题ModuleFinder是它检查 Python脚本(带if __name__ == '__main__'分支),而不是模块。
如果我们考虑一个玩具脚本 script.py:
if __name__ == '__main__':
import foo
Run Code Online (Sandbox Code Playgroud)
那么结果是:
>>> mf = ModuleFinder
>>> mf.run_script('script.py')
>>> 'foo' in mf.modules
True
Run Code Online (Sandbox Code Playgroud)
如果脚本作为模块导入,则应该为 False。
我不想列出所有导入的模块 - 仅由我的模块导入的模块 - 所以sys.modules(建议在 python 中列出所有导入的模块的最佳方法是什么?)会返回太多。
我可以比较sys.modules模块代码开头和结尾的快照。但是那样我会错过我的模块使用的所有模块,但之前由任何其他模块导入。
列出模块从中导入对象的模块也很重要。
如果我们考虑一个玩具模块 example.py:
from foo import bar
import baz
Run Code Online (Sandbox Code Playgroud)
那么结果应该是这样的:
>>> import example
>>> moduleImports(example)
{'foo': <module 'foo' …Run Code Online (Sandbox Code Playgroud) 我没有使用python,BeautifulSoup,Selenium等的经验,但是我很想从网站上抓取数据并将其存储为csv文件。我需要的单个数据样本编码如下(单个数据行)。
<div class="box effect">
<div class="row">
<div class="col-lg-10">
<h3>HEADING</h3>
<div><i class="fa user"></i> NAME</div>
<div><i class="fa phone"></i> MOBILE</div>
<div><i class="fa mobile-phone fa-2"></i> NUMBER</div>
<div><i class="fa address"></i> XYZ_ADDRESS</div>
<div class="space"> </div>
<div style="padding:10px;padding-left:0px;"><a class="btn btn-primary btn-sm" href="www.link_to_another_page.com"><i class="fa search-plus"></i> more info</a></div>
</div>
<div class="col-lg-2">
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要的输出是
Heading,NAME,MOBILE,NUMBER,XYZ_ADDRESS
我发现这些数据没有ID或类别,但仍以通用文本形式出现在网站中。为此,我分别尝试使用BeautifulSoup和Python Selenium,在这两种方法中我都被困于提取方法,因为我没有看到任何教程,指导我从这些方法和标签中提取文本
我的代码使用BeautifulSoup
import urllib2
from bs4 import BeautifulSoup
import requests
import csv
MAX = 2
'''with open("lg.csv", "a") as f:
w=csv.writer(f)'''
##for i in range(1,MAX+1)
url="http://www.example_site.com"
page=requests.get(url)
soup = BeautifulSoup(page.content,"html.parser")
for h in soup.find_all('h3'):
print(h.get('h3'))
Run Code Online (Sandbox Code Playgroud)
我的硒代码
import …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用BeautifulSoup在维基百科页面中找到一个表格,由于某种原因,我没有得到该表格.任何人都可以告诉我为什么不拿桌子?
我的代码:
import BeautifulSoup
import requests
url='https://en.wikipedia.org/wiki/List_of_National_Historic_Landmarks_in_Louisiana'
r=requests.get(url)
url=r.content
soup = BeautifulSoup(url,'html.parser')
tab=soup.find("table",{"class":"wikitable sortable jquery-tablesorter"})
print tab
Run Code Online (Sandbox Code Playgroud)
打印:无
我是Django Framework的新用户.我目前正在使用django_rest_framework构建REST API.启动我的服务器时,我收到了弃用警告,我不知道如何修复.
RemovedInDjango110Warning:'get_all_related_objects是一个已弃用的非官方API.你可以用opts.get_all_related_objects()中的关系'get_fields()'替换它
以上是第一个.有谁知道如何解决这个问题.我现在在API中的所有内容都是使用内置ModelViewSet的标准休息调用,我也用自己的方式覆盖了默认的身份验证和用户系统,所以我不知道为什么我会收到这些警告,因为我一直在使用Django 1.9从一开始.
我也得到了这个:
RemovedInDjango110Warning:必须使用dict调用render(),而不是RequestContext
从我最初的研究来看,这与模板有关.我没有使用任何模板,所以我不知道为什么会这样.
任何人都可以帮我解决这些问题吗?
我有以下 HTML,我应该如何从变量中提取 JSON:window.__INITIAL_STATE__
<!DOCTYPE doctype html>
<html lang="en">
<script>
window.sessConf = "-2912474957111138742";
/* <sl:translate_json> */
window.__INITIAL_STATE__ = { /* Target JSON here with 12 million characters */};
/* </sl:translate_json> */
</script>
</html>
Run Code Online (Sandbox Code Playgroud) 我必须从我的GA电子商务中检索尽可能多的不同指标.我正在使用google api ruby客户端.并继续收到错误:
"message"=>"Selected dimensions and metrics cannot be queried together."
Run Code Online (Sandbox Code Playgroud)
例如,对于此请求:
result = client.execute(api_method: api_method.data.ga.get, parameters: {
'ids' => 'ga:95561781',
'start-date' => Date.new(2006,1,1).to_s,
'end-date' => Date.today.to_s,
'dimensions' => 'ga:source,ga:medium,ga:country,ga:transactionId,ga:affiliation,ga:productSku,ga:productName',
'metrics' => 'ga:sessions,ga:transactionRevenue,ga:transactions,ga:uniquePurchases,ga:totalValue,ga:transactionTax'
})
Run Code Online (Sandbox Code Playgroud)
我如何知道我可以结合哪些请求指标和维度?我怎么能尽可能多地检索数据呢?
我真的以为我已经在这上面找到了一些东西,也许它就在那里,我想念它.如果是这种情况我道歉,我会关闭这个问题.
我正在检查模运算是否返回零结果,我想知道哪些是"更好"(更多pythonic,更快,无论如何):
if not count % mod 要么 if count % mod == 0
我想我应该澄清并说我对truthy和falsey值非常了解,我只是想知道是否有一个具体的理由使用一个而不是另一个.特别是考虑到这总是一个数字(否则%操作符会抛出一个TypeError).
我有2个数组
a = [2,3,1,4]
b = [{id: 1}, {id: 2}, {id: 3}, {id: 4}]
Run Code Online (Sandbox Code Playgroud)
如何b根据排序a?我想要的输出是
c = [{id: 2}, {id: 3}, {id: 1}, {id: 4}]
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用Ramda或常规JS。
我有这个脚本:
#!/usr/bin/python
import subprocess
import sys
HOST="cacamaca.caca"
COMMAND="display mac-address 0123-4567-8910"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
Run Code Online (Sandbox Code Playgroud)
因为输出有空格和不同的行,它也打印回车和换行,所以结果不是一个干净的:
['\r\n', 'cacamaca.caca\r\n', '信息:最大VTY用户数为10,当前在线VTY用户数\r\n','为2。\ r\n', ' 当前登录时间为 2017-07-20 20:10:54+03:00 DST。\r\n', '---------------- -------------------------------------------------- -------------\r\n', 'MAC Address VLAN/VSI Learned-From Type \r\n', '-------------- -------------------------------------------------- ---------------\r\n', '0123-4567-8910 1234/- Eth-Trunk9 动态 \r\n', '\r\n', '-- -------------------------------------------------- ---------------------------\r\n', '显示的项目总数 = 1 \n', '\r\n', '']
如何删除 '\n' 和 '\r\n' …
python ×7
html ×2
python-2.7 ×2
activerecord ×1
django ×1
django-views ×1
javascript ×1
json ×1
python-3.x ×1
ramda.js ×1
rest ×1
ruby ×1
selenium ×1
web-crawler ×1