miércoles, 14 de diciembre de 2011

Fair power making decisions.

This post is not python related i just about i thought that came to me while i was reading the different takes VC's and entrepreneurs have on the work hours, on working hard.
This post is here to capture that thought because I'm building a theory of administration that's based on the balance between power and motivation. I firmly believe that many institutions in my country(México) could be 'fixed' if in the process of design them, the focus were placed on these two concepts. But that's material for another post lets focus on the subject at hand.

To sum it all up these are:
The VC's want you to work hard to make them money.
The entrepreneur wants to work hard to get ahead of the competition.

The decisive argument on the matter, as usual is up to science, and science basically says, the most efficient way for a human being to work is 8 hours at day, 5 days a week, more than that an productivity drops until it actually is dangerous for the human and for the work being done to put more hours. 

The programmers speak 


Science speak


The thought i was talking about is somehow related. In the comment area of the first link there is one particular argument that shows one flaw of the way we rationally argue, quote
We need to readdress the point that work is a negotiation between employer and employee. The first pays mainly money and maybe some other stuff in exchange for something, mainly the ‘production’. As such, the employer has the right to demand high output and quality. The employee has the right to work some place else.
Let me state first that my motivation for this critique is fairness. So the comment sound smart and enlighting and in a way it is. Put it plainly, the employer here has a range of options, a huge degree of possible decisions to make when dealing with a lazy employee, and the employee has only two, stay or leave.

The unfairness resides exactly in that difference, the employer can torture the employee, can motivate him, can move it to other place in the company, can do many many things, he is in a power position. The employee only two. Let me make this clear, there is no unbalance of power here, when it adds up, they both have tremendous power, because the employee is valuable and he carries knowledge and know-how useful for the company, and costly to lose. Is just that the power is too focused on two decisions while the power of the employer is spread in many possible decisions.

TLDR, the power relationships can be balanced fairly if we take into account, the weight each possible decision it enables the parties and the plain number of those possible decisions, not just the sum of the total weight.

In the next post i want to talk about the importance of perceived fairness in motivation.

miércoles, 30 de noviembre de 2011

Using heroku on webfaction

The idea here is to be able to fetch from a herokuapp to our webfaction server so we can have a nice development process like this:
We work on our local repo, this is the dev server.
We push to heroku, this is our staging area, we try and test new features here.
Then we go to webfaction and fetch from heroku and it will automatically update our production server.

Ok lets get started.

Open your ssh session to webfaction
mkdir ~/gems
Open the file .bashrc located at your home
nano ~/.bashrc
Paste this lines on the end of the file
export GEM_HOME=~/gems
export GEM_PATH=~/gems
Save the file with ctrl+o and hit ctrl+x to exit nano
Paste again these lines on your ssh session
export GEM_HOME=~/gems
export GEM_PATH=~/gems
Now install heroku
gem install heroku
Now you'll need to generate ssh keys for heroku we'll use this article,
Please visit the original article, i'll do the copy pasta because i really don't want to lose this information and the internets can be faulty.
 In quite a few situations its preferred to have ssh keys dedicated for a service or a specific role. Eg. a key to use for home / fun stuff and another one to use for Work things, and another one for Version Control access etc. Creating the keys is simple, just use:
ssh-keygen -t rsa -f ~/.ssh/id_rsa.heroku -C "Key for Word stuff"
Use different file names for each key. Lets assume that there are 2 keys, ~/.ssh/id_rsa.work and ~/.ssh/id_rsa.misc . The simple way of making sure each of the keys works all the time is to now create config file for ssh:
touch ~/.ssh/config
chmod 600 ~/.ssh/config
echo "IdentityFile ~/.ssh/id_rsa.heroku" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/id_rsa" >> ~/.ssh/config
This would make sure that both the keys are always used whenever ssh makes a connection. However, ssh config lets you get down to a much finer level of control on keys and other per-connection setups. And I recommend, if you are able to, to use a key selection based on the Hostname. My ~/.ssh/config looks like this :
Host *.heroku.com
  IdentityFile ~/.ssh/id_rsa.heroku
  User nombre.falso@gmail.com
Ofcourse, if I am connecting to a remote host that does not match any of these selections, ssh will default back to checking for and using the 'usual' key, ~/.ssh/id_dsa or ~/.ssh/id_rsa

