python

Is there a better, pythonic way to do this?

This is my first python program - Requirement: Read a file consisting of {adId UserId} in each line. For each adId, print the number of unique userIds. Here is my code, put together from reading the python docs. Could you give me feedback on how I can write this in more python-ish way? CODE : import csv adDict = {} reader = csv.rea...

How to store dynamically generated HTML form elements from Javascript in Python?

I have an HTML form that a user can add an arbitrary amount of input fields to through jQuery. The user is also able to remove any input field from any position. My current implementation is that each new input box has an id of "field[i]" so when the form is posted it is processed in Python as field1, field2 field3, ...field[n] i = 0 wh...

Where do I start with a web bot?

I simply want to create an automatic script that can run (preferably) on a web-server, and simply 'clicks' on an object of a web page. I am new to Python or whatever language this would be used for so I thought I would go here to ask where to start! This may seem like I want the script to scam advertisements or do something illegal, but ...

Printing basenames by Python

How can you print the basenames of files by Python in the main folder and subfolders? My attempt #!/usr/bin/python import os import sys def dir_basename (dir_name): for dirpath, dirnames, filenames in os.walk(dir_name): for fname in filenames: print os.path.basename(fna...

Pythonic way to only do work first time a variable is called

Hello, my Python class has some variables that require work to calculate the first time they are called. Subsequent calls should just return the precomputed value. I don't want to waste time doing this work unless they are actually needed by the user. So is there a clean Pythonic way to implement this use case? My initial thought was ...

Specific use case for Django admin

I have a couple special use cases for Django admin, and I'm curious about other peoples' opinions: I'd like to use a customized version the admin to allow users to edit certain objects on the site (customized to look more like the rest of the site). At this point users can only edit objects they own, but I'll eventually open this up to...

Adding row to numpy recarray

Is there an easy way to add a record/row to a numpy recarray without creating a new recarray? Let's say I have a recarray that takes 1Gb in memory, I want to be able to add a row to it without having python take up 2Gb of memory temporarily. ...

How to read continous HTTP streaming data in Python?

Hi How to read binary streams from a HTTP streamer server in python. I did a search and someone said urllib2 can do the job but had blocking issues. Someone suggested Twisted framework. My questions are: If it's just a streaming client reads data on background, can I ignore the blocking issues caused by urllib2? What will happen if u...

Encoding of arguments to subprocess.Popen

I have a Python extension to the Nautilus file browser (AFAIK this runs exclusively on GNU/Linux/Unix/etc environments). I decided to split out an expensive computation and run it as a subprocess, pickle the result and send it back over a pipe. My question concerns the arguments to the script. Since the computation requires a path argume...

How to use tabs-as-spaces in Python in Visual Studio 2008?

I have some IronPython scripts that are embedded in a C# project, and it would be convenient to be able to edit them in the VS editor. VS evidently knows about Python because it provides syntax coloring for it. Unfortunately however the editor uses tab characters for indentation, whereas I want spaces. Is there a setting to change this? ...

Rounding Decimals with New Python Format Function

How do I round a decimal to a particular number of decimal places using the Python 3.0 format function? ...

Pure python solution to convert XHTML to PDF

Hello, I am after a pure Python solution (for the GAE) to convert webpages to pdf. I had a look at reportlab but the documentation focuses on generating pdfs from scratch, rather than converting from HTML. What do you recommend? - pisa? Edit: My use case is I have a HTML report that I want to make available in PDF too. I will make up...

Python Console Website

I believe that I once saw a website that is like an online Python console. Does anyone know of such a website? ...

Elegant setup of Python logging in Django

I have yet to find a way of setting up Python logging with Django that I'm happy with. My requirements are fairly simple: Different log handlers for different events - that is, I want to be able to log to different files Easy access to loggers in my modules. The module should be able to find its logger with little effort. Should be eas...

Atomic increment of a counter in django

I'm trying to atomically increment a simple counter in Django. My code looks like this: from models import Counter from django.db import transaction @transaction.commit_on_success def increment_counter(name): counter = Counter.objects.get_or_create(name = name)[0] counter.count += 1 counter.save() If I understand Django c...

How can I get an accurate UTC time with Python?

I wrote a desktop application and was using datetime.datetime.utcnow() for timestamping, however I've recently noticed that some people using the application get wildly different results than I do when we run the program at the same time. Is there any way to get the UTC time locally without using urllib to fetch it from a website? ...

Python (Django) Shopify API Client -- For a Beginner

Hello, I have a requirement to build a client for Shopify's API, building it in Python & Django. I've never done it before and so I'm wondering if someone might advise on a good starting point for the kinds of patterns and techniques needed to get a job like this done. Here's a link to the Shopify API reference Thanks. ...

Asynchronous Stream Processing in Python

Let's start with a simple example. A HTTP data stream comes in the following format: MESSAGE_LENGTH, 2 bytes MESSAGE_BODY, REPEAT... Currently, I use urllib2 to retrieve and process streaming data as below: length = response.read(2) while True: data = response.read(length) DO DATA PROCESSING It works, but since all message...

Finding out contacts from a group (GDATA)

I want to find all contacts belonging to a particular group (eg "Friends"). I have worked out how to get a the groups a contact belongs to but that group has a URI rather than a friendly name (I could iterate over all contacts but that seems a bit heavy handed). I have seen the python examples but it doesnt seem to be one doing quite w...

Is there easy way in python to extrapolate data points to the future?

I have a simple numpy array, for every date there is a data point. Something like this: >>> import numpy as np >>> from datetime import date >>> from datetime import date >>> x = np.array( [(date(2008,3,5), 4800 ), (date(2008,3,15), 4000 ), (date(2008,3, 20), 3500 ), (date(2008,4,5), 3000 ) ] ) Is there easy way to extrapolate data po...