# Python: problème avec type de variable

**URL:** <https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310>\
**Category:** Programmation\
**Created:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310 "2016-02-21T21:27:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/1 "2016-02-21T21:27:22Z")

</div>

Bonjour,

j’ai un petit problème avec ce code python:

`niv = os.system("rec -n stat trim 0 .01 2>&1 | awk '/^Maximum amplitude/' | cut -d':' -f2 | sed 's/ //g'")
print niv*100`

Je pense que c’est parce que je récupère une valeur de type string et du coup la multiplication ne marche pas…  
C’est peut-être tout con, mais je ne trouve pas comment faire… 🙂

J’ai évidemment essayé float(os.system("…")) mais sans succès…

Merci!

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/2 "2016-02-21T21:27:23Z")

</div>

Trouvé comme ça:

```auto

```

---

<div class="post-metadata">

**Author:** ![seb-ksl](https://www.debian-fr.org/user_avatar/www.debian-fr.org/seb-ksl/32/2946_2.png) [@seb-ksl](https://www.debian-fr.org/u/seb-ksl)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/3 "2016-02-21T21:27:24Z")

</div>

À la place d’os.system et os.popen, la doc recommande d’utiliser le module subprocess. J’utilise souvent subprocess.call pour appeler des commandes système.

Sinon, pour vérifier le type d’une variable, tu as la fonction embarquée type.

[code]\>\>\> type(«&nbsp;foo&nbsp;»)  
\<class ‹&nbsp;str&nbsp;›\>

> > > type(2)  
> > > \<class ‹&nbsp;int&nbsp;›\>  
> > > type(2.0)  
> > > \<class ‹&nbsp;float&nbsp;›\>[/code]

---

<div class="post-metadata">

**Author:** ![limax](https://www.debian-fr.org/user_avatar/www.debian-fr.org/limax/32/3886_2.png) [@limax](https://www.debian-fr.org/u/limax)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/4 "2016-02-21T21:27:24Z")

</div>

Bonjour  
J’utiliserai aussi le module re (regex) à la place de awk | cut | sed

Remarque même avec bash je n’utiliserai que sed (ne connaissant pas awk) avec une expression régulière.

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/5 "2016-02-21T21:27:25Z")

</div>

Bonjour et merci de vos réponses!

En fait j’ai un autre soucis: la commande ci-dessus avec rec me prend trop de ressources (pulseaudio en permanence à 15-20%). J’ai une autre solution qui à l’air moins gourmande:

```auto

```

qui me renvoie quelque chose comme ça:

`Capture WAVE '/dev/null' : Signed 16 bit Little Endian, Frquence 8000 Hz, Stro
Avertissement: la frquence n'est pas prcise (demand = 8000Hz,
obtenu = 44100Hz)
         veuillez essayez le greffon de branchement 
Hardware PCM card 0 'HDA Intel' device 0 subdevice 0
Its setup is:
  stream : CAPTURE
  access : RW_INTERLEAVED
  format : S16_LE
  subformat : STD
  channels : 2
  rate : 44100
  exact rate : 44100 (44100/1)
  msbits : 16
  buffer_size : 16384
  period_size : 4096
  period_time : 92879
  tstamp_mode : NONE
  period_step : 1
  avail_min : 4096
  period_event : 0
  start_threshold : 1
  stop_threshold : 16384
  silence_threshold: 0
  silence_size : 0
  boundary : 1073741824
  appl_ptr : 0
  hw_ptr : 0
Pic max. (8192 chantillons): 0x00000071 # 0%
Pic max. (8192 chantillons): 0x00000056 # 0%
Pic max. (8192 chantillons): 0x00000060 # 0%
Pic max. (8192 chantillons): 0x000000b7 # 0%
Pic max. (8192 chantillons): 0x00000129 # 0%
Pic max. (8192 chantillons): 0x000000de # 0%
Pic max. (8192 chantillons): 0x00000047 # 0%
Pic max. (8192 chantillons): 0x00000056 # 0%
Pic max. (8192 chantillons): 0x00000095 # 0%
Pic max. (8192 chantillons): 0x000000a1 # 0%
Pic max. (8192 chantillons): 0x0000011b # 0%
Pic max. (8192 chantillons): 0x000000a7 # 0%
Pic max. (8192 chantillons): 0x0000016f # 1%
Pic max. (8192 chantillons): 0x0000009a # 0%
Pic max. (8192 chantillons): 0x00000074 # 0%
Pic max. (8192 chantillons): 0x000000fb # 0%
Pic max. (8192 chantillons): 0x00000051 # 0%
Pic max. (8192 chantillons): 0x00000073 # 0%
Pic max. (8192 chantillons): 0x00000088 # 0%
Pic max. (8192 chantillons): 0x0000022d # 1%
Pic max. (8192 chantillons): 0x00000126 # 0%
Pic max. (8192 chantillons): 0x00000109 # 0%
Pic max. (8192 chantillons): 0x00000211 # 1%
Pic max. (8192 chantillons): 0x000001bf # 1%
Pic max. (8192 chantillons): 0x00000048 # 0%
Pic max. (8192 chantillons): 0x000000c4 # 0%
Pic max. (8192 chantillons): 0x000000f5 # 0%
Pic max. (8192 chantillons): 0x000003f9 # 3%
Pic max. (8192 chantillons): 0x000003f3 # 3%
Pic max. (8192 chantillons): 0x00000327 # 2%
Pic max. (8192 chantillons): 0x0000138c #### 15%
Pic max. (8192 chantillons): 0x000015ca #### 17%`

et je voudrais juste récupérer la valeur avant le %

Merci

---

<div class="post-metadata">

**Author:** ![seb-ksl](https://www.debian-fr.org/user_avatar/www.debian-fr.org/seb-ksl/32/2946_2.png) [@seb-ksl](https://www.debian-fr.org/u/seb-ksl)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/6 "2016-02-21T21:27:30Z")

</div>

```auto
import re, subprocess
arec = subprocess.call(["arecord", "-D", "hw:0", "-c 2", "-d 0", "-f", "S16_LE", "-vvv", "/dev/null"])
max_amp = re.search("(\d*)%", arec)
```

À vérifier et adapter, je n’ai pas testé le code.  
Tu as la doc du module re ici : [docs.python.org/3.4/library/re.html](https://docs.python.org/3.4/library/re.html)

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Février 21, 2016, 9:27 UTC](https://www.debian-fr.org/t/python-probleme-avec-type-de-variable/65310/7 "2016-02-21T21:27:33Z")

</div>

OK merci… Je garde ça dans un coin et je testerai plus tard (plus trop le temps là…)
