An warware: rubuta fitarwa a wuri guda

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.

Shafi posts:

Leave a Comment