小编kdb*_*kdb的帖子

在 Fortran 中初始化参数化派生类型的参数大小数组?

Fortran 允许参数化派生类型元素的大小。但是,在固定大小的元素可以在类型声明中分配默认值的情况下,参数化条目似乎没有办法:

PROGRAM main
  IMPLICIT NONE

  TYPE data1
     INTEGER :: array(5) = 2   ! allowed
  END type data1

  TYPE data2(n)
     INTEGER, LEN :: n
     INTEGER :: array(n) = 2   ! incorrect: error #8737 with intel fortran 19,
  END type data2               !            ignored by gfortran 8.2.1

END PROGRAM main
Run Code Online (Sandbox Code Playgroud)

分配默认值很方便,因为它可以避免每次使用类型时重复初始化,但对于参数大小的字段,这是不允许的;Gfortran 只是默默地忽略默认值,并且 Intel Fortran 发出错误

error #8737: For a default initialized component every type parameter and array bound
             must be a constant expression.   [ARRAY]
Run Code Online (Sandbox Code Playgroud)

是否有任何语法允许定义默认值?

fortran

6
推荐指数
1
解决办法
222
查看次数

C 中非本地退出和清理的最佳实践?

C 中错误中止的最佳实践是什么?

在我们的代码库中,我们目前有一个使用的模式

#define CHECKERROR(code) if(code) { return code; }
Run Code Online (Sandbox Code Playgroud)

但这会导致资源在以下形式的代码中不被关闭

/* not actual code due to non-disclosure restrictions */
int somefunction() {
    handle_t res1, res2;
    int errorcode;

    res1 = getResource();
    res2 = getResource();

    errorcode = action1(res1, res2);
    CHECK(errorcode);
    errorcode = action2(res1, res2);
    CHECK(errorcode);

    freeResource(res1);
    freeResource(res2);
    return errorcode;
}
Run Code Online (Sandbox Code Playgroud)

我发现了这个模式

/* initialize resources */
do {
    /* ... */
    errorcode = action();
    if(errorcode) break;
    /* ... */
} while(0);
/* cleanup resources */
return errorcode;
Run Code Online (Sandbox Code Playgroud)

以前在文章中,但现在找不到任何讨论它的来源。

什么是良好的实践,这对于 C 来说是惯用的?该do …

c resource-management

6
推荐指数
1
解决办法
351
查看次数

Emacs lisp:将字符翻译为标准 ASCII 转录

我正在尝试编写一个函数,将包含 unicode 字符的字符串转换为某种默认的 ASCII 转录。理想情况下,我希望\xc3\x85ngstr\xc3\xb6m成为Angstroem,或者,如果不可能的话,成为Angstrom。同样\xce\xb1=\xcf\x87应该成为a=x(c?) 或类似的。

\n\n

Emacs 有这样的内置功能吗?我知道我可以得到角色的名字和相似之处(get-char-code-property),但我不知道没有内置的转录表。

\n\n

目的是将条目标题翻译为有意义的可读文件名,避免无法理解 unicode 的软件出现问题。

\n\n

我当前的策略是手动构建翻译表,但这种方法相当有限并且需要大量维护。

\n

unicode emacs translation elisp character

5
推荐指数
1
解决办法
692
查看次数

Emacs-lisp:LaTeX的美化符号模式

我试图将"漂亮的实体"行为移植org-modelatex-mode使用内置的Emacs prettify-symbols-mode.此模式用于font-lock-mode将缓冲区中的字符序列显示为单个(unicode)字符.默认情况下,例如emacs-lisp代码

(lambda () t)
Run Code Online (Sandbox Code Playgroud)

(? () t)
Run Code Online (Sandbox Code Playgroud)

然而,它似乎要求字符序列由一些字符分隔,例如白色空格.例如在我的设置中,替换

