python

How to define a system-wide alias for a Python script?

I am working on Mac OS X and I have a Python script which is going to be called by other scripts and programs (Apple's launchd in particular). I could call it with python /Users/xyz/long/absolute/path/to/script.py arg1 arg2 Since the location of the script might change, I want to decouple other scripts and the launchd configuration fi...

How do I calculate r-squared using Python and Numpy?

I'm using Python and Numpy to calculate a best fit polynomial of arbitrary degree. I pass a list of x values, y values, and the degree of the polynomial I want to fit (linear, quadratic, etc.). This much works, but I also want to calculate r (coefficient of correlation) and r-squared(coefficient of determination). I am comparing my re...

Python PySerial readline function wrong use

I'm using a script importing PySerial to read from COM4 messages I would like to intercept end with a couple of # so I tried to use bus.readline(eol='##') where bus is my connection. I expected to read like: *#*3## *#*3## *#*3## Unfortunalyy I found also *#*1##*1*1*99## that I expected to read spleetted into 2 lines *#*1...

Python: Stopping miniDOM from expanding escape sequences

When xml.dom.minidom parses a piece of xml, it automagically converts escape characters for greater than and less than into their visual representation. For example: >>> import xml.dom.minidom >>> s = "<example>4 &lt; 5</example>" >>> x = xml.dom.minidom.parseString(s) >>> x.firstChild.firstChild.data u'4 < 5' Does anyone know ...

PyQt: Show menu in a system tray application

Hello First of all, I'm an experienced C programmer but new to python. I want to create a simple application in python using pyqt. Let's imagine this application it is as simple as when it is run it has to put an icon in the system tray and it has offer an option in its menu to exit the application. This code works, it shows the menu (...

How do I get the current file, current class, and current method with Python?

Hello, could i get: Name of the file from where code is running Name of the class from where code is running Name of the method (attribute of the class) where code is running Thanks. ...

More Pythonic conversion to binary?

Here is a contrived example of how a lot of our classes return binary representations (to be read by C++) of themselves. def to_binary(self): 'Return the binary representation as a string.' data = [] # Binary version number. data.append(struct.pack('<I', [2])) # Image size. data.append(struct.pack('<II', *self....

Programmatically make HTTP requests through proxies with Python

How do I use Python to make an HTTP request through a proxy? What do I need to do to the following code? urllib.urlopen('http://www.google.com') ...

How does setuptools decide which files to keep for sdist/bdist?

I'm working on a Python package that uses namespace_packages and find_packages() like so in setup.py: from setuptools import setup, find_packages setup(name="package", version="1.3.3.7", package=find_packages(), namespace_packages=['package'], ...) It isn't in source control because it is a bundle of upstream components. T...

Multi-server monitor/auto restarter in python

I have 2 server programs that must be started with the use of GNU Screen. I'd like to harden these servers against crashes with a Python based program that kicks off each screen session then monitors the server process. If the server process crashes, I need the python code to kill the extraneous screen session and restart the server with...

Has Python changed to more object oriented?

I remember that at one point, it was said that Python is less object oriented than Ruby, since in Ruby, everything is an object. Has this changed for Python as well? Is the latest Python more object oriented than the previous version? ...

Unable to replace the word in a given folder's contents by Sed/Python/Perl

I have a project where I have folders, subfolders, and files. I need to replace the word Masi by the word Bond in each files. I run the following Sed script called replace unsuccessfully s/Masi/Bond/ in Zsh by sed -f PATH/replace PATH2/project/** It gives me all files, also the ones which do not have Masi, as an output. Sed is n...

How can I launch a python script on windows?

I have run a few using batch jobs, but, I am wondering what would be the most appropriate? Maybe using time.strftime? Big noob here, thanks in advance. ...

Circular dependency in Python

I have two files, node.py and path.py, which define two classes, Node and Path, respectively. Up to today, the definition for Path referenced the Node object, and therefore I had done from node.py import * in the path.py file. However, as of today I created a new method for Node that references the Path object. I had problems when ...

CreateDatabase often fails on the google data api

The following test program is suppossed to create a new spreadsheet: #!/usr/bin/python import gdata.spreadsheet.text_db import getpass import atom import gdata.contacts import gdata.contacts.service import smtplib import time password = getpass.getpass() client = gdata.spreadsheet.text_db.DatabaseClient(username='[email protected]',passwo...

Compiled Python CGI.

Assuming the webserver is configured to handle .exe, Can i compile a python CGI file into an exe for speed. What would some pros and cons be to such a desession? ...

Bubble Sort Homework

In class we are doing sorting algorithms and, although I understand them fine when talking about them and writing pseudocode, I am having problems writing actual code for them. This is my attempt in Python: mylist = [12, 5, 13, 8, 9, 65] def bubble(badList): length = len(badList) - 1 unsorted = True while unsorted: f...

What if I want to store a None value in the memcache?

This is specifically related to the Google App Engine Memcache API, but I'm sure it also applies to other Memcache tools. The dictionary .get() method allows you to specify a default value, such as dict.get('key', 'defaultval') This can be useful if it's possible you might want to store None as a value in a dictionary. However, the m...

Using python to develop web application

I have been doing some work in python, but that was all for stand alone applications. I'm curious to know whether any offshoot of python supports web development? Would some one also suggest a good tutorial or a website from where I can pick up some of the basics of web development using python? ...

Django App Dependency Cycle

I am in the middle of developing a Django application, which has quite complicated models (it models a university - courses, modules, lectures, students etc.) I have separated the project into apps, to make the whole thing more organised (apps are courses, schools, people, modules and timeperiods). I am having a problem whereby a model ...