{"id":927,"date":"2016-01-02T14:56:15","date_gmt":"2016-01-02T14:56:15","guid":{"rendered":"http:\/\/malemuk.com\/olofee\/?p=927"},"modified":"2016-01-02T14:56:15","modified_gmt":"2016-01-02T14:56:15","slug":"python-tips","status":"publish","type":"post","link":"http:\/\/malemuk.com\/olofee\/python-tips\/","title":{"rendered":"Python tips"},"content":{"rendered":"<p>Check also out the excellent documentation at <a href=\"http:\/\/python.org\">python.org<\/a><\/p>\n<p><strong>Environment<\/strong><\/p>\n<p>Reimport a module after modifications:<br \/>\n<code>import imp<br \/>\nimp.reload(mymodule)<br \/>\n<\/code><\/p>\n<p>Append directories to your Python Path:<br \/>\n<code>import sys<br \/>\nsys.path.append(\"home\/mydir\") # UNIX path<br \/>\nsys.path.append(\"D:\\\\data\\\\mydir\") # Windows path<br \/>\n<\/code><\/p>\n<p>In order to run self-coded functions, you need to place them in a module and then import the module. A module is a folder containing a file called __init__.py<\/p>\n<p>If you want to be able to call<br \/>\n<code>from module import *<\/code><br \/>\nthe file must contain a definition of the<br \/>\n<code>__all__<\/code><br \/>\nvariable:<br \/>\n<code>__all__ = [\"func1\" \"func2\"]<\/code><br \/>\nwhere the brackets contain the list of submodules (remember to keep it up-to-date). In the example above, the submodule files are &#8216;func1.py&#8217; and &#8216;func2.py&#8217;.<br \/>\nThe file may also contain code that you&#8217;d like executed each time the module is imported.<\/p>\n<p>Importing a module:<br \/>\n<code>import module<\/code><\/p>\n<p>You may still import submodules if __init__.py is empty:<br \/>\n<code>import module.submodule<\/code><\/p>\n<p><strong>Useful commands<\/strong><\/p>\n<p><code>&gt;&gt;&gt; import os<br \/>\n&gt;&gt;&gt; os.getcwd() # returns current folder path<br \/>\n&gt;&gt;&gt; os.chdir('newdir')<br \/>\n&gt;&gt;&gt; execfile('myfile.py')<br \/>\n<\/code><\/p>\n<p><strong>IDLE shortcuts<\/strong><\/p>\n<p>Alt-P: previous command<br \/>\nAlt-N: next command<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p><code># this is a comment<\/code><\/p>\n<p><strong>List<\/strong><br \/>\n<code>&gt;&gt;&gt; a = ['word', 100, 13]<br \/>\n&gt;&gt;&gt; a[2]<br \/>\n13<br \/>\n&gt;&gt;&gt; len(a)<br \/>\n3<br \/>\n&gt;&gt;&gt; range(5)<br \/>\n[0, 1, 2, 3, 4]<br \/>\n<\/code><\/p>\n<p><strong>Functions<\/strong><br \/>\n<code>def fib(n):<br \/>\n\"\"\"Print a Fibonacci series up to n.\"\"\"<br \/>\na, b = 0, 1<br \/>\nwhile b &lt; n:<br \/>\nprint b,<br \/>\na, b = b, a + b<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check also out the excellent documentation at python.org Environment Reimport a module after modifications: import imp imp.reload(mymodule) Append directories to your Python Path: import sys sys.path.append(&#8220;home\/mydir&#8221;) # UNIX path sys.path.append(&#8220;D:\\\\data\\\\mydir&#8221;) # Windows path In order to run self-coded functions, you need to place them in a module and then import the module. A module is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-computer-stuff"],"jetpack-related-posts":[{"id":753,"url":"http:\/\/malemuk.com\/olofee\/create-bookmarks-in-pdf\/","url_meta":{"origin":927,"position":0},"title":"pdftk tips","date":"November 9, 2015","format":false,"excerpt":"Number pages Source:\u00a0http:\/\/cazencott.info\/index.php\/post\/2015\/04\/30\/Numbering-PDF-Pages Create a PDF that contains only page numbers. In numbers.tex: \\documentclass[12pt,a4paper]{article} \\usepackage{multido} \\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry} \\begin{document} \\multido{}{31}{\\vphantom{x}\\newpage} \\end{document} (where 31 is your total number of pages). Then create numbers.pdf pdflatex numbers.tex Finally, stamp your file with page numbers: pdftk file.pdf multistamp numbers.pdf output single_document_numbered.pdf Create bookmarks Instructions for pdftk\u2026","rel":"nofollow","context":"In \"Computer stuff\"","img":{"src":"","width":0,"height":0},"classes":[]},{"id":910,"url":"http:\/\/malemuk.com\/olofee\/emacs-tips\/","url_meta":{"origin":927,"position":1},"title":"Emacs tips","date":"December 31, 2015","format":false,"excerpt":"Essential shortcuts M-: META key (ALT on most keyboards) C-: CONTROL key C-x C-f : open file C-x b : switch to buffer C-x k : kill buffer C-s : search text forward C-r : search text backward C-+ \/ C-- : Zoom in\/out C-space: mark set (start of the\u2026","rel":"nofollow","context":"In \"Computer stuff\"","img":{"src":"","width":0,"height":0},"classes":[]},{"id":137,"url":"http:\/\/malemuk.com\/olofee\/alpine-configuration\/","url_meta":{"origin":927,"position":2},"title":"Alpine configuration","date":"October 23, 2010","format":false,"excerpt":"Alpine is to email software what Emacs is to text-editing software. I love it. First, create a collection list -- I think you may skip this part if you're using pop instead of imap: S L A Nickname: any nickname Server: my.server.net:port\/ssl\/user=my_username If you're not using ssl, write tls instead.\u2026","rel":"nofollow","context":"In \"Computer stuff\"","img":{"src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/comments?post=927"}],"version-history":[{"count":2,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":934,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/posts\/927\/revisions\/934"}],"wp:attachment":[{"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/malemuk.com\/olofee\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}