Popular recipes by Filippo Squillace http://code.activestate.com/recipes/users/4174931/2013-12-07T23:24:29-08:00ActiveState Code RecipesPrint an object only a limited number of times (Python) 2012-12-09T19:41:20-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578374-print-an-object-only-a-limited-number-of-times/ <p style="color: grey"> Python recipe 578374 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>The psome function detects the position into the source files where the function itself is called. It prints the object according a counter variable. Therefore, whenever the psome function is called inside a loop the object will be printed only a limited number of times. This can useful for debugging code in particular when the data structure we want to scan is quite big and we want to print only the first elements of it.</p> Easy way to use Graph Facebook API without ad-hoc libraries (Ruby) 2012-11-20T14:14:27-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578343-easy-way-to-use-graph-facebook-api-without-ad-hoc-/ <p style="color: grey"> Ruby recipe 578343 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/facebook/">facebook</a>, <a href="/recipes/tags/http/">http</a>, <a href="/recipes/tags/ruby/">ruby</a>). </p> <p>I was painfully lokking for a simple function that allow easily make GET or POST requests in Ruby without using complex libraries such as Koala for accessing to the Facebook Graph. At the end I gave up and did it by myself, so the function fb_api, based on net/http, is able to make GET or POST requests (depending if the request is for retrieving information of the graph or is for changing the graph such as post feed etc.). It returns a dict from a JSON data structure.</p> <p>This might be useful for your facebook app ;)</p> Hill Climbing Template Method (Python) 2013-12-07T23:24:29-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578157-hill-climbing-template-method/ <p style="color: grey"> Python recipe 578157 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . Revision 3. </p> <p>This is a template method for the hill climbing algorithm. It doesn't guarantee that it will return the optimal solution. If the probability of success for a given initial random configuration is p the number of repetitions of the Hill Climbing algorithm should be at least 1/p. Note: generate_configuration() is not implemented yet but is quite easy to understand what it does.</p> Simple Dijkstra Algorithm (Python) 2012-06-08T02:20:18-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578156-simple-dijkstra-algorithm/ <p style="color: grey"> Python recipe 578156 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/graph/">graph</a>). Revision 3. </p> <p>The algorithm uses the priority queue version of Dijkstra and return the distance between the source node and the others nodes d(s,i). </p> String Matching using a Finit State Machine (Python) 2012-06-07T06:03:52-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578161-string-matching-using-a-finit-state-machine/ <p style="color: grey"> Python recipe 578161 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>This module executes the string matching between an input sequence T and a pattern P using a Finite State Machine. The complexity for building the transition function is O(m^3 x |A|) where A is the alphabet. Since the string matching function scan the input sequence only once, the total complexity is O(n + m^3 x |A|)</p> An extensible Conway's Game of Life (Python) 2011-12-05T11:45:45-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577976-an-extensible-conways-game-of-life/ <p style="color: grey"> Python recipe 577976 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/animation/">animation</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/life/">life</a>). </p> <p>This program is an extensible Conway's game of life. It allows to define different type of grid (for example 2D or 3D) and more complex rules. Each grid inherits an Abstract grid that implement the method (next()) to pass for the next configuration. Furthermore, each element can be whatever type. In this example I designed Grid2DBool that represent the simple Conway's game of life, but could be possible to develop and easily implement more complex grids and rules.</p> <p>Note: The demo save also the animation in a file .mp4 and plot it through pyplot. The demo could take long time because of storing all the configurations before showing the animation. Therefore, the visualization can be improved using other libraries (as wxpython) that paint the configuration of the grid once it's created. With a more complex view it's convenient to apply MVC pattern declaring the model AbstractGrid as the Observable class.</p> Parser Keylogger based on a Finite State Machine (Python) 2011-11-21T14:53:52-08:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577952-parser-keylogger-based-on-a-finite-state-machine/ <p style="color: grey"> Python recipe 577952 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/keylogger/">keylogger</a>, <a href="/recipes/tags/parser/">parser</a>, <a href="/recipes/tags/state_machine/">state_machine</a>). Revision 3. </p> <p>This program parses the logfile given by the execution of the keylogger command <strong>'script -c "xinput test ID_CODE" | cat LOG_FILE'</strong> and it is based on a Finite State Machine (FSM) to manage all the possible combinations of the modifiers that represent the state of the FSM. The parser gets the mapping between the couple of keycode and modifier typed and the corresponding char by xmodmap command. The parser is able to manage also extended combinations such as Control or Super that don't give a real char. To introduce new possible states that represent new combinations between modifiers, it's just necessary to update the list of state (<em>mod_keys</em>) and add new rules in the transition function properly. For example to introduce the Caps Lock state just add it in mod_keys and the data structure transition has to handle the release event of the corresponding key. For the dependency of xmodmap the parser works only in X11 based systems.</p> Future decorator in a pretty pythonic way (Python) 2011-10-20T16:51:48-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577915-future-decorator-in-a-pretty-pythonic-way/ <p style="color: grey"> Python recipe 577915 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/threading/">threading</a>). </p> <p>Without thinking in thread creation the idea is to call several times a function assigning a thread for each call with related parameters and returning the list of results in a pretty pythonic way. For example, if we have already defined the function 'func': res = func(par) we want to call that several times and get the values in the future. So: func(par1) func(par2) func(par3).get() func(par4).get_all()</p> <p>assigning a thread for each call. The 'get' method returns the first possible results, and 'get_all' returns all the values in a list. The decorator works fine with kwargs too.</p> <p>This recipe is based on: <a href="http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/" rel="nofollow">http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/</a> that use only one call for the function. The problem in that recipe is that each call blocks the execution.</p> <p>Vote if you like it!</p> Backtracking template method (Python) 2011-07-03T20:56:37-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577777-backtracking-template-method/ <p style="color: grey"> Python recipe 577777 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/backtracking/">backtracking</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>It's a faster pythonic solution for backtracking. It combine a list of choice points with a list of choices according to a function that define when a choice is assignable to a choice point. As you see, it's easy to formulate a 8 queens and 4 colors problems.</p> Combinations of a sequence without replacement using dynamic programming (Python) 2011-06-20T20:09:24-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/ <p style="color: grey"> Python recipe 577764 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>This recipe shows a way to solve the K-combinations problem without replacement with a sequence of items using the dynamic programming technique.</p> <p>If you want a divine but slower solution take into account a "divide et impera" paradigm like in this recipe: <a href="http://code.activestate.com/recipes/190465-generator-for-permutations-combinations-selections/" rel="nofollow">http://code.activestate.com/recipes/190465-generator-for-permutations-combinations-selections/</a></p> <p>This implementation could be improved overcoming some drawbacks. In particular, the inefficiency is due to the data structure used to store every set of combinations.</p> Managment command to deploy a Django project (Python) 2011-06-21T22:10:31-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577767-managment-command-to-deploy-a-django-project/ <p style="color: grey"> Python recipe 577767 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/django/">django</a>). </p> <p>This recipe shows a management command for django project that enables automatic synchronization between the local and remote project. Of course, the deploy exclude to overwrite the settings.py located in the remote server.</p> <p>This is a clean way to deploy your own project without calling external commands, although this command needs rsync to work.</p> Generator of combinations without replacement for a sequence using dynamic programming (Python) 2011-06-21T09:11:19-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577766-generator-of-combinations-without-replacement-for-/ <p style="color: grey"> Python recipe 577766 by <a href="/recipes/users/4174931/">Filippo Squillace</a> . </p> <p>From the previous recipe <a href="http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/" rel="nofollow">http://code.activestate.com/recipes/577764-combinations-of-a-sequence-without-replacement-usi/</a> we can easily build a generator that return all the K-combinations without replacement.</p>