通过一些Python代码,我注意到十六进制值有许多不同的表示.例如,如果我选择这样的数字:
xx = '\x03\xff'
Run Code Online (Sandbox Code Playgroud)
然后是以下命令(我用它来将little endian转换为big endian的版本)
yy = hex(struct.unpack('>H', xx)[0])
Run Code Online (Sandbox Code Playgroud)
将返回:
'0x3ff'
Run Code Online (Sandbox Code Playgroud)
但是,这个命令
zz = xx.encode('hex')
Run Code Online (Sandbox Code Playgroud)
将返回:
'03ff'
Run Code Online (Sandbox Code Playgroud)
最后,只打印出值将返回此值
'\x03\xff'
Run Code Online (Sandbox Code Playgroud)
从它的外观来看,有三种不同类型的十六进制.
'\xFF''0xFF''FF'有什么不同?
如果有人可以建议更好的方法将小端转换为大端数,则可以获得奖励积分.上面的方法yy不适用于大于两个字节的数字,而且我正在使用一些16字节长的十六进制字符串(包括与ascii/integer值不对应的值)
冰淇淋三明治引入了许多新的UI设计元素,但市场渗透率仍然只有4%左右.如果一个人想要面向未来的应用并利用蜂窝/冰淇淋三明治引入的一些设计元素,例如动作栏,配色方案等,那么确保你保持一些向后调整的最佳方法是什么兼容性?
我有一个快速的问题.
我对Swing有一点经验,最简单的方法是绘制一个相当大的GUI.
作为GUI的一部分,我想要前进和后退按钮.我试图采用的方法是实现将当前JPanel推送到堆栈并检索前一个值的方法(在前向或反向(因此为2个堆栈)).我不能让它工作.也许我会以完全错误的方式解决它,或者堆栈不能以我使用它的方式使用.在任何一种情况下,它真的让我烦恼.我想有可能更简单的方式,如卡布局,但我认为这种方法应该工作,这是非常烦人的.
值得注意的是,我正在使用JFrame"基类"并根据屏幕更改中央JPanel.然而,导航栏是常量,作为"基类"的一部分
这个"基类"的代码:
public class Main_Frame extends JFrame{
static JPanel nav_bar_panel;
JButton home;
JButton back;
JButton forward;
JPanel currentPanel;
static Stack<JPanel> previousPanels;
static Stack<JPanel> forwardPanels;
public Main_Frame(){
super("DEMO");
setSize(800,600);
setLayout(new BorderLayout());
setVisible(true);
add(nav_bar(), BorderLayout.NORTH);
currentPanel = init_display();
add(currentPanel, BorderLayout.CENTER);
previousPanels = new Stack<JPanel>();
forwardPanels = new Stack<JPanel>();
}
private JPanel nav_bar(){
ButtonPressHandler handler = new ButtonPressHandler();
nav_bar_panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
back = new JButton("Back");
back.addActionListener(handler);
home = new JButton("Home");
home.addActionListener(handler);
forward = new JButton("Forward");
forward.addActionListener(handler); …Run Code Online (Sandbox Code Playgroud) 为什么要使用AlertDialog.Builder类而不是直接使用的方法AlertDialog,例如,为什么要使用AlertDialog.Builder.setCancellable而不是AlertDialog.setCancellable?当然这是一个冗余的情况?
是否可以使用PySerial实现全双工通信?具体来说,是否可以在需要时连续监视端口以进行输入和写入?我想应该可以使用线程(并且串行接口是全双工的吗?).如果没有,那么在不传输时监控串口的最佳方法是什么?超时?
编辑:这是我的尝试.此代码针对TI的CC2540蓝牙LE芯片.在发送GATT初始化消息时,我期待一个回复(详细说明芯片的操作参数)......我什么都没得到
import serial
import threading
from time import sleep
serial_port = serial.Serial()
GAP_DeviceInit = \
"\x01\x00\xfe\x26\x08\x03\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00"
def read():
while True:
data = serial_port.read(9999);
if len(data) > 0:
print 'Got:', data
sleep(0.5)
print 'not blocked'
def main():
serial_port.baudrate = 57600
serial_port.port = '/dev/ttyACM0'
serial_port.timeout = 0
if serial_port.isOpen(): serial_port.close()
serial_port.open()
t1 = threading.Thread(target=read, args=())
while True:
try:
command = raw_input('Enter a command to send to the Keyfob: \n\t')
if (command == "1"):
serial_port.write(message)
except KeyboardInterrupt:
break
serial_port.close()
Run Code Online (Sandbox Code Playgroud) 问题:给定git repo,显示由特定用户集修改的文件,以及这些文件的最后一个编辑器(来自该组用户).
潜在解决方案
git ls-files | xargs -n 1 git log -1 --author="example.com" --name-only --pretty=format:'%aN' --follow -p | paste -d";" - -
Run Code Online (Sandbox Code Playgroud)
这将产生所需的输出(下图),但速度很慢:
<author_a>;<file_a>
<author_b>;<file_b>
<author_b>;<file_c>
...
Run Code Online (Sandbox Code Playgroud)
有没有更快/更好的方法来做到这一点?
我正在为Android设计一个音乐播放器应用程序,它将具有弹出控件功能.我目前正试图让这些控件在一段时间不活动后关闭,但似乎没有一个明确记录的方法来做到这一点.到目前为止,我已经设法使用本网站和其他人的一些建议来拼凑以下解决方案.
private Timer originalTimer = new Timer();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playcontrols);
View exitButton = findViewById(R.id.controls_exit_pane);
exitButton.setOnClickListener(this);
View volUpButton = findViewById(R.id.controls_vol_up);
volUpButton.setOnClickListener(this);
View playButton = findViewById(R.id.controls_play);
playButton.setOnClickListener(this);
View volDownButton = findViewById(R.id.controls_vol_down);
volDownButton.setOnClickListener(this);
musicPlayback();
originalTimer.schedule(closeWindow, 5*1000); //Closes activity after 10 seconds of inactivity
}
Run Code Online (Sandbox Code Playgroud)
应该关闭窗口的代码
//Closes activity after 10 seconds of inactivity
public void onUserInteraction(){
closeWindow.cancel(); //not sure if this is required?
originalTimer.cancel();
originalTimer.schedule(closeWindow, 5*1000);
}
private TimerTask closeWindow = new TimerTask() {
@Override
public void run() {
finish(); …Run Code Online (Sandbox Code Playgroud) 我最近一直在练习Android框架和修订控制系统(Git),并且我对我经常使用的应用程序的UI进行了一些修改.这是一个小型的开源项目,我很好奇提交这些项目变更的最佳做法是什么?我确信它主要依赖于dev本身,但是在提交更改时是否有任何禁忌或区域(代码或其他方面)要避免?我应该先联系开发人员还是只提交修改等?
我正在尝试使 TI MSP430 Launchpad 板上的 LED 闪烁。我有两段代码。一个有效,而另一个则无效。唯一的区别是工作版本中包含 volatile 关键字。为什么程序执行需要这个关键字?
这段代码有效...
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Configure Port Directions
P1DIR |= 0x01; // 0000 0001
volatile unsigned int i;
for(;;)
{
P1OUT ^= 0x01; // Set P1.0 LED on
for (i = 20000; i > 0; i--); // Delay
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这段代码不...
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Configure Port Directions
P1DIR |= 0x01; // 0000 …Run Code Online (Sandbox Code Playgroud) 我可以获取 Tcl 脚本,并proc从该脚本运行一个,如下所示:
import Tkinter
>>> tclsh = Tkinter.Tcl()
>>> tclsh.eval('source {myscript.tcl}')
>>> tclsh.eval('myproc')
...[output of proc]
>>>
Run Code Online (Sandbox Code Playgroud)
但是,如果我想将变量传递给这个过程,我必须这样做(假设将procadict作为参数:
>>> tclsh.eval('dict set spec num 10000')
>>> tclsh.eval('dict set spec time 10000')
>>> tclsh.eval('dict set spec rate 10')
Run Code Online (Sandbox Code Playgroud)
有没有一种更简单、更 Pythonic 的方法可以从 Tkinter 的上下文中做到这一点?我见过变量classes,但它们似乎没有字典风格的变量,甚至根本没有绑定到代码的Tcl解释器部分。
鉴于使用sudo打开的pexpect衍生进程,如下所示:
#!/usr/bin/env python
import pexpect
cmd = ['sudo', 'bash', '-c', '"some long-running sudo command"']
cmd = ' '.join(cmd)
child = pexpect.spawn(cmd, timeout=60)
i = child.expect([
'success',
'error'])
if i == 0:
print('ok')
else:
print('fail')
# insert code here
Run Code Online (Sandbox Code Playgroud)
如何在失败时取消此过程(或成功,就此而言)?
我尝试过以下(替换# insert code here):
child.kill(0)
child.close(force=True)
Run Code Online (Sandbox Code Playgroud)
两者都给出了以下错误,这是有道理的,因为Python脚本不是根进程,并且它试图杀死一些根进程.
Traceback (most recent call last):
File "./myscript.py", line 85, in <module>
requires_qemu()
File "./myscript.py", line 82, in requires_qemu
child.close(0)
File "/usr/lib/python2.7/site-packages/pexpect/__init__.py", line 747, in close
raise ExceptionPexpect('Could not …Run Code Online (Sandbox Code Playgroud) android ×4
python ×4
git ×2
java ×2
c ×1
git-ls-files ×1
hex ×1
msp430 ×1
open-source ×1
pexpect ×1
pyserial ×1
python-2.7 ×1
serial-port ×1
stack ×1
swing ×1
tcl ×1
tkinter ×1
types ×1