我有几次使用Python,我发现了"pythonic"编码方式.我在代码中使用了很多元组,其中大部分是极地或笛卡尔位置.
我发现自己在写这个:
window.set_pos([18,8])
Run Code Online (Sandbox Code Playgroud)
而不是这个:
window.set_pos((18,8))
Run Code Online (Sandbox Code Playgroud)
摆脱我发现难以阅读的双重括号.
似乎python自动进行从列表到元组的类型转换,因为我的代码正常工作.
但它是一种很好的编码方式吗?你有任何可用于编写可读代码的演示提示吗?
提前感谢你肯定有启发性的答案.
我有一个矢量类,我定义了__mul__将矢量乘以数字的方法.
这是__mul__方法:
def __mul__(self, other):
x = self.x * other
y = self.y * other
new = Vector()
new.set_pos((x, y))
return new
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道数字和矢量之间是哪个.如果self是数字,则self.x会引发错误.(我可能会误解这一点:"其他"总是一个数字吗?)
__rmul__ = __mul__
Run Code Online (Sandbox Code Playgroud)
但是我怎么能在课程定义中这样做呢?
就像是 :
def __rmul__ = __mul__
Run Code Online (Sandbox Code Playgroud) python overriding class operator-overloading operator-keyword
我试图扩大可选省略号(~optional datum:my-class ...)成一种形式,使用从属性,my-class像这样:#'(list datum.attr ...)。
但是由于表单是可选的,因此当表单不存在时,它将解析为#f,省略号模式不喜欢它:
;; ?: attribute contains non-list value
;; value: #f
Run Code Online (Sandbox Code Playgroud)
我尝试使用的#:defaults参数~optional,如下所示:
(~optional datum:my-class ... #:defaults ([(datum 1) null]))
Run Code Online (Sandbox Code Playgroud)
;; which works for usage:
#'(list datum ...)
;; but not for:
#'(list datum.attr ...)
Run Code Online (Sandbox Code Playgroud)
我通过使用以下技巧使它起作用:
(~optional datum:my-class ...)
#'(list #,@(if (attribute datum) #'(datum.attr ...) #'()))
Run Code Online (Sandbox Code Playgroud)
我正在尝试寻找是否有更好的方法。
我尝试将其保持在最低限度。检出测试子模块。要查看此问题,请取消注释parse-bag和运行的四个实现之一raco test file.rkt。
#lang racket/base
(provide parse-bag)
(require
(for-syntax racket/base syntax/parse) …Run Code Online (Sandbox Code Playgroud) 我知道这个标题看起来很主观,但是我希望这个问题可以顺利地介绍一下对于不是来自emacs的新手来说(这是我的情况).
您可能想知道为什么我决定使用spacemacs,这是一个高度自定义的黑客而不是emacs,而没有花时间习惯先使用vanilla emacs.好吧,事实上,我一直在尝试使用emacs和vim很长一段时间,因为我可以理解为什么这些软件能够提高工作效率并让你在编码/黑客时感觉更"在家".
不幸的是,当我开始习惯vim时,你需要花时间学习,最重要的是,你需要花费在配置这些软件上的时间对我来说太高了.
然后我发现了spacemacs,它包含了vim中的一些优点,emacs中的优点,并将它们组合成一个很好的大多数预先配置的包.
问题是大多数配置是通过emacs-lisp完成的,并且期望用户在启动软件时理解代码的加载和执行方式,我完全没有(因为我开始意识到越来越多)我深入研究了代码).
我希望能够启动emacs,并看到它执行一些我想写的自定义代码,以便:
我希望能够在必要时对这些功能(以及可能是其他功能)进行实际编码,或者在它们已经可用时安装它们.
(什么不起作用)
我(天真)配置我的spacemacs就像任何lisp/emacs新手一样:
(defun dotspacemacs/user-config ()
"Configuration function for user code.
This function is called at the very end of Spacemacs initialization after
layers configuration. You are free to put any user code."
;; TODO
;; - Display whitespaces
;; - Install workgroups2
;; interface ;; this works
(setq powerline-default-separator 'arrow)
;; …Run Code Online (Sandbox Code Playgroud) 我正在运行Linux,Ubuntu 10.04.
这不是我第一次尝试使用autotools.我做了很多研究并遵循了很多教程:这是我想要做的,我尝试过的以及我面临的问题.
目标:
我尝试了什么,我在哪里:
库名称为FXPLib,包名称为libfxplib-0.1
我的项目文件夹是:
Makefile.am (the only one)
include
include/fxplib.h (main header)
include/FXPLib
include/FXPLib/header1.h (these are public headers)
include/FXPLib/header2.h
include/FXPLib/...
src
src/sub1/file1.c
src/sub2/file1.c
src/sub3 ...
Run Code Online (Sandbox Code Playgroud)我运行了autoscan,得到了一个configure.ac,在这里编辑了它:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT([libfxplib-0.1], [0.1], [<>])
AC_CONFIG_SRCDIR([src/object_system/FXP_image_s.h])
AC_CONFIG_HEADERS(config.h)
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE
AM_PROG_LIBTOOL
LT_PREREQ([2.2])
LT_INIT
# Checks for programs.
AC_PROG_CC
AC_PROG_MAKE_SET
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h])
# Checks for typedefs, structures, and …Run Code Online (Sandbox Code Playgroud)我从3个月开始使用glade和pygtk在python中开发,但即使在我有时间习惯它之前,它已经过时了.
使用Archlinux,我的系统一直都是最新的,所以即使我发现它与gtk2相比有点缺乏功能,我也不得不使用gtk3.
所以我决定切换到pygobject.不幸的是,文档不完整.
我成功地将我的glade文件和我的python代码升级到新系统,但是存在一个错误.
在我的一个程序中,我有一个带有条目的组合框.我用来调用方法get_active_text()来获取条目的内容,无论它是从组合框中选择还是由用户输入.
这个方法不再存在(我想,因为它给了我一个错误)所以我用它代替:
def get_license(self):
#return self.combobox_license.get_active_text()
tree_iter = self.combobox_license.get_active_iter()
if tree_iter != None:
model = self.combobox_license.get_model()
return model[tree_iter][0]
else:
entry = self.combobox_license.get_child()
return entry.get_text()
Run Code Online (Sandbox Code Playgroud)
如您所见,旧代码已被评论.
这段代码有效,但我有一个奇怪的问题:我不能使用该条目!
我可以从组合框中选择文本,但该条目不可用.我可以选择,但我无法输入.
这是我需要在某处激活的新行为吗?使用gtk2版本的程序,我没有任何问题.
以下是我的glade文件中描述组合框条目的部分:
<object class="GtkComboBox" id="combobox_license">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">liststore_license</property>
<property name="has_entry">True</property>
<property name="entry_text_column">0</property>
<signal name="changed" handler="on_combobox_license_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText" id="cellrenderertext_license"/>
</child>
<child internal-child="entry">
<object class="GtkEntry" id="combobox-entry2">
<property name="can_focus">False</property>
<property name="buffer">entrybuffer1</property>
</object>
</child>
</object>
Run Code Online (Sandbox Code Playgroud)
我创建了一个列表库,其中包含一个包含文本的gchararray类型的列.单元格由GtkCellRenderer呈现(但是未定义cellrenderer的属性"text",因为如果我将其定义为0(gchararray),我会得到两次文本!)
我认为添加一个entrybuffer会有所帮助,但它不会改变任何东西.
编辑:我找到了解决方案:嵌入条目的can_focus为false.现在它可以工作,而不需要输入缓冲区.
我在发布之前找到了解决方案,但我发布了以防其他用户也遇到此问题.
有关更新,请参阅编辑1,2和3.我离开这里完整的研究过程.
我知道我们可以使用typed/racket无类型球拍的模块(反之亦然).但是当这样做时,typed/racket模块就像它一样typed/racket/no-check,它会禁用优化并将其用作普通的无类型模块.
例如,如果您有这样的typed/racket模块:
#lang typed/racket
(require math)
(provide hello)
(define (hello [str : String])
(define result : (Matrix Flonum) (do-some-crazy-matrix-operations))
(display (format "Hello ~a! Result is ~a" str result)))
Run Code Online (Sandbox Code Playgroud)
你想在一个像这样的无类型程序中使用它:
#lang racket/base
(require "hello-matrix.rkt")
(hello "Alan Turing")
Run Code Online (Sandbox Code Playgroud)
你会得到非常糟糕的性能结果(在我的情况下,我正在进行大约600000次矩阵乘法,程序甚至没有完成),而使用#lang typed/racket使我的程序在3秒内完成.
缺点是我的无类型代码被类型感染,迫使我在TR中编写我的所有程序,让我很快就疯了.
但我的救世主并不是那么遥远.我偶然发现了一个有趣的四月愚蠢的包裹杰伊麦卡锡在一个阴天黑暗的夜晚写的,叫做live-free-or-die,这几乎是这样的:
http://docs.racket-lang.org/live-free-or-die/index.html
#lang racket/base
(require (for-syntax racket/base
typed-racket/utils/tc-utils))
(define-syntax (live-free-or-die! stx)
(syntax-case stx ()
[(_)
(syntax/loc stx
(begin-for-syntax
(set-box! typed-context? #t)))]))
(provide live-free-or-die!
(rename-out [live-free-or-die!
Doctor-Tobin-Hochstadt:Tear-down-this-wall!]))
Run Code Online (Sandbox Code Playgroud)
通过在我的typed/racket …