python

User authentication in Django

I learned how to authenticate users in Django months ago, but I've since upgraded and am having some problems so it occurred to me this morning that I may not have been doing it correctly from the start so I decided to ask. In my project's urls.py file I've got ^accounts/login/$ and ^accounts/logout/$ both wired up to the built-in login...

How to get back to the for loop after exception handling

Hello, I am ready to run this code but before I want to fix the exception handling: for l in bios: OpenThisLink = url + l try: response = urllib2.urlopen(OpenThisLink) except urllib2.HTTPError: pass bio = response.read() item = re.search('(JD)(.*?)(\d+)', bio) .... As suggested here, I added th...

FieldError on Django's sessions

Just experienced a very strange error on our production server on all page requests. FieldError: Cannot resolve keyword 'session_key' into field. Choices are: expire_date, session_data, session_key The error persisted for about 10 minutes until a graceful restart of apache2 was done. I couldn't find anything online regarding Dja...

UnicodeEncodeError with csvwriter

Hello, I have one more error to fix. row = OpenThisLink + titleTag + JD try: csvwriter.writerow([row]) except (UnicodeEncodeError, UnicodeDecodeError): pass This gives the error (for this character: "ń") row = OpenThisLink + str(titleTag) + JD UnicodeEncodeError: 'ascii' codec can't encode ...

[Python] Strings are wrapped in b'...'

import urllib.request #name = input("What is your screenname? "); name = "zezima" page = urllib.request.urlopen('http://hiscore.runescape.com/index_lite.ws?player=' + name) page = page.readlines() skills = [] for line in page: skills += [line] print(skills) Outputs: [[b'478,2372,1224928266\n'], [b'458,99,59502162\n'], [b'262,99...

Patterns for dealing with memcache Caching in Django

I have a big Django project with several interrelated projects and a lot of caching in use. It currently has a file which stores cache helper functions. So for example, get_object_x(id) would check cache for this object and if it wasn't there, go to the DB and pull it from there and return it, caching it along the way. This same pattern ...

Using xvkbd to read a barcode. How to disable Enter key?

Hello, I am using zbarcam to read barcode from a webcam in my webapps. But, since zbarcam display a \n at the end, my form is submit. Here is what I use : read_one.py #!/usr/bin/python from sys import argv import zbar import webbrowser # create a Processor proc = zbar.Processor() # configure the Processor proc.parse_config('enable'...

Problem with the size of the arrows on a vector field plot

I would like to know how can I make it so the arrows have a length of sqrt(2) (as their x and y coordinates have length 1). As you can see for the picture shown bellow, they seem quite small. Thanks! from pylab import * from numpy import ma X = (0, 0, 0) Y = (0, 1, 2) quiver(X, Y, (1, 1, 1), (1, 1, 1)) #axis([0, 1, 0, 3]) show...

Howto Embed , load .swf into Pygame?

Hi all, How to load a .swf(flash) file in Pygame? THANK YOU. ...

Remove a sublayout in qt?

In PyQt 4.5, I have a layout inside another layout. I'd like to remove the sublayout from its parent, and hide it. I can say parent_layout.removeItem(child_layout) to remove the layout from its parent, but it still shows on the widget. I can't find any way to hide it in one step, as QLayout doesn't have a hide() method like QWidget do...

Programmatically pass unknown number of parameters in Python

I'd like an easy way to plug in a function and arguments to be executed later so I can have a kind of wrapper around it. In this case, I'd like to use it to benchmark, example: def timefunc(fn, *args): start = time.clock() fn(args) stop = time.clock() return stop - start How do I pass arguments where the number of paramet...

[Python] How to add a scrollbar to a window with tkinter?

I have a tkinter program: import urllib.request from tkinter import * root = Tk() root.iconbitmap(default='icon.ico') root.wm_title('Got Skills\' Skill Tracker') frame = Frame(width="500",height="500") frame.pack() def show(): name = "zezima" page = urllib.request.urlopen('http://hiscore.runescape.com/index_lite.ws?player=' + na...

Matrices and inverse Matrices in Python

For a project that I am doing, I decompose a graph that I created using NetworkX into an adjacency matrix using the NetworkX adj_matrix() function. However, one of the problems that I have come across is that every single graph that I decompose gives me the following error when I try to find the inverse of the matrix. str: Traceback (m...

Popularity of path hooks (PEP 302 custom import)

My project has the ability to run python functions remotely. Doing so requires transmitting modules a given function utilizes. Determining what to send is conducted via a modified modulefinder. As I modify the modulefinder to support arbitrary path_hooks, I've started to get the impression that path_hooks are not all that popular. Q...

Get all table names in a Django app

How to get all table names in a Django app? I use the following code but it doesn't get the tables created by ManyToManyField from django.db.models import get_app, get_models app = get_app(app_name) for model in get_models(app): print model._meta.db_table ...

How can I search for particular cell data in column of 100 records using Google spreadsheet

Hi All, Can any one guide me how to search a particular cell data in column of 100 records (in Google spreadsheet) using the code of Google app engine. It very urgent please let me know how can I solve this issue. Ref: http://code.google.com/apis/spreadsheets/data/1.0/developers%5Fguide%5Fpython.html#addRow Regards SKSK ...

How to set shell variables in subprocess.Popen (in a less ugly way...)

NOTE: This is not the same question as Python: Persistent shell variables in subprocess, as that question is actually about environment variables, not shell variables. I'm trying to automate a basic benchmark that I'm doing in csh using the shell built-in time command. You can tweak the output of time by setting the variable of the sam...

Python: WindowsError when editing Registry values using _winreg on Windows 7

I am trying to execute this script by Ned Batchelder to switch .py file association between my two Python installations on Windows. This Python script uses the _winreg module (winreg in Python 3.x) to edit certain Registry values (the path and value pairs modified can be seen in the todo list in the script). I execute this script as fol...

Submenu items in Nautilus right-click menu

Hello. I am trying to write an extension for nautilus, which add an item to the menu that appears when you right-click a file (as shown in image) However, I would like to add a submenu to my custom menu item. I downloaded a 'nautilus-python' package which includes examples of how to write extensions for Nautilus (and so far it turned...

Python: get the return code of ant sub-process in windows

I use python to call ant, I want to get the return code of the ant for detect ant error. for example, in cmd.exe, C:\Documents and Settings\Administrator>ant sfsf Buildfile: build.xml does not exist! Build failed C:\Documents and Settings\Administrator>echo %ERRORLEVEL% 1 but in python: >>> a = subprocess.Popen("ant hahah",shell=Tr...