python Programming Glossary: here's
How to generate all permutations of a list in Python http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python listed in the documentation of itertools.permutations . Here's one def permutations iterable r None # permutations 'ABCD' 2..
subprocess with timeout http://stackoverflow.com/questions/1191374/subprocess-with-timeout with timeout Here's the Python code to run an arbitrary command returning its stdout..
Build a Basic Python Iterator http://stackoverflow.com/questions/19151/build-a-basic-python-iterator captured by looping constructs to stop iterating. Here's a simple example of a counter class Counter def __init__ self..
Import a module from a relative path http://stackoverflow.com/questions/279237/import-a-module-from-a-relative-path dirBar contains Bar.py how do I import Bar.py into Foo.py Here's a visual representation dirFoo Foo.py dirBar Bar.py Foo wishes..
Python “is” operator behaves unexpectedly with integers http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers id b 9987148 a 257 b 257 id a 11662816 id b 11662828 EDIT Here's what I found in the Python documentation 7.2.1 Plain Integer..
How do you split a list into evenly sized chunks in Python? http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python chunks python list chunks share improve this question Here's a generator that yields the chunks you want def chunks l n Yield..
Suggestions for a Cron like scheduler in Python? http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler-in-python daylight savings time timezones to be wary of though . Here's a quick implementation class CronTab object def __init__ self..
Python/Tkinter: Interactively validating Entry widget content http://stackoverflow.com/questions/4140437/python-tkinter-interactively-validating-entry-widget-content to get this information passed to your validate command. Here's an example that only allows lowercase and prints all those funky..
How to improve performance of this code? http://stackoverflow.com/questions/4295799/how-to-improve-performance-of-this-code 'F' 'G' 'F' 'F' real 0m20.883s user 0m20.549s sys 0m0.020s Here's the code import Queue fCamel 'F' bCamel 'B' gap 'G' def solution..
Does python have an equivalent to Java Class.forName()? http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname we can help you find a more pythonic way of doing it. Here's a function that does what you want def get_class kls parts kls.split.. value of this function as if it were the class itself. Here's a usage example D get_class datetime.datetime D type 'datetime.datetime'..
Python read a single character from the user http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user Thanks. python input share improve this question Here's a link to a site that says how you can read a single character..
Simple Prime Generator in Python http://stackoverflow.com/questions/567222/simple-prime-generator-in-python loop iteration but you really want to stop it using break Here's your code with a few fixes it prints out only primes import.. see the Sieve of Erastothenes as others have suggested. Here's a nice optimized implementation with many comments # Sieve of..
How do I duplicate sys.stdout to a log file in python? http://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python simply reversing the dup2's should work. Why doesn't it Here's my test import os sys ### my broken solution so se open a.log..
Python's use of __new__ and __init__? http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init implementation into the __new__ which feels quite hacky . Here's an example class A object _dict dict def __new__ cls if 'key'..
Python HTML sanitizer / scrubber / filter http://stackoverflow.com/questions/699468/python-html-sanitizer-scrubber-filter html filter sanitizer share improve this question Here's a simple solution using BeautifulSoup from BeautifulSoup import..
How do I access the child classes of an object in django without knowing the name of the child class? http://stackoverflow.com/questions/929029/how-do-i-access-the-child-classes-of-an-object-in-django-without-knowing-the-nam the instance depending how large your inheritance tree is. Here's how I did it in one project from django.contrib.contenttypes.models..
Why is reading lines from stdin much slower in C++ than Python? http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python to read chunks of 16k bytes at a time and count new lines. Here's a python equivalent based on J.F.'s code just showing the relevant.. C code. Again this is for a 100M line file on a fast disk. Here's the complete table now Implementation Lines per second cin default..
How to generate all permutations of a list in Python http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python for some reason or are just curios to know how it works here's one nice approach taken from http code.activestate.com recipes..
Python hashable dicts http://stackoverflow.com/questions/1151658/python-hashable-dicts as keys to other dict s Having hacked a bit on the answers here's the more complete solution I'm using. Note that this does a..
How can I parse JSON in Google App Engine? http://stackoverflow.com/questions/1171584/how-can-i-parse-json-in-google-app-engine The link above has examples of Django's serializer and here's the link for simplejson's documentation . If you're looking..
Python @classmethod and @staticmethod for beginner? http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner Though classmethod and staticmethod are quite similar there's a slight difference in usage for both entities classmethod must.. feature as overloading but Python lacks that feature so here's when classmethod applies. Lets create another constructor ...
Python string 'join' is faster(?) than '+', but what's wrong here? http://stackoverflow.com/questions/1349311/python-string-join-is-faster-than-but-whats-wrong-here and your big O's never worse than N So for the rest of us here's a more meaningful and interesting set of measurements python..
Should you always favor xrange() over range()? http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range range will be upgraded by the 2to3 tool. For the record here's the output of running the tool on some sample usages of range..
`xrange(2**100)` -> OverflowError: long int too large to convert to int http://stackoverflow.com/questions/1482480/xrange2100-overflowerror-long-int-too-large-to-convert-to-int biginteger xrange share improve this question Okay here's a go at a fuller reimplementation. class MyXRange object def..
What determines whether different Python processes are assigned to the same or different cores? http://stackoverflow.com/questions/15639779/what-determines-whether-different-python-processes-are-assigned-to-the-same-or-d dd for dd in data if __name__ '__main__' run ...and here's what I see in htop while this script is running I'm running..
Google Search from a Python App http://stackoverflow.com/questions/1657570/google-search-from-a-python-app api google search api share improve this question There's a simple example here peculiarly missing some quotes . Most.. supported AJAX API that's definitely the one you want Edit here's a more complete Python 2.6 example with all the needed quotes..
Python: removing duplicates from a list of lists http://stackoverflow.com/questions/2213923/python-removing-duplicates-from-a-list-of-lists it's easier to use it at a shell prompt. For example here's a short module to showcase the general approach for this problem..
Print in terminal with colors using Python? http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python is by printing ANSI escape sequences. For a simple example here's some python code from the blender build scripts class bcolors..
Django equivalent for count and group by http://stackoverflow.com/questions/327807/django-equivalent-for-count-and-group-by and doesn't have a friendly API on top of it yet. But here's how you can use it anyway until 1.1 arrives at your own risk..
Assign output of os.system to a variable and prevent it from being displayed on the screen http://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on told that subprocess is a much better way to solve this so here's the corresponding code import subprocess proc subprocess.Popen..
Does Python have a built in function for string natural sort? http://stackoverflow.com/questions/4836710/does-python-have-a-built-in-function-for-string-natural-sort 'elm1' 'Elm2' 'elm9' 'elm10' 'Elm11' 'Elm12' 'elm13' And here's the sorted version of the above list what I have 'Elm11' 'Elm12'..
Using strides for an efficient moving average filter http://stackoverflow.com/questions/4936620/using-strides-for-an-efficient-moving-average-filter numpy share improve this question For what it's worth here's how you'd do it using fancy striding tricks. I was going to.. functions. Such as scipy.ndimage etc At any rate here's how you do it import numpy as np def rolling_window_lastaxis..
Python 2.x gotcha's and landmines http://stackoverflow.com/questions/530530/python-2-x-gotchas-and-landmines outside of Stack Overflow. As some sort of compromise here's the URL http www.ferg.org projects python_gotchas.html Note..
How to dynamically load a Python class http://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class improve this question From the python documentation here's the function you want def my_import name mod __import__ name..
How to get console window width in python http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python EDIT oh I'm sorry. That's not a python standard lib one here's the source of console.py I don't know where it's from . The..
Detect & Record Audio in Python http://stackoverflow.com/questions/892199/detect-record-audio-in-python this question As a follow up to Nick Fortescue's answer here's a more complete example of how to record from the microphone..
|