[Python-de] Re: OT: Python

[email protected] (Stefan Ram)
Newsgroups gmane.comp.python.general.german
Organization Stefan Ram
Message-ID <[email protected]>
Newsgroups: de.alt.folklore.computer,de.comp.lang.python

Hermann Riemann <[email protected]> writes:
>manchmal überlegend, den Präpozessor für C
>auch für Python zu verwenden.
>( Und bei Fehlermeldungen rückrechnen )

  (Dieses Posting enthält Zeilen mit mehr als 72 Zeichen.)

  (Falls das Thema by Python bleibt, könnte
  "de.alt.folklore.computer" aus der Newsgroups-Kopfzeile
  entfernt werden.)

  Ich hatte kürzlich Funktionen für Python 3.9 geschrieben,
  die praktisch einen Quelltext aus einer anderen Datei an
  dieser Stelle ausführen können. Nach den beiden
  untenstehenden Definitionen kann man dann beispielsweise mit 

util_include_from( _pathlib.Path( __file__ ).parent, "example.py", globals() )

  die Datei "example.py" aus demselben Verzeichnis so
  ausführen lassen, als würde sie an dieser Stelle eingefügt
  werden (sie teilt sich die globalen Variablen also mit dem
  Aufrufer). Genauso möglich sollte auch

util_execfile( "C:/dir1/dir2/example.py", globals=globals() )

  sein.

import pathlib as _pathlib

def util_execfile( filepath, globals=None, transform=lambda text: text, encoding="utf-8" ):
    """Execute a given file with exec.

    Condition: (none).

    Parameters:
    
    filepath -- path to the file to be executed (of a subtype of "str" or of "pathlib.PurePath").
    globals -- dictionary of globals to be used for the execution as if by "exec"
    transform -- a function mapping an str string to an str string for transforming the code read from the file before execution
    encoding -- the encoding to be used to convert the file to a text

    Examples:
    execfile( r"C:\example.py" )

    Effects: (see above). Value: (None).
    """
    assert( isinstance( filepath, str )or isinstance( filepath, _pathlib.PurePath ))
    file = open( filepath, encoding=encoding ) # todo: use "with"
    contents = file.read()
    contents = transform( contents )
    compiled = compile( contents, filepath, 'exec' )
    result = exec( compiled, globals )
    return result

def util_include_from( directory, filepath, globals_, transform=lambda text: text, encoding="utf-8" ):
    """Include a given file from a given directory.

    As if "util_execfile( pathlib.Path( directory ).joinpath( filepath ), globals_, transform, encoding )",
    see there.

    """
    util_execfile( _pathlib.Path( directory ).joinpath( filepath ), globals_, transform, encoding )

Newsgroups: de.alt.folklore.computer,de.comp.lang.python

_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.