python

Writing unit tests in Python: How do I start?

I completed my first proper project in Python and now my task is to write tests for it. Since this is the first time I did a project, this is the first time I would be writing tests for it. The question is, how do I start? I have absolutely no idea. Can anyone point me to some documentation/ tutorial/ link/ book that I can use to start...

Call int() function on every list element in Python

I have a list with numeric strings, like so: numbers = ['1', '5', '10', '8']; I would like to convert every list element to integer, so it would look like this: numbers = [1, 5, 10, 8]; I could do it using a loop, like so: new_numbers = []; for n in numbers: new_numbers.append(int(n)); numbers = new_numbers; Does it have to ...

Way to query database with SQLAlchemy where a date is a particular day of the week?

I have a table (called 'entry') which has a datetime column (called 'access_date'), and I want to do an SQLAlchemy query that only produces results where entry.access_date is a Monday (or any other day of the week specified by a number [0..6]). Is this possible? I am using sqlite & SQLalchemy 0.5.8 if that makes any difference. ...

How to add audio to video?

I record a video with no sound. I save a snapshots and then I compile them to .avi with 25FPS. Now I want to record audio (instead time.sleep(0.04) between following snapshots I will record audio in this time) and compile it wits video. Now I have avi file, and wave file, and I find a solution to mix them. Python, Win32 It is my "video...

pyqt, webkit and facebook authentication?

Are there any examples how to authenticate your desktop Facebook application using PyQT and embedded webkit? This is to provide better user experience than opening Facebook authentication page in a separate browser window. ...

Calling Python instance methods in function decorators

Is there a clean way to have a decorator call an instance method on a class only at the time an instance of the class is instantiated? class C: def instance_method(self): print('Method called') def decorator(f): print('Locals in decorator %s ' % locals()) def wrap(f): print('Locals in wrapper ...

String concatenation in Python.

Can you describe difference between two ways of string concatenation: simple __add__ operator and %s patterns? I had some investigation in this question and found %s (in form without using parentheses) a little faster. Also another question was appeared: why result of 'hell%s' % 'o' refers to another memory region than 'hell%s' % ('o',)...

FreeTDS translating MS SQL money type to python float, not Decimal

I am connecting to an MS SQL Server db from Python in Linux. I am connecting via pyodbc using the FreeTDS driver. When I return a money field from MSSQL it comes through as a float, rather than a Python Decimal. The problem is with FreeTDS. If I run the exact same Python code from Windows (where I do not need to use FreeTDS), pyodbc ...

Pythonic solution to my reduce getattr problem

I used to use reduce and getattr functions for calling attributes in a chain way like "thisattr.thatattr.blaattar" IE: reduce(getattr, 'xattr.yattr.zattr'.split('.'), myobject) Works perfectly fine, however now I have a new requirement, my strings can call for a specific number of an attribute as such: "thisattr.thatattr[2].blaattar" ...

enabling tty in a ssh session

I would to take in some login information for a script have written in to be used by many users. In python I set the input_raw to read from dev/tty but it fails horribly when i am connecting to the script being run on a server through ssh. Thoughts? Workarounds? I would prefer to avoid hard coding usernames into the script. Please and...

Python: best/efficient way of finding a list of words in a text?

hi, I have a list of approximately 300 words and a huge amount of text that I want to scan to know how many times each word appears. I am using the re module from python: for word in list_word: search = re.compile(r"""(\s|,)(%s).?(\s|,|\.|\))""" % word) occurrences = search.subn("", text)[1] but I want to know if there is a m...

Dynamic Loading of Modules then using "from x import *" on loaded module

I have some django apps which are versioned by app name. appv1 appv2 The models.py in the apps are slightly different based on the version, but have the same model names. I'm attempting to load the models dynamically into the current namespace. So I've made a function that attempts to get the module and return it: def get_models_modu...

Algorithm to determine exchange rate

Given a data set of various currency pairs, how do I efficiently compute the implied fx rate for a pair not supplied in the data set? For example, say my database/table looks like this (this data is fudged): GBP x USD = 1.5 USD x GBP = 0.64 GBP x EUR = 1.19 AUD x USD = 1.1 Notice that (GBP,USD) != 1/(USD,GBP). I would expect the...

py2exe + pywin32 MemoryLoadLibrary import fail when bundle_files=1

I have created a simple program which uses pywin32. I want to deploy it as an executable, so I py2exe'd it. I also didn't want a huge amount of files, so I set bundle_files to 1 (meaning bundle everything together). However, when I attempt running it, I get: Traceback (most recent call last): File "pshelper.py", line 4, in <module> ...

Profile Memory Allocation in Python (with support for Numpy arrays)

I have a program that contains a large number of objects, many of them Numpy arrays. My program is swapping miserably, and I'm trying to reduce the memory usage, because it actually can't finis on my system with the current memory requirements. I am looking for a nice profiler that would allow me to check the amount of memory consumed b...

Dealing with timezones in Django

I'm trying to deal with timezone information in Django. I tried doing something like: results = Competitor.objects.raw("SELECT official_start AT TIME ZONE 'UTC', official_finish AT TIME ZONE 'UTC' FROM competitor WHERE race_id=1") Thinking that this way I would know that the timezone was UTC but say I store a time in the database that...

How do I fix a "JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)"?

I'm trying to get Twitter API search results for a given hashtag using Python, but I'm having trouble with this "No JSON object could be decoded" error. I had to add the extra % towards the end of the URL to prevent a string formatting error. Could this JSON error be related to the extra %, or is it caused by something else? Any suggesti...

Measuring rectangles at odd angles with a low resolution input matrix (Linear regression classification?)

I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000 0011111111110000 0011111111110000 0000000000000000 0000000111111110 0000000111111110 0000000000000000 I need to find the width and height of all rectangles in the field. The input is actually a single column at a time (think like a sc...

When to thread?

I have never written any code that uses threads. I have a web application that accepts a POST request, and creates an image based on the data in the body of the request. Would I want to spin off a thread for the image creation, as to prevent the server from hanging until the image is created? Is this an appropriate use, or merely a so...

FFMpeg: Recording Video errors

I try to record video (with audio!) in this way: ffmpeg = "C:\bin\ffmpeg.exe" cmd = '%s -r 15 -f vfwcap -i 0 c:/output2.mpg' % (ffmpeg) os.system(cmd) And I have error: "The filename, directory name, or volume label syntax is incorrect." I think that is sth with vfwcap, but I don't know how to fixed thi problem. Any ideas? Maby sth el...