Re: [Python] modo furbo per trovare pattern differenti in una stringa

2024-02-14 Per discussione Massimiliano Tornati
Ciao,
visto che il GCode è un codice strutturato in cui se trovi dei comandi
specifici hai delle funzioni apposite; puoi definire una classe con gli
oggetti che la compongono così che poi ti puoi gestire tutto il codice
strutturato secondo le varie funzioni che ti servono perchè in realtà nono
sono a caso.

Il giorno mar 13 feb 2024 alle ore 14:32 Perini Matteo <
perini.mat...@gmail.com> ha scritto:

> Ciao a tutti,
>
> è da un po' che non scrivo.
>
> Vi chiedo aiuto perchè sto cercando di leggere delle coordinate da un file 
> Gcode.
>
> Un esempio del testo da parsare è:
>
> ;LAYER:1
> ;MESH:untitled.stl
> G0 F7200 X1298 Y1798 Z4
> ;TYPE:WALL-OUTER
> G1 F1800 X702 Y1798 E1494.81223
> G1 X702 Y1202 E2242.21834
> G92 E0
> G1 X1298 Y1202 E747.40611
> G1 Y1798 X1298 E1494.81223
> ;TIME_ELAPSED:177.752007
> ;CHANGE;
> ;LAYER:2
> ;MESH:untitled.stl
> G0 F7200 X1298 Y1798 Z6
> ;TYPE:WALL-OUTER
> G1 F1800 X702 Y1798 E2242.21834
> G92 E0
> G1 X702 Y1202 E747.40611
>
> non ho grossi problemi ad effettuare un parsing corretto ma lo sto facendo 
> con vari if, elif, case,  ecc. cosa che non mi piace molto.
>
> Stavo provando ad utilizzare le regex ma non sto riuscendo a fare quello che 
> voglio (in modo pulito) ovvero identificare le coordinate X, Y e Z anche se 
> in alcuni casi hanno posizioni invertite.
> Vorrei anche attribuire un nome al gruppo identificato in modo da usarlo come 
> dizionario.
> Stavo usando alcuni pattern tipo ".*Z(?P\d+[.]?\d*)" che vanno bene per 
> individuare le coordinate di un asse ma capita che le righe contengano x y e 
> z oppure solo Z oppure solo x e y.
> Importante, in alcuni casi potrebbero essere anche in ordine differente tipo 
> z y e x.
>
> Per ora i numeri dopo E e F posso trascurarli ma vi chiederei aiuto per 
> riuscire ad ottenere le coordinate da ogni riga.
>
> se tutte le righe rispettassero lo stesso pattern non avrei problemi ma non 
> so come affrontare soprattutto il problema di avere pattern diversi in ordine 
> (quasi) random.
>
> Spero di essere stato abbastanza chiaro.
>
> Grazie a tutti
>
> Matteo
>
>
>
>
> ___
> Python mailing list
> Python@lists.python.it
> https://lists.python.it/mailman/listinfo/python
>


-- 


mobile phone : +39 348 002 8107
tel: +39 0721 1630292
Per appuntamento: https://zcal.co/massimiliano_tornati

TORNATI PROJECT SRL
via Callegari 9-11
61122 Pesaro (PU)
P.IVA/C.F.  02533270415
site: TORNATI PROJECT 
site:https://itofood.com/





e-mail t...@tornatiproject.com
Whatapp:https://wa.me/message/G6CHK7323ZODI1

The information contained in this e-mail and any attachment is
confidential. It is intended only for the named addressee(s).
If you are not the named addressee(s) please notify us immediately at the
e-mail  t...@tornatiproject.com and then destroy this message.The copy or
distribution of the contents to any other person other than the intended
addressee(s) is strictly prohibited and will be punished according to  the
relevant laws on privacy and Intellectual Property.

Think before you print!
Before you print, please consider if it is necessary. Printing uses
electricity, ink and paper.
___
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python


Re: [Python] modo furbo per trovare pattern differenti in una stringa

2024-02-13 Per discussione Alessandro T.

On 13/02/24 14:32, Perini Matteo wrote:

Ciao a tutti,
è da un po' che non scrivo.
Vi chiedo aiuto perchè sto cercando di leggere delle coordinate da un file 
Gcode.
[...]
Vorrei anche attribuire un nome al gruppo identificato in modo da usarlo come 
dizionario.


Ciao,
se ho capito bene qualcosa così dovrebbe andare:

import re

origin =""";LAYER:1
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z4
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E1494.81223
G1 X702 Y1202 E2242.21834
G92 E0
G1 X1298 Y1202 E747.40611
G1 Y1798 X1298 E1494.81223
;TIME_ELAPSED:177.752007
;CHANGE;
;LAYER:2
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z6
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E2242.21834
G92 E0
G1 X702 Y1202 E747.40611"""


pattern = re.compile(r"([EFXYZ])(\d+\.?\d*)")

