python

python dictionary key Vs object attribute

suppose i have object has key 'dlist0' with attribute 'row_id' the i can access as getattr(dlist0,'row_id') then it return value but if i have a dictionary ddict0 = {'row_id':4, 'name':'account_balance'} getattr(ddict0,'row_id') it is not work my question is how can i access ddict0 and dlist0 same way any one can help me? ...

Python Chain getattr as a string

import amara def chain_attribute_call(obj, attlist): """ Allows to execute chain attribute calls """ splitted_attrs = attlist.split(".") current_dom = obj for attr in splitted_attrs: current_dom = getattr(current_dom, attr) return current_dom doc = amara.parse("sample.xml") print chain_attribute_call(...

Importing csv file into postgres

Using Mysql Administrator GUI tool - I have exported some data tables retrieved from an sql dumpfile into csv files. I then tried to import these csv files into a postgres database using the postgres COPY command. I've tried entering "COPY articles FROM '[insert .csv dir here]' DELIMITERS ',' CSV;" and also the same command without the ...

Why is this code not accepting this png images transparency?

I've isolated the cause of the problem to be the image, since the code seems to work with other png images with transparency. However, it doesn't seem to work with the one image I need it to. This would be of great help seeing as I'm trying to make a nice shaped window. The image: The code: import wx class PictureWindow(wx.Frame):...

Negative coordinates in pygame

What would be the best way to use negative coordinates in pygame? At the moment I have a surface that is 1.5 times the original surface then everything that needs to be drawn is shifted up by a certain amount (to ensure the negative coordinates become positive) and drawn. Is there an easier/alternate way of doing this? ...

Python pack arguments??

Hi, is to possible to "pack" arguments in python? I have the following functions in the library, that I can't change (simplified): def g(a,b=2): print a,b def f(arg): g(arg) I can do o={'a':10,'b':20} g(**o) 10 20 but can I/how do I pass this through f? That's what I don't want: f(**o) Traceback (most recent call last): ...

Invert colormap in matplotlib

I would like to know how to simply reverse the color order of a given colormap in order to use it with plot_surface. ...

Python implementation of Jenkins Hash?

Does there exist a native Python implementation of the Jenkins hash algorithm(s)? I need a hash algorithm that takes an arbitrary string and turns it into an 32-bit integer. For a given string, it must guarantee to return the same integer across platforms. I have looked at the ELF hash algorithm, of which I have found a Python implemen...

Python: How do i manipulate the list to get the string starting with '+' ?

Hi, I am comparing 2 txt files that are ls -R of the etc directory in a linux system. I compared the 2 files using difflib.differ and got this list as my result (i put the dots to keep the list short in here): result = [' etc:\n', ' ArchiveSEL\n', ' HOSTNAME\n', ' RMCPUser\n', ..., ' qcleaner\n', '+ extraFile\n', ' rc.d\n', '+ extra...

Method for dry runs?

Hello, at the moment my python code often looks like this: ... if not dry_run: result = shutil.copyfile(...) else: print " DRY-RUN: shutil.copyfile(...) " ... I now think about writting something like a dry runner method: def dry_runner(cmd, dry_run, message, before="", after=""): if dry_run: print before + "DR...

How do I convert a python string to ucs2 hex?

Hi Guys, I've been searching for this one and couldn't find it, although it seems simple. I need to send in a ucs2 hex string in the url, and I don't know how to convert a python string to be ucs2 hex. Any thoughts? ...

How to use a custom __init__ of an app engine Python model class properly?

I'm trying to implement a delayed blog post deletion scheme. So instead of an annoying Are you sure?, you get a 2 minute time frame to cancel deletion. I want to track What will be deleted When with a db.Model class (DeleteQueueItem), as I found no way to delete a task from the queue and suspect I can query what's there. Creating a Del...

Python: How do i split the file?

I have this txt file which is ls -R of etc directory in a linux system. Example file: etc: ArchiveSEL xinetd.d etc/cmm: CMM_5085.bin cmm_sel storage.cfg etc/crontabs: root etc/pam.d: ftp rsh etc/rc.d: eth.set.sh rc.sysinit etc/rc.d/init.d: cmm functions userScripts etc/security: access.conf ...

Sorting a list pairs by frequency of the pair elements

I'm completely new to Python and while trying various random bits and pieces I've struck upon a problem that I believe I've "solved", but the code doesn't feel right - I strongly suspect there is going to be a better way to get the desired result. FYI - I'm using whatever the latest version of Python 3 is, on Windows. Problem definitio...

Using select_related and extra clause

I'm trying to achieve some extra select on a queryset and wants to add the needed table to the pool of tables in the query using the select_related method in order to benefit for the '__' syntax. Here is an example with simple models : from django.db import models # Create your models here. class testA(models.Model): code = model...

httplib2 and socks server

Is there any way to use httplib2 via protected socks server using some password and username, does add_credentials method work well in this case? For now I have a code shown below proxy.add_credentials(account.get('USER'), account.get('PASS')) or do I need to user proxy_info attribute with the necessary information? ...

Best way to check new-line-independent-identity of 2 files with python

I tried filecmp.cmp(file1,file2) but it doesn't work since files are identically except for new line characters. Is there an option for that in filecmp or some other convenience function/library or do I have to read both files line by line and compare those? ...

Bandwidth test, delay test using urllib2

I want to make a python script that tests the bandwidth of a connection. I am thinking of downloading/uploading a file of a known size using urllib2, and measuring the time it takes to perform this task. I would also like to measure the delay to a given IP address, such as is given by pinging the IP. Is this possible using urllib2? ...

Adding server certificate validation to httplib.HTTPSConnection

I've found that httplib.HTTPSConnection doesn't perform an automatic server certificate check. As far as I've understood the problem, I need to add that functionality manually, e.g. by subclassing this class as described here. As I'm using Python2.4.5 and an upgrade is not possible under the given circumstances, I cannot use the workaro...

Content-based routing with RabbitMQ and Python

Is it possible with RabbitMQ and Python to do content-based routing? The AMQP standard and RabbitMQ claims to support content-based routing, but are there any libraries for Python which support specifying content-based bindings etc.? The library I am currently using (py-amqplib http://barryp.org/software/py-amqplib/) seems to only supp...