Popular recipes tagged "python" but not "tkinter" and "xtopdf"http://code.activestate.com/recipes/tags/python-tkinter-xtopdf/2017-05-06T20:53:54-07:00ActiveState Code RecipesThe Game of Tic Tac Toe in Python (Python) 2014-01-31T02:39:48-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578816-the-game-of-tic-tac-toe-in-python/ <p style="color: grey"> Python recipe 578816 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>I thought this is a fun game to program. Easy to program and can teach a lot. </p> <p><a href="http://thelivingpearl.com">Captain DeadBones</a></p> Implementing function-based callbacks in Python (Python) 2017-04-19T18:03:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580787 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/techniques/">techniques</a>). </p> <p>This recipe shows a simple way of implementing callbacks in Python. There are a few ways this can be done. The way shown here uses a simple function-based approach.</p> <p>In a nutshell, a callback can be informally described like this: function <strong>a</strong> calls function <strong>b</strong>, and wants to make <strong>b</strong> run a specific independent chunk of code at some point during <strong>b</strong>'s execution. We want to be able to vary which chunk of code gets called in different calls to <strong>b</strong>, so it cannot be hard-coded inside <strong>b</strong>. So function <strong>a</strong> passes another function, <strong>c</strong>, to <strong>b</strong>, as one argument, and <strong>b</strong> uses that parameter <strong>c</strong> to call the functionality that <strong>a</strong> wants <strong>b</strong> to call. (Function <strong>b</strong> may pass some parameters to the function represented by <strong>c</strong>, when it calls it. These could be either internally generated, passed from <strong>a</strong>, or a combination of both). So, by changing the value of the function <strong>c</strong> that gets passed to <strong>b</strong> (on different calls to <strong>b</strong>), <strong>a</strong> can change what chunk of code <strong>b</strong> calls.</p> <p>More details and full code, description and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html</a></p> Implementing class-based callbacks in Python (Python) 2017-04-20T23:34:50-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580788-implementing-class-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580788 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callbacks/">callbacks</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a follow-on to this recently posted recipe:</p> <p>Implementing function-based callbacks in Python: <a href="https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/?in=user-4173351</a></p> <p>This new recipe shows how to create and use callbacks in Python, using classes with methods, instead of plain functions, as was done in the recipe linked above. All other points such as reasons and benefits for using callbacks, are more or less the same as mentioned in the previous recipe, except that class instances can carry state around, so to that extent, the two approaches are different.</p> <p><a href="https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/python-callbacks-using-classes-and.html</a></p> Simulating an unless (reverse if) statement in Python (Python) 2017-02-23T22:38:50-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580758-simulating-an-unless-reverse-if-statement-in-pytho/ <p style="color: grey"> Python recipe 580758 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/features/">features</a>, <a href="/recipes/tags/if/">if</a>, <a href="/recipes/tags/perl/">perl</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/trick/">trick</a>). </p> <p>This recipe shows how to simulate an unless statement (a sort of reverse if, like Perl has), in Python. It is just for fun and as an experiment, not meant for real use, because the effect of unless can easily be got by negating the sense of the condition in an if statement.</p> <p>More details and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/02/perl-like-unless-reverse-if-feature-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/02/perl-like-unless-reverse-if-feature-in.html</a></p> Unix tee-like functionality via a Python class (Python) 2017-03-31T14:30:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580767-unix-tee-like-functionality-via-a-python-class/ <p style="color: grey"> Python recipe 580767 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tee/">tee</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>The Unix tee commmand, when used in a command pipeline, allows you to capture the output of the preceding command to a file or files, while still sending it on to standard output (stdout) for further processing via other commands in a pipeline, or to print it, etc.</p> <p>This recipe shows how to implement simple tee-like functionality via a Python class. I do not aim to exactly replicate the functionality of the Unix tee, only something similar.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html</a></p> A simple text file pager in Python (Python) 2017-02-10T21:34:45-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580755-a-simple-text-file-pager-in-python/ <p style="color: grey"> Python recipe 580755 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/pagination/">pagination</a>, <a href="/recipes/tags/paging/">paging</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to create a simple text file pager in Python. It allows you to view text content a page at a time (with a user-definable number of lines per page). Like standard Unix utilities, it can either take a text file name as a command-line argument, or can read the text from its standard input, which can be redirected to come from a file, or to come from a pipe. The recipe is for Windows only, though, since it uses the msvcrt.getch() function, which is Windows-specific. However, the recipe can be modified to work on Unix by using things like tty, curses, termios, cbreak, etc.</p> <p>More details here:</p> <p><a href="https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html" rel="nofollow">https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html</a></p> Get disk partition information with psutil (cross-platform) (Python) 2016-12-23T18:05:41-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580737-get-disk-partition-information-with-psutil-cross-p/ <p style="color: grey"> Python recipe 580737 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/device/">device</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/file_system/">file_system</a>, <a href="/recipes/tags/psutil/">psutil</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/system/">system</a>). </p> <p>This is a recipe that shows how to easily get disk partition information, in a cross-platform manner (for the supported OSes), from your computer's operating system, using the psutil library for Python.</p> Merge unique items from multiple lists into a new list (Python) 2016-04-05T18:11:49-07:00Johannes Shttp://code.activestate.com/recipes/users/4193888/http://code.activestate.com/recipes/580634-merge-unique-items-from-multiple-lists-into-a-new-/ <p style="color: grey"> Python recipe 580634 by <a href="/recipes/users/4193888/">Johannes S</a> (<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/set/">set</a>). Revision 2. </p> <p>Suppose you have multiple lists. You want to print all unique items from the list. Now, what you could do is merge the lists into one_big_list (e.g., a + b +c), and then iterate over each item in one_big_list, etc. The solution proposed here gets this done faster and in one line of code. How? By using <strong>a python set</strong>. A python set is a dictionary that contains only keys (and no values). And dictionary keys are, by definition, unique. Hence, duplicate items are weeded out automatically. Once you have the set, you can easily convert it back into a list. As easy as that!</p> TicTacToe (text based) (Python) 2016-11-27T07:10:56-08:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580723-tictactoe-text-based/ <p style="color: grey"> Python recipe 580723 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/tac/">tac</a>, <a href="/recipes/tags/tic/">tic</a>, <a href="/recipes/tags/tictactoe/">tictactoe</a>, <a href="/recipes/tags/toe/">toe</a>). </p> <p>A text based version of TicTacToe for Python. Difficulty is not yet implemented, however, a computer player has been implemented that cannot be beaten. Posting just for fun.</p> Python-C-Interface: Check whether a given dictionary contains only valid keys (C) 2017-05-06T20:53:54-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580799-python-c-interface-check-whether-a-given-dictionar/ <p style="color: grey"> C recipe 580799 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/c_interface/">c_interface</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This function is given a Python dictioanry and a list of string / unicode values. It will check whether all dictionary keys occur in this list and will return 1 (true) or 0 (false).</p> Show OS error codes and messages from the os.errno module (Python) 2017-03-01T17:18:23-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580759-show-os-error-codes-and-messages-from-the-oserrno-/ <p style="color: grey"> Python recipe 580759 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/libraries/">libraries</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe is a simple Python introspection utility that displays the defined OS error codes and messages (that Python knows about) from the os.errno module. It works for both Python 2 and Python 3. For each kind of OS error defined in Python, it will display a serial number, the error code, and the corresponding error name, and English error message. E.g. the first few lines of its output are shown below:</p> <p>$ py -2 os_errno_info.py</p> <p>Showing error codes and names</p> <p>from the os.errno module:</p> <p>Python sys.version: 2.7.12</p> <p>Number of error codes: 86</p> <p>Idx Code Name Message</p> <p>0 1 EPERM Operation not permitted</p> <p>1 2 ENOENT No such file or directory</p> <p>2 3 ESRCH No such process</p> <p>3 4 EINTR Interrupted function call</p> <p>4 5 EIO Input/output error</p> <p>More information, full output and other details are available here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html</a></p> Find the arity of a Python function (Python) 2017-01-30T14:09:47-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580753-find-the-arity-of-a-python-function/ <p style="color: grey"> Python recipe 580753 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows how to find the arity of a given Python function. The arity of a function is the number of arguments the function takes. The recipe uses the inspect module of Python.</p> <p>More details and sample output (including some limitations) here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/finding-arity-of-python-function.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/finding-arity-of-python-function.html</a></p> Classifying letters as vowels or consonants and counting their frequencies (Python) 2017-01-17T20:05:10-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580749-classifying-letters-as-vowels-or-consonants-and-co/ <p style="color: grey"> Python recipe 580749 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/comprehension/">comprehension</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>, <a href="/recipes/tags/dict_comp/">dict_comp</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/tuple/">tuple</a>, <a href="/recipes/tags/unpack/">unpack</a>). </p> <p>This recipe shows how to take a string as input and classify the characters in it as vowels, consonants or neither. The frequency of each vowel is calculated and the frequency of all the consonants in total is calculated. The program logic is fairly simple, and uses a dictionary comprehension and a dict; the more interesting thing about it, is that it illustrates 8 Python language features in under 35 lines of code.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html</a></p> A command-line musical alarm clock (Python) 2016-12-30T19:06:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580739-a-command-line-musical-alarm-clock/ <p style="color: grey"> Python recipe 580739 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/multimedia/">multimedia</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/playsound/">playsound</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This is a simple musical alarm clock in Python. You pass a command-line argument specifying the time in minutes after which the alarm should go off. When that time arrives, it plays a musical sound.</p> Give Python code a web plus command-line interface with hug (Python) 2017-01-05T16:57:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580742-give-python-code-a-web-plus-command-line-interface/ <p style="color: grey"> Python recipe 580742 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/hug/">hug</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/user_interface/">user_interface</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/web_server/">web_server</a>). </p> <p>This recipe shows how to take a Python function and wrap it with both a web and a command-line interface, somewhat easily, using the hug Python library. The example used shows how to wrap a function that uses the psutil library to get information on disk partitions. So you can see the disk partition info either via the web browser or the command line. The code for the recipe is shown below. It is also possible to wrap multiple functions in the same Python file, and expose all of them via both the web and the command-line.</p> <p>More information and multiple sample outputs are available here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html</a></p> Send autohotkey commands with Python (Python) 2017-01-02T01:33:31-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580740-send-autohotkey-commands-with-python/ <p style="color: grey"> Python recipe 580740 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/autohotkey/">autohotkey</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>I use an asterisk (*) as a command line argument to read the autohotkey script from standard input (stdin). <a href="https://autohotkey.com/docs/Scripts.htm" rel="nofollow">https://autohotkey.com/docs/Scripts.htm</a></p> Simple directory list command with filename wildcard support (Python) 2016-12-02T20:52:56-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580724-simple-directory-list-command-with-filename-wildca/ <p style="color: grey"> Python recipe 580724 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/filesystem/">filesystem</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utilities/">utilities</a>). </p> <p>This recipe shows a simple directory listing program. It can accept multiple command-line arguments specifying filenames. These filenames can include wildcard characters like * (asterisk) and ? (question mark), as is common in OS command shells like bash (Unix) and CMD (Windows). Tested on Windows but should work on Unix too, since it uses no OS-specific functions, or rather, it does use them, but that happens under the hood, within the libraries used.</p> Simulate C's switch statement (Python) 2016-12-11T16:28:50-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580730-simulate-cs-switch-statement/ <p style="color: grey"> Python recipe 580730 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/c/">c</a>, <a href="/recipes/tags/features/">features</a>, <a href="/recipes/tags/language/">language</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/switch/">switch</a>). </p> <p>This recipe shows a Python construct that can behave somewhat like C's switch statement. It is not a perfect one-to-one simulation. But it does have some of the features of the C switch. One feature not supported is the fall-through feature in C's switch.</p> Process Delimiter-Separated Values data with Python (Python) 2016-11-24T23:57:35-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580722-process-delimiter-separated-values-data-with-pytho/ <p style="color: grey"> Python recipe 580722 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/dsv/">dsv</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This recipe shows how to read and process delimiter-separated values (DSV) data with a Python command-line program. It provides two ways of specifying the delimiter character, by an ASCII character or an ASCII code, which makes it more flexible than allowing only a character. It allows the DSV data to be specified as one or more filenames on the command line, or given via the standard input of the program.</p> Publish a Windows Process List to PDF with xtopdf (Batch) 2015-12-27T20:45:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579142-publish-a-windows-process-list-to-pdf-with-xtopdf/ <p style="color: grey"> Batch recipe 579142 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/process_management/">process_management</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how you can generate a Windows process list or task list (basically, a list of running processes, with some information about each of them), to a PDF file, using the Windows TASKLIST command along with the xtopdf toolkit. The list is sorted in ascending order of memory usage of the processes, before writing it to PDF.</p> <p>It differs somewhat from other xtopdf recipes, in that no additional code needs to be written, over and above what is already in the xtopdf package. We just have to use the needed commands there, in a series of commands or a pipeline.</p> <p>However, one can still write additional code, by modifying the program used (StdinToPDF.py), if needed, to customize the PDF output.</p>