python

sharing a string between two objects

I want two objects to share a single string object. How do I pass the string object from the first to the second such that any changes applied by one will be visible to the other? I am guessing that I would have to wrap the string in a sort of buffer object and do all sorts of complexity to get it to work. However, I have a tendency...

Best way to generate CRC8/16 when input is odd number of BITS (not byte)? C or Python

So I'm stuck with a protocol that adds a CRC8/CRC16 over odd number of bits. (ie. it's not divisible by 8) What's the best method to generate the CRC for it in software? There are plenty of CRC algorithm that uses table, but they are lookup per byte. Of course, there's the "fail-safe" of doing it one bit at a time. But is there a be...

calculating factorial in Python

When calculating math.factorial(100) I get: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L Why is there an L at the end of the number? ...

Pylons / SQLAlchemy - ConfigParser.MissingSectionHeaderError: File contains no section headers

I've been working on a Pylons site and just now started to create my database. I made my User model class and then issued paster setup-app development.ini. Worked great. However, now when I try to paster serve --reload development.ini, I get the following error: ConfigParser.MissingSectionHeaderError: File contains no section headers. ...

Python - '>>' operator

What does the >> operator do? For example 10 >> 1 = 5 Thanks ...

Multiple character replace with python

I need to replace some characters as follows : & -> \&, # -> \#, ... I coded as follows, but I guess there should be a much better way. Any hints? strs = strs.replace('&', '\&') strs = strs.replace('#', '\#') ... ...

Django Error: TemplateSyntaxError:

Hello, Trying to get massivecoupon running and am running into a bunch of errors. The most recent is: File "/home/usr/.local/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module import(name) TemplateSyntaxError: Caught ImportError while rendering: No module named libsmassivecoupon.countries ...

Simple "Hello-World" program for python-evince

I'm trying to write a simple "hello-world"-type program using the python-evince package for lucid-lynx gnome, that embeds Evince in a python-gtk window. The samples I've found on the web go like this: import evince import gtk w = gtk.Window() w.show() e = evince.View() w.add(e) e.show() document = evince.document_factory_get_document...

Querying Many to many fields in django template

This may not be relevant but just wanted to ask, IF an object is passed from views to template and in the template will i be able to query many to many fields Models code: class Info(models.Model): xls_answer = models.TextField(null=True,blank=True) class Upload(models.Model): access = models.IntegerField() info ...

Rename Pdf from Pdf title

Dear all: I want to organize my pdf file downloaded from the internet. It is clear that many of them are ill-named. I want to extract the real title from the file. Here many of them are generated from Latex and I think from the compiled pdf we can find the \title{} keyword or something like that. I want then use this to rename the file. ...

device behind firewall connect via ssh

Hi all, There have been a few questions like this around the place but none have really answered my question specifically.(for example http://stackoverflow.com/questions/2529941/connecting-to-device-behind-firewall ) What I want is a central server, that receives a heartbeat from multiple ( say 100's) embedded devices behind personal f...

Threading in Python

I have two definitions or methods in python. I'd like to run them at the same exact time. Originally I tried to use forking but since the child retained the memory from the parent, it's writing multiple things that I don't need in a file. So I switched to threading. I have something similar to import threading class test(threading.Thr...

Pattern matching in dictionary using python

In the following dictionary,can the elements be sorted according the last prefix in the key opt_dict=( {'option1':1, 'nonoption2':1, 'nonoption3':12, 'option4':6, 'nonoption5':5, 'option6':1, 'option7':1, }) for key,val in opt_dict.items(): if "answer" in key: //match keys last prefix and print output ...

Python cut a string after Xth sentence.

I have to cut a unicode string which is actually an article (contains sentences) I want to cut this article string after Xth sentence in python. A good indicator of a sentence ending is that it ends with full stop (".") and the word after start with capital name. Such as myarticle == "Hi, this is my first sentence. And this is my secon...

Monitor Global keyboard input on a Mac with python

Trying to write a program that allows users to control a program via global shortcuts. I already have a working version on windows using pyHook but can't figure out how to capture global input on a Mac. The other questions I have read here so far mostly seemed to be about Linux not Mac. (which is why I'm asking) Is there any way to do th...

Printing the values of a tuple

Accessing tuple values How to access the value of a and b in the following >> t=[] >> t.append(("a" , 1)) >> t.append(("b" , 2)) >> print t[0][0] a >> print t[1][0] b How to print the values of a and b ...

Is there any way to create an app for python3 script?

Py2app will create the app for python2. But for python3? Has anyone succeeded in creating an app for python3 script? Any clue would be helpful for my script in creating that. ...

python line editing telnet server

Hi, I am creating a server in python (what it is doing is irrelevant), but I would like it to accept telnet connections and provide a command line interface with line editing capabilities (tabcompletion, emacs/vi-mode, etc) and history per session. I have successfully created the telnet session, disabled line mode and enabled server echo...

How much faster is Python 2.7's new IO library compared to earlier versions?

The Python 2.7 update note says: A new version of the io library, rewritten in C for performance. I've played with Python 2.7 a bit, but I don't see any performance gain: >>> from timeit import Timer >>> t = Timer('f = open("E:\\db.txt", "r"); f.read(); f.close()') >>> t.timeit(10000) And the result: Python 2.6.5 -- 12....

Python not sorting unicode properly. Strcoll doesn't help.

I've got a problem with sorting lists using unicode collation in Python 2.5.1 and 2.6.5 on OSX, as well as on Linux. import locale locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8') print [i for i in sorted([u'a', u'z', u'ą'], cmp=locale.strcoll)] Which should print: [u'a', u'ą', u'z'] But instead prints out: [u'a', u'z', u'ą'] ...