\alpha \beta -> ? ?`
Run Code Online (Sandbox Code Playgroud)

会工作,但是当字符串没有分开时它会失败,例如

\alpha\beta -> \alpha?
Run Code Online (Sandbox Code Playgroud)

这是一个特别的问题,因为我想使用这种美化来使量子力学方程更具可读性,例如我更换的

|\psi\rangle -> |??
Run Code Online (Sandbox Code Playgroud)

是否可以避免使用此分隔符问题prettify-symbols-mode?如果不是,是否可以font-lock-mode在较低的水平上使用?

emacs latex elisp font-lock

5
推荐指数
1
解决办法
1437
查看次数

Emacs Lisp:验证树结构的标准方法?

在emacs lisp中,各种树结构很常见.custom.el通过:type参数提供了defcustom一种标准方法来定义自定义变量的预期形状.但有没有一种标准的方法来验证一些随机emacs lisp值的结构?

可以说,我有一份表格清单

LIST = (ENTRY ...)
ENTRY = (NAME . ((1 VAL1) (2 VAL2) ...))
Run Code Online (Sandbox Code Playgroud)

我能以某种方式定义类似于自定义类型的结构,然后检查该结构定义吗?

types elisp data-structures

5
推荐指数
1
解决办法
162
查看次数

"character*10 :: a"和"character :: a(10)"之间的区别

试图为项目刷新我的Fortran 90知识,在使用内部文件时遇到了一些奇怪的问题.考虑示例代码:

! ---- internal_file_confusion.f90 ----
program internal_file_confusion
  implicit none 

  character*40 :: string1
  character :: string2(40)

  write(string1, *) "Hello World 1"
  write(*,*) "string1 = ", string1

  write(string2, *) "Hello World 2"
  write(*,*) "string2 = ", string2

end program 
Run Code Online (Sandbox Code Playgroud)

当用gfortran崩溃编译时,写入STDOUT

 string1 =  Hello World 1                          
At line 10 of file e:/Daten/tmp/fortran-training/internal_file_confusion.f90
Fortran runtime error: End of record
Run Code Online (Sandbox Code Playgroud)

使用*length表示法声明时,字符数组可用于内部写入,但在使用name(length)表示法声明时则不能.此外,我注意到*length符号似乎只允许用于字符数组,而禁止使用类似的错误消息

Error: Old-style type declaration INTEGER*40 not supported at (1)
Run Code Online (Sandbox Code Playgroud)

对于其他数据类型.

这些符号之间有什么区别?为什么它会影响内部文件的使用?

arrays syntax fortran

5
推荐指数
1
解决办法
612
查看次数

如何在自定义python 3类中支持漂亮打印?

为自定义python3类添加漂亮打印支持的最可靠方法是什么?

对于交互式数据评估,我发现漂亮的打印支持非常重要.但是,默认情况下,iPython的漂亮打印机IPython.lib.pretty.pprint和标准库pprint.pprint都只支持内置结构类型(列表,元组,字典),并将plain repr()用于其他所有内容.值得注意的是,这甚至包括其他非常有用的实用程序collections.namedtuple().

结果,漂亮的打印输出通常是奇怪的格式.

我当前的awkard解决方法是定义类

class MyPrettyClass(dict):
    def __init__(self, ...):
        self.__dict__ = self
        self._class = self.__class__ # In order to recognize the type.
        ... 
        <A LOT OF FIELDS>
        ...
Run Code Online (Sandbox Code Playgroud)

在一个真实世界的例子中,这导致了

{'__lp_mockup_xml': <lxml.etree._ElementTree object at 0x0000021C5EB53DC8>,
 '__lp_mockup_xml_file': 'E:\\DataDirectory\\mockup.xml',
 '__lp_realrun_xml_file': 'E:\\DataDirectory\\realrun.xml',
 '_class': <class '__main__.readall'>,
 '_docopy': False,
 'dirname': 'E:\\DataDirectory'}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来获得漂亮的打印支持?

弱相关:我的问题在python3中低级内省?最初旨在建立我自己的类不可知的漂亮打印机,但没有产生任何结果.

python pretty-print

5
推荐指数
1
解决办法
130
查看次数

本地修补缺失的 Python 类型注释?

Python 现在支持类型提示,所以...耶!这似乎是避免一些更隐蔽的运行时错误的好方法。

遗憾的是,第三方库支持仍然是一个问题。虽然typeshed项目(也由mypy使用)部分解决了这个问题,但当尝试移植我的一些代码以使用类型提示时,我遇到了由于缺少存根而遇到的问题。

例如

# file:mypytest.py
import lxml.etree as et
tree = et.fromstring('<root><a>1</a><b>2</b><a>3</a></root>')
items = tree.xpath('/root/a')
print([i.text for i in items])
Run Code Online (Sandbox Code Playgroud)

会工作得很好,但 mypy 会产生虚假的错误消息

>>> mypy mypytest.py
mypytest.py:3: error: "_Element" has no attribute "xpath"
Run Code Online (Sandbox Code Playgroud)

因为存根目前不完整。

对于较大的项目,从 typeshed 下载存根、添加缺少的条目,甚至提交相应的拉取请求都是理所当然的事情。

但是有没有某种方法可以在快速而肮脏的场景中对丢失的信息进行猴子修补呢?

不好的解决方法

我能想到的最好的办法是

items = tree.xpath('/root/a') # type: ignore
Run Code Online (Sandbox Code Playgroud)

这会消除错误,但也会禁用items随后使用变量的类型检查。例如items[0] + 1不会再引起警告。

为了保留类型检查,可以使用

items_tmp = tree.xpath('/root/a') # type: ignore
items = items_tmp # type: List[et._Element]
Run Code Online (Sandbox Code Playgroud)

但这似乎有些骇人听闻;.xpath在使用该方法的所有地方都必须重复该操作。

2017-09-12 更新:或者可以使用以下语法

items_tmp : List[et._Element] = …
Run Code Online (Sandbox Code Playgroud)

python static-typing mypy typeshed

5
推荐指数
0
解决办法
565
查看次数

在“git gui”中自定义颜色?

git gui是一个非常有用的图形客户端,但它的实用性在某种程度上是通过差异的颜色来实现的:上下文文本以黑色排版(因此过度强调),而更改以红色和绿色的弱阴影突出显示 \xe2\x80\x93 一个问题患有部分红绿色盲。

\n\n

可以git diff配置颜色(设置color.diff.*),但git gui会忽略这些设置。

\n\n

是否有任何可用设置会影响 中的颜色选择git gui

\n

git

5
推荐指数
1
解决办法
1161
查看次数

grepdiff:查找与给定正则表达式不匹配的帅哥?

与使用 git 通过正则表达式提交琐碎更改相关:

使用git diff -U0and grepdiff --output-matching=hunk,有没有办法只获取与给定正则表达式不匹配的块?

我正在尝试生成一个补丁,该补丁应用所有“微不足道”的更改,即仅添加或删除以下行的更改

  • 为空,或者
  • 仅包含注释字符(!在本例中)。

似乎grepdiff只能找到与正则表达式匹配的更改,而不能找到相反的更改,并且似乎没有办法说“整个块必须与正则表达式匹配”——如果任何一行匹配,则该块被视为匹配。

git diff grep

5
推荐指数
0
解决办法
283
查看次数