python

Prototype based object orientation. The good, the bad and the ugly?

Hello, I come from classes object orientation languages and recently I have been learning those fancy dynamic languages (JavaScript, Python and Lua) and I want some tips about how to use OO in those languages. It would be useful to know the pitfalls and the shortcomings of such approach and the advantages compared to traditional OO. The...

Python and regex question, extract float/double value

How do I extract a double value from a string using regex. import re pattr = re.compile('??????????????') x = pattr.match("4.5") ...

Need help-typecasting in Python

Help me people Last week I asked about unicode conversion.I got only one reply.I need more suggestions. I need to convert strings in Python to other types such as unsigned and signed int 8 bits,unsigned and signed int 16 bits,unsigned and signed int 32 bits,unsigned and signed int 64 bits,double,float,string,unsigned and signed 8 bi...

Phone numbers to links in Python

I'm working a piece of code to turn phone numbers into links for mobile phone - I've got it but it feels really dirty. import re from string import digits PHONE_RE = re.compile('([(]{0,1}[2-9]\d{2}[)]{0,1}[-_. ]{0,1}[2-9]\d{2}[-_. ]{0,1}\d{4})') def numbers2links(s): result = "" last_match_index = 0 for match in PHONE_RE.f...

need help - bit-field conversion

I want to convert strings to bit-fields.Also,convert them to binary and then use. Need help with this..help me .. ...

Python Performance - have you ever had to rewrite in something else?

Has anyone ever had code in Python, that turned out not to perform fast enough? I mean, you were forced to choose another language because of it? We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most scenarios (compared to say, Java) because it rel...

Tricky Python string literals in passing parameter to timeit.Timer() function

I'm having a hard time with the setup statement in Python's timeit.Timer(stmt, setup_stmt). I appreciate any help to get me out of this tricky problem: So my sniplet looks like this: def compare(string1, string2): # compare 2 strings if __name__ = '__main__': str1 = "This string has \n several new lines \n in the middle" s...

How do I convert part of a python tuple (byte array) into an integer

I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values: response = (0, 0, 117, 143, 6) The first 4 values are a 32-bit int telling me how many bytes have been used and the last value is the percentage used. I can acc...

Evaluate environment variables into a string

I have a string representing a path. Because this application is used on Windows, OSX and Linux, we've defined environment variables to properly map volumes from the different file systems. The result is: "$C/test/testing" What I want to do is evaluate the environment variables in the string so that they're replaced by their respect...

Using user input to find information in a Mysql database

I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. I am a bit stuck on how to get started. Does anyone have any tips for me? ...

Mysql Connection, one or many?

I'm writing a script in python which basically queries WMI and updates the information in a mysql database. One of those "write something you need" to learn to program exercises. In case something breaks in the middle of the script, for example, the remote computer turns off, it's separated out into functions. Query Some WMI data Upda...

What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?

I have a Client and Groupe Model. A Client can be part of multiple groups. Clients that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (ClientGroupe) comes in with that extra data. For now, when I try to save the m2m data, it just dies and says I should use the C...

printing a list of persons with more than one home, each home with more than one phone number...

I have a class Person which can have several Homes, each one with one or many Phone numbers. I have defined the classes, but now i am trying to create a view wich list every person, with all its homes and all the phone numbers for each home address... something like: john smith 123 fake str 305-99-8877 305-99-8876 321 oak road 44...

Python graceful future feature (__future__) import

How do you gracefully handle failed future feature imports? If a user is running using Python 2.5 and the first statement in my module is: from __future__ import print_function Compiling this module for Python 2.5 will fail with a: File "__init__.py", line 1 from __future__ import print_function SyntaxError: future feature prin...

Python: flush a buffer before program termination via a finalizer

I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since __del__ is no longer guaranteed to be called on every object, is the appropriate approach to hook a similar function (or __del__ itself) into atexit.register (during initialization)? If I'm not mistaken, this will ca...

Scientific libraries for Lua?

Are there any scientific packages for Lua comparable to Scipy? Thanks for your time and help :) ...

Tidier way of trying to import a module from multiple locations?

Is there a way to tidy-up the following code, rather than a series of nested try/except statements? try: import simplejson as json except ImportError: try: import json except ImportError: try: from django.utils import simplejson as json except: raise "Requires either simplejson...

Django: How do I use the built in password reset/change views with my own templates.

For example I can point the url '^/accounts/password/reset/$' to django.contrib.auth.views.password_reset with my template filename in the context but I think need to send more context details. I need to know exactly what context to add for each of the password reset & change views. Thanks. ...

Trouble with encoding in emails

I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email) Then a PHP script runs through the files and displays them. I am having an issue with ISO-8859-1 (Latin-1) encoded email Here's an example of the text i get: =?iso-8859-1?Q?G=EDsli_Karlsson?= and Sj=E1um hva=F0 =F3li...

Producing documentation for Python classes

I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general. Is there a good (free) system that I can use to provide documentation for classes and functions based on the code I...