Not lost but found - Pythonhttp://blog.find-method.de/
Holger's findings and opinionsenSerendipity 2.1.1 - http://www.s9y.org/http://blog.find-method.de/templates/default/img/s9y_banner_small.pngRSS: Not lost but found - Python - Holger's findings and opinionshttp://blog.find-method.de/
10021Refactoring (the name of) a python packagehttp://blog.find-method.de/index.php?/archives/192-Refactoring-the-name-of-a-python-package.html
Pythonhttp://blog.find-method.de/index.php?/archives/192-Refactoring-the-name-of-a-python-package.html#commentshttp://blog.find-method.de/wfwcomment.php?cid=1921http://blog.find-method.de/rss.php?version=2.0&type=comments&cid=192nospam@example.com (Holger Schauer)
<p>I had the need to refactor a python package, better said, I had to rename the package. The package in question is using namespaces, which complicates the matter, as this implies that there are two steps to the process: you have to exchange all references to the module name and you have to re-arrange the package structure. Assuming you&#8217;re at the toplevel of your (otherwise clean) package directory, the following shows the manual steps involved in the process.</p>
<p>First we rearrange the directories:</p>
<a class="block_level" href="http://blog.find-method.de/index.php?/archives/192-Refactoring-the-name-of-a-python-package.html#extended">Continue reading "Refactoring (the name of) a python package"</a>
Wed, 26 Dec 2012 10:38:00 +0100http://blog.find-method.de/index.php?/archives/192-guid.htmlpythonshell programminghttp://creativecommons.org/licenses/by-nc/3.0/python-mode vs. slimehttp://blog.find-method.de/index.php?/archives/76-python-mode-vs.-slime.html
EmacsPythonhttp://blog.find-method.de/index.php?/archives/76-python-mode-vs.-slime.html#commentshttp://blog.find-method.de/wfwcomment.php?cid=760http://blog.find-method.de/rss.php?version=2.0&type=comments&cid=76nospam@example.com (Holger Schauer)
<p>I&#8217;ve become a python programmer, too, lately, due to a job change. Python is a fine language so far, although to me it&#8217;s mostly just like Ruby, though with even less functional flavour. However, just as with Ruby, I&#8217;m really missing <a href="http://blog.find-method.de/exit.php?url_id=173&amp;entry_id=76" title="http://common-lisp.net/project/slime" onmouseover="window.status='http://common-lisp.net/project/slime';return true;" onmouseout="window.status='';return true;">slime</a>, the superior lisp interaction mode for Emacs, when hacking python code. I could now start to write down a list of things I&#8217;m missing (which I&#8217;ve intended to do), however, Andy Wingo spares me the hassle, as he has just written an <a href="http://blog.find-method.de/exit.php?url_id=174&amp;entry_id=76" title="http://wingolog.org/archives/2006/01/02/slime" onmouseover="window.status='http://wingolog.org/archives/2006/01/02/slime';return true;" onmouseout="window.status='';return true;">excellent article on slime from a python programmers view</a>.</p>
<p>However, I would like to elaborate a little on the main difference for me: the client/server socket approach of slime. Let me briefly recapulate what this implies: slime consists of two parts, a client written in Emacs lisp and a server written in Common Lisp (AFAIK there is at least also an implementation for clojure, maybe also one for some scheme implementation). In order to use slime in it&#8217;s full glory, it&#8217;s hence required that you have a common lisp process running which in turn runs the slime server part. If you now fire up slime, you&#8217;ll get an interaction buffer over which you can access the REPL of the lisp process, which in python would be the interpreter prompt. You can then interact with the lisp process, evaluating pieces of code from your lisp source code buffer directly in the connected lisp process. What is incredibly useful for me is that you can not only start a new lisp process but also connect to an already running lisp process, given that it has the slime server started (this is obviously mainly useful if the lisp implementation you use has multi-threading capabilities). I use it to connect to a running web server application, which I can then inspect, debug and modify. Modification includes redefinition of functions, macros and classes, which of course is also a particular highlight of Common Lisp. I would like to cite a comment of the reddit user &#8220;fionbio&#8221; he made wrt. to the linked article: <cite>In fact, Python language wasn&#8217;t designed with lisp-style interactive development in mind. In CL, you can redefine (nearly) anything you want in a running program, and it just does the right thing. In Python, there are some problems, e.g. instances aren&#8217;t updated if you modify their class. Lisp programmers often, though not always, refer to various things (functions, classes, macros, etc.) using symbols, while Python programs usually operate with direct references, so when you update parts of your program you have much higher chances that there will be a lot of references to obsolete stuff around.</cite></p>
<p>To complement <a href="http://blog.find-method.de/exit.php?url_id=175&amp;entry_id=76" title="http://bc.tech.coop/blog/081209.html" onmouseover="window.status='http://bc.tech.coop/blog/081209.html';return true;" onmouseout="window.status='';return true;">Bill clementsons excellent article series on slime</a> a little, I&#8217;m going to describe how I&#8217;m using/configuring python-mode to make it match my expectations a little closer. Essentially I would like to access my python process just as I would with slime/Common Lisp, but that&#8217;s not possible. The reason, btw., is nearly unchanged: I need to code on a web server app (written in Zope) which may not even run on the same machine I&#8217;m developing on. Let&#8217;s first cover the simple stuff: To enable a reasonable command interface to the python interpreter, I require the ipython emacs library. If the python interpreter runs locally, I also use py-complete, so that I can complete my code at least a little. Unfortunately, this breaks when the python interpreter doesn&#8217;t run locally, because the py-complete needs to setup some things in the running python process, which it does by writing to a local temp file and feeding it to the python process. Unfortunately, the code in py-complete lacks customizability, i.e., you can&#8217;t specify where that temp file should be located &#8212; I should be able to come up with a small patch in the near future, which I will add below. Finally, I also require doctest-mode as a support for writing doctests, but that&#8217;s not really relevant. </p>
<p>Now, on to the more involved stuff: I introduce some new variables and a new function <code>py-set-remote-python-environment</code>, which uses the those variables to do a remote call (via ssh) to python. This at least allows me to do things like setting py-remote-python-command to &#8220;/home/schauer/zope/foo-project/bin/instance&#8221; and py-remote-python-command-args to &#8220;debug&#8221;, so that I can access a remote debug shell of my current zope product. That alone will only allow me to fire up and access the remote python, so I could now develop the code locally, having it executed remote. More typical though is that you would also want to keep the code on the remote machine, too: for this I use <a href="http://blog.find-method.de/exit.php?url_id=176&amp;entry_id=76" onmouseover="window.status='http://www.gnu.org/software/tramp/';return true;" onmouseout="window.status='';return true;" title="http://www.gnu.org/software/tramp/">tramp</a>, a package for remotely accessing files/directories from within emacs. In combination, this allows me to edit and execute the code on the remote machine. It is still nowhere near what is possible with slime, but at least it allows me to persue my habit of incremental and interactive development from within my usual emacs installation (i.e., it doesn&#8217;t require me to deal with any Emacs related hassle on the remote machine). </p>
<pre><code>
;;; python-stuff.el --- python specific configuration
(when (locate-library "ipython")
(require 'ipython))
(when (locate-library "doctest-mode")
(require 'doctest-mode))
(defvar py-remote-connect-command "ssh""*Command for connecting to a remote python, typically \"ssh\"")
(defvar py-remote-connection-args '("user@remotemachine")"*List of strings of connection options.")
(defvar py-remote-python-command "python""*Command to execute for python")
(defvar py-remote-python-command-args '("-i")"*List of strings arguments to be passed to `py-remote-python-command`.")
(defvar py-remote-python-used nil"Remember if remote python is used.")
(defun py-set-remote-python-environment ()
(interactive)
(let ((command-args (append py-remote-connection-args
(list py-remote-python-command)
py-remote-python-command-args)))
(setq py-python-command py-remote-connect-command)
(setq py-python-command-args command-args))
(setq py-remote-python-used t))
(when (locate-library "py-complete")
(autoload 'py-complete-init "py-complete")
(defun my-py-complete-init ()"Init py-complete only if we're not using remote python"
(if (not py-remote-python-used)
(py-complete-init)))
(add-hook 'python-mode-hook 'my-py-complete-init))
</code></pre>
Wed, 10 Dec 2008 10:52:44 +0100http://blog.find-method.de/index.php?/archives/76-guid.htmlemacspythonhttp://creativecommons.org/licenses/by-nc/3.0/