Popular recipes tagged "meta:requires=__future__" but not "xtopdf"http://code.activestate.com/recipes/tags/meta:requires=__future__-xtopdf/2017-04-27T21:26:00-07:00ActiveState Code RecipesImplementing 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> 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> Classifying characters using nested conditional expressions (Python) 2017-04-27T21:26:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580792-classifying-characters-using-nested-conditional-ex/ <p style="color: grey"> Python recipe 580792 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/characters/">characters</a>, <a href="/recipes/tags/classification/">classification</a>, <a href="/recipes/tags/conditional_expressions/">conditional_expressions</a>, <a href="/recipes/tags/expressions/">expressions</a>, <a href="/recipes/tags/join/">join</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/map/">map</a>). </p> <p>Python has a feature called conditional expressions, similar to C's ternary operator. For example:</p> <p>print n, 'is odd' if n % 2 == 1 else 'is even'</p> <p>Here, the conditional expression is this part of the print statement above:</p> <p>'is odd' if n % 2 == 1 else 'is even'</p> <p>This expression evaluates to 'is odd' if the condition after the if keyword is True, and evaluates to 'is even' otherwise.</p> <p>The Python Language Reference section for conditional expressions shows that they can be nested. This recipe shows that we can use nested conditional expressions (within a return statement in a user-defined function) to classify characters into lowercase letters, uppercase letters, or neither.</p> <p>It also shows how to do the same task using map, lambda and string.join, again with a nested conditional expression, but without using return or a user-defined function.</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> Easily create a Python REPL in Python (Python) 2016-10-31T21:53:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580712-easily-create-a-python-repl-in-python/ <p style="color: grey"> Python recipe 580712 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/code_module/">code_module</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/evaluation/">evaluation</a>, <a href="/recipes/tags/read_eval_print_loop/">read_eval_print_loop</a>, <a href="/recipes/tags/repl/">repl</a>). </p> <p>This recipe shows how to easily create a Python REPL (Read-Eval-Print Loop) in Python itself. This can allow the user to interact with a running Python program, including typing in Python statements at the REPL prompt, defining functions, using and changing variables that were set before the interaction started, and those variables modified during the interaction, will persist in the memory of the program, for any use, even after the interaction is over, as long as the program continues to run.</p> Quick-and-dirty Windows drive detector (Python) 2016-09-20T17:46:37-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580699-quick-and-dirty-windows-drive-detector/ <p style="color: grey"> Python recipe 580699 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/drives/">drives</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/system_programming/">system_programming</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This is a quick-and-dirty Python script to detect the currently available drives on your Windows PC.</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> Trap KeyboardInterrupt and EOFError for graceful program termination (Python) 2016-11-13T20:17:03-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580718-trap-keyboardinterrupt-and-eoferror-for-graceful-p/ <p style="color: grey"> Python recipe 580718 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/terminate/">terminate</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This recipe shows how to trap the KeyboardInterrupt and EOFError Python exceptions so that they do not crash your program. As a vehicle to show this, it uses a small Python utility that shows the ASCII code for any ASCII character you type.</p> Get names and types of all attributes of a Python module (Python) 2016-10-06T17:21:42-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580705-get-names-and-types-of-all-attributes-of-a-python-/ <p style="color: grey"> Python recipe 580705 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/documentation/">documentation</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/reflection/">reflection</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to get the names and types of all the attributes of a Python module. This can be useful when exploring new modules (either built-in or third-party), because attributes are mostly a) data elements or b) functions or methods, and for either of those, you would like to know the type of the attribute, so that, if it is a data element, you can print it, and if it is a function or method, you can print its docstring to get brief help on its arguments, processsing and outputs or return values, as a way of learning how to use it.</p> <p>The code for the recipe includes an example call to it, at the end of the code. Note that you first have to import the modules that you want to introspect in this way.</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> The many uses of randomness - Part 2 (Python) 2016-07-17T17:26:47-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580690-the-many-uses-of-randomness-part-2/ <p style="color: grey"> Python recipe 580690 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>This is the second recipe in the series about the uses of randomness in Python. The first recipe is here:</p> <p><a href="https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351</a></p> <p>This second recipe shows some uses of random numbers to generate random characters and random strings of various categories, and some purposes for which these generated strings can be used in testing.</p> Finding the sizes of various Python data types (Python) 2016-04-28T18:28:59-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580655-finding-the-sizes-of-various-python-data-types/ <p style="color: grey"> Python recipe 580655 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to find the sizes of various common data types in Python, both built-in and user-defined. It uses the sys.getsizeof() function and also discusses a few other points of interest.</p> Using ctypes to call C code from Python (Python) 2016-05-09T18:00:44-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580660-using-ctypes-to-call-c-code-from-python/ <p style="color: grey"> Python recipe 580660 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This recipe shows basic usage of the ctypes module to call C code from Python code.</p> The many uses of randomness - Part 1 (Python) 2016-06-01T19:36:28-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/ <p style="color: grey"> Python recipe 580674 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>). </p> <p>This recipe shows some of the many uses of random numbers, using the random function from the random module from Python's standard library. A subsequent recipe or two will show other uses, both of other functions from the module, and for other purposes.</p> <p>The uses shown in this recipe have to do with using random float values, and scaling them and offsetting them, and also how to get a repeated/predictable series of random numbers.</p> data_dump.py, like the Unix od (octal dump) command (Python) 2015-11-01T12:43:38-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579120-data_dumppy-like-the-unix-od-octal-dump-command/ <p style="color: grey"> Python recipe 579120 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/dump/">dump</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/octal/">octal</a>, <a href="/recipes/tags/od/">od</a>, <a href="/recipes/tags/representation/">representation</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe implements a simple data dump tool, roughly like the od command of Unix, which stands for octal dump (though od can also dump data in hex and other formats). This tool dumps data in character and hex formats, in this version. This is data_dump.py version 1.</p> Improved Signals/Slots implementation in Python (Python) 2011-12-12T22:47:25-08:00Christopher S. Casehttp://code.activestate.com/recipes/users/4180238/http://code.activestate.com/recipes/577980-improved-signalsslots-implementation-in-python/ <p style="color: grey"> Python recipe 577980 by <a href="/recipes/users/4180238/">Christopher S. Case</a> (<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/qt4/">qt4</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/slot/">slot</a>). </p> <p>I've modified the excellent <a href="http://code.activestate.com/recipes/576477-yet-another-signalslot-implementation-in-python/"><a href="http://code.activestate.com/recipes/576477/">recipe 576477</a></a> to allow for non method functions as well as method functions. This implementation also uses a WeakKeyDictionary instead of a WeakValueDictionary for reasons of code simplification/style.</p> CLOS-like around/before/after auxiliary methods (Python) 2011-08-25T22:59:22-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577859-clos-like-aroundbeforeafter-auxiliary-methods/ <p style="color: grey"> Python recipe 577859 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/auxiliary/">auxiliary</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/clos/">clos</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/super/">super</a>). </p> <p>This module provides an easy way to define and use your own <strong>around/before/after auxiliary methods</strong>, similar to <a href="http://www.aiai.ed.ac.uk/~jeff/clos-guide.html#meth-comb">those used in CLOS</a> (Common Lisp Object System).</p>