python

Technique for multi-language support of big static portions text in Django

For small portions of text we use django standart {% trans %} tag What to do with big texts such as FAQ, terms and other static pages ...

Web scraping with Python

I'd like to grab daily sunrise/sunset times from here. Is it possible to scrape web content with Python? what are the modules used? Is there any tutorial available? Thanks ...

Python raw strings and unicode : how to use Web input as regexp patterns ?

EDIT : This question doesn't really make sense once you have picked up what the "r" flag means. More details here. For people looking for a quick anwser, I added on below. If I enter a regexp manually in a Python script, I can use 4 combinations of flags for my pattern strings : p1 = "pattern" p2 = u"pattern" p3 = r"pattern" p4 = ru"p...

What exactly do "u" and "r"string flags in Python, and what are raw string litterals ?

While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a django trainer, this suck. I know what an encoding is, and I know what "u" alone does since I get what is unicode But what does "r" exactly, what kind of string does it result in ? And above all, what the heck can "ur" do ? Fina...

Approaching refactoring

I have a very data-centric application, written in Python / PyQt. I'm planning to do some refactoring to really separate the UI from the core, mainly because there aren't any real tests in place yet, and that clearly has to change. There is some separation already, and I think I've done quite a few things the right way, but it's far fro...

Python list : How to sort by timestamp? ( App Engine related )

I have ten entities in my Feed model ( this is an App Engine model) class Feed(db.Model): sometext = db.StringProperty() timestamp = db.DateTimeProperty(auto_now=True) list_of_keys = ["key1","key2","key3".... "key10"] so i call my entities using db.key() method: feeds = db.keys(list_of_keys) # this loop below prints the feed fo...

How to create a custom django filter tag

I am having trouble in getting my site to recognise custom template tags. I have the following dir structure: project_name project_name templatetags _ _init _ .py getattribute.py views _init _ _.py index.html views settings.py main.py manage.py urls.py nbproject Then I have added this to the INSTALLED_APPS: INSTALLED_APPS ...

Reading specific lines only (Python)

I'm using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this? Thanks ...

Passing multiple values to a function call (Python)

import re def strip_tags(value): "Return the given HTML with all tags stripped." return re.sub(r'<[^>]*?>', '', value) I have this function to strip HTML tags, but it seems to accept only single value, what do I need to change if I want to pass multiple (not fixed) values at once? Thanks ...

Regex for removing whitespace

def remove_whitespaces(value): "Remove all whitespaces" p = re.compile(r'\s+') return p.sub(' ', value) The above code strips tags but doesn't remove "all" whitespaces from the value. Thanks ...

Simplest way to integrate python gui app with c console app

I have a c console app which converts a c file to a html file, the c file location is passed to the program as a command line argument.(the app is for the windows platform) What I would like to do is have a python gui app to allow the user to select a file and pass the location of the file to the c app for processing. I already know ho...

Strange bug while combining images in Python

I have a hundred 10x10 px images, and I want to combine them into a big 100x100 image. I'm using the Image library to first create a blank image and then paste in the smaller images: blank = Image.new('P',(100,100)) blank.paste(im,box) The smaller images are in color, but the resulting image turns out in all grayscale. Is there a fix ...

Case insensitive dictionary

Hi, I'd like my dictionary to be case insensitive. I have this example code: text = "practice changing the color" words = {'color': 'colour', 'practice': 'practise'} def replace(words,text): keys = words.keys() for i in keys: text= text.replace(i ,words[i]) return text text = replace(words,text) prin...

How to export c# methods?

Hello, how can we export c# methods? I hava a dll and I want to use its methods in the Python language with the ctypes module. Because I need to use the ctypes module, I need to export the c# methods for them to be visible in Python. So, how can I export the c# methods (like they do in c++)? ...

Reading input from raw_input() without having the prompt overwritten by other threads in Python

I'm trying to let the user input commands at a console using raw_input(), this works fine. The problem is I have background threads that occasionally output log-information to the screen and when they do they mess up the input prompt (since the output go wherever the cursor happens to be at the moment). This is a small Python program th...

preprocessing RIPEMD-160

is the padding of RIPEMD-160 exactly the same as MD4 padding, down to the little-endian change? if i input "abc" in ascii, the processed data in hex should be 8063626100000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000001800000000 right? ...

Python function returning None after recursion

I can't figure out why this python function returns None if it calls itself recursively. It was part of my solution to a Project Euler problem. I have solved the problem in a better way anyhow, but this is still annoying me as the function seems to work OK - and it seems to know the value of the variable I wanted to return. def next_p...

real time subprocess.Popen via stdout and PIPE

I am trying to grab stdout from a subprocess,Popen call and although I am achieving this easily by doing: cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE) for line in cmd.stdout.readlines(): print line I would like to grab stdout in "real time". With the above method, PIPE is waiting to grab all the stdout and then it retu...

Using Nose & NoseXUnit on a Python package

This is a previous post detailing a CI setup for Python. The asker and answerer detail the use of Nose and NoseXUnit with Hudson for their builds. However, NoseXUnit throws an error when run on any source folder where init.py is present: File "build/bdist.linux-x86_64/egg/nosexunit/tools.py", line 59, in packages nosexunit.except...

Import all the modules in a directory

Is there any way to import all modules in the current directory, and return a list of them? For example, for the directory with: mod.py mod2.py mod3.py It will give you [<module 'mod'>, <module 'mod2'>, <module 'mod3'>] ...