我正在研究一个竞争机器人,它通过sys.stdin接收'输入,并使用Python的print()输出.我有以下内容:
import sys
def main():
while True:
line = sys.stdin.readline()
parts = line.split()
if len(parts) > 0:
# do stuff
Run Code Online (Sandbox Code Playgroud)
问题是输入是通过流进入的,使用上面的方法阻止我打印任何东西直到流关闭.我能做些什么来完成这项工作?
我想捕获一个通用异常类的所有变体,并且我想知道是否有一种方法可以在没有多个catch块的情况下进行。例如说我有一个异常类:
public class MyException<T> : Exception
{
public string MyProperty { get; }
public MyException(T prop) : base(prop.ToString())
{
MyProperty = prop?.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
和两个派生类:
public class MyDerivedStringException : MyException<string>
{
public MyDerivedStringException(string prop) : base(prop)
{
}
}
public class MyDerivedIntException : MyException<int>
{
public MyDerivedIntException(int prop) : base(prop)
{
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以同时捕获MyDerivedStringException
和MyDerivedIntException
捕获一个捕获块。
我已经试过了:
try
{
...
}
catch(Exception e) when (e is MyDerivedStringException || e is MyDerivedIntException)
{
}
Run Code Online (Sandbox Code Playgroud)
但是它不是很干净,意味着我无法使用MyProperty
。
我对问题的一般解决方案感兴趣,但是在我的情况下,通用异常是在第三方库中定义的,如下所述,该库为问题增加了一些其他约束。
我一直在研究Windows窗体应用程序,并且最近添加了一个简单的设置页面,允许用户选择输出所在的文件夹.OpenFileDialog是丑陋的,不好用,所以我已经在Windows API CodePack中添加了访问CommonOpenFileDialog-所有好处.
当我打开CommonOpenFileDialog时,Windows窗体应用程序缩小到较小的大小,如附加的图像所示.
左边是程序,右边是打开的对话框:
我之前和之后都试过检查表格的大小,这没有改变,所以我打了一个砖墙.任何信息都是有用的,如果需要我可以提供更多细节.
打开表单的代码是:
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.DefaultDirectory = selectedFolderTextBox.Text;
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() != CommonFileDialogResult.Ok) return;
selectedFolderTextBox.Text = dialog.FileName;
Run Code Online (Sandbox Code Playgroud) 我现在正在为公司自助餐厅创建pos应用程序,收银员扫描员工ID并显示其交易信息.
我的问题是收银员也可以使用他们的键盘输入(Employeeid),这是非常危险的.
if employee(true)
show employee information
then add orders
else
Exception
Run Code Online (Sandbox Code Playgroud)
处理这种情况的最佳方法是什么?规则只是必须使用条形码扫描仪.
谢谢你的问候
我有一个生成 Pronsole.py(3D 打印)的程序(假设它称为“PROG”)。如果它只是由 Python 解释,它在 GNU/Linux 和 Windows 中运行良好。这是有效的行:
self.pronTranspProc=reactor.spawnProcess(self.pronProtProc, pythonPath, [pythonPath, "pronsole.py"], os.environ, self.pronPathPrintrun)
Run Code Online (Sandbox Code Playgroud)
当 Python 是普通解释器时,“pythonPath”将只是该解释器的路径,因为它是 sys.executable。但是当使用 Pyinstaller 制作捆绑包因此应用程序被冻结时,sys.executable 不是解释器,而是由 pyinstaller 生成的可执行文件(在本例中,它将是 PROG.EXE 而不是 Python.exe)。该可执行文件嵌入了 python 解释器。问题是,如果我像 Python 解释器一样调用可执行文件,那么程序 PROG 再次打开,这是逻辑,并且不会与打印机进行通信。
有没有办法调用嵌入式Python解释器?
到目前为止,我所达到的解决方案是将 Python 解释器作为一个简单的文件添加到包中。但这是多余的,因为我知道解释器是嵌入的。这样做的任何pythonic方式?
任何帮助,将不胜感激
我试图在Canvas
我的背景中定位元素。
窗口重新调整大小以保持纵横比。背景随着窗口大小而拉伸。
问题是一旦窗口重新调整大小,元素位置就不正确。如果窗口稍微调整大小,元素会稍微调整它们的大小并且仍然在正确的位置,但如果窗口调整大小使其大小加倍,则定位完全关闭。
到目前为止,我使用了Grid
,但也无济于事。这是 XAML
<Window x:Class="CanvasTEMP.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" ResizeMode="CanResizeWithGrip" SizeToContent="WidthAndHeight" MinHeight="386" MinWidth="397.5" Name="MainWindow1"
xmlns:c="clr-namespace:CanvasTEMP" Loaded="onLoad" WindowStartupLocation="CenterScreen" Height="386" Width="397.5" WindowStyle="None" AllowsTransparency="True" Topmost="True" Opacity="0.65">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas Height="77" Width="218">
<Label Content="{Binding OwnerData.OwnerName}" Height="36" Canvas.Left="8" Canvas.Top="55" Width="198" Padding="0" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Center"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas>
<Canvas.Background>
<ImageBrush ImageSource="Resources\default_mapping.png" Stretch="Uniform"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding OwnerData.left}" />
<Setter Property="Canvas.Top" Value="{Binding OwnerData.top}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
用于数据绑定的类
public class Owner …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用合并单元格创建类似数据网格的视图。一切都很好,除了我不能ShareSizeScope
在标题行和其余行之间。您可以在下面的屏幕截图中看到,除了第一列之外,列没有对齐。有人可以指出我的代码有什么问题吗?提前致谢。
我的 XAML:
<Grid Grid.Row ="0" Grid.Column="0" Margin="0,0,2,0" Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="35" MaxHeight="35" MinHeight="35"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Height="35" VerticalAlignment="Top" Background="{StaticResource customBlueBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Col_A" />
<ColumnDefinition SharedSizeGroup="Col_B" />
<ColumnDefinition SharedSizeGroup="Col_C" />
<ColumnDefinition SharedSizeGroup="Col_D" />
<ColumnDefinition SharedSizeGroup="Col_E" />
</Grid.ColumnDefinitions>
<Border BorderThickness="0,0,1,0" BorderBrush="Black" Margin="-1" Grid.Column="0"></Border>
<Border BorderThickness="0,0,1,0" BorderBrush="Black" Margin="-1" Grid.Column="1"></Border>
<Border BorderThickness="0,0,1,0" BorderBrush="Black" Margin="-1" Grid.Column="2"></Border>
<Border BorderThickness="0,0,1,0" BorderBrush="Black" Margin="-1" Grid.Column="3"></Border>
<TextBlock Text="Col 1" FontSize="14" FontFamily="Segoe Ui Dark" Foreground="White" SnapsToDevicePixels="True" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" ></TextBlock>
<TextBlock Text="Col 2" FontSize="14" FontFamily="Segoe …
Run Code Online (Sandbox Code Playgroud) 我将枚举常量作为位标志传递给期望枚举类型作为输入的函数,如下所示:
// Enumeration type
typedef enum
{
LED_RED = (1 << 0),
LED_GREEN = (1 << 1),
LED_YELLOW = (1 << 2),
LED_ORANGE = (1 << 3),
} LedType;
...
// Function declaration
void setOnLed(LedType led);
...
// Function call
setOnLed(LED_RED | LED_GREEN | LED_YELLOW);
Run Code Online (Sandbox Code Playgroud)
当我调用该函数时,我收到一条警告:
警告:#188-D:枚举类型与另一种类型混合
警告是因为LED_RED | LED_GREEN | LED_YELLOW
转换为整数而不是LedType
.
我可以通过在LedType
枚举中添加 LED 组合来避免警告,但这意味着我必须添加所有可能的组合……如果我向 中添加更多 LED 选项enum
,它会变得非常混乱……
我可以使用数组作为函数的输入,但是在调用函数时需要更多的代码,我更喜欢简单的函数调用来设置 LED。
我正在使用 Keil µVision IDE 对基于 ARM 的微控制器 (STM32) 进行编程。
是否有一种简单安全的方法来避免此警告或另一种方法将所有 LED …
我需要使用 kubernetes python 客户端将文件从 pod 复制到主机。它会像kubectl cp pod:file file
.
我正在测试来自:https : //github.com/prafull01/Kubernetes-Utilities/blob/master/kubectl_cp_as_python_client.py的代码。
具体这段代码:
command_copy = ['tar', 'cf', '-', source_path]
with TemporaryFile() as tar_buffer:
exec_stream = stream(self.coreClient.connect_get_namespaced_pod_exec, pod_name, name_space,
command=command_copy, stderr=True, stdin=True, stdout=True, tty=False,
_preload_content=False)
# Copy file to stream
try:
while exec_stream.is_open():
exec_stream.update(timeout=1)
if exec_stream.peek_stdout():
out = exec_stream.read_stdout()
tar_buffer.write(out.encode('utf-8'))
if exec_stream.peek_stderr():
logger.debug("STDERR: %s" % exec_stream.read_stderr())
exec_stream.close()
tar_buffer.flush()
tar_buffer.seek(0)
with tarfile.open(fileobj=tar_buffer, mode='r:') as tar:
member = tar.getmember(source_path)
tar.makefile(member, destination_path)
return True
except Exception as e:
raise manage_kubernetes_exception(e) …
Run Code Online (Sandbox Code Playgroud) ComboBox
当我使用Compact Framework 3.5 中的控件时,我想禁用右箭头。
我已经尝试过:
private void Combo_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
TxtPass.Focus();
return;
}
}
Run Code Online (Sandbox Code Playgroud)
但ComboBox
值发生变化。
我查看了此页面上描述的两种方法:http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/F8F2FE7D-A908-4AAF-BD5C-627529BB9805
但这两种方法不适用于紧凑框架。(该属性不存在)。
有人有好主意吗?
问候
编辑
我刚刚找到解决方案
private void Combo_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
TxtPass.Focus();
e.Handled = true;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的回答