Skip to content
Advertisement

Tag: python-2.7

How do you declare a global constant in Python?

I have some heavy calculations that I want to do when my program starts, and then I want to save the result (a big bumpy matrix) in memory so that I can use it again and again. My program contains multiple files and classes, and I would like to be able to access this variable from anywhere, and if possible

python random mouse movements

I would like to make random mouse movements in specified rectangle area (limited with coordinates x1, y1, x2, y2, x3, y3, x4, y4). Movements should be smooth, random, not just straight lines, go randomly up/down/left/right/etc for specified time duration. Could you give me a hand or working example I can learn from? many thanks Answer This code works on Windows

Python passing callback function in a class

I have to try to move from the non-class-based coding style into class-based coding style, but facing an issue. The optimize() function takes a callback function mycallback(). The code works perfectly fine in Non-class-based method, but when I moved it to class-based method, I got an error “mycallback() takes exactly 3 arguments (1 given)”. What is the right way to

Remove comma and change string to float

I want to find “money” in a file and change the string to float , for example, I use regular expression to find “$33,326” and would like to change to [33326.0, “$”] (i.e., remove comma, $ sign and change to float). I wrote the following function but it gives me an error Can you help me debug my code? Answer

Python DNS module import error

I have been using python dns module.I was trying to use it on a new Linux installation but the module is not getting loaded. I have tried to clean up and install but the installation does not seem to be working. $ python –version Python 2.7.3 $ sudo pip install dnspython Downloading/unpacking dnspython Downloading dnspython-1.11.1.zip (220Kb): 220Kb downloaded Running setup.py

query from postgresql using python as dictionary

I’m using Python 2.7 and postgresql 9.1. Trying to get dictionary from query, I’ve tried the code as described here: http://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL It is printing the next answer: printing the item itself, show me that it is list. The excepted answer was dictionary. Edit: Trying the next: returns Answer If you don’t want to use a psycopg2.extras.DictCursor you can create a

Convert one row of a pandas dataframe into multiple rows

I want to turn this: Into this: Context: I have data stored with one value coded for all ages (age = 99). However, the application I am developing for needs the value explicitly stated for every id-age pair (id =1, age = 25,50, and 75). There are simple solutions to this: iterate over id’s and append a bunch of dataframes,

Advertisement