Popular recipes by Alexander James Wallar http://code.activestate.com/recipes/users/4179768/2012-08-25T17:16:47-07:00ActiveState Code RecipesOpenKinect Mouse Control Using Python (Python) 2012-04-15T18:49:00-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578104-openkinect-mouse-control-using-python/ <p style="color: grey"> Python recipe 578104 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/3d/">3d</a>, <a href="/recipes/tags/computer/">computer</a>, <a href="/recipes/tags/computer_vision/">computer_vision</a>, <a href="/recipes/tags/depth/">depth</a>, <a href="/recipes/tags/hand_tracking/">hand_tracking</a>, <a href="/recipes/tags/kinect/">kinect</a>, <a href="/recipes/tags/mouse/">mouse</a>, <a href="/recipes/tags/opencv/">opencv</a>, <a href="/recipes/tags/openkinect/">openkinect</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tracking/">tracking</a>, <a href="/recipes/tags/vision/">vision</a>). Revision 2. </p> <p>This is a simple code that lets a user control the mouse and left-click using the Microsoft Kinect, Python, and OpenKinect. </p> <pre class="prettyprint"><code>Computer Prerequisites: -OpenKinect -Python Wrapper for OpenKinect -A Linux machine using Ubuntu -OpenCV 2.1 -OpenCV 2.3 -Python 2.7.2 -A Microsoft Kinect -A Microsoft Kinect USB Adapter -PyGame -Xlib for Python </code></pre> <p>To run this code you either need to start it in the terminal or you need to write a short bash script that runs the code. This is necessary because it requires super-user privileges.</p> <p>The Bash script is (Assuming the code is saved by the name 'Hand Tracking.py' in /home/$USER directory:</p> <pre class="prettyprint"><code>#!bin/bash cd 'home/$USER' gksudo python 'Hand Tracking.py' </code></pre> <p>The code is heavily commented and most of what you will need to know is there. </p> Find Multiple Elements In a List (Python) 2011-11-05T23:40:41-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577943-find-multiple-elements-in-a-list/ <p style="color: grey"> Python recipe 577943 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/enumerate/">enumerate</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/search/">search</a>, <a href="/recipes/tags/search_list/">search_list</a>). </p> <p>This algorithm searches for more than one element in a list. The input is a list that you want to search through and a list of elements that you want to search for. The output is a multidimensional list with the positions of the elements you are searching for in the search list in the order that you listed them. </p> Control your mouse with the Microsoft Kinect using OpenKinect (Python) 2012-03-19T21:28:08-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578082-control-your-mouse-with-the-microsoft-kinect-using/ <p style="color: grey"> Python recipe 578082 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/computer/">computer</a>, <a href="/recipes/tags/control/">control</a>, <a href="/recipes/tags/freenect/">freenect</a>, <a href="/recipes/tags/kinect/">kinect</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/microsoft/">microsoft</a>, <a href="/recipes/tags/mouse/">mouse</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/openkinect/">openkinect</a>, <a href="/recipes/tags/vision/">vision</a>, <a href="/recipes/tags/xlib/">xlib</a>). </p> <p>This is a simple code that uses the Kinect in order to simulate the mouse on the screen. In order for this code to work you must have a Linux machine, the freenect module from OpenKinect, NumPy, and Xlib. The last three can be downloaded and installed by running sudo apt-get install {PROGRAM NAME HERE}. Freenect needs to be downloaded and installed for python using the terminal and there are good instructions here how to do it <a href="http://openkinect.org/wiki/Getting_Started" rel="nofollow">http://openkinect.org/wiki/Getting_Started</a>. You will also need to install the python wrapper. This can be done by following the instructions on this site: <a href="http://openkinect.org/wiki/Python_Wrapper" rel="nofollow">http://openkinect.org/wiki/Python_Wrapper</a>. To run the code you must first cd into the directory in which the code resides and then in the terminal run the command sudo python {THE NAME YOU STORED THE CODE BELOW AS}.py. This should run the code. The code needs superuser privileges. Also if you wonder why the mouse will stop tracking your hand, there are two reasons. 1) This code is not super sophisticated, all it does is find the minimum point in the depth image and scales the coordinates to the size of the screen and moves the mouse there so if your stomach is the closest thing to the screen it will track it and place the mouse there. The other reason the mouse might stop tracking is because there is a counter in the that will only let it run 10000 iterations. This is just a safety precaution in case you are not able to control the mouse and the computer freaks out. The last thing you need to know to run this code is that the main_mouse() function takes to arguments, the first is the x dimension of your screen and the second is the y dimension. THE CODE WILL NOT FUNCTION PROPERLY ON MULTIDISPLAY COMPUTERS. Thank You</p> Libreenect (OpenKinect) Minimum Value Joystick With Display - Kinect Demo Using OpenKinect SDK (Python) 2012-03-18T18:52:42-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578080-libreenect-openkinect-minimum-value-joystick-with-/ <p style="color: grey"> Python recipe 578080 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/3d/">3d</a>, <a href="/recipes/tags/computer_vision/">computer_vision</a>, <a href="/recipes/tags/depth/">depth</a>, <a href="/recipes/tags/joystick/">joystick</a>, <a href="/recipes/tags/kinect/">kinect</a>, <a href="/recipes/tags/libfreenect/">libfreenect</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/opencv/">opencv</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/virtual_joystick/">virtual_joystick</a>). Revision 3. </p> <p>This demo requires you to be using a Linux machine and to have libfreenect installed with the python wrapper. This demo also requires you to have opencv, numpy, and pygame. They can all be installed using sudo apt-get install {PROGRAM NAME HERE}. Here are instructions on installing libfreenect: <a href="http://openkinect.org/wiki/Getting_Started" rel="nofollow">http://openkinect.org/wiki/Getting_Started</a>. Okay, so what this demo does is finds the minimum point on the depth image and uses the index of this minimum point to create a four button joystick that codes for 'A', 'B', 'X', 'Y'. It also uses the minimum point and the RGB image to put a circle on the minimum point on the screen. So basically a dot will follow your hand on the RGB image. If the minimum value is larger than 600 (arbitrary units), the color of the dot will change from red to purple and the dot will remain stationary. Also, if the Kinect is not properly opened the first time, unplug it and plug it back in and run in the terminal sudo freenect-glview. After try running the program again. One more thing, this code requires super user privileges so run it through the terminal. Here is how to do this for linux n00bs.</p> <ol> <li>Change your directory the the directory the code is in (use cd {PATH})</li> <li>Run the code using sudo python {FILENAME}</li> <li>Don't forget to add the the .py at the end</li> </ol> Batch Local Network Messaging System (Batch) 2012-02-03T01:41:41-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578028-batch-local-network-messaging-system/ <p style="color: grey"> Batch recipe 578028 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/alex/">alex</a>, <a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/batch_file/">batch_file</a>, <a href="/recipes/tags/dos/">dos</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/messenger/">messenger</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/wallar/">wallar</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/wired/">wired</a>, <a href="/recipes/tags/wired_network/">wired_network</a>). Revision 3. </p> <p>This is a Batch Local Messaging System that can be used to send messages between computers that share the same file system.</p> <p>There are three files in the code below. They are separated with dashed lines. There is a comment on the top of each segment of code indicating the file name that the code needs to be saved as. They need to be saved as three separate batch files in the same folder. </p> <p>The first file, MessengerMain.bat will ask you for your name and then an address. The name should be whatever you wish your screen name to be. The address needs to be the path of the FOLDER that is shared between you and the other computer that you are communicating with. Once these pieces of information are entered, two screens will appear. The first is the Sender screen and the other is the Receiver screen. </p> <p>How does it work? The MessengerMain.bat file will create two text files, Name.txt and Address.txt, that will hold your user name and the common folder address. It will then use the START command to run Sender.bat and Receiver.bat. Sender.bat will create a text file named msgtext.txt in the address folder you specified in MessengerMain.bat. If there is already a file of this sort in existence in the address folder, it will overwrite it. Once this file is created, any message you enter will be saved in the msgtext.txt file after the name you entered (%name%: %msg%). Sender.bat the CLS (clears) the screen and asks you for another message continuously. Receiver.bat is very similar. Receiver.bat, first off, reads the Address.txt file and the Name.txt file to find your information given in MessengerMain.bat just like Sender.bat does. It will count the number of lines in the msgtext.txt file and will clear the whole screen and print out the whole contents of the file if the number of lines in msgtext.txt increases. So basically you are writing to a shared file and reading from a shared file. </p> <p>Example:</p> <p>I have a folder called MSGFolder located in the folder as I have my MessengerMain.bat, Receiver.bat, and Sender.bat files. I then run MessengerMain.bat by double clicking on the icon. I enter Alex for my Name and MSGFolder as the Address. I then double click MessengerMain.bat again and enter the name Bob for Name and MSGFolder for Address. Once you do this you can type in either of the Sender.bat interfaces and it will show up as if you are talking. </p> Get multiple elements from a list (Python) 2011-11-21T06:41:57-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577953-get-multiple-elements-from-a-list/ <p style="color: grey"> Python recipe 577953 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/get/">get</a>, <a href="/recipes/tags/getvar/">getvar</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/retrieve/">retrieve</a>, <a href="/recipes/tags/retrieving/">retrieving</a>). </p> <p>This code lets you retrieve multiple elements from a list at one time</p> Random Binary List (Python) 2011-11-09T19:38:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577944-random-binary-list/ <p style="color: grey"> Python recipe 577944 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_binary_list/">random_binary_list</a>). </p> <p>This recipe returns a list of size n such that the contents of the returned list are random 0s and 1s. It returns a random binary list of size n.</p> Python Multidimensional List Searcher (Python) 2011-10-29T22:21:39-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577929-python-multidimensional-list-searcher/ <p style="color: grey"> Python recipe 577929 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elements/">elements</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/finder/">finder</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>). </p> <p>This module/function lets you find a 2 dimensional list of indices for elements you are looking for in a super list. Example:</p> <p>find([1,1,1,2,1,2,3,3],[1,2,3])</p> <p>returns: [[0, 1, 2, 4], [3, 5], [6, 7]]</p> Polynomial Factoring Using Rational Root Theorem (Python) 2011-12-03T23:51:07-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577974-polynomial-factoring-using-rational-root-theorem/ <p style="color: grey"> Python recipe 577974 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/poly/">poly</a>, <a href="/recipes/tags/polynomial/">polynomial</a>, <a href="/recipes/tags/polyroots/">polyroots</a>, <a href="/recipes/tags/poly_roots/">poly_roots</a>, <a href="/recipes/tags/rational/">rational</a>, <a href="/recipes/tags/rational_root_theorem/">rational_root_theorem</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/roots/">roots</a>, <a href="/recipes/tags/theorem/">theorem</a>). </p> <p>This algorithm factors a polynomial but will only factor it by giving the rational roots. For instance if one of the roots in the polynomial was irrational, the polynomial would not be factored correctly. </p> Encrypt and Decrypt Text and Text Files (BETA) (Python) 2011-11-22T04:18:15-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577954-encrypt-and-decrypt-text-and-text-files-beta/ <p style="color: grey"> Python recipe 577954 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/cryptology/">cryptology</a>, <a href="/recipes/tags/decrypt/">decrypt</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/hidding/">hidding</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_files/">text_files</a>). Revision 7. </p> <p>This program interactively lets you encrypt and decrypt text as well as text files using a key system as well as a cipher.</p> Yahoo Stock Information (Python) 2012-08-25T17:16:47-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577989-yahoo-stock-information/ <p style="color: grey"> Python recipe 577989 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/computation/">computation</a>, <a href="/recipes/tags/corey_goldberg/">corey_goldberg</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/information/">information</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/realtime/">realtime</a>, <a href="/recipes/tags/stock/">stock</a>, <a href="/recipes/tags/yahoo/">yahoo</a>). Revision 3. </p> <p>This recipe is solely due to the programming of Corey Goldberg (<a href="mailto:corey@goldb.org">corey@goldb.org</a>). The only thing I did to it was make it into a class. I thought that by uploading it here that more people would come across it and more people could use it. It is really good. It gives you stock information.</p> Encrypt and Decrypt Text and Text Files (BETA) Jython GUI (Python) 2011-11-23T05:37:04-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577957-encrypt-and-decrypt-text-and-text-files-beta-jytho/ <p style="color: grey"> Python recipe 577957 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/cryptology/">cryptology</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/encryption_decryption/">encryption_decryption</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/hiding/">hiding</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is a Jython GUI of my previous recipe: Encrypt and Decrypt Text and Text Files</p> Evolutionary Algorithm (Generation of Prime Numbers) (Python) 2011-11-27T06:45:00-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577964-evolutionary-algorithm-generation-of-prime-numbers/ <p style="color: grey"> Python recipe 577964 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/genetic/">genetic</a>, <a href="/recipes/tags/genetic_algorithm/">genetic_algorithm</a>, <a href="/recipes/tags/genetic_algorithms/">genetic_algorithms</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>). </p> <p>This is an evolutionary algorithm that returns a random list of prime numbers. This code is highly inefficient for a reason. This algorithm is more of a proof of concept that if a prime was a heritable trait, it would not be a desired one. </p> <p>Parameters:</p> <p>isPrime --> n: number to check if it is prime allPrimes --> n: size of list of random primes, m: the primes in the list will be between 0 and m</p> primeList (Python) 2011-11-05T23:42:37-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577935-primelist/ <p style="color: grey"> Python recipe 577935 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/prime/">prime</a>, <a href="/recipes/tags/primelist/">primelist</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/theory/">theory</a>). Revision 3. </p> <p>This module returns all the prime numbers strictly less than n. For this code to print out all of the primes n inclusive, in the range, n+1 must be substituted for n.</p> Password Generator (Short Code) (Python) 2011-11-18T03:02:13-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577949-password-generator-short-code/ <p style="color: grey"> Python recipe 577949 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/alphabet/">alphabet</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/passwords/">passwords</a>, <a href="/recipes/tags/password_generator/">password_generator</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/secure/">secure</a>, <a href="/recipes/tags/security/">security</a>, <a href="/recipes/tags/short_code/">short_code</a>). Revision 2. </p> <p>This recipe generates a pseudo-random password of a prescribed length. It also lets you specify what characters are not permitted in the password or specify what characters are.</p> Base Expansion/Conversion Algorithm Python (Python) 2011-11-21T19:58:14-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577939-base-expansionconversion-algorithm-python/ <p style="color: grey"> Python recipe 577939 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/base16/">base16</a>, <a href="/recipes/tags/base2/">base2</a>, <a href="/recipes/tags/base_conversion/">base_conversion</a>, <a href="/recipes/tags/base_expansion/">base_expansion</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/number_theory/">number_theory</a>, <a href="/recipes/tags/python/">python</a>). Revision 3. </p> <p>This algorithm converts a base c number into a base b number. Parameters c and b are arbitrary are not constrained by any bounds. The input n is a list and the output is a list.</p>