python

Is there a Python equivalent to Perl's Data::Dumper?

Is there a Python module that can be used in the same way as Perl's Data::Dumper module? Edit: Sorry, I should have been clearer. I was mainly after a module for inspecting data rather than persisting. BTW Thanks for the answers. This is one awesome site! ...

How to URL encode this so I can pass it to Facebook Share?

This is the URL I want to share: http://mydomain.com/#url=http://stackoverflow.com Inside my site, I do this in Django so that everything will work: http://mydomain.com/#url={{external|urlencode}} However, when I pass it to Facebook Share, everything gets messed up. http://www.facebook.com/sharer.php?u=&lt;url to share>&t=<title o...

Can't import pygame to Netbeans on Mac

I am running python 2.6.5 and pygame 1.9.1 It seems to me I've tried everything but it keeps showing 'module not found' errors... Please help! ...

pairwise crossproduct in Python?

how can I get the list of cross product pairs from a list of arbitrarily long lists in python? e.g. a = [1,2,3] b = [4,5,6] crossproduct(a,b) should yield [[1,4], [1, 5], [1,6], ...] thanks. ...

Exposing a pointer in Boost.Python

I have this very simple C++ class: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_<Tree>("Tree") .def_readwrite("head",&Tree::head) ; } I want to access the head variable from Python, but the message I see is: No to_python (by-value) converter found for C++ type: Node* From w...

Return template as string - Django

Hi All, I'm still not sure this is the correct way to go about this, maybe not, but I'll ask anyway. I'd like to re-write wordpress (justification: because I can) albeit more simply myself in Django and I'm looking to be able to configure elements in different ways on the page. So for example I might have: Blog models A site update me...

Noob Python Question: List Confusion

I'm trying to transfer the contents of one list to another, but it's not working and I don't know why not. My code looks like this: list1 = [1, 2, 3, 4, 5, 6] list2 = [] for item in list1: list2.append(item) list1.remove(item) But if I run it my output looks like this: >>> list1 [2, 4, 6] >>> list2 [1, 3, 5] My question is...

Access to content of Lotus Notes database without Lotus Notes software installed

I am looking for a programatic way to access content in a Lotus Notes database (.nsf file) without having Lotus Notes software installed. Python would be preferred but I'm also willing to look at other languages e.g. C/C++ or other means e.g. SQL From what I have read, all of the methods e.g. Python COM access, pyodbc rely on having Lo...

Why am I getting a " instance has no attribute '__getitem__' " error?

Here's the code: class BinaryTree: def __init__(self,rootObj): self.key = rootObj self.left = None self.right = None root = [self.key, self.left, self.right] def getRootVal(root): return root[0] def setRootVal(newVal): root[0] = newVal def getLeftChild(root): ret...

Most secure way to generate a random session ID for a cookie?

I'm writing my own sessions controller that issues a unique id to a user once logged in, and then verifies and authenticates that unique id at every page load. What is the most secure way to generate such an id? Should the unique id be completely random? Is there any downside to including the user id as part of the unique id? ...

how best do I find the intersection of multiple sets in python?

I have a list of sets: setlist = [s1,s2,s3...] I want s1 ∩ s2 ∩ s3 ... I can write a function to do it by performing a series of pairwise s1.intersection(s2), etc., but is there a recommended, better, or built-in way? ...

Copying 2D lists in python

Hi I want to copy a 2D list, so that if I modify 1 list, the other is not modified. For 1 D list, I just do this: a = [1,2] b = a[:] And now if I modify b, a is not modified. But this doesn't work for 2D list: a = [[1,2],[3,4]] b = a[:] If I modify b, a gets modified as well. How do I fix this? ...

Is there a good way of automatically generating javascript client code from server side python

I basically want to be able to: Write a few functions in python (with the minimum amount of extra meta data) Turn these functions into a web service (with the minimum of effort / boiler plate) Automatically generate some javascript functions / objects for rpc (this should prevent me from doing as many stupid things as possible like mis...

Most efficient way to store IP Address in MySQL

What is the most efficient way to store and retrieve IP addresses in MySQL? Right now I'm doing: SELECT * FROM logins WHERE ip = '1.2.3.4' Where ip is a VARCHAR(15) field. Is there a better way to do this? I'm using Python. ...

Read/Write a file from/to network folder/share using Python?

How to Write/Read a file to/from a network folder/share using python? The application will run under Linux and network folder/share can be a Linux/Windows System. Also, how to check that network folder/share has enough space before writing a file? What things should i consider? ...

Why am I getting a SyntaxError in the Python interpreter?

This code works when I try it from a .py file, but fails in the command line interpreter and Idle. >>> try: ... fsock = open("/bla") ... except IOError: ... print "Caught" ... print "continue" File "<stdin>", line 5 print "continue" ^ SyntaxError: invalid syntax I'm using python 2.6 ...

Why isn't my os.rename working?

Hi All, I'm trying to rename some files, but getting a baffling error*. When I run this: if os.path.isfile(fullPath): print 'fmf exists' print fullPath print newFilePath os.rename(fullPath,newFilePath) I get the following error: fmf exists (correct fullPath) (correct newFilePath, ie. destination) Traceback (most rec...

Subclassed django models with integrated querysets

Like in this question, except I want to be able to have querysets that return a mixed body of objects: >>> Product.objects.all() [<SimpleProduct: ...>, <OtherProduct: ...>, <BlueProduct: ...>, ...] I figured out that I can't just set Product.Meta.abstract to true or otherwise just OR together querysets of differing objects. Fine, but...

Python 3: receive user input including newline characters

I'm trying to read in the following text from the command-line in Python 3 (copied verbatim, newlines and all): lcbeika rraobmlo grmfina ontccep emrlin tseiboo edosrgd mkoeys eissaml knaiefr Using input, I can only read in the first word as once it reads the first newline it stops reading. Is there a way I could read in them all with...

Get a user's timezone in Python + Pylons

Is there a way to get the timezone of the connecting user using Pylons, and to adjust the content before rendering accordingly? Or can this only be done by JS? Thanks. ...