python

How to leave a python virtualenv?

I'm using virtualenv and the virtualenvwrapper. I can switch between virtualenv's just fine using the workon command. me@mymachine:~$ workon env1 (env1)me@mymachine:~$ workon env2 (env2)me@mymachine:~$ workon env1 (env1)me@mymachine:~$ However, how do I exit all virtual machines and workon my real machine again? Right now, the only w...

Reclassing an instance in Python

I have a class that is provided to me by an external library. I have created a subclass of this class. I also have an instance of the original class. I now want to turn this instance into an instance of my subclass without changing any properties that the instance already has (except for those that my subclass overrides anyway). The fo...

Closing file opened by ConfigParser

I have the following: config = ConfigParser() config.read('connections.cfg') sections = config.sections() How can I close the file opened with config.read? In my case, as new sections/data are added to the config.cfg file, i update my wxtree widget, however, it only updates once, and i suspect it's because config.read leaves the file...

String reversal in Python

I have taken an integer input and tried to reverse it in Python but in vain! I changed it into a string but still I am not able to. Is there any way to reverse it ? Is there any built-in function? I am not able to convert the integer into a list so not able to apply the reverse fuction. ...

how to find out a mod b in python?

is there any modulo function in math lib in python? isn't 15 % 4 3?? but 15 mod 4 is 1, right?? ...

an error in taking an input in python

111111111111111111111111111111111111111111111111111111111111 when i take this as input , it appends an L at the end like this 111111111111111111111111111111111111111111111111111111111111L thus affecting my calculations on it .. how can i remove it? import math t=raw_input() l1=[] a=0 while (str(t)!="" and int(t)!= 0): l=1 k=...

Deleting files by type in Python on Windows

I know how to delete single files, however I am lost in my implementation of how to delete all files in a directory of one type. Say the directory is \myfolder I want to delete all files that are .config files, but nothing to the other ones. How would I do this? Thanks Kindly ...

Counting repeated characters in a string in Python

I want to count the number of times each character is repeated in a string. Is there any particular way to do it apart from comparing each character of the string from A-Z and incrementing a counter? Update (in reference to Anthony's answer): Whatever you have suggested till now I have to write 26 times. Is there an easier way? ...

re.search() breaks for key in form.keys() loop

Perhaps I am just crazy or missing something really basic. Why would this happen? If I use this url index.cgi?mode=pos&pos_mode=checkout&0_name=Shampoo&0_type=Product&0_price=4.50&0_qty=1&0_total=4.50 which runs this code form = cgi.FieldStorage() for key in form.keys() print key if re.search("name", key): print "F...

Code reuse between django and appengine Model classes

I created a custom django.auth User class which works with Google Appengine, but it involves a fair amount of copied code (practically every method). It isn't possible to create a subclass because appengine and django have different database models with their own metaclass magic. So my question is this: is there an elegant way to copy...

Very simple Python script, puzzling behaviour.

Hi, first of all, I'm not a programmer, so the answer to this might be completely obvious to someone more experienced. I was playing around with python(2.5) to solve some probability puzzle, however I kept getting results which were way off from the mark I thought they should be. So after some experimenting, I managed to identify the beh...

Changing brightness of the Macbook(Pro) keyboard backlight

Programaticly, how can I modify the brightness of the backlit keyboard on a Macbook or Macbook Pro using Python? ...

Creating a doctype with lxml's etree

I want to add doctypes to my XML documents that I'm generating with LXML's etree. However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option. I was expecting something along the lines of how PI's are added in etree: pi = etree.PI(...) doc.addprevious(pi) But it's not working for me. How ...

Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?

I'm hoping someone can provide some insight as to what's fundamentally different about the Java Virtual Machine that allows it to implement threads nicely without the need for a Global Interpreter Lock (GIL), while Python necessitates such an evil. ...

What is this function doing in Python involving urllib2 and BeautifulSoup?

So I asked a question earlier about retrieving high scores form an html page and another user gave me the following code to help. I am new to python and beautifulsoup so I'm trying to go through some other codes piece by piece. I understand most of it but I dont get what this piece of code is and what its function is: def parse_stri...

Why are 008 and 009 invalid keys for Python dicts?

Why is it that I can't have 008 or 009 be keys for a Python dict, but 001-007 are fine? Example: some_dict = { 001: "spam", 002: "eggs", 003: "foo", 004: "bar", 008: "anything", # Throws a SyntaxError 009: "nothing" # Throws a SyntaxError } Update: Problem solved. I wasn't aware that starting a literal wi...

Can client side python use threads?

I have never programed in Python before, so excuse my code. I have this script that will run in a terminal but I can't get it to run client side. I am running this in Appcelerator's Titanium application. Anyway, I have been troubleshooting it and it seems that it isn't running the threads at all. Is this a limitation? does anyone know? ...

Building a Python shared object binding with cmake, which depends upon external libraries.

Hi, We have a c file called dbookpy.c, which will provide a Python binding some C functions. Next we decided to build a proper .so with cmake, but it seems we are doing something wrong with regards to linking the external library 'libdbook' in the binding: The CMakeLists.txt is as follows: PROJECT(dbookpy) FIND_PACKAGE(PythonInterp)...

I'm new at python, building a high low game. I need some help!

Make the computer guess a number that the user chooses between 1 and 1000 in no more than 10 guesses.This assignment uses an algorithm called a binary search. After each guess, the algorithm cuts the number of possible answers to search in half. Pseudocode for the complete program is given below; your task is to turn it into a working py...

Does running separate python processes avoid the GIL?

I'm curious in how the Global Interpreter Lock in python actually works. If I have a c++ application launch four separate instances of a python script will they run in parallel on separate cores, or does the GIL go even deeper then just the single process that was launched and control all python process's regardless of the process that s...