python

How to convert string to hexadecimal integer in Python?

hi I get user argv from command line as follows: '0x000aff00' and I want python to treat it as hex directly... str = sys.argv[1] how is it possible? thanks! ...

Python memchached client library with CAS support

I need to use gets and cas (check and set) commands of memcached from Python application. The only Python client library supporting them I found is Twisted. But Twisted requires quite different design of application, so it's not an option. Is there any other full-featured (not listed on official page) Python library for memcached? Pure P...

python byte to "unsigned 8 bit integer"

hi i am reading in byte array/list from socket... but now i want python to treat the first byte as "unsigned 8 bit integer". how is this possible? and how is it possible to get its integer value as unsigned 8 bit integer? thanks in advance! ...

Access to class attributes by using a variable in Python?

In PHP i can access class attributes like this: <?php // very simple :) class TestClass {} $tc = new TestClass{}; $attribute = 'foo'; $tc->{$attribute} = 'bar'; echo $tc->foo // should echo 'bar' How can i do this in Python? class TestClass() tc = TestClass attribute = 'foo' # here comes the magic? print tc.foo # should echo 'bar' ...

how to redirect complete output of a script

I have a simple cronjob running every day at 18:35: 05 18 * * * ~/job.sh 2>&1 >> ~/job.log So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.: echo 'doing xyz' python doXYZ.py Now, whatever output the python scripts produce, they are not wr...

Python setuptools custom configuration

I'm packaging up a python module, and I would like users to be able to build the module with some custom options. Specifically, the package will do some extra magic if you provide it with certain executables that it can use. So ideally, users would be able to run "setup.py install" or "setup.py install --magic-doer=/path/to/executable",...

Simplifying small code example.

Lets pretend I have the following code. num1 = 33 num2 = 45 num3 = 76 lst = ['one', 'two', 'three'] for item in lst: if item == 'one': print num1 elif item == 'two': print num2 elif item == 'three': print num3 Is there a way to make this more elegant when there is no correlation between the list an...

python string as hex terminated with null

I receive strings in their hex format, i.e. s = "0x0ff" or s = "0fd" how can I check whether the above type of strings are null terminated or not? thanks! ...

Embed pickle (or arbitrary) data in python script

In Perl, the interpreter kind of stops when it encounters a line with __END__ in it. This is often used to embed arbitrary data at the end of a perl script. In this way the perl script can fetch and store data that it stores 'in itself', which allows for quite nice opportunities. In my case I have a pickled object that I want to stor...

Python hexadecimal comparison

Hey guys, I got a problem I was hoping someone could help me figure out! I have a string with a hexadecimal number = '0x00000000' which means: 0x01000000 = apple 0x00010000 = orange 0x00000100 = banana All combinations with those are possible. i.e., 0x01010000 = apple & orange How can I from my string determine what fruit it ...

Python twisted: where to start

Hi, I am trying to start learning twisted for socket servers creation. I want to add some useful features (like auth, and maybe some other). Maybe someone can point me to a good tutorial which will help me to start (+ maybe some other ideas) ...

Skipping delete confirmation in Django Admin

What is the best way to skip the delete confirmation page in the Django admin and go straight to deleting the objects? Thank you! ...

give an active to class to active link

Hello, I am writing a python website built on the back of the django framework, I am looking for a way to highlight the current link the user is on depening on what the URL, I thought doing some thing like this would work. What I have done is create a new application called nav and built some templatetags, like so, from django impor...

numpy : How to convert an array type quickly

I find the astype() method of numpy arrays not very efficient. I have an array containing 3 million of Uint8 point. Multiplying it by a 3x3 matrix takes 2 second, but converting the result from uint16 to uint8 takes another second. More precisely : print time.clock() imgarray = np.dot(imgarray, M)/255 print time.clock() ...

How to Sort Arrays in Dictionary?

I'm currently writing a program in Python to track statistics on video games. An example of the dictionary I'm using to track the scores : ten = 1 sec = 9 fir = 10 thi5 = 6 sec5 = 8 games = { 'adom': [ten+fir+sec+sec5, "Ancient Domain of Mysteries"], 'nethack': [fir+fir+fir+sec+thi5, "Nethack"] } Right now, I'...

Problem creating N*N*N list in Python

I'm trying to create a 3-dimensional N*N*N list in Python, like such: n=3 l = [[[0,]*n]*n]*n Unfortunately, this does not seem to properly "clone" the list, as I thought it would: >>> l [[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]] >>> l[0][0][0]=1 >>> l [[[1, 0, 0], [1, 0, ...

Writing to stdout from within a Microsoft VBA macro

I'm using Python (and the Win32 extensions) to execute macros in an Excel spreadsheet via the COM interface, as shown below: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible = 1 o.Workbooks.Open (r"C:\test.xls") o.Application.Run("macro1") What I'd like to do is have the Excel macro output text warnin...

SQLAlchemy Many-to-Many Relationship on a Single Table

I have a SQLAlchemy model set up in my application that should mimic the functionality of "followers" on Twitter, ie. users have have a many-to-many relationship with eachother (both followers and following). The tables are structured as follows (sa is the sqlalchemy module): t_users = sa.Table("users", meta.metadata, sa.Column("id...

setting XML value using xmlrpc & python

Hi , I need to set the value of a field in an XML file which exists on a remote Linux box. How do I find out which port I should connect to ? But even a proper ping is not happening: import xmlrpclib server = xmlrpclib.ServerProxy('http://10.77.21.240:9000') print server.ping() print "I'm in hurray" bUT instead I got: Traceback (mo...

List of dictionaries, in a dictionary - in Python

I have a case where I need to construct following structure programmatically (yes I am aware of .setdefault and defaultdict but I can not get what I want) I basically need a dictionary, with a dictionary of dictionaries created within the loop. At the beginning the structure is completely blank. structure sample (please note, I want to...