本文将分享javascript中是否有类似于Python的实现.__contains__()的.contains方法?-节点.js的详细内容,此外,我们还将为大家带来关于【解决】MacOS下Pyth
本文将分享javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js的详细内容,此外,我们还将为大家带来关于【解决】MacOS 下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter、63.Python中contains和icontains、contains和compareDocumentPosition 方法来确定是否HTML节点间的关系_javascript技巧、Hashtable的contains() 、containsKey()和containsValue() 区别的相关知识,希望对你有所帮助。
本文目录一览:- javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js
- 【解决】MacOS 下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter
- 63.Python中contains和icontains
- contains和compareDocumentPosition 方法来确定是否HTML节点间的关系_javascript技巧
- Hashtable的contains() 、containsKey()和containsValue() 区别
javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js
如何解决javascript 中是否有类似于 Python 的实现 .__contains__() 的 .contains 方法? - 节点.js?
我想知道 javascript 中是否有任何 object.contains
方法的实现,其工作方式类似于 Python 版本 object.__contains__()
的工作方式,因为我希望能够扫描整个对象嵌套对象以查看是否存在包含该方法与之进行比较的内容的键或值。我知道在 Javascript 中有其他方法可以做到这一点,例如使用 filter
和 some
,但它们对我不起作用。
person: {
name: ''Bob'',age: 40,items: {
cars: 4,house: 1,computer: 2
}
}
我需要扫描整个对象的东西,而不仅仅是它的第一级(这将是 name
、age
和 items
,如果您搜索 {{1 }} 你不会得到任何回应)。
解决方法
我不这么认为。一个天真的想法是
JSON.stringify(target).includes( JSON.stringify( search ) )
如果 search
不是字符串,它就不能很好地工作,因为它也会在字符串内部匹配。一个非天真的方法是这样的:
const contains = (obj,search) =>
Object.keys(obj).includes(search) ||
Object.values(obj).includes(search) ||
Object.values(obj)
.filter(it => typeof it === "object" && it !== null)
.some(it => contains(it,search));
,
我猜没有标准函数,但您肯定可以使用一些 typeof
和 Object.entries()
编写自定义递归函数。
let person = {
name: ''Bob'',age: 40,items: {
cars: 4,house: 1,computer: 2
}
};
function searchForTerm(obj,item){
for(let [key,val] of Object.entries(obj)){
if(key == item){
return true;
}
else if(typeof val === ''object'')
{
if(searchForTerm(val,item)){
return true;
}
}
else if(val == item){
return true;
}
}
return false;
}
console.log(searchForTerm(person,40));
console.log(searchForTerm(person,43));
console.log(searchForTerm(person,''Bob''));
console.log(searchForTerm(person,''cars''));
console.log(searchForTerm(person,''truck''));
我在第一种情况下使用 ==
,所以没有进行类型检查。
显然,这是一个初学者,您可以根据需要进行修改。但这应该能让你继续前进。
【解决】MacOS 下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter
Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS
使用 Pyinstaller 打包时候报错
3027 ERROR: Tcl/Tk improperly installed on this system.
(lyj_venv) ➜ liyongjiandeMBP.lan [/Users/liyongjian/lyj] pyinstaller --windowed --onefile --clean --noconfirm ServenDigitsDrawV2.py
77 INFO: PyInstaller: 3.4
77 INFO: Python: 3.7.3
84 INFO: Platform: Darwin-18.5.0-x86_64-i386-64bit
85 INFO: wrote /Users/liyongjian/lyj/ServenDigitsDrawV2.spec
88 INFO: UPX is not available.
89 INFO: Removing temporary files and cleaning cache in /Users/liyongjian/Library/Application Support/pyinstaller
96 INFO: Extending PYTHONPATH with paths
[''/Users/liyongjian/lyj'', ''/Users/liyongjian/lyj'']
96 INFO: checking Analysis
96 INFO: Building Analysis because Analysis-00.toc is non existent
96 INFO: Initializing module dependency graph...
97 INFO: Initializing module graph hooks...
99 INFO: Analyzing base_library.zip ...
2805 INFO: running Analysis Analysis-00.toc
2814 INFO: Caching module hooks...
2817 INFO: Analyzing /Users/liyongjian/lyj/ServenDigitsDrawV2.py
3015 INFO: Loading module hooks...
3015 INFO: Loading module hook "hook-_tkinter.py"...
3027 ERROR: Tcl/Tk improperly installed on this system.
3027 INFO: Loading module hook "hook-encodings.py"...
3086 INFO: Loading module hook "hook-xml.py"...
3281 INFO: Loading module hook "hook-pydoc.py"...
3292 INFO: Looking for ctypes DLLs
3292 INFO: Analyzing run-time hooks ...
3295 INFO: Including run-time hook ''pyi_rth__tkinter.py''
3303 INFO: Looking for dynamic libraries
3411 INFO: Looking for eggs
3411 INFO: Using Python library /Library/Frameworks/Python.framework/Versions/3.7/Python
3413 INFO: Warnings written to /Users/liyongjian/lyj/build/ServenDigitsDrawV2/warn-ServenDigitsDrawV2.txt
3439 INFO: Graph cross-reference written to /Users/liyongjian/lyj/build/ServenDigitsDrawV2/xref-ServenDigitsDrawV2.html
3446 INFO: checking PYZ
3446 INFO: Building PYZ because PYZ-00.toc is non existent
3447 INFO: Building PYZ (ZlibArchive) /Users/liyongjian/lyj/build/ServenDigitsDrawV2/PYZ-00.pyz
3756 INFO: Building PYZ (ZlibArchive) /Users/liyongjian/lyj/build/ServenDigitsDrawV2/PYZ-00.pyz completed successfully.
3760 INFO: checking PKG
3760 INFO: Building PKG because PKG-00.toc is non existent
3760 INFO: Building PKG (CArchive) PKG-00.pkg
6336 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
6339 INFO: Bootloader /Users/liyongjian/lyj/lyj_venv/lib/python3.7/site-packages/PyInstaller/bootloader/Darwin-64bit/runw
6339 INFO: checking EXE
6339 INFO: Building EXE because EXE-00.toc is non existent
6339 INFO: Building EXE from EXE-00.toc
6339 INFO: Appending archive to EXE /Users/liyongjian/lyj/dist/ServenDigitsDrawV2
6346 INFO: Fixing EXE for code signing /Users/liyongjian/lyj/dist/ServenDigitsDrawV2
6352 INFO: Building EXE from EXE-00.toc completed successfully.
6354 INFO: checking BUNDLE
6354 INFO: Building BUNDLE because BUNDLE-00.toc is non existent
6354 INFO: Building BUNDLE BUNDLE-00.toc
6366 INFO: moving BUNDLE data files to Resource directory
执行的时候报错
➜ liyongjiandeMBP.lan [/Users/liyongjian] /Users/liyongjian/lyj/dist/ServenDigitsDrawV2 ; exit;
Traceback (most recent call last):
File "PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 28, in <module>
FileNotFoundError: Tcl data directory "/var/folders/f9/6m090zhs6w55rmbmhkmpff440000gn/T/_MEITVlJln/tcl" not found.
[39503] Failed to execute script pyi_rth__tkinter
[进程已完成]
解决办法:
在文件中增加
and ''Python'' not in path_to_tcl
PyInstaller/hooks/hook-_tkinter.py
@@ -180,7 +180,7 @@ def _find_tcl_tk(hook_api):
180
181 # _tkinter depends on Tcl/Tk compiled as frameworks.
182 path_to_tcl = bins[0][1]
- if ''Library/Frameworks'' in path_to_tcl:
183 + if ''Library/Frameworks'' in path_to_tcl and ''Python'' not in path_to_tcl:
参考链接:https://github.com/pyinstaller/pyinstaller/pull/3830
https://stackoverflow.com/questions/51847801/pyinstaller-app-not-opening-on-mac
63.Python中contains和icontains
1. contains: 进行大小写敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写敏感进行判断,因此在被翻译成“SQL”语句的时候,会使用“like binary”, 而“like binary”就是使用大小写敏感进行判断。
2. icontains: 进行大小写不敏感的判断,某个字符串是否包含在指定的字段中,这个判断条件使用大小写不敏感进行判断,因此在被翻译成“SQL”语句的时候,会使用“like”, 而“like”就是使用大小写不敏感进行判断。其中icontains前面的i指的就是ignore(忽略)。
具体实例,示例代码如下:
models.py文件中模型的定义,示例代码如下:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
# 重写类的__str__(self)方法
def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)
# 重新定义表的一些属性,注意:此处的类的名字一定要为Meta
class Meta:
db_table = ''article''
1. contains: views.py文件中视图函数的示例代码如下:
from django.http import HttpResponse
from .models import Article
def index(request):
# 注意:此处是使用filter(),只有使用filter()之后才能够在article(QuerySet)上调用query属性,查看django底层转化的sql语句。
article = Article.objects.filter(title__contains=''hello'')
print(article)
print(article.query)
return HttpResponse(''success !'')
在mysql数据库中article表的数据信息如下:
因为contains进行的是大小写敏感的查找,所以不能找到匹配的数据,则会返回QuerySet为空。并且我们可以看到执行我们的查找条件的时候,django底层翻译成的sql语句中的:WHERE
article.
title LIKE BINARY %hello%
,这里的LIKE BINARY 就可以保证进行的查找为大小写敏感的查找,并且hello字符串的两边会有%(代表hello字符串前面可以含有别的字符,后面也可以含有别的字符),这就可以验证我们上面的解释。
2.icontains: views.py文件中视图函数示例代码如下:
from django.http import HttpResponse
from .models import Article
def index(request):
# icontains进行查找title中包含“hello”的字符串
article = Article.objects.filter(title__icontains=''hello'')
print(article)
print(article.query)
return HttpResponse("success")
因为使用icontains采用的是大小写不敏感的的查找方式,所以会返回两条满足条件的QuerySet,并且重以下打印的结果中可以看出,我们的查询条件被翻译成了:WHERE
article.
title LIKE %hello%
,这里的LIKE进行的查找就是大小写不敏感的查找。
总结:iexact和exact进行的是准确性的查找,只有完全匹配我们的值“hello”的时候,才能够被找到。而icontains和contains中的查询条件,在进行查找的时候,会被翻译成sql语句中包含“%hello%”,在这里%意味着,前面可以含有n个字符,后面也可以含有n个字符。只要数据中包含hello字符串,就满足条件。
contains和compareDocumentPosition 方法来确定是否HTML节点间的关系_javascript技巧
从那起,我已经对这些方法做了大量的研究,并且已经在很多场合使用他们。在很多任务中,他们被证明是非常有用的(特别关于结构的抽象 DOM 选择器)。
1、DOMElement.contains(DOMNode)
这个方法起先用在 IE ,用来确定 DOM Node 是否包含在另一个 DOM Element 中。
当尝试优化 CSS 选择器遍历(像:“#id1 #id2”),这个方法很有用。你可以通过 getElementById 得到元素,然后使用 .contains() 确定 #id1 实际上是否包含 #id2。
注意点:如果 DOM Node 和 DOM Element 相一致,.contains() 将返回 true ,虽然,一个元素不能包含自己。
这里有一个简单的执行包装,可以运行在:Internet Explorer, Firefox, Opera, and Safari。
function contains(a, b) {
return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(arg) & 16);
}
2、NodeA.compareDocumentPosition(NodeB)
这个方法是 DOM Level 3 specification 的一部分,允许你确定 2 个 DOM Node 之间的相互位置。这个方法比 .contains() 强大。这个方法的一个可能应用是排序 DOM Node 成一个详细精确的顺序。
使用这个方法你可以确定关于一个元素位置的一连串的信息。所有的这些信息将返回一个比特码(Bit,比特,亦称二进制位)。
对于那些,人们知之甚少。比特码是将多重数据存储为一个简单的数字(译者注:0 或 1)。你最终打开 / 关闭个别数目(译者注:打开/关闭对应 0 /1),将给你一个最终的结果。
这里是从 NodeA.compareDocumentPosition(NodeB) 返回的结果,包含你可以得到的信息。
Bits Number Meaning
000000 0 元素一致
000001 1 节点在不同的文档(或者一个在文档之外)
000010 2 节点 B 在节点 A 之前
000100 4 节点 A 在节点 B 之前
001000 8 节点 B 包含节点 A
010000 16 节点 A 包含节点 B
100000 32 浏览器的私有使用
现在,这意味着一个可能的结果类似于:
<script> <BR>alert( document.getElementById("a").compareDocumentPosition(document.getElementById("b")) == 20); <BR></script>