python Programming Glossary: or
What is a metaclass in Python? http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python to me. So what are metaclasses What do you use them for Concrete examples including snippets much appreciated python.. share improve this question Classes as objects Before understanding metaclasses you need to master classes in Python... And Python has a very peculiar idea of what classes are borrowed from the Smalltalk language. In most languages classes..
Fastest way to list all primes below N in python http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python to list all primes below N in python This is the best algorithm I could come up with after struggling with a couple of Project.. stmt 'get_primes.get_primes 1000000 ' setup 'import get_primes' .timeit 1 1.1499958793645562 Can it be made even.. faster EDIT This code has a flaw Since numbers is an unordered set there is no guarantee that numbers.pop will remove..
The Python yield keyword explained http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained Python yield keyword explained What is the use of the yield keyword in Python What.. yield keyword explained What is the use of the yield keyword in Python What does it do For example I'm trying to understand.. is the use of the yield keyword in Python What does it do For example I'm trying to understand this code def node._get_child_candidates..
How do I protect Python code? http://stackoverflow.com/questions/261638/how-do-i-protect-python-code restricted license file. If we distribute the .py files or even .pyc files it will be easy to decompile and remove the.. read by our customers fearing that the code may be stolen or at least the novel ideas . Is there a good way to handle this.. in cases like this you have to make a tradeoff. How important is it really to protect the code Are there real secrets..
Python 'self' explained http://stackoverflow.com/questions/2709821/python-self-explained 'self' explained What is the purpose of the self word in Python I understand it refers to the specific object created.. Python could have done something else to distinguish normal names from attributes special syntax like Ruby has or requiring.. normal names from attributes special syntax like Ruby has or requiring declarations like C and Java do or perhaps something..
Python “is” operator behaves unexpectedly with integers http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers &ldquo is&rdquo operator behaves unexpectedly with integers Why does the following behave.. is internally implemented such that small integers are stored in a different way than larger integers and the is operator.. in a different way than larger integers and the is operator can tell the difference. Why the leaky abstraction What is a..
Flattening a shallow list in Python http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python list with a nested list comprehension like this image for image in menuitem.image_set.all for menuitem in list_of_menuitems.. like this image for image in menuitem.image_set.all for menuitem in list_of_menuitems But I get in trouble of the NameError.. in list_of_menuitems But I get in trouble of the NameError variety there because the name 'menuitem' is not defined . After..
How to install pip on windows? http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows pip on windows Additional edits This question was asked originally for Python 2.x but the answers also refer to python.. Additional edits This question was asked originally for Python 2.x but the answers also refer to python 3. The accepted.. 362951 There is a simple exe installer which just works http stackoverflow.com a 16377781 362951 If you are an ActivePython..
Python's slice notation http://stackoverflow.com/questions/509211/pythons-slice-notation but I haven't quite got my head around it and am looking for a good guide. python slice share improve this question .. if step is 1 the default . The other feature is that start or end may be a negative number which means it counts from the.. to the programmer if there are fewer items than you ask for. For example if you ask for a 2 and a only contains one element..
Calling an external command in Python http://stackoverflow.com/questions/89228/calling-an-external-command-in-python at the subprocess module in the stdlib from subprocess import call call ls l The advantage of subprocess vs system is that.. ls l The advantage of subprocess vs system is that it is more flexible you can get the stdout stderr the real status code.. can get the stdout stderr the real status code better error handling etc... . I think os.system is deprecated too or will..
Python: How do I pass a variable by reference? http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference unclear about whether parameters are passed by reference or value and the following code produces the unchanged value 'Original'.. you're done the outer reference will still point at the original object. If you pass an immutable object to a method you.. to' the_list outer_list 'one' 'two' 'three' print 'before outer_list ' outer_list try_to_change_list_contents outer_list..
Common pitfalls in Python [duplicate] http://stackoverflow.com/questions/1011431/common-pitfalls-in-python try file open file_path except OSError as e # do something Or even better with python 2.6 3 with open file_path as file It..
What can you use Python generator functions for? http://stackoverflow.com/questions/102535/what-can-you-use-python-generator-functions-for to allocate the memory for all results at the same time. Or for situations where the generator uses another generator or.. all of the results would be in memory at the same time. Or you could display the results while you find them which would..
Reading binary file in Python http://stackoverflow.com/questions/1035340/reading-binary-file-in-python f.read 1 while byte b # Do stuff with byte. byte f.read 1 Or as benhoyt says skip the not equal and take advantage of the..
Python subprocess readlines() hangs http://stackoverflow.com/questions/12419198/python-subprocess-readlines-hangs pexpect pexpect.run ruby ruby_sleep.rb logfile sys.stdout Or stdbuf to enable line buffering in non interactive mode from.. b'' print line proc.stdout.close proc.wait Or using pty based on @Antti Haapala's answer import os import..
Use different Python version with virtualenv http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv some virtualenv. Is it enough to overwrite the binary file Or do I have to change something in respect to the libraries python..
Threading in a PyQt application: Use Qt threads or Python threads? http://stackoverflow.com/questions/1595649/threading-in-a-pyqt-application-use-qt-threads-or-python-threads threading module What are advantages disadvantages of each Or do you have a totally different suggestion Edit re bounty While..
Flatten (an irregular) list of lists in Python http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python this L 1 2 3 4 5 6 Where the desired output is 1 2 3 4 5 6 Or perhaps even better an iterator. The only solution I saw that..
In a django form, How to make a field readonly (or disabled) so that it cannot be edited? http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-b return instance.sku else return self.cleaned_data 'sku' Or replace if instance and instance.pk with another condition indicating..
Python/Tkinter: Interactively validating Entry widget content http://stackoverflow.com/questions/4140437/python-tkinter-interactively-validating-entry-widget-content events And other related events that I might have missed Or should we forget interactive validation altogether and only..
python limiting floats to two decimal points http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points not dollars and then divide by 100 to convert to dollars. Or use a fixed point number like decimal share improve this answer..
Running shell command from python and capturing the output http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output As in output Popen mycmd myarg stdout PIPE .communicate 0 Or import subprocess p subprocess.Popen 'ls' ' a' stdout subprocess.PIPE..
Javascript equivalent of Python's zip function http://stackoverflow.com/questions/4856717/javascript-equivalent-of-pythons-zip-function JSON.stringify zip 'abcde' 1 2 3 4 5 a 1 b 2 c 3 d 4 e 5 Or you could use a range ... python style function if you've written..
Is it Pythonic to use list comprehensions for just side effects? http://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects ...conditions... note that I don't save the list anywhere Or should I call this func like this for x in y if ...conditions.....
Creating a singleton in python http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python cls class Logger object __metaclass__ Singleton Or in Python3 class Logger metaclass Singleton pass If you want..
Django dynamic model fields http://stackoverflow.com/questions/7933596/django-dynamic-model-fields Something.objects.filter data__contains 'a' Django MongoDB Or other NoSQL Django adaptations with them you can have fully..
How do you validate a URL with a regular expression in Python? http://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python Do you want the path to look unix like Or windows like Do you want to remove the query string Or preserve.. Or windows like Do you want to remove the query string Or preserve it These are not RFC specified validations. These are..
Python try-else http://stackoverflow.com/questions/855759/python-try-else the try exits normally what does the else block do for you Or is that just it python share improve this question The..
How to remove convexity defects in a Sudoku square? http://stackoverflow.com/questions/10196198/how-to-remove-convexity-defects-in-a-sudoku-square on the correct boundary of the Sudoku i.e. the red line OR how can I remove the difference between red line and green line..
OR behaviour in python: http://stackoverflow.com/questions/10460963/or-behaviour-in-python behaviour in python I have written the following piece of code..
How do I detect whether sys.stdout is attached to terminal or not? [duplicate] http://stackoverflow.com/questions/1077113/how-do-i-detect-whether-sys-stdout-is-attached-to-terminal-or-not is run via python foo.py # user types this on console OR python foo.py output.txt # redirection python foo.py grep ......
Python: tf-idf-cosine: to find document similarity http://stackoverflow.com/questions/12118720/python-tf-idf-cosine-to-find-document-similarity To rseymour@reed.edu Organization Reed College Portland OR Lines 26 In article 1993Apr20.174246.14375@wam.umd.edu lerxst@wam.umd.edu..
Bitwise Operation and Usage http://stackoverflow.com/questions/1746613/bitwise-operation-and-usage 0001 x 2 # Shift left 2 bits 0100 # Result 4 x 2 # Bitwise OR 0011 # Result 3 x 1 # Bitwise AND 0001 # Result 1 I can understand.. is 1 only if both of its inputs are 1 otherwise it's 0. OR is 1 if one or both of its inputs are 1 otherwise it's 0. XOR.. is 1 if one or both of its inputs are 1 otherwise it's 0. XOR is 1 only if exactly one of its inputs are 1 otherwise it's..
Python: unsigned 32 bit bitwise arithmetic http://stackoverflow.com/questions/210629/python-unsigned-32-bit-bitwise-arithmetic Is there a standard way in Python to carry on bitwise AND OR XOR NOT operations assuming that the inputs are 32 bit maybe.. there a standard way in Python to carry on bitwise AND OR XOR NOT operations assuming that the inputs are 32 bit maybe negative..
Django: multiple models in one template using forms http://stackoverflow.com/questions/569468/django-multiple-models-in-one-template-using-forms of selecting a Customer that's a whole separate project OR creating a new Customer then creating a Ticket and finally creating..
Python dynamic inheritance: How to choose base class upon instance creation? http://stackoverflow.com/questions/7057019/python-dynamic-inheritance-how-to-choose-base-class-upon-instance-creation 10 return f.read 12 # # class ImageZIP ### ImageJPG OR ImagePNG ### '''Class representing a compressed file. Sometimes..
Python scipy needs BLAS? http://stackoverflow.com/questions/7496547/python-scipy-needs-blas std legacy fno second underscore c .f # with gfortran ## OR for GNU compiler on 64 bit systems #g77 O3 m64 fno second underscore.. m64 fno second underscore fPIC c .f # with gfortran ## OR for Intel compiler #ifort FI w90 w95 cm O3 unroll c .f # Continue..
Decrypting strings in Python that were encrypted with MCRYPT_RIJNDAEL_256 in PHP http://stackoverflow.com/questions/8217269/decrypting-strings-in-python-that-were-encrypted-with-mcrypt-rijndael-256-in-php len plaintext BLOCK_SIZE 1 BLOCKSIZE ' 0' # OR #padded_text plaintext.ljust len plaintext BLOCK_SIZE len plaintext..
Creating a logging handler to connect to Oracle? http://stackoverflow.com/questions/935930/creating-a-logging-handler-to-connect-to-oracle AND FITNESS. IN NO EVENT SHALL # VINAY SAJIP BE LIABLE FOR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES.. SHALL # VINAY SAJIP BE LIABLE FOR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING.. LIABLE FOR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES OR # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE DATA OR..
|