Rubutun kayan aikin na'ura a wuri ษaya na iya zama dabara mai amfani ga masu haษakawa yayin aiki tare da aikace-aikacen Python, musamman lokacin haษaka mu'amalar mai amfani a cikin layin umarni, ฦirฦirar alamun ci gaba, da sabunta bayanan wasan bidiyo a cikin ainihin lokaci. Wannan labarin zai tattauna mafita don sake rubuta kayan aikin wasan bidiyo, bayyana lambar mataki-mataki, da nutsewa cikin takamaiman ษakunan karatu da ginanniyar ayyukan Python waษanda ke sa wannan aikin ya yiwu.
Don cimma wannan, za mu iya amfani da sanannen ษakin karatu na Python โlaโananneโ wanda aka tsara musamman don ฦirฦirar aikace-aikacen tushen tasha waษanda suka dogara sosai kan amfani da muโamalar masu amfani da rubutu. Koyaya, don manufar sauฦi da sauฦin fahimta, za mu yi amfani da ginanniyar ginanniyar โsysโ da โlokaciโ na Python don sake rubuta abubuwan na'ura wasan bidiyo.
Rubutun Console a cikin Python
Babban ra'ayin shine amfani da sys.stdout.write() aiki, wanda ke ba mu damar buga a cikin layi ษaya, tare da dawowar kaya hali ("r") don komawa farkon layin, yana ba mu damar sake rubuta abin da aka fitar.
Ga misali na sake rubuta kayan aikin wasan bidiyo ta amfani da Python:
import time import sys for i in range(10): sys.stdout.write("rStep: %d" % i) sys.stdout.flush() time.sleep(1)
Bayanin mataki-mataki na Code
1. Da farko, shigo da samfuran da ake buฦata:
import time import sys
The lokaci za a yi amfani da module don ฦara jinkiri tsakanin maimaitawa, da sys za a yi amfani da module don rubuta fitarwa zuwa na'ura mai kwakwalwa.
2. Na gaba, ฦirฦiri madauki don ฦididdige yawan lambobi, yin kwatankwacin ma'aunin ci gaba:
for i in range(10):
Wannan madauki yana maimaita daga 0 zuwa 9, yana aiki sosai sau goma.
3. A cikin madauki, yi amfani da sys.stdout.write() aiki don buga lambar izza ta yanzu tare da lakabi:
sys.stdout.write("rStep: %d" % i)
Halin "r" shine komawar karusar da ke aiki azaman sake saiti zuwa farkon layin, yana barin fitarwa ta gaba ta sake rubuta na yanzu.
4. Tabbatar da amfani sys.stdout.flush() bayan rubuta zuwa console:
sys.stdout.flush()
Aikin flush() yana share buffer na ciki kuma yana tabbatar da cewa an nuna fitarwa nan da nan.
5. A ฦarshe, ฦara jinkiri ta amfani da lokaci.barci() aiki:
time.sleep(1)
Wannan tsaikon zai ษauki tsawon daฦiฦa ษaya, yana sauฦaฦa ganin an sake rubuta abin da aka fitar.
Yanzu zaku iya ganin yadda ake sake rubuta kayan aikin na'ura akan kowane juzu'i.
Bayanin Laburare na "sys".
The sys ษakin karatu wani ฦaฦฦarfan ginannen tsarin Python ne mai ฦarfi wanda ke ba da dama ga abubuwan ciki na mai fassarar da takamaiman takamaiman tsarin tsarin. A cikin wannan labarin, mun mayar da hankali kan amfani sys.stdout.write() da kuma sys.stdout.flush() ayyuka don sake rubuta kayan aikin wasan bidiyo. Koyaya, ษakin karatu na "sys" yana ba da wasu ayyuka masu yawa, kamar gardamar layin umarni, byteorder, keษancewa, da hanyoyin da aka riga aka ayyana.
Overview na "lokaci" Library
The lokaci ษakin karatu wani tsarin Python ne wanda aka gina a ciki wanda ke ba da ayyuka iri-iri masu alaฦa da sarrafa lokaci da sarrafawa. A cikin misalinmu, mun yi amfani da kayan aiki lokaci.barci() aiki don ฦirฦirar jinkiri tsakanin maimaitawa. Laburaren "lokaci" kuma yana ba da wasu kayan aiki don auna lokacin kisa, canzawa tsakanin tsarin lokaci, da samun lokacin da ake ciki. Wannan tsarin yana da mahimmanci ga masu haษaka aiki tare da ayyuka masu alaฦa da lokaci ko tsara ayyuka a cikin aikace-aikacen Python.