for linea in origin.splitlines():
    if not linea.startswith(';'):
    print(dict(pattern.findall(linea)))



--
Alessandro T.

R: Perché leggiamo dall'alto al basso e da sinistra a destra.
D: Perché dovrei iniziare la risposta all'e-mail dopo il testo citato?

___
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python


Re: [Python] modo furbo per trovare pattern differenti in una stringa

2024-02-13 Per discussione Yuri

Ciao,

 se fai split sulla riga con separatore lo spazio e guardi quelle che 
cominciano con GX, non è meglio? Lì prendi quelli che cominciano per X, 
Y e Z. Ignori le righe che non servono (quelle che cominciano per ; ad 
esempio).


Con regexp, posso suggerirti questo:

https://stackoverflow.com/a/3533526/15822654

che sembra affrontare il tuo problema, cioè il match di token sparsi 
casualmente in una stringa.


Il 13/02/24 14:32, Perini Matteo ha scritto:

Ciao a tutti,
è da un po' che non scrivo.
Vi chiedo aiuto perchè sto cercando di leggere delle coordinate da un file 
Gcode.
Un esempio del testo da parsare è:
;LAYER:1
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z4
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E1494.81223
G1 X702 Y1202 E2242.21834
G92 E0
G1 X1298 Y1202 E747.40611
G1 Y1798 X1298 E1494.81223
;TIME_ELAPSED:177.752007
;CHANGE;
;LAYER:2
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z6
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E2242.21834
G92 E0
G1 X702 Y1202 E747.40611
non ho grossi problemi ad effettuare un parsing corretto ma lo sto facendo con 
vari if, elif, case,  ecc. cosa che non mi piace molto.
Stavo provando ad utilizzare le regex ma non sto riuscendo a fare quello che 
voglio (in modo pulito) ovvero identificare le coordinate X, Y e Z anche se in 
alcuni casi hanno posizioni invertite.
Vorrei anche attribuire un nome al gruppo identificato in modo da usarlo come 
dizionario.
Stavo usando alcuni pattern tipo ".*Z(?P\d+[.]?\d*)" che vanno bene per individuare le coordinate di 
un asse ma capita che le righe contengano x y e z oppure solo Z oppure 
solo x e y. Importante, in alcuni casi potrebbero essere anche in 
ordine differente tipo z y e x. Per ora i numeri dopo E e F posso 
trascurarli ma vi chiederei aiuto per riuscire ad ottenere le 
coordinate da ogni riga. se tutte le righe rispettassero lo stesso 
pattern non avrei problemi ma non so come affrontare soprattutto il 
problema di avere pattern diversi in ordine (quasi) random. Spero di 
essere stato abbastanza chiaro. Grazie a tutti Matteo




___
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python___
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python


[Python] modo furbo per trovare pattern differenti in una stringa

2024-02-13 Per discussione Perini Matteo

Ciao a tutti,

è da un po' che non scrivo.

Vi chiedo aiuto perchè sto cercando di leggere delle coordinate da un file 
Gcode.

Un esempio del testo da parsare è:

;LAYER:1
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z4
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E1494.81223
G1 X702 Y1202 E2242.21834
G92 E0
G1 X1298 Y1202 E747.40611
G1 Y1798 X1298 E1494.81223
;TIME_ELAPSED:177.752007
;CHANGE;
;LAYER:2
;MESH:untitled.stl
G0 F7200 X1298 Y1798 Z6
;TYPE:WALL-OUTER
G1 F1800 X702 Y1798 E2242.21834
G92 E0
G1 X702 Y1202 E747.40611

non ho grossi problemi ad effettuare un parsing corretto ma lo sto facendo con 
vari if, elif, case,  ecc. cosa che non mi piace molto.

Stavo provando ad utilizzare le regex ma non sto riuscendo a fare quello che 
voglio (in modo pulito) ovvero identificare le coordinate X, Y e Z anche se in 
alcuni casi hanno posizioni invertite.
Vorrei anche attribuire un nome al gruppo identificato in modo da usarlo come 
dizionario.
Stavo usando alcuni pattern tipo ".*Z(?P\d+[.]?\d*)" che vanno bene per individuare le coordinate di un 
asse ma capita che le righe contengano x y e z oppure solo Z oppure solo 
x e y. Importante, in alcuni casi potrebbero essere anche in ordine 
differente tipo z y e x. Per ora i numeri dopo E e F posso trascurarli 
ma vi chiederei aiuto per riuscire ad ottenere le coordinate da ogni 
riga. se tutte le righe rispettassero lo stesso pattern non avrei 
problemi ma non so come affrontare soprattutto il problema di avere 
pattern diversi in ordine (quasi) random. Spero di essere stato 
abbastanza chiaro. Grazie a tutti Matteo


___
Python mailing list
Python@lists.python.it
https://lists.python.it/mailman/listinfo/python