Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
家好,我是DD,已經(jīng)是封閉在家的第51天了!
最近一直在更新Java新特性(https://www.didispace.com/java-features/)和IDEA Tips(https://www.didispace.com/idea-tips/)兩個(gè)原創(chuàng)專欄,其他方向內(nèi)容的動(dòng)態(tài)關(guān)注少了。昨天天晚上刷推的時(shí)候,瞄到了這個(gè)神奇的東西,覺(jué)得挺cool的,拿出來(lái)分享下:
相信你看到圖,不用我說(shuō),你也猜到是啥了吧?html里可以跑python代碼了!
看到好多Python公眾號(hào)已經(jīng)開(kāi)始猛吹未來(lái)了,但乍看怎么覺(jué)得有點(diǎn)像JSP?或者一些模版引擎?是進(jìn)步還是倒退呢?與其瞎想,不如仔細(xì)看看這個(gè)東東的能力吧!
根據(jù)官方介紹,這個(gè)名為PyScript的框架,其核心目標(biāo)是為開(kāi)發(fā)者提供在標(biāo)準(zhǔn)HTML中嵌入Python代碼的能力,使用 Python調(diào)用JavaScript函數(shù)庫(kù),并以此實(shí)現(xiàn)利用Python創(chuàng)建Web應(yīng)用的功能。
看到介紹里提到了調(diào)用JavaScript函數(shù)庫(kù)的能力,看來(lái)跟JSP或者模版引擎還是有區(qū)別的。
官方給了一個(gè)例子,可以幫助我們觀的感受這個(gè)開(kāi)發(fā)框架的能力,不妨跟著DD看看,它能做啥吧!
第一個(gè)案例,hello world
代碼很簡(jiǎn)單,就下面這幾行。你只需要?jiǎng)?chuàng)建一個(gè)html文件,然后復(fù)制進(jìn)去就可以了。
|
保存好之后,在瀏覽器里打開(kāi)就能看到這樣的頁(yè)面了:
回頭再看看這個(gè)html里的內(nèi)容,三個(gè)核心內(nèi)容:
如果你懶得自己敲代碼的話,本文的兩個(gè)案例代碼我打包放在公眾號(hào)了,需要的朋友可以關(guān)注公眾號(hào)“程序猿DD”,回復(fù):pyscript 獲取。
第二個(gè)案例,數(shù)據(jù)定義 + 數(shù)據(jù)展示
先創(chuàng)建一個(gè)data.py文件,然后加入前面的代碼。功能很簡(jiǎn)單,就是隨機(jī)生成(x,y)的坐標(biāo)
|
再創(chuàng)建一個(gè)html文件,加入下面的代碼
|
這里就稍微復(fù)雜一些了,除了hello world中的幾個(gè)要點(diǎn)外,這里還有這幾個(gè)要關(guān)注的地方:
這個(gè)頁(yè)面的執(zhí)行效果是這樣的:
是不是很神奇呢?整個(gè)過(guò)程中都沒(méi)有大家熟悉的cs、js內(nèi)容,就完成了這樣一個(gè)圖的頁(yè)面實(shí)現(xiàn)。
最后,談?wù)勗谡麄€(gè)嘗試過(guò)程中,給我的幾個(gè)感受:
這個(gè)開(kāi)發(fā)框架目前還只是alpha版本,未來(lái)一定還會(huì)有更多特性與優(yōu)化出來(lái),總體上我覺(jué)得這個(gè)框架還是非常cool的,尤其對(duì)于剛學(xué)會(huì)Python,或者只會(huì)Python,但又想快速開(kāi)發(fā)Web應(yīng)用的小伙伴來(lái)說(shuō),可能將會(huì)是個(gè)不錯(cuò)的選擇,那你覺(jué)得這個(gè)框架如何?未來(lái)會(huì)不會(huì)火?留言區(qū)聊聊吧!
數(shù)字的游戲
根據(jù)官方教程,照寫一個(gè)程序,來(lái)體驗(yàn)一下rust的程序開(kāi)發(fā)過(guò)程。
游戲很簡(jiǎn)單, 就是一次次輸入比對(duì)預(yù)先生成的隨機(jī)數(shù)。
Cargo是rust的包管理工具, 對(duì)于本需求需要rand的方法, 但是該方法再rust中并不存在, 所以通過(guò)Cargo引入了第三方包的方式進(jìn)行使用。
Cargo 不僅僅是一個(gè)包管理器,同時(shí)還是Rust項(xiàng)目的管理工具。
可以通過(guò)cargo的幫助信息查看命令以及功能說(shuō)明. cargo -h
Rust's package manager
Usage: cargo [+toolchain] [OPTIONS] [COMMAND]
cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]...
Options:
-V, --version Print version info and exit
--list List installed commands
--explain <CODE> Provide a detailed explanation of a rustc error message
-v, --verbose... Use verbose output (-vv very verbose/build.rs output)
-q, --quiet Do not print cargo log messages
--color <WHEN> Coloring: auto, always, never
-C <DIRECTORY> Change to DIRECTORY before doing anything (nightly-only)
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
--offline Run without accessing the network
--config <KEY=VALUE> Override a configuration value
-Z <FLAG> Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
-h, --help Print help
Commands:
build, b Compile the current package
check, c Analyze the current package and report errors, but don't build object files
clean Remove the target directory
doc, d Build this package's and its dependencies' documentation
new Create a new cargo package
init Create a new cargo package in an existing directory
add Add dependencies to a manifest file
remove Remove dependencies from a manifest file
run, r Run a binary or example of the local package
test, t Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this package to the registry
install Install a Rust binary. Default location is $HOME/.cargo/bin
uninstall Uninstall a Rust binary
... See all commands with --list
See 'cargo help <command>' for more information on a specific command.
可以通過(guò)cargo --list查看所提供的命令
Installed Commands:
add Add dependencies to a Cargo.toml manifest file
b alias: build
bench Execute all benchmarks of a local package
build Compile a local package and all of its dependencies
c alias: check
check Check a local package and all of its dependencies for errors
clean Remove artifacts that cargo has generated in the past
clippy Checks a package to catch common mistakes and improve your Rust code.
config Inspect configuration values
d alias: doc
doc Build a package's documentation
fetch Fetch dependencies of a package from the network
fix Automatically fix lint warnings reported by rustc
fmt Formats all bin and lib files of the current crate using rustfmt.
generate-lockfile Generate the lockfile for a package
git-checkout This command has been removed
help Displays help for a cargo subcommand
init Create a new cargo package in an existing directory
install Install a Rust binary. Default location is $HOME/.cargo/bin
locate-project Print a JSON representation of a Cargo.toml file's location
login Log in to a registry.
logout Remove an API token from the registry locally
metadata Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-readable format
miri
new Create a new cargo package at <path>
owner Manage the owners of a crate on the registry
package Assemble the local package into a distributable tarball
pkgid Print a fully qualified package specification
publish Upload a package to the registry
r alias: run
read-manifest Print a JSON representation of a Cargo.toml manifest.
remove Remove dependencies from a Cargo.toml manifest file
report Generate and display various kinds of reports
rm alias: remove
run Run a binary or example of the local package
rustc Compile a package, and pass extra options to the compiler
rustdoc Build a package's documentation, using specified custom flags.
search Search packages in crates.io
t alias: test
test Execute all unit and integration tests and build examples of a local package
tree Display a tree visualization of a dependency graph
uninstall Remove a Rust binary
update Update dependencies as recorded in the local lock file
vendor Vendor all dependencies for a project locally
verify-project Check correctness of crate manifest
version Show version information
yank Remove a pushed crate from the index
Cargo中,每個(gè)命令均可通過(guò)-help的方式來(lái)查看具體命令的用法, 例如:cargo run -help
較為常用的如下:
作為包管理工具,cargo可以下載第三方庫(kù)。
同時(shí)cargo 也可以用來(lái)建立自己的rust項(xiàng)目, 包括可行性程序以及庫(kù)。
可以通過(guò)
cargo new project_name -bin 創(chuàng)建一個(gè)rust程序
cargo new project_name --lib 創(chuàng)建一個(gè)庫(kù)
再rust 第三方庫(kù)叫crates, 可以通過(guò)https://crates.io/上找到。
本例子中使用rand, 可以找到該庫(kù)文件。
工程建立一些代碼的編寫, 本章節(jié)不做細(xì)節(jié)描述。 概要說(shuō)明一下
$ cargo new guess-game-app
Created binary (application) `guess-game-app` package
$ cd guess-game-app
修改toml文件,加入rand依賴
[package]
name = "guess-game-app"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand="0.8.5"
編譯cargo build或cargo check
引入包后,習(xí)慣的執(zhí)行編譯, 會(huì)導(dǎo)入對(duì)應(yīng)的包文件。
Updating crates.io index
Downloaded rand_chacha v0.3.1
Downloaded cfg-if v1.0.0
Downloaded ppv-lite86 v0.2.17
Downloaded rand_core v0.6.4
Downloaded getrandom v0.2.11
Downloaded rand v0.8.5
Downloaded libc v0.2.150
Downloaded 7 crates (910.0 KB) in 4.23s
Compiling libc v0.2.150
Compiling cfg-if v1.0.0
Compiling ppv-lite86 v0.2.17
Compiling getrandom v0.2.11
Compiling rand_core v0.6.4
Compiling rand_chacha v0.3.1
Compiling rand v0.8.5
Compiling guess-game-app v0.1.0 (/Users/zhuchunlei/work/08_rust/guess-game-app)
Finished dev [unoptimized + debuginfo] target(s) in 12.11s
修改源文件,src/main.rs(代碼來(lái)源教程中,目前為止只是照著敲, 具體含義再逐漸學(xué)習(xí)理解)
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {guess}");
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
序員寶藏庫(kù):https://gitee.com/sharetech_lee/CS-Books-Store
你想要的,這里都有!
作為程序員用的比較多的工具是什么?
我覺(jué)得搜索引擎絕對(duì)能名列前茅。
在開(kāi)發(fā)過(guò)程中,總會(huì)遇到這樣或者那樣的問(wèn)題超出我們自身的知識(shí)范圍。這些問(wèn)題可能是編程語(yǔ)言方面的,可能是Linux操作系統(tǒng)方面的,可能是服務(wù)部署方面的等等。
當(dāng)遇到這種問(wèn)題時(shí),很多同學(xué)會(huì)選擇求助于搜索引擎,懂得懂得,搜索引擎搜出的很多結(jié)果要么和問(wèn)題無(wú)關(guān)、要么是個(gè)大坑。
所以,從事開(kāi)發(fā)工作這么多年,我深刻意識(shí)一個(gè)道理,能夠快速解決問(wèn)題真的是一項(xiàng)非常突出的本領(lǐng),哪怕是在借助于互聯(lián)網(wǎng)的情況下。
話說(shuō)回來(lái),正是因?yàn)閷で髥?wèn)題的道路非常曲折繁瑣,因此,網(wǎng)上出現(xiàn)了各種各樣的速查表「CheatSheet」,估計(jì)有不少同學(xué)過(guò)去都看到過(guò)很多了。
但是,我認(rèn)為這并沒(méi)有從效率上徹底解決問(wèn)題。
今天我在瀏覽Github時(shí)發(fā)現(xiàn)一款非常不錯(cuò)的開(kāi)源工具,真的讓我眼前一亮。
這款開(kāi)源工具的名字叫做cheat.sh,目前已經(jīng)33K+ Star了。
這款工具的簡(jiǎn)潔是「你唯一需要的速查表」,簡(jiǎn)單概括,這款工具把知名、權(quán)威社區(qū)的答案、資料進(jìn)行了統(tǒng)一匯總,我們只需要這一款工具就可以快速得到最準(zhǔn)確、最可靠的答案。
它背后的知識(shí)來(lái)源于tldr、StackOverflow、cheat.sheets等社區(qū)或者開(kāi)源項(xiàng)目。
為了更加清晰的理解它的用處,下面先來(lái)看一個(gè)示例:
$ curl cht.sh/lua/table+keys
-- lua: retrieve list of keys in a table
local keyset={}
local n=0
for k,v in pairs(tab) do
n=n+1
keyset[n]=k
end
--[[
[ Note that you cannot guarantee any order in keyset. If you want the
[ keys in sorted order, then sort keyset with table.sort(keyset).
[
[ [lhf] [so/q/12674345] [cc by-sa 3.0]
]]
上面執(zhí)行的curl命令包含下面幾個(gè)信息:
通過(guò)這樣,它就可以快速在終端下給出問(wèn)題的答案。
如果你不想要文字描述,只想要「純凈」的答案,還可以通過(guò)參數(shù)來(lái)處理:
$ curl cht.sh/lua/table+keys\?Q
local keyset={}
local n=0
for k,v in pairs(tab) do
n=n+1
keyset[n]=k
end
想必到這里,很多同學(xué)都已經(jīng)知道它的用法了,概括一下這款工具的價(jià)值,主要有以下幾點(diǎn):
可能很多同學(xué)還有一絲疑慮,感覺(jué)用curl每次都要輸入很長(zhǎng)的命令,太麻煩。
沒(méi)事,前面只是一個(gè)小示例,下面系統(tǒng)的介紹一下如何安裝并使用客戶端。
方式1:
PATH_DIR="$HOME/bin" # or another directory on your $PATH
mkdir -p "$PATH_DIR"
curl https://cht.sh/:cht.sh > "$PATH_DIR/cht.sh"
chmod +x "$PATH_DIR/cht.sh"
方式二:
curl -s https://cht.sh/:cht.sh | sudo tee /usr/local/bin/cht.sh && sudo chmod +x /usr/local/bin/cht.sh
安裝后就可以在終端下使用了。
如果想用交互式命令模式,還需要通過(guò)命令sudo apt install rlwrap安裝rlwrap。
通過(guò)上述命令安裝客戶端之后,就不用再像前面curl命令那樣每次輸入很長(zhǎng)的命令,可以用更加貼近人為描述的方式查詢問(wèn)題答案。
舉幾個(gè)例子:
$ cht.sh go reverse a list
$ cht.sh python random list elements
$ cht.sh js parse json
這樣看,估計(jì)很多開(kāi)發(fā)同學(xué)都能夠懂得它的用法了,「命令+語(yǔ)言+問(wèn)題」。
可以看看效果:
$ cht.sh python random number
# Try:
from random import randrange
print(randrange(10))
# **Docs:**
# https://docs.python.org/3/library/random.htmlrandom.randrange
#
# [kovshenin] [so/q/3996904] [cc by-sa 3.0]
再嘗試一個(gè):
$ cht.sh python matplotlib plot line
# You can directly plot the lines you want by feeding the `plot` command
# with the corresponding data (boundaries of the segments):
#
# `plot([x1, x2], [y1, y2], color='k', linestyle='-', linewidth=2)`
#
# (of course you can choose the color, line width, line style, etc.)
#
# From your example:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(5)
x = np.arange(1, 101)
y = 20 + 3 * x + np.random.normal(0, 60, 100)
plt.plot(x, y, "o")
# draw vertical line from (70,100) to (70, 250)
plt.plot([70, 70], [100, 250], 'k-', lw=2)
# draw diagonal line from (70, 90) to (90, 200)
plt.plot([70, 90], [90, 200], 'k-')
plt.show()
# 
#
# [gcalmettes] [so/q/12864294] [cc by-sa 3.0]
可以看得出來(lái),它不僅支持編程語(yǔ)言的基本語(yǔ)法,還支持查詢語(yǔ)言基礎(chǔ)之外的工具包的用法。
前面介紹了它在命令行下的用法,其實(shí),cht.sh更強(qiáng)大的是它不僅支持命令行,它還可以在常用的IDE、編輯器下用。
例如:
除此之外,它還是跨平臺(tái)的,在Windows、Linux、macOS下都可以用。
另外,這款工具在編輯器、IDE下功能更加豐富,甚至可以自動(dòng)生成代碼片段,直接補(bǔ)全答案。
下面看一下效果!
Sublime:
IDEA:
VS Code:
最后再補(bǔ)充一點(diǎn),在知識(shí)方面,它覆蓋也非常全,Python、JavaScript、Go、C++、Linux、php,我們?cè)陂_(kāi)發(fā)中遇到的很多問(wèn)題通過(guò)cht.sh都可以快速得到答案!
傳送門:
建議大家有空可以多瀏覽Github,有很多好用的開(kāi)源免費(fèi)工具。但是,目前Github上項(xiàng)目多如牛毛,而且刷榜現(xiàn)在也非常嚴(yán)重,想要找到優(yōu)質(zhì)的項(xiàng)目變得越來(lái)越難。這里,給大家推薦Github上一個(gè)非常不錯(cuò)的項(xiàng)目《DevWeekly》,這個(gè)項(xiàng)目每周都會(huì)精挑細(xì)選一些優(yōu)質(zhì)的開(kāi)源項(xiàng)目,涵蓋C++、JAVA、JavaScript、Python、Go等不同編程語(yǔ)言。此外,還有一些開(kāi)源工具、優(yōu)秀的技術(shù)文章:
https://github.com/Jackpopc/DevWeekly
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。