Cs2py (from codesource to python)

Contenu du snippet

A propos d'un source python du site, jpountz écrit le 27/08/2006 : "ça serait bien si tu pouvais mettre un zip, parce que le copier/coller depuis le site n'est pas top."

Pour résoudre ce problème, j'utilise un petit script python qui fait le ménage après un copier/coller :

- supression des dièse+espace
ou
- suppression des numéro_de_ligne+point+espace

selon les cas.

C'est sous GPL.

Source / Exemple :


#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

# nettoyer le copier/coller du CodeSource vers python
# en effacant le # ou le numero de ligne

############################################################################
# Copyright (C) 2006 Andre Connes
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
############################################################################
# File    : cs2py.py
# Author  : Andre Connes
# Mailto  : andre.connes@wanadoo.fr
# Date    : 29/08/2006
# Licence : GNU/GPL Version 2 ou plus
#
# Description:
# ------------
#
# @version    $Id: Exp $
# @author     Andre Connes
# @project
# @copyright  Andre Connes 29/08/2006
#
#
#########################################################################

import sys

if len(sys.argv) < 3:
    print "Usage : %s fichier_source fichier_but" % sys.argv[0]
    sys.exit(1)

fsname = sys.argv[1]
fbname = sys.argv[2]
  
fs  =open(fsname,'r')
fb  =open(fbname,'w')

while 1:
    ligne=fs.readline()	# lit le caractere eol
    if ligne=="":
      break
    if ligne[0] == "#":
        nouvelleligne = ligne[2:]
    else:
        nouvelleligne = ligne[ligne.index(".")+2:]
    print ".",
    fb.write(nouvelleligne)
print "\nTermine !\n\n*** Faites un chmod +x fichier_but (sous *NIX) ***"
fs.close()
fb.close

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.