python 使用c模塊_您可能沒有使用(但應該使用)的很棒的Python模塊

python 使用c模塊

by Adam Goldschmidt

亞當·戈德施密特(Adam Goldschmidt)

您可能沒有使用(但應該使用)的很棒的Python模塊 (Awesome Python modules you probably aren’t using (but should be))

Python is a beautiful language, and it contains many built-in modules that aim to help us write better, prettier code.

Python是一種漂亮的語言,它包含許多內置模塊,旨在幫助我們編寫更好,更漂亮的代碼。

目的 (Objective)

Throughout this article, we will use some lesser-known modules and methods that I think can improve the way we code - both in visibility and in efficiency.

在本文中,我們將使用一些鮮為人知的模塊和方法,我認為它們可以改善我們的編碼方式-可見性和效率。

命名元組 (NamedTuple)

I believe that some of you already know the more popular namedtuple from the collections module (if you don't - check it out), but since Python 3.6, a new class is available in the typing module: NamedTuple. Both are designed to help you quickly create readable immutable objects.

我相信有些人已經從collections模塊中了解到了更流行的namedtuple (如果您不這樣做的話,請檢查一下 ),但是自Python 3.6起, typing模塊中提供了一個新類: NamedTuple 。 兩者都旨在幫助您快速創建可讀的不可變對象。

NamedTuple is actually a typed version of namedtuple, and in my opinion, is much more readable:

NamedTuple實際上是一個類型版本namedtuple ,在我看來,是更可讀:

Here’s the namedtuple alternative:

這是namedtuple替代方案:

array.array (array.array)

Efficient arrays of numeric values. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. — Python docs

高效的數值數組。 數組是序列類型,其行為與列表非常相似,不同之處在于數組中存儲的對象類型受到約束。 — Python文檔

When using the array module, we need to instantiate it with a typecode, which is the type all of its elements will use. Let's compare time efficiency with a normal list, writing many integers to a file (using pickle module for a regular list):

使用array模塊時,我們需要使用類型碼實例化它,這是其所有元素將使用的類型。 讓我們將時間效率與普通列表進行比較,向文件中寫入許多整數(對常規列表使用pickle模塊):

14 times faster. That’s a lot. Of course it also depends on the pickle module, but still - the array is way more compact than the list. So if you are using simple numeric values, you should consider using the array module.

14倍 。 好多啊。 當然,它也取決于pickle模塊,但是仍然-數組比列表更緊湊。 因此,如果使用簡單的數值,則應考慮使用array模塊。

itertools.combinations (itertools.combinations)

itertools is an impressive module. It has so many different time-saving methods, all of them are listed here. There's even a GitHub repository containing more itertools!

itertools是一個令人印象深刻的模塊。 它有很多不同的省時方法,所有這些都在這里列出。 甚至還有一個包含更多itertools的GitHub存儲庫!

I got to use the combinations method this week and I thought I'd share it. This method takes an iterable and an integer as arguments, and creates a generator consisting of all possible combinations of the iterable with a maximum length of the integer given, without duplication:

我本周必須使用combinations方法,我想我會分享的。 此方法將一個iterable和一個整數作為參數,并創建一個生成器,該生成器由iterable的所有可能組合以及給定整數的最大長度組成,且不重復:

dict.fromkeys (dict.fromkeys)

A quick and beautiful way of creating a dict with default values:

使用默認值創建字典的快速美觀的方法:

最后但并非最不重要的dis模塊 (Last but not least - the dis module)

The dis module supports the analysis of CPython bytecode by disassembling it.

dis模塊通過反匯編來支持CPython 字節碼的分析。

As you may or may not know, Python compiles source code to a set of instructions called “bytecode”. The dis module helps us handle these instructions, and it's a great debugging tool.

您可能知道也可能不知道,Python將源代碼編譯為一組稱為“字節碼”的指令。 dis模塊可幫助我們處理這些指令,它是一個出色的調試工具。

Here’s an example from the Fluent Python book:

這是Fluent Python書中的一個示例:

We got an error — but the operation still succeeded. How come? Well, if we look at the bytecode (I added comments near the important parts):

我們遇到了錯誤-但操作仍然成功。 怎么會? 好吧,如果我們看一下字節碼(我在重要部分附近添加了注釋):

你走之前… (Before you go…)

Thanks for reading! For more Python related articles and other cool stuff, you can follow me on Medium or GitHub (I star some awesome repos!).

謝謝閱讀! 有關更多與Python相關的文章和其他有趣的內容,您可以在Medium或GitHub上關注我(我給一些超贊的存儲庫加注!)。

If you enjoyed this article, please hold down the clap button ? to help others find it. The longer you hold it, the more claps you give!

如果您喜歡這篇文章,請按住拍手按鈕? 幫助別人找到它。 握的時間越長,拍手就越多!

And do not hesitate to share more Python hidden gems in the comments below.

并且不要猶豫在下面的評論中分享更多Python隱藏的寶石。

翻譯自: https://www.freecodecamp.org/news/awesome-python-modules-you-probably-arent-using-but-should-be-ec926da27439/

python 使用c模塊

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392176.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392176.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392176.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

分布與并行計算—生產者消費者模型實現(Java)

在實際的軟件開發過程中,經常會碰到如下場景:某個模塊負責產生數據,這些數據由另一個模塊來負責處理(此處的模塊是廣義的,可以是類、函數、線程、進程等)。產生數據的模塊,就形象地稱為生產者&a…

通過Xshell登錄遠程服務器實時查看log日志

主要想總結以下幾點: 1.如何使用生成密鑰的方式來登錄Xshell連接遠端服務器 2.在遠程服務器上如何上傳和下載文件(下載log文件到本地) 3.如何實時查看log,提取錯誤信息 一. 使用生成密鑰的方式來登錄Xshell連接遠端服務器 ssh登錄…

如何將Jupyter Notebook連接到遠程Spark集群并每天運行Spark作業?

As a data scientist, you are developing notebooks that process large data that does not fit in your laptop using Spark. What would you do? This is not a trivial problem.作為數據科學家,您正在開發使用Spark處理筆記本電腦無法容納的大數據的筆記本電腦…

是銀彈嗎?業務基線方法論

Fred.Brooks在1987年就提出:沒有銀彈。沒有任何一項技術或方法可以能讓軟件工程的生產力在十年內提高十倍。 我無意挑戰這個理論,只想討論一個方案,一個可能大幅提高業務系統開發效率的方案。 方案描述 我管這個方案叫做“由基線擴展…

linux core無權限,linux – 為什么編輯core_pattern受限制?

當我試圖為故意崩潰的程序生成核心文件時,最初的核心文件生成似乎被abrt-ccpp阻礙了.所以我嘗試用vim手動編輯/ proc / sys / kernel / core_pattern:> sudo vim /proc/sys/kernel/core_pattern當我試圖保存文件時,vim報告了這個錯誤:"/proc/sys…

nsa構架_我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰

nsa構架by Denis Nu?iu丹尼斯努尤(Denis Nu?iu) 我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰 (How I solved a simple CrackMe challenge with the NSA’s Ghidra) Hello!你好! I’ve been playing recently a bit with Ghidra, which is a reverse engi…

分布與并行計算—生產者消費者模型隊列(Java)

在生產者-消費者模型中&#xff0c;在原有代碼基礎上&#xff0c;把隊列獨立為1個類實現&#xff0c;通過公布接口&#xff0c;由生產者和消費者調用。 public class Consumer implements Runnable {int n;CountDownLatch countDownLatch;public Consumer(BlockingQueue<In…

python 日志內容提取

問題&#xff1a;如下&#xff0c;一個很大的日志文件&#xff0c;提取 start: 到 end: 標志中間的內容 日志文件a.log xxxxx yyyyy start: start: hahahaha end: start: hahahahha end: ccccccc kkkkkkk cdcdcdcd start: hahahaha end: code import reisfindFalse with open(&…

同一服務器部署多個tomcat時的端口號修改詳情

2019獨角獸企業重金招聘Python工程師標準>>> 同一服務器部署多個tomcat時&#xff0c;存在端口號沖突的問題&#xff0c;所以需要修改tomcat配置文件server.xml&#xff0c;以tomcat7為例。 首先了解下tomcat的幾個主要端口&#xff1a;<Connector port"808…

linux優盤驅動目錄,Linux U盤加載陣列卡驅動步驟(.dd或img).doc

Linux U盤加載陣列卡驅動步驟(.dd或img)如果沒有Linux的機器,可以使用安裝光盤的Linux環境&#xff1a;將?U?盤完全慢速格式化&#xff0c;將驅動拷貝到U盤&#xff0c;將U盤插在服務器上&#xff0c;用Linux安裝光盤第一張啟動到圖形安裝界面&#xff0c;按Ctrl&#xff0b;…

第一章-從雙向鏈表學習設計

鏈表學習鏈表是一種動態的數據結構使用節點作為鏈表的基本單位存儲在節點包括數據元素和節點指針一個完整的數據鏈表應包括轉載于:https://www.cnblogs.com/cjxltd/p/7125747.html

twitter 數據集處理_Twitter數據清理和數據科學預處理

twitter 數據集處理In the past decade, new forms of communication, such as microblogging and text messaging have emerged and become ubiquitous. While there is no limit to the range of information conveyed by tweets and texts, often these short messages are …

ios 動態化視圖_如何在iOS應用中使高度收集視圖動態化

ios 動態化視圖by Payal Gupta通過Payal Gupta 如何在iOS應用中使集合視圖的高度動態化 (How to make height of collection views dynamic in your iOS apps) 充滿活力&#xff0c;就像生活一樣… (Be dynamic, just like life…) Table views and collection views have alw…

新開通博客

新開通博客&#xff0c;希望兄弟們積極更新。 轉載于:https://www.cnblogs.com/ydhliphonedev/archive/2011/07/28/2119720.html

思維導圖分析http之http協議版本

1.結構總覽 在http協議這一章&#xff0c;我將先后介紹上圖六個部分&#xff0c;本文先介紹http的協議版本。 2.http協議版本 http協議的歷史并不長&#xff0c;從1991的0.9版本到現在(2017)僅僅才20多年&#xff0c;算算下來,http還是正處青年&#xff0c;正是大好發展的好時光…

分布與并行計算—生產者消費者模型RabbitMQ(Java)

連接工具 public class ConnectionUtil {public static final String QUEUE_NAME"firstQueue";private static final String RABBIT_HOST "11";private static final String RABBIT_USERNAME "";private static final String RABBIT_PASSWORD…

飛騰 linux 內核,FT2004-Xenomai

移植Xenomai到基于飛騰FT2004 CPU的FT Linux系統1 目前飛騰FT2000/4相關設備驅動還沒有開源&#xff0c;需要先聯系飛騰軟件生態部獲取FT Linux源代碼2 如需在x86交叉編譯arm64內核&#xff0c;推薦使用Linaro gcc編譯器&#xff0c;鏈接如下&#xff1a;https://releases.lina…

使用管道符組合使用命令_如何使用管道的魔力

使用管道符組合使用命令Surely you have heard of pipelines or ETL (Extract Transform Load), or seen some method in a library, or even heard of any tool to create pipelines. However, you aren’t using it yet. So, let me introduce you to the fantastic world of…

關于網頁授權的兩種scope的區別說明

關于網頁授權的兩種scope的區別說明 1、以snsapi_base為scope發起的網頁授權&#xff0c;是用來獲取進入頁面的用戶的openid的&#xff0c;并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁&#xff08;往往是業務頁面&#xff09; 2、以snsapi_userinfo為…

安卓流行布局開源庫_如何使用流行度在開源庫之間進行選擇

安卓流行布局開源庫by Ashish Singal通過Ashish Singal 如何使用流行度在開源庫之間進行選擇 (How to choose between open source libraries using popularity) Through my career as a product manager, I’ve worked closely with engineers to build many technology prod…