假设我有一个 python 函数,它的单个参数是一个非平凡类型:
from typing import List, Dict
ArgType = List[Dict[str, int]] # this could be any non-trivial type
def myfun(a: ArgType) -> None:
...
Run Code Online (Sandbox Code Playgroud)
...然后我有一个从 JSON 源中解压出来的数据结构:
import json
data = json.loads(...)
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何检查在运行时是data有正确的类型被用作论据来myfun()使用它作为一个参数之前myfun()?
if not isCorrectType(data, ArgType):
raise TypeError("data is not correct type")
else:
myfun(data)
Run Code Online (Sandbox Code Playgroud) 我似乎无法让iTunes显示我的播客图像.确切地说,iTunes甚至没有尝试为我的播客下载图像.Apache日志显示iTunes播放的播客和第一个音频文件,但它完全忽略了<image>和<itunes:image>部分(如下所示):
<image>
<url><?php echo htmlentities($imageURL) ?></url>
<title>My Podcast</title>
<link>http://<?php echo $_SERVER['HTTP_HOST'] ?></link>
<width>300</width>
<height>300</height>
</image>
<itunes:image>
<url><?php echo $imageURL ?></url>
<title>My Podcast</title>
<link>http://<?php echo $_SERVER['HTTP_HOST'] ?></link>
</itunes:image>
Run Code Online (Sandbox Code Playgroud)
我也试过这个略短的替代品,没有运气.
<image>
<url><?php echo $imageURL ?></url>
<title>My Podcast</title>
<link>http://<?php echo $_SERVER['HTTP_HOST'] ?></link>
<width>300</width>
<height>300</height>
</image>
<itunes:image href="<?php echo $imageURL ?>" />
Run Code Online (Sandbox Code Playgroud)
几点说明:
我正在用python编写一个游戏,并决定为地图数据文件创建一个DSL.我知道我可以用regex编写自己的解析器,但我想知道是否有现成的python工具可以更容易地做到这一点,比如在PHP引擎中使用的re2c.
一些额外的信息:
DSL只包含数据(声明?),它不会被"执行".大多数行看起来像:
SOMETHING: !abc @123 #xyz/123
我只需要阅读数据树.
我似乎经常使用诸如节点,属性,孩子(等)等平淡无奇的单词,而且我担心其他人会因为部分的名称含糊不清而常常难以理解我的代码.
如何找到类和组件的创意名称,使它们更难忘?
我特别遇到通用工具的问题,除了它们相当通用的功能目的之外没有真正的描述.我想知道其他人是否找到了创造性的方法来命名,而不是简单地通过它们的实用程序命名它们,例如AnonymousFunctionWrapperCallerExecutorFactory.
我的代码看起来像这样:
$data = file_get_contents($tempFile); // perhaps 30MB of file data, now in PHP's memory
$hash = md5($data);
$query = "INSERT INTO some_table
SET BlobData = '" . mysql_real_escape_string($data) . "',
BlobHash = '$hash'
";
mysql_query($query);
Run Code Online (Sandbox Code Playgroud)
我知道每个''都不是很有效率.运算符将重新分配更大的内存块,30MB字符串将被复制多次.
有没有比以下解决方案更有效的方法?
$data = file_get_contents($tempFile); // perhaps 30MB of file data, now in PHP's memory
$hash = md5($data);
$query = "INSERT INTO some_table SET BlobData = '%s', BlobHash = '$hash'";
mysql_query(sprintf($query, mysql_real_escape_string($data)));
Run Code Online (Sandbox Code Playgroud) 我对以下C代码片段感到有些困惑:
printf("Peter string is %d bytes\n", sizeof("Peter")); // Peter string is 6 bytes
Run Code Online (Sandbox Code Playgroud)
这告诉我,当C用双引号编译一个字符串时,它会自动为空终止符添加一个额外的字节.
printf("Hello '%s'\n", "Peter");
Run Code Online (Sandbox Code Playgroud)
该printf函数知道何时停止读取字符串"Peter",因为它到达空终止符,所以......
char myString[2][9] = {"123456789", "123456789" };
printf("myString: %s\n", myString[0]);
Run Code Online (Sandbox Code Playgroud)
在这里,printf打印所有18个字符,因为没有空终止符(并且它们不适合不取出9).C不在变量定义中添加空终止符吗?
如何找到使用一个单词命令设置的vim变量的值
例如:set foldmarker = {,}
我正在为foldtext()编写一个简单的自定义函数来设置折叠区域的自定义一行摘要
当我用任何折叠标记打开文档而不是我硬编码到函数中时,它的效果很好但看起来很有趣
这是功能
set foldtext=GetCustomFoldText()
function GetCustomFoldText()
let foldClose = '}'
let foldTtl = v:foldend - v:foldstart
return getline(v:foldstart) . ' (+) ' . foldTtl . ' lines... ' . foldClose
endfunction
Run Code Online (Sandbox Code Playgroud)
这使得:
function myAwsomeFunction()
{
// awsomeness here
// awsomeness here
// awsomeness here
}
Run Code Online (Sandbox Code Playgroud)
折成了这个:
function myAwsomeFunction()
{ (+) 5 lines... }
Run Code Online (Sandbox Code Playgroud)
在我使用不同的foldmarker编辑文档之前,这很棒
我正试图从foldmarker动态确定foldClose
当低权限非管理员用户成功登录我的Web应用程序时,我将以下数据存储在$_SESSION数组中:
$_SESSION = array(
'user_id' => 2343, // whatever their user_id number is from the DB
'allow_admin' => false, // don't give them access to admin tools
'allow_edit' => false, // don't let them edit stuff
);
Run Code Online (Sandbox Code Playgroud)
$_SESSION除了以某种方式编辑会话文件之外,还有什么办法可以操纵数组给他们Admin或Edit访问权限/tmp吗?(上面的代码是添加这些项目的唯一地方$_SESSION)
是有可能存在的任何类型的值中$_GET或$_POST它是不阵列或字符串?
对于那些更好地阅读代码的人来说,是否可以在Web服务器上运行这个简单的脚本并让它抛出异常?
// crash-me.php
<?php
function must_be_array_or_string($value) {
if(is_string($value))
return;
if(is_array($value)) {
foreach($value as $subValue)
must_be_array_or_string($subValue);
return;
}
throw new Exception("Value is " . gettype($value));
}
if(isset($_GET))
must_be_array_or_string($_GET);
if(isset($_POST))
must_be_array_or_string($_POST);
Run Code Online (Sandbox Code Playgroud)