python

how to calculate timedelta python

What I am trying to do is to subtract 7 hours from a date. I searched stack overflow and found the answer on how to do it here. I then went to go read the documentation on timedelta because I was unable to understand what that line in the accepted answer does, rewritten here for ease: from datetime import datetime dt = datetime.strptim...

Does Django has testing tools comparable to what Rails has?

Hey, Ruby/Rails enjoy some really nice and powerful testing frameworks like Cucumber and RSpec. Does Python/Django enjoy the same thing (I'm not talking about simple unit testing like PyUnit)? Thanks for help and time. ...

Working on Dictionary of Classes in Python

Hello. For this example, I have a dictionary, that when I call on it, "Ember Attack" is displayed. #import shelve class Pokemon(): """Each pokemon's attributes""" def __init__(self): self.id=[] self.var1=[] self.var2=[] self.var3=[] self.var4=[] self.var5=[] def __str__(self): showList=['id','var1', 'var2'...

How do I use SQL parameters with python?

I am using python 2.7 and pymssql 1.9.908. In .net to query the database I would do something like this: using (SqlCommand com = new SqlCommand("select * from Customer where CustomerId = @CustomerId", connection)) { com.Parameters.AddWithValue("@CustomerID", CustomerID); //Do something with the command } I am trying to figure...

objective C and python - pyobjc

Is it possible for an objective c application to run python files and read their data, ect? If so, can someone post code? or lead me in the right direction? Thanks, Elijah ...

Python's join() won't join the string representation (__str__) of my object.

I'm not sure what I'm doing wrong here: >>> class Stringy(object): ... def __str__(self): ... return "taco" ... def __repr__(self): ... return "taco" ... >>> lunch = Stringy() >>> lunch taco >>> str(lunch) 'taco' >>> '-'.join(('carnitas',lunch)) Traceback (most recent call last): File "<stdin>", line...

Calling cgi.FieldStorage for an arbitrary url

Hi! I'd like to get field values corresponding to an arbitrary URL. I.e. given "http://example.com/hello?q=1&amp;b=1" I want a dictionary {'q':1, 'b':1}. How do I use cgi.FieldStorage for that? Thanks! ...

How to round a number to significant figures in Python

I need to round a float to be displayed in a UI. E.g, to one significant figure: 1234 -> 1000 0.12 -> 0.1 0.012 -> 0.01 0.062 -> 0.06 6253 -> 6000 1999 -> 2000 Is there a nice way to do this using the Python library, or do I have to write it myself? ...

Fastest implementation to do multiple string substitutions in Python

Is there any recommended way to do multiple string substitutions other than doing 'replace' chaining on a string (i.e. text.replace(a, b).replace(c, d).replace(e, f) ...)? How would you, for example, implement a fast function that behaves like PHP's htmlspecialchars in Python? I compared (1) multiple 'replace' method, (2) the regular ex...

Python: Checking if new file is in folder

Hi, I am relatively new to python, but I am trying to create an automated process where my code will listen for new file entries in a directory. For example, someone can manually copy a zip file into a particular folder, and I want my code to recognize the file once it has completely been copied into the folder. The code can then do som...

Summarizing a dictionary of arrays in Python

I got the following dictionary: mydict = { 'foo': [1,19,2,3,24,52,2,6], # sum: 109 'bar': [50,5,9,7,66,3,2,44], # sum: 186 'another': [1,2,3,4,5,6,7,8], # sum: 36 'entry': [0,0,0,2,99,4,33,55], # sum: 193 'onemore': [21,22,23,24,25,26,27,28] # sum: 196 } I need to efficiently filter out and...

Why does the Python 2.7 AMD 64 installer seem to run Python in 32 bit mode?

I've installed Python 2.7 from the python-2.7.amd64.msi package from python.org. It installs and runs correctly, but seems to be in 32-bit mode, despite the fact that the installer was a 64 bit installer. Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" ...

Where does django dev server (manage.py runserver) get its path from?

I recently moved a django app from c:\Users\user\django-projects\foo\foobar to c:\Python25\Lib\site-packages\foo\foobar (which is on the python path). I started a new app in the django-projects directory, and added foo.foobar to the INSTALLED_APPS setting. When I try to run the dev server (manage.py runserver) for my new app, I get the e...

Case Insensitive Translation

I'm displaying certain strings in my app in some places as regular case and in some places as upper case: {% trans item.name %} {% trans item.name.upper %} I'm specifying translations using the .po/.mo files: msgid "Welcome" msgstr "歓迎" And the translation seems to be case-sensitive. 'Welcome' gets translated to '歓迎' but 'WELCOME...

Things to consider when creating a web framework

I am not trying to create yet another web framework. For one of the applications I am working on, I want to create a custom framework. I don't want to use any already available framework. What are the common things to consider? What should be the architecture? Thanks :) ...

Using module's own objects in __main__.py

Hi, I am trying to access a module's own data inside it's __main__.py. The structure is as follow: mymod/ __init__.py __main__.py Now, if I expose a variable in __init__.py like this: __all__ = ['foo'] foo = {'bar': 'baz'} how can I access foo from __main__.py? ...

wxPython: Window and Event Id's

I have a Panel on which I display a StaticBitmap initialised with an id of 2. When I bind a mouse event to the image and call GetId() on the event, it returns -202. Why? import wx class MyFrame(wx.Frame): def __init__(self, parent, id=-1): wx.Frame.__init__(self,parent,id) self.panel = wx.Panel(self,wx.ID_ANY) ...

How to use simplejson to decode following data?

I grab some data from a URL, and search online to find out the data is in in Jason data format, but when I tried to use simplejson.loads(data), it will raise exception. First time deal with jason data, any suggestion how to decode the data? Thanks ================= result = simplejson.loads(data, encoding="utf-8") File "F:\My Doc...

Simple Twisted server won't write with Timer

I'm just learning Python and Twisted and I can't figure out for the life of me why this simple server won't work. The self.transport.write doesn't work when called from a timer. I get no error at all. Any help appreciated. Thank you very much! from twisted.internet.protocol import Factory, Protocol from twisted.internet import reactor f...

Python permutation generator puzzle

I am writing a permutation function that generate all permutations of a list in Python. My question is why this works: def permute(inputData, outputSoFar): for elem in inputData: if elem not in outputSoFar: outputSoFar.append(elem) if len(outputSoFar) == len(inputData): print outputSoF...