[Python-mode] Fwd: Re: Evaluating python code blocks in python-mode

Andreas Röhler andreas.roehler at easy-emacs.de
Sun Jun 21 08:21:56 CEST 2015




-------- Weitergeleitete Nachricht --------
Betreff: 	Re: Evaluating python code blocks in python-mode
Datum: 	Sun, 21 Jun 2015 08:20:46 +0200
Von: 	Andreas Röhler <andreas.roehler at easy-emacs.de>
An: 	help-gnu-emacs at gnu.org



Am 20.06.2015 um 21:09 schrieb Andreas Röhler:
>
> Am 20.06.2015 um 12:50 schrieb Yuri D'Elia:
>> Hi everyone,
>>
>> I'd like some advice about using python-mode with a python subprocess.
>> I always preferred to use emacs+file+external process as opposed to use
>> an ipython's notebook-like interface: it's just more convenient,
>> especially when debugging existing code.
>>
>> I often use the same setup when doing some analysis, and this is when an
>> inotebook-like interface is more convenient for evaluation: I'd like to
>> evaluate code not line-by-line, or by defun, but by my custom-defined
>> blocks. I realized I was continuously selecting a region, and using
>> py-send-region over and over.
>>
>> Right now I narrow to the region I'm editing, and use py-send-buffer
>> instead. It's ok-eish, but narrow/widen narrow/widen, while less common,
>> is still inconvenient.
>>
>> I've seen people using org-mode for this kind of setup instead. Which
>> might be ok, but I'd like to know what other choices I could have as
>> well.
>>
>> I was thinking of defining a custom region using comments, and defining
>> my own py-send-custom-block using comment markers as boundaries. Surely,
>> something like this must already exists.
>>
>>
>
>
> IIUC in question are arbitrary chunks of code.
> What about something like that:
>
> ;;;;
>
> (defvar py-section-start "# {{")
> (defvar py-section-end "# }}")
>
> (defun py-send-section ()
>   (interactive)
>   (save-excursion
>     (unless (looking-at py-section-start)
>       (search-backward py-section-start)
>       (set-mark (point))
>       (if (and (looking-at py-section-start)(search-forward
> py-section-end))
>       (py-execute-region (region-beginning) (region-end))
>     (error "Can't see boundaries of py-section")))))
>
> ;;;;
>
> BTW didn't call it block, as this is already used by forms heading
> others.
>
>
>

As python-mode sends a plain string to interpreter anyway, must not
touch mark in buffer:

(defun py-send-section ()
   (interactive)
   (save-excursion
     (let ((start (progn (unless (looking-at py-section-start)
              (search-backward py-section-start)
              (point)))))
       (if (and (looking-at py-section-start)(search-forward
py-section-end))
       (py-execute-region start (point))
     (error "Can't see boundaries of py-section")))))



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-mode/attachments/20150621/4dff6e5a/attachment.html>


More information about the Python-mode mailing list