Again, thanks to Karanbir Singh for this article, i've customized the code for our purposes.
Now, to make our life easier we will create an alias to the heroku bin
Open .bashrc again
nano .bashrc
Paste at the end of the file
alias heroku=~/gems/bin/heroku
Now you can follow the heroku tutorial to configure your account(register one if you don't have it) here are the steps
heroku config
Answer the questions, finish the wizard
Now go to where you want to keep your git repo fetched from heroku
cd ~/webapps/git/repos/
Initialize a new git repo i named mine heroku
git init heroku
cd heroku
Now add the remote heroku
git remote add heroku git@heroku.com:nameofyouraccount.git
Coincidentally my remote is named heroku, but you can name yours whatever you want, substitute nameofyouraccount with the name of your heroku account.
Now do,
git fetch heroku

And tada! you now have an easy way to pull changes from heroku to webfaction.

martes, 29 de noviembre de 2011

installing psycopg2 in Webfaction

If you use webfaction and you'r machine name is Web300 and above(Web301, Web302, etc) and you want to install psycopg2 you'll need
nano ~/.bashrc
Paste this
export PATH=/usr/pgsql-9.1/bin/:$PATH
Now hit ctrl+o and then ctrl+x
Now on the terminal export the path again
export PATH=/usr/pgsql-9.1/bin/:$PATH
And when you do
easy_install psycopg2
or
pip install psycopg2
It won't complain about pg_config executable missing.

sábado, 26 de noviembre de 2011

why post

I just read an awesome tip when learning new stuff, from the article on motivation and learning something from the Freakonomics guys(read the original book it explains the internet as we know it from an economic point of view)
Wheeler recommended that, after we solve any problem, we think of one sentence that we could tell our earlier self that would have “cracked” the problem. This kind of thinking turns each problem and its solution into an opportunity for reflection and for developing transferable reasoning tools.
I would have loved saying to my self.
Writting a new django feature takes this steps:

  • Think of the feature.
  • Think how you would put the data needed to drive the feature in a 2d table.
  • Create a new django app .
  • Write the name of the columns in a model inside models.py.
  • Write the logic that uses that data in views.py
  • Make sure your processed data ends ups in the context that view returns.
  • Write the template that uses the processed data.
And make sure you have the django official docs with your all the time!

martes, 4 de octubre de 2011

Heroku push rejected, failed to compile Python/django app

       

You might have seen this error when trying to push to heroku your django app using pip:

Running setup.py install for distribute
           Before install bootstrap.
           Scanning installed packages
           Setuptools installation detected at /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
           Egg installation
           Patching...
           Renaming /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg into /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg.OLD.1317780660.95
           Patched done.
           Relaunching...
           Traceback (most recent call last):
             File "<string>", line 1, in <module>
           NameError: name 'install' is not defined
           Complete output from command /tmp/build_2pim0d1o5mgy0/bin/python2.7 -c "import setuptools;__file__='/tmp/build_2pim0d1o5mgy0/build/distribute/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-UtGheN-record/install-record.txt --install-headers /tmp/build_2pim0d1o5mgy0/include/site/python2.7:
           Before install bootstrap.
      
       Scanning installed packages
      
       Setuptools installation detected at /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
      
       Egg installation
      
       Patching...
      
       Renaming /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg into /tmp/build_2pim0d1o5mgy0/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg.OLD.1317780660.95
      
       Patched done.
      
       Relaunching...
      
       Traceback (most recent call last):
      
         File "<string>", line 1, in <module>
      
       NameError: name 'install' is not defined
      
       ----------------------------------------
       Command /tmp/build_2pim0d1o5mgy0/bin/python2.7 -c "import setuptools;__file__='/tmp/build_2pim0d1o5mgy0/build/distribute/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-UtGheN-record/install-record.txt --install-headers /tmp/build_2pim0d1o5mgy0/include/site/python2.7 failed with error code 1
       Storing complete log in /app/.pip/pip.log
 !     Heroku push rejected, failed to compile Python/django app

The problem is the distribute package, open requirements.txt and delete that line.
It should work now.

UPDATE:
The problem is that my distribute was too old so the real solution is to
pip install distribute -U
To upgrade it, don't forget to also update your requirements.txt
pip freeze > requirements.txt

martes, 27 de septiembre de 2011

Using pathogen with a custom compiled vim



Pathogen is great for managing your plugins but it needs to know if you changed the default ~/.vim to another folder with the path VIMRUNTIME, so you have to export it INSIDE vim, yeah i don't know why.
So if you want pathogen to succesfully rebuild the help tags of your plugins when you do
:call pathogen#helptags()
You have to add this to your .vimrc
:let $VIMRUNTIME='/path/to/the/root/of/your/vim/installation/'
For regular users VIMRUNTIME leads to .vim in my case i compiled vim to be installed in ~/apps/vim73 so i had to add:
:let $VIMRUNTIME='~/apps/vim73'

To my .vimrc hope it helps somebody with the same problem.

lunes, 12 de septiembre de 2011

Vim workflow for python/django development

INTRO

I've being customizing my Vim installation and i wanted to share how i solved some common problems, my customizations were all about productivity and removing annoyances.

What are you going to get:

  • The latest Vim compiled with python support
  • Code completion for python AND django(full working no annoyances).
  • Textmate like snippets to aid you when defining functions or classes and other python structures.
  • Autoclosing some characters like ( with ) or [ with ] and so on.
  • Being able to surround code with ' " ( [ or any html tag, quickly and delete surrounding code too.
  • Python syntax highlighting.
  • Python syntax error highlighting on the fly.
  • Inside vim you'll be able to see the Docstrings for functions, methods, classes, really for newbies, optional for pros.
  • Being able to comment code with a toggle hotkey, the quickest way.
  • Auto indenting done right for python (the default autoindeting in Vim is not very good for python).
  • A ver intuitive and quick interface to navigate through folders for files, lighting quick.
  • Quickly opening your most recently used files.
  • Some fixes for users coming from using GUI text editors.
  • Running python scripts with a hotkey.


How is this post laid out

I'll include as many details as possible, because, while researching to solve my problems, i often ran into undocumented issues. I'll put this document in the order of events. I strongly recommend using ctrl+f in your browser to search for specific parts. I also added relevant links like this [1] check them out if you want additional information.


Some basic assumptions:

  • GNU Linux - Ubuntu 10.10 Maverick
  • This reference guide will probably work with newer ubuntu versions,
  • I'm assuming you can do basic stuff on the command line.
  • You really want the best Vim experience for python.
  • And you know basic Vim usage.

The first thing i did was compile Vim from sources because the version included in ubuntu 10.10 is Vim 7.2 and i wanted the latest Vim 7.3 with full python support. You can see which version of vim you have with.
vim --version


COMPILING VIM 7.3

First you have to get it from the repository using Mercurial
hg clone https://vim.googlecode.com/hg/
If you don't have the hg command install it
 
sudo apt-get install mercurial

The hg clone will create a directory called vim then you need to go to the src folder inside that dir.
 
cd vim/src

And then you need to configure your installation for your own system, as you don't want to mix this compiled vim with the one you get using apt-get on ubuntu (i uninstalled that one) you can do it, but you don't have to unless you want your compiled version to open when you type vim on your terminal.
If you want to completly uninstall the built-in ubuntu vim do
 
sudo apt-get purge vim
And then you can delete the folder vim uses to customize for your user
 
rm -r ~/.vim
Another file important to remember is .vimrc this file is located in your home folder and even the compiled Vim will use it so you can keep previous modifications to this file.

Now we want to configure the installation before compiling this is the line i issued
 
./configure --prefix=${HOME}/apps/vim73 --with-features=huge --enable-gui=gnome2 --enable-pythoninterp --enable-rubyinterp --enable-multibyte --with-python-config-dir=/usr/lib/python2.6/config

Now take this line and paste it in your text editor(vim, hopefully) to edit it to your needs, ill guide you in each part of that line. The first part:
 
./configure
Configure is the standard script in many linux sources used to configure them before compiling them you can ask it all the commands available using:
./configure --help

The next option i added was
 --prefix=${HOME}/apps/vim73
The part after = is the installation path, thats where you are going to keep all the vim files, including plugins and customizations (with the exception of the .vimrc that still lives in the root of your home folder)
I chose to use my home dir in a directory where i keep all my custom apps.

Continuing with the line
 
--with-features=huge
With this you tell vim, that you want the full featured vim default installation, i noticed that this gives you lots of plugins for standard functionality like editing zip files directly or highlighting for PHP and C. This line is important because when you are getting help online(in forums, blogs, the Vim manual) many things are assumed of your Vim installation, so you better keep it, you can always delete the added plugins.

This next line is only needed if you want gVim or the stand alone graphical interface vim, i really only use the command line vim but i could needed the GUI someday.

 --enable-gui=gnome2
I must say that you won't get a menu entry for gVim in your ubuntu menu, but you will get the binary and you can add the entry your self.

This next line is for python, add it:
 
--enable-pythoninterp

You need ruby support for some plug-ins, and if of course if you want to edit ruby files sometime in the future.

--enable-rubyinterp



--enable-multibyte
You have to add that line if you are going to work with characters outside the ASCII world, basically if you are going to work with text thats not english, add it.

The last option has to be customized for the version of python you want vim to use to run some plugins. And don't worry you'll be able to run your own code with whatever version of python you want,
--with-python-config-dir=/usr/lib/python2.6/config
Just change the 2.6 to the version you have.
This config is actually config.c a filed added in all python installations from the ubuntu repositories, i had to install Python 2.5 from external repos and i didn't get this file but the only problem i've seeing is with google appengine applications, as google only supports Python 2.5 and Vim will show warnings when running some legacy code, like importing the sets module, that got deprecated in Python 2.6.
I've seen people online compiling vim with python 3.0 [1] but they ran into some problems, i can't help in that regard.

Copy your customized configure line and paste it on the terminal thats browsing the
src folder, and run it then and wait for it to finish, if you have errors check for spelling mistakes using 
 ./configure --help as reference.

Now that we have our vim sources configured lets put the machine to actually compile issuing a simple:
 
make

It will take a while depending on your machine, you can go take a coup of coffee and come back when its done to issue this next command:

 make install

Once make install is done you'll have a working vim installation on the folder you choose using the --prefix option.
 
cd /home/grillermo/apps/vim73

Inside that dir you'll have 2 folders
share and bin
The
bin dir is where the vim binary lives and you can run it inside there using a simple:
 ./vim
Or for the graphical gnome vim
 
./gvim

Inside
share/vim/vim73 you'll put your plugins and add-ons, this folder is the equivalent of the ~/.vim directory that the repositories Vim uses for plugins and add-ons.

If you decided to keep the default Vim and your compiled Vim you can check that they are two different versions easily with the --version flag:
 
vim --version
./vim --version


THE DEVELOPMENT ENVIRONMENT

I recommend reading
Tools of the Modern Python Hacker: for the installation and usage of virtualenv, fabric and pip, i don't use fabric but i know i will when i start deploying.
The main issue we will solve here is how to fix the autocompletion for django and python, the problem is that we need to add some enviromental variables for each file, lets create that file:
sudo ./vim /usr/local/bin/vi

We created a file called vi, this is how you can keep 2 versions of Vim, one is called with Vim and your compiled one with Vi.
Now, read this code and paste it on our newly created file.

#!/bin/bash
export PROJECT=`python -c 'import os; print os.path.basename(os.getcwd())'`
export PYTHONPATH="/home/grillermo/c/bandtastic/python/"
export DJANGO_SETTINGS_MODULE=$PROJECT.settings
/home/grillermo/apps/vim73/bin/vim $@
You have to customize a couple of things, first thing to know is that i keep all my django projects in /home/grillermo/c/bandtastic/python/ . Right now i'm working on /home/grillermo/c/bandtastic/python/dev , my project is called dev i cd to that path and then i open files like this
vi manage.py

And if i have a django app called polls i would open its model like this
vi polls/model.py
Why you need to know this? because this is the workflow you want, you want to keep all your django projects in one folder in my case is /home/grillermo/c/bandtastic/python/ and inside that folder create a folder for each project and do not cd into any subdirectories, because otherwise the script we created won't work.
Ok if you already customized the vi file, now save it and go back to the terminal you'll need to give it execution privileges.
sudo chmod +xX vi
And thats it, if you wan't to test if this worked go to the folder you keep all your django projects type
vi
And then type
:python from django import db
If you don't see any errors, your autocompletion will work now.

THE PLUGINS

In the interest of doing the plugin part of this post as simple as posible and if you want all the features i talked about at the benning of this posts i made a small zip with all the plugins so you can just extract the zip on your vim73/share/vim/vim73 folder,
The zip [MU]

Note: you'll probably want the latest version of the plugins, this zip file has the newest version up to the time of this post September 11, 2011 , links are provided to each plugin's home to get the latest version.

You can the plugins installation using nautilus or your favorite file browser.
Lets review them one by one, so you'll know exactly what you are getting.

PATHOGEN

This plugin is basic to avoid littering your plugins directory, it allows you to put the plugins in their own folders so if you don't like one, instead of hunting down the many files some plugins have scattered in different folders you just delete one folder. This plugin works by expecting you to make a folder inside vim73/share/vim/vim73/bundle for each plugin where you can extract the plugin's zip file content. Very handy for trying out different plugins.

To install it
put the pathogen.vim in your vim73/share/vim/vim73/autoload/ folder.
In the .vimrc customization section of this post you'll find the line needed to actually enable it.
Create the folder bundle in your vim73/share/vim/vim73/ folder so we can put the rest of the plugins there.
Update:

Solve the problem with the help tags rebuilding functionality of pathogen.

NEOCOMPLCACHE

This is the all mighty autocompletion plugin, it works along side the omnicompletion feature include since vim 7 to provide autcompletion with the key on your keyboard, when you type a word and then hit tab it will autocomplete with the best choice, while editing a .py file if you type:
str.
You'll get a popup window with the available methods for the str object. The cool thing about this is that it'll work with django objects so for example when creating a model you'll see in a nice pop up with all the posible model fields and autocomplete them with .

To install it create create a folder(the name doesnt matter) inside bundle and extract the contents of neocomplcache-6.1.zip file on that folder.
And you'll get the line needed to enable it on the .vimrc section of this post.
You can study the different options for this plugin here, but only adding the line i provide its enough.

PYFLAKES
This is the sintax error highlighting plugin for python, it marks a line with red if the line is not valid python or if an import is not being used.

To install it create a folder inside bundle and extract the contents of pyflakes-vim.zip there.
No need to add lines to .vimrc to enable it.

AUTOCLOSE
This plugin as its name suggests it will auto close your parens ( or your brakets [ and put the cursor inside them, it will work with quotes ' and double quotes " too, pretty basic for every coder.

To install it copy autoclose.vim to vim73/share/vim/vim73/plugin , for this plugin we don't use the bundle folder because its only one file to install or remove.

COMMAND-T
This is a very intuitive file opener, it will autocomplete paths and show you the file thats the most likely to what you are writting, go to the site and see its amazing features.

The installation
for this one is different because by default the author provides a vimball and vimballs break the pathogen usage, so we have to install it differently using git, if you don't have git a simple

sudo apt-get install git
Is enough. Now cd to to your bundle dir and do
git clone git://git.wincent.com/command-t.git command-t 
Since this is a plugin with ruby, we need rake to compile it,
 sudo apt-get install rake

Then cd to the command-t folder and do
rake make
While the Vimball installation automatically generates the help tags, under Pathogen it is necessary to do so explicitly from inside Vim:
:call pathogen#helptags()

*This installation instructions comes directly from the docs of the plugin.
To see this plugin in all its awesomness go to the authors website.

NERDTREE
This plugin is a file and directory browser, you'll be editing multiple files and this plugin will be useful if you are not sure whats the name of the file or in which dir is it. It does have a learning curve and you'll need to learn a few hotkeys. But it is way faster than going to your file browser looking for the file and then going back to vim to write all the path to the file to open it.

To install it, create folder in bundle and extract the contents of the zip.
In the plugin's wiki you'll find all you need to use it.

SURROUND
Visit the plugins page right now.
You'll need this plugin in python if you usually get into deeply nested functions or to quickly convert a tuple with hardcoded items to a list or a double quoted string to single quote.

To install
it make a folder for it inside the bundle dir and extract the surround.zip in it.
And then learn its commands, i found that if i say the commands in my mind i remember them, the plugins page's first example goes like this:

Press cs"' (that's c, s, double quote, single quote) inside
"Hello world!"
Now, while typing cs"' you can say in your mind: change surrounding double quote into quotes following the typing and you'll have no trouble remembering them.

TCOMMENT
Of all the plugins to comment code out there i found this the easiest to use, you just type ctrl _ thats ctrl shift - , on american keyboards and you comment the line in the cursor, and you use the same hotkey with a visual block selected. If you found another thats simpler than that let me know.
To install make a folder in bundle and extract tcomment_vim.zip in it.


SIMPLYFOLD
There are many folding plugins, but this one is the closest to get it right, the author has fixed a couple of bugs i reported really quickly, he seems determined to make it the best, and it shows. Save your self all the trouble and use it.

THE VIMRC

Now lets begin customizing vim, the best way to do it is first showing you my .vimrc exactly as it is. Lets remember that file is the per-user config file vim uses for any user customizations, every Vim user has this file heavly edited. It lives in ~/.vimrc

.vimrc
I'm linking to the file because blogger tends to do ugly stuff to ' " and other characters. Open it in a text editor, see the lines you are interested and add them to your own .vimrc , in the PLUGINS area you'll see the lines needed to enable neocomplcache and pathogen plugins.




TROUBLESHOTTING
 
One particular problem you'll face is the that the autocomplete function won't work if you Vim doesn't see the DJANGO_SETTINGS_MODULE environmental variable, this used to require hacks and nasty work arounds, but today i found a nice plugin for Vim that handles all those ugly details.

FINAL NOTES






Autocomplete will work thanks to neocomplcache, but using it you won't get the docstrings of each method and class, i leave it like this because the docstrings are displayed in a new window inside Vim and that can be annoying, you can access only when you need them using ctrl+space.
Vim is all about productivity i suggest you keep open the Vim documentation all the time while you are learning.

#TODO:
I'm still working on improving my Vim installation but i set out a personal policy to only spend time customizing Vim when i actually need it, because you don't want to over do customizations and never get actual work done, thats why i HAVE NOT worked on adding these features:

  • Code folding.
  • Quick navigation through classes and defs.
  • Django template system highlighting and autocompletion.
  • Integrating django commands on my work flow, like deploying and testing.
  • Help in running tests on my code.
  • Help when refactoring code.

I'll update this post as i add them.

Please comment and ask freely i want to know what issues you have so i can update the post.



References:

 

 


sábado, 13 de agosto de 2011

HOW TO FIX No module named pywintypes



THE ERROR
Is cause because there is a cluster fuck of references to microsoft dlls on the python win32 module and for random reasons the modules.py lost track of them.

THE FIX
so this particular error i got it fixed copying the file C:\Python31\Lib\site-packages\pywin32_system32\pywintypes31.dll to C:\Python31\Lib\site-packages\win32\lib

Thats for python 3.1 for 3.2 just look for pywintypes32.dll.

Good luck

miércoles, 10 de agosto de 2011

Vim cheatsheet vertical



Hi, i found an understandable Vim Cheatsheet and i wanted it vertical so i did it, here i share it with you guys.

All credits to Andrea Benazzo www.qitty.net

viernes, 29 de julio de 2011

SOPORTE LABSIA

Hola, en esta página podrás mantener contacto conmigo si trabajas en LABSIA, aquí podrás preguntar todo lo que quieras y procuraré responder rápido y con lo mejor de mis conocimientos.

Sobre las máquinas:
Algunas fuentes de poder tienden a "bloquearse" previniendo que la computadora encienda, apaga el switch de la fuente y espera un par de horas y luego vuelve a encenderlo, la computadora debería prender.

Sobre jefeRemoto:
Como sabrás escribí un pequeño programa para mandar ejecutables y archivos en masa a todas las computadoras, este programa está instalado en todas las máquinas en los 3 windows, menos en las nuevas Macs. El controlador está instalado solo en la maquina de Valentín y solo el puede enviarles archivos, usando varias técnicas de seguridad.

lunes, 4 de julio de 2011

Desinstalar programas en windows usando python

Buscando una solución en Python para automatizar la desinstalación de programas en Windows me encontré con este post donde el autor comparte algunas funciones para leer datos del registro de Windows, después me encontré con este artículo de Microsoft donde podemos ver que Windows guarda en el registro los datos para desinstalar aplicaciones específicamente en:
HKLM/SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Con la sorpresa de que la mayoría de los instaladores agregan una llave en el registro que se llama uninstallstring, esta llave es tal cual la línea de comandos que ejecutará para desinstalar el programa en cuestión.
Ya puesto todo junto el script:
Requerimientos
el módulo wmi(que permite administrar windows) para python
Y ya, el otro módulo viene incluido en la librería estandar de python desde 2.4
Para este código supongamos que queremos desinstalar todos los programas de Adobe
Tomamos las funciones que nos hizo favor de compartir Thejaswi Raya


Podemos utilizarlas para ver una lista de todos los programas instalados asi


Y luego si quisieramos guardar las uninstallstrings para poder usarlas después las guardamos en una lista modificando el código anterior asi:



Después podemos iterar sobre esa lista de strings ejecutando cada una asi:


Según la documentación oficial y la comunidad no deberíamos usar system.os(comando) para ejecutar cosas, está depreciado sin embargo la alternativa propuesta del módulo subprocess, Popen(comando) no bloquea nuestro script usualmente es lo que queremos, que el código siga ejecutando una vez se dispare nuestro comando de sistema, sin embargo en este caso no, porque los desinstaladores necesitan ser únicos, surgen errores si mas de un desinstalador intenta ejecutarse al mismo tiempo por lo que en este caso necesitamos esperar a que el desinstalador acabe, osea regrese algún código de error a system.os() para que se ejecute el siguiente.



Averiguando mas sobre el tema me entere de que algunos programas implementan una clave adicional que se llama QuietUninstallString que nos ahorraría el problema de los desinstaladores que necesitan clicks, no se que tan común sea esta clave pero me temo que no mucho, en todo caso podemos automatizar los clicks usando el fabuloso proyecto
Sikulli

cheers!

jueves, 26 de mayo de 2011

Copiar automáticamente al portapapeles la url pública de archivos agregados a dropbox

Hola, hoy les traigo un pequeño script que utilizo para copiar al portapapeles la url pública de algún archivo que agrego a mi carpeta pública de dropbox, se copia en cuanto el archivo se crea o actualiza en el sistema de archivos, no cuando termina de subirse.
Antes de darles el script les dejo las dependencias:

Windows (versión de Linux sobre pedido)
python32 (para evitarme los problemas con archivos que tienen ñ o acentos)
pywin32 (manejar el portapeles)
Si llegas a tener problemas al importar pywin32 checa este post donde pongo la solución.



Solo tienen que personalizar las primeras 2 lineas, obtén el número de tu cuenta de dropbox desde la url pública de algún archivo que ya estes compartiendo.
Les dejo el código en licencia GPL
El código de la función que monitorea cambios en el sistema de archivos no es mio es de Tim Golden

sábado, 14 de mayo de 2011

Hoy traigo un script de python que cuenta el número de veces que se repite un alguna extensión de archivo(.txt .exe) en todo mi sistema de archivos, tenía curiosidad.
Este script devuelve los 30 mas comunes.




Debo confesar que la parte ordered no es código mio, aun no entiendo bien como usar lambdas en python solo se que sorted devuelve (en este caso) una lista ordenada de tuplas de menor a mayor dependiendo de los valores en el diccionario.

Update:
Ya entendí como funciona aquí la función sorted.
Vamos a deshuesar la función

Primero veamos, sorted necesita un parametro obligatorio, un iterable, en este caso estamos usando el metodo items de nuestro diccionario lo convertimos en una lista de tuplas, y usaremos esta lista de tuplas como iterable, considera este código

Esa es la lista de tuplas que usará sorted().
Ahora, a sorted() le queremos decir que nos ordene por el elemento [1] de la tuplas osea

El elemento 1 es el número de veces que se repite la extensión porque las tuplas en el caso real se ven asi ('.txt',9318) ok?
Ok ya entendemos porque x[1]
Entendamos porque lambda
Lambda crea funciones en una sola linea, estas son funciones completas que acepta parametros y devuelven algo, y resulta que el parametro key necesita ser obligatoriamente una función por eso no podemos ponerle simplemente

Porque eso no seria una función sino un int(el numero de veces que se repite .txt es un int), necesitamos una función entonces creamos una función a la que sorted() le va a enviar como parametro el iterable en cuestión es decir una ('.txt',9318) y esta función va a decir que ('.txt',9318) ahora es = x, y que de x queremos return x[1]
es como si pusieramos

Donde x es la tupla en cuestión.

Lindo no? cheers humanos!


Si tienen curiosidad este es el resultado

9550 archivos .py! yay!

lunes, 7 de marzo de 2011

Fixing pywin32 dll error

So, you installed pywin32 and you wonder why is this happening

ImportError: DLL load failed: The specified module could not be 
found.


when you do
import win32api
or try to import any of the modules included
I have no idea why, my googling lead me to some people talking about dll dependency issues,
 the solution is brute force copying and pasting dlls in the folder
C:\Python32\Lib\site-packages\pywin32_system32
and in
C:\Windows\system32
I’m not sure wich of these dlls fixed the issue so I paste them all for you to do the same, 
it can’t hurt since you won’t replace any(when asked say no)

hmmapi.dll
iecompat.dll
iedvtool.dll
ieproxy.dll
IEShims.dll
iessetup.dll
jsdbgui.dll
jsdebuggeride.dll
JSProfilerCore.dll
jsprofilerui.dll
msdbg2.dll
pdm.dll
python32.dll
pythoncom32.dll
pythoncomloader32.dll
pywintypes32.dll
sqmapi.dll


So just copy all these to both folders without replacing any of the existing dlls, and you should be fine.

miércoles, 23 de febrero de 2011

Manipulating iTunes library with Python 3

I wanted to fix some songs in my iTunes library that I rated low but somehow the last.fm scrobbler managed to get them unusually high play counts, so I searched a bit on google for iTunes Python and found this script that was written to remove dead tracks. I worked on it to build the script I needed and with the help of the iTunes Windows COM interface SDK(downloaded from the apple’s developer site) I got a working script.

If you want the iTunes COM documentation and you don’t want to register here it is the help file.

And of course the code.

 

# this programs evaluates each song in an iTunes.xml file and reduces de number
# of play counts of the song if the song is rated with certain ammount of stars
# and the song has a determined ammount of play counts.

import win32com.client

STARS = 20,40 #stars the song has to be an offender, 1 star = 20(internally)
MAX_PLAYS = 55 #minimum number of playcounts the song has to have to be an offender
RED_PERCENT = 90 #the ammount the played count will be reduce, 90%

#First, we create an instance of the iTunes Application
itunes= win32com.client.Dispatch("iTunes.Application")

#please consult the hierarchy used here in the iTunes COM interface.chm
mainLibrary = itunes.LibraryPlaylist
NumTracks = mainLibrary.Tracks.Count

for track in range(1,NumTracks):
    currTrack = mainLibrary.Tracks.Item(track)
    if currTrack.Rating in STARS:
        if currTrack.PlayedCount >= MAX_PLAYS:
            print ("the song "+str(currTrack.Name)+" by "+str(currTrack.Artist)+" has been played "+str(currTrack.PlayedCount)+" times with a rating of "+str(currTrack.Rating/20))
            currTrack.PlayedCount = int(currTrack.PlayedCount*0.2)
            print ("new played count is "+str(currTrack.PlayedCount))

For this code to work you need to install the win32 module, get the latest build it from here.

I ran into some weird bug with Python 3.1.3, bug and fix here.

lunes, 21 de febrero de 2011

Pymes mexicanas crecen gracias a la adopción de Software Libre

Artículo original:
http://vivirmexico.com/2011/01/pymes-mexicanas-crecen-gracias-a-la-adopcion-
de-software-libre


Copia del artículo

Pymes mexicanas crecen gracias a la adopción de Software Libre

Por Hugo Torres el 29 de Enero de 2011 en Empresas, Tecnología.

Las Pymes mexicanas tienen cierto rezago en la adopción de tecnologías. Según un estudio elaborado por VISA-Nielsen en América Latina, en México solo el 25 por ciento de las medianas empresas utiliza el Internet en sus procesos y tan solo el 10 por ciento tiene una página web para acercar sus productos o servicios a más potenciales clientes.

Según los especialistas, la incorporación de software en las empresas es un fuerte gasto debido a los altos costos de las licencias de los programas. En una empresa promedio, el uso de tecnología puede significar hasta un 18 por ciento de su presupuesto; cantidad que limita a más de una empresa a la adopción de tecnologías del mercado tradicional.

Debido a esto, según la Fundación México Estados Unidos para la Ciencia (Fumec), la adopción de Software Libre (u open source) en las Pymes mexicanas está ganando terreno, por lo que calculan que este 2011 su uso crecerá hasta un 40 por ciento con respecto al año anterior:

Estas herramientas benefician a las empresas que no están totalmente estructuradas y le ponen orden a sus procesos con una imagen más innovadora. Las empresas que incorporan estas aplicaciones son más rentables porque pueden acceder a un ámbito global, por lo que esperamos un crecimiento del 40 por ciento de estas aplicaciones libres en la industria nacional en 2011.

El Software Libre no es precisamente aquel programa que es gratuito, sino el que su licencia da la libertad a sus usuarios de usar, copiar, distribuir, estudiar, modificar el software e incluso venderlo.

Estos números son muy alentadores para las comunidades de Software Libre en México y puede abrirse una puerta más para presionar a nuestro Gobierno para que haga uso de este tipo de software en las diferentes administraciones, tal como la Comisión Federal de Electricidad (CFE) lo ha estado haciéndolo desde hace casi 20 años y así como lo prometió Carlos Navarrete.

Sin embargo, este tipo de software no es la única alternativa. La Asociación Mexicana Empresarial de Software Libre (Amesol) calcula que el 67% de las empresas en México usan algún software pirata. Algo muy común en nuestro país aunque no muy correcto que digamos.


Comentarios por: Guillermo Siliceo Trueba

Adopción de software libre: enséñelo en las universidades y mate usted dos pájaros de un tiro para el país: uno: le ahorra al país el costo de importar licencias y disminuya el uso de software pirata.


Las empresas ya lo están demandando, las soluciones libres ya están siendo implementadas y cada vez más empresarios se dan cuenta de sus ventajas de costo. ¿Cómo podemos reducir aun más estos costos y proveerles valor con nuestros egresados? Ahorrémosle la capacitación, graduemos estudiantes diestros en soluciones pre-existentes, preparados para modificarlas según sus necesidades y enseñémosles a estos estudiantes el etos del desarrollo de software libre, estrictamente ligado al desarrollo en equipos a distancia.


Las ventajas de dinero son claras,¿pero que hay del etos del desarrollo de software libre? Quiero abundar un poco más sobre este último punto pues el modelo de desarrollo de software libre es muy distinto al empresarial en cuanto que en el primero los individuos trabajan independiente uno de los otros creando código y valor para el software del proyecto de manera tentativa, es decir, el programador de software libre TIENE que ser propositivo, tiene que proponer características y arreglos de bugs para integrarse al proyecto y estas propuestas son evaluadas por la comunidad y el equipo pre-existente de desarrollo(osea tiene que ser muy bueno para ser aceptado) en cambio en el desarrollo comandado por un jefe de área o programador en jefe es vertical, el programador tiene que ser reactivo, hace lo que se le dice y nada más(su código puede ser malo pero si cumple las fechas de entrega, se acepta).


El contraste en la mentalidad que estos dos estilos crea en el programador no podría ser mayor, en uno se invita a la creatividad al pensamiento independiente, no se fuerza al programador a integrarse a un proyecto sino que se le invita a colaborar pero solo si lo que produce es una solución de calidad(pues su código es revisado por mucha, mucha mas gente y no solo su jefe o supervisor), esto crea un marco para las interacciones sociales entre los programadores mucho mas amigable y compensadora en un proyecto de software libre que él se crea por la fuerza del trabajado pagado y las fechas límite como sucede en las empresas.


Entonces hay que preguntarse, si las empresas mexicanas cada vez lo demandan más(como lo dice el artículo) ¿Qué tipo de soluciones queremos darles desde las universidades? ¿Empleados preparados para obedecer(y hacer el mínimo necesario por el cheque) o trabajadores independientes que ofrezcan soluciones innovadoras escrutadas por la comunidad? No acaso queremos proponerle cosas al mundo usando software libre, desarrollando sobre él, inyectándole trabajo mexicano a las soluciones existentes, creando nuevas, que el mundo sepa que también podemos contribuir mientras nos beneficiamos con los frutos del trabajo de otros, que los egresados sean consultores externos o trabajadores en nómina, es una gran diferencia.

sábado, 19 de febrero de 2011

Python uno de los lenguajes mas populares

Fuente original:
http://www.infernodevelopment.com/python-becoming-most-popular-programming-language

Copia del artículo

Python is One of the Most Popular Programming Languages

Python is quickly taking the lead as one of the most popular programming language. You might be skeptical, but Python is growing at a faster rate than any other language. It received TIOBE Programming Language of the Year award twice, once in 2007, and again in 2010. Even now it still has the highest growth of any other language. What makes Python so great?

While statistical analysis always has its flaws, the trends are pretty clear. Java, C#, even PHP are losing popularity, Python, Ruby, Haskell, Ada are gaining popularity, and even Google Trends seems to agree.

Python is an interpreted language, like PHP, Ruby, and Perl. While PHP was very popular throughout the 2000s. Recently PHP and Python have become equally as popular. Now they are neck in neck. Both of them have enough features to be capable of anything. Some subtle differences however, make Python more amazing than PHP.

The Competition

PHP is one of the main competitors to Python's dominance, but mostly in the web development arena. PHP is very integrated with web development and can be used for it right out of the box like ASP. How does Python compete with that? Well Python has many web frameworks whose features so outperform PHP that it would be crazy to take on huge custom projects with PHP instead of Python and a framework like Django or Plone.

For example, Django's features allow for model generation using OO design philosophy and immediate database table generation based on your Python code.

I myself, am a Drupal PHP developer. Drupal is by far my favorite web framework. The modular-hook design and constant major updates (without backwards compatibility) were at first very attractive, but now I've become dissatisfied with it after being involved in a major custom-designed Drupal project. I find it superior to Wordpress in every way, but it's hard to customize it without some work.

Perhaps you didn't know this, but InfernoDevelopment is designed with Drupal. However, in our next redesign, our developers are planning on using Python and Django. We've started a huge project to release IDv3 in our secret forums, which includes volunteers who are interested in the project.

The fact that Python has a framework like Django that is dominant in web development is beneficial to Python. Standardization is needed for success. While competition is great for PHP, with the many frameworks like CodeIgniter, CakePHP, Drupal, and Zend, they also show a lack of superiority of any one API.

Some people may argue that Ruby and Perl might pose competition as well. I think they lack elegance. Ruby is a solid language but does have scaling problems. I have studied Perl and use it in Linux quite often, but I don't think it has the potential to continue its dominance.

What's great about Python?

Python is extremely well documented. The only other documentation I've seen that rivals it would be the C++ Qt framework. Such easy documentation allows more people to quickly adapt to the language.

Python's language design is incredibly well thought out. PHP's design however, seems to have been duck taped together over years of progress with little regard to design. PHP is basically a language that just seems to grow in the size of its function list. It eliminates the need to be OO. While PHP5 introduced a lot of OO features, it is still not widely used and some frameworks don't really utilize its features.

Python encourages good coding skills. It's exception and error handling is much superior to PHP or C#. Debugging is easy in Python. Difficult in C# (especially when ASP is involved), and somewhat stressful in PHP (whitescreens definitely don't help).

Python has an extensive arsenal of libraries that are extremely important for any mature language.

Python removed the brackets. At first I felt unsafe without the brackets. I felt like relying on white space or tabs would be dangerous. I've come to realize that it actually makes the code more elegant and less confusing. I already indent everything in PHP, C#, C++, so why bother including brackets?

Who's using Python and how will it be used in the future?

Python is being used extensively by Google (with some services using C++ for resource intensive tasks). Artificial Intelligence classes all over the US have adapted Python and dropped LISP.

Some popular sites that use Python:
Google, YouTube, Reddit (Pylon), Facebook (PHP & Python), FriendFeed, Tabblo (Django), SuggestionBox (Django), Bitbucket (Django), Disqus, Quora.

Corporations have begun to shed themselves of compiled languages for web development. The reason being is, compiling all the time is quite an annoyance for a lot of projects. Though you can use C# ASP.NET applications without compiling constantly if it is a website project, the amount of errors it throws on just about anything and its inconsistency is troubling.

I see the trend of Python's popularity continuing to grow especially in web development, AI, and game programming (Civilization uses Python for Civ4). C and C++ will definitely continue to be leaders because of their efficient use of resources and low level programming capability.

What do you think? Do you believe this trend will continue or am I just speculating too much?

Comentarios por: Guillermo Siliceo Trueba
Lo mas relevante del artículo son los "datos duros" :

Usuarios de Python: Google, YouTube, Reddit (Pylon), Facebook (PHP & Python), FriendFeed, Tabblo (Django), SuggestionBox (Django), Bitbucket (Django), Disqus, Quora.
Los nombres hablan por si solos, como el artículo no incluye fuentes, he obtenido algunas citas relevantes de boca de ingenieros que trabajan en estas empresas, link

"Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect, YouTube.com

"Python has been an important part of Google since the beginning, and remains so as the system grows and evolves. Today dozens of Google engineers use Python, and we're looking for more people with skills in this language." said Peter Norvig, director of search quality at Google, Inc.

"Python plays a key role in our production pipeline. Without it a project the size of Star Wars: Episode II would have been very difficult to pull off. From crowd rendering to batch processing to compositing, Python binds all things together," said Tommy Burnette, Senior Technical Director, Industrial Light & Magic.
Me parece importante este punto porque una de las razones por las que una Universidad debe enseñar alguna habilidad es porque esta está demandada por la industria, y aunque aquí este citando industrias norte americanas estas son las que suelen imponer las modas y los estandares en los paises en vias de desarrollo como lo es México.

El otro dato relevante que menciona el artículo es el índice de la comunidad de programación por TIOBE, que es un indicador de la popularidad de los lenguajes de programación. El índice es actualizado una vez al mes. Los ratings están basados en un número de ingenieros de todo el mundo, cursos y vendedores terceros. Los motores de busqueda populares como Google, Bing, Yahoo!, Wikipedia, Youtube y Baidu[el google chino] son utilizados para calcular los raitings.*
Este índice pone a Python en 4º lugar arriba de PHP, C# y abajo de C, C++ y Java, lo que es impresionante por si mismo.

Recordando mi artículo anterior sobre las razones para enseñar Python el artículo que aquí comento incluye algunas de ellas y además habla de que Python motiva el uso de buenas prácticas de programación, lo bien documentado que está.

Como una nota aparte quiero explicar mi interés por Python.
Mi interés en Python surgió cuando investigaba la manera mas fácil de hacer un script que necesitaba cuando me estaba mundando de Windows a Linux(migrar mi iTuneslibrary.xml a Rhythmbox), me di cuenta lo fácil que era pero no le preste mas atención hasta unos años después que me dijeron que me iban a enseñar programación, y dije "hey yo ya conozco algo con que programar". Para cuando mis primeras tareas de programación llegaron en las que se me pedía pseudocódigo pensé en el gran parecido del pseudocódigo a lo que ya había usado y todas las tareas que entregué las resolvía en Python primero y ya que sabía que mi solución funcionaba entonces si la transcribía a C ó a DFD, simplemente porque era mas fácil. Viendo las dificultades de mis compañeros para resolver los temas que recientemente hemos visto en Programación 1(puro C ANSI) me fue natural pensar en Python como una manera de hacerles mas fácil la vida.

*Traducción de la descripción que TIOBE mismo hace de su índice. aquí