小编mad*_*n25的帖子

Python 3.9+ 类型.Collection、元组、类型.Iterable

有什么区别和用例typing.Collection

例如:

from typing import Collection, Iterable

foo: Collection[int] = (1, 2)
bar: Iterable[int] = (1, 2)
spam: tuple[int] = (1, 2)
Run Code Online (Sandbox Code Playgroud)

对于我来说,一切看起来都一样,我不明白为什么不使用 always listtuple等作为类型提示。

python python-3.x python-typing

14
推荐指数
1
解决办法
8397
查看次数

Xamarin.Forms ListView在ContextMenu上更改Cell打开

我使用Xamarin.Forms来定义一个ListView.这ListView定义了ContextAction内部的一些内容ViewCell.然后,根据平台,将这些上下文动作呈现给用户.在Android中,这是通过长按特定项目来触发的.可悲的是,这个项目不会(正确地)突出显示,正如在这个截图中可以看到的那样(我长期压下第三项,遗憾的是我还没有嵌入图像).

在此输入图像描述

有没有办法在上下文菜单打开时修改Cell?特别是要求Android的解决方案,但也欢迎一般的答案.目标最终是改善突出显示,例如通过更改单元格的背景颜色.当ContextAction按下一个单元时,修改单元格不是我想要的.

我浏览了Xamarin.Forms的源代码,并考虑以某种方式继承自例如ViewCell类,但找不到长按项目时会触发/调用的事件或命令.我已经设置了一个简单的存储库来说明行为:GitHub存储库

最重要的代码片段

  • XAML中的ListView定义

    <?xml version="1.0" encoding="utf-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ListViewContextMenu" x:Class="ListViewContextMenu.ListViewContextMenuPage">
        <ListView x:Name="MyListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Text="Action" Command="{Binding OnAction}" CommandParameter="{Binding .}"/>
                        </ViewCell.ContextActions>
                        <Label Text="{Binding Name}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage>
    
    Run Code Online (Sandbox Code Playgroud)
  • MyItem定义(MVVM)

    using System.Diagnostics;
    using Xamarin.Forms;
    
    namespace ListViewContextMenu
    {
        public class MyItem
        {
            public string Name { get; set; }
            public Command OnAction { get; set; }
    
            public MyItem()
            {
                OnAction = …
    Run Code Online (Sandbox Code Playgroud)

c# listview listviewitem xamarin.forms

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

删除 setup.py 中的所有默认编译器参数

我正在使用 distutils 构建一个扩展模块,不幸的是,它使用一堆我不想要的参数调用编译器。具体来说,它使用CFLAGS/usr/lib64/python2.7/config/Makefile 中定义的变量,该变量也由

>>> from distutils import sysconfig
>>> print(sysconfig.get_config_vars()['CFLAGS'])
-fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4  -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv   -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4  -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv
Run Code Online (Sandbox Code Playgroud)

这个问题的答案是行不通的;CFLAGS=""从命令行设置会导致 gcc 被调用

-DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv
Run Code Online (Sandbox Code Playgroud)

好像OPTmakefile中为空,但实际上设置OPT=""没有效果。有什么方法可以告诉 distutils 不要添加这些标志吗?我知道我可以通过添加自己的标志来覆盖其中的大部分(手动取消定义宏,如-DNDEBUGwith-UNDEBUG和覆盖-O2with -O0),但这真的很丑陋,肯定应该有更好的方法。

c++ python distutils build

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

为不存在的文件夹结构生成文本树

我想生成一个不存在的文件系统/文件夹结构的漂亮文本呈现。想象一下,我想提出一个文件系统布局,我想以一种可以快速打字的简单方式来表达这一点。例如,给定如下输入:

\n
another\n    folder\n    log.txt\nfinal\nhi.py\nsome\n    folder\n        file.txt\n
Run Code Online (Sandbox Code Playgroud)\n

其中层次结构简单地由选项卡表示。但也可以是任何其他编码。

\n

我想得到一个漂亮的输出,比如tree可以生成:

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 another\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 folder\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 log.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 final\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 hi.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 some\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 folder\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file.txt\n
Run Code Online (Sandbox Code Playgroud)\n

这里的目标是避免输入不可输入但漂亮的字符,例如\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80。理想情况下,该工具的网络版本将是可取的。

\n

有一堆看似相关的 SO 帖子:What tool to use to绘制文件树图Representing Directory & File Structure in Markdown Syntax。但是,它们都需要现有的文件夹结构。

\n

filesystems tree utility-method

4
推荐指数
1
解决办法
3284
查看次数

如何从tf.estimator.Estimator获取最后的global_step

如何global_step从完成tf.estimator.Estimator后获得最后一个train(...)?例如,可以像这样建立一个典型的基于Estimator的训练例程:n_epochs = 10 model_dir ='/ path / to / model_dir'

def model_fn(features, labels, mode, params):
    # some code to build the model
    pass

def input_fn():
    ds = tf.data.Dataset()  # obviously with specifying a data source
    # manipulate the dataset
    return ds

run_config = tf.estimator.RunConfig(model_dir=model_dir)
estimator = tf.estimator.Estimator(model_fn=model_fn, config=run_config)

for epoch in range(n_epochs):
    estimator.train(input_fn=input_fn)
    # Now I want to do something which requires to know the last global step, how to get it?
    my_custom_eval_method(global_step)
Run Code Online (Sandbox Code Playgroud)

仅该 …

python tensorflow tensorflow-estimator

3
推荐指数
1
解决办法
1273
查看次数

tf.estimator.Estimator.evaluate()是否始终在一个GPU上运行吗?

我的印象中,调用evaluate()一个tf.estimator.Estimator实例时运行在多GPU模式,即使分配策略MirroredStrategy,配置为使用至少2个GPU。

这是表明此行为的示例脚本:https : //gist.github.com/patzm/b69fcdf33fc9062683d749d0ea936b5e

这是nvidia-smi显示此行为的屏幕截图: nvidia-smi的示例屏幕截图

如果evaluate()应该在多个GPU上运行(如上所述),请在回答中突出显示我的错误。

python tensorflow tensorflow-estimator

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