python

Why is getattr() not working like I think it should? I think this code should print 'sss'.

the next is my code: class foo: def __init__(self): self.a = "a" def __getattr__(self,x,defalut): if x in self: return x else:return defalut a=foo() print getattr(a,'b','sss') i know the __getattr__ must be 2 argument,but i want to get a default attribute if the attribute is no being. h...

Specify input() type in Python?

I'm still learning so please forgive if this sounds like a dumb question. Is it possible to define input times, like time, date, currency or that should be verified manually? Like for example: morning = input('Enter morning Time:') evening = input('Enter evening Time:') .. I need (only) time here, how do I make sure that user enters i...

Split a large string into multiple substrings containing 'n' number of words via python

Source text: United States Declaration of Independence How can one split the above source text into a number of sub-strings, containing an 'n' number of words? I use split(' ') to extract each word, however I do not know how to do this with multiple words in one operation. I could run through the list of words that I have, and create...

Python TypeError when using json.dumps

When I do json.dumps with a dictionary that maps strings to a list of unicodes, python raises a type error. Why does that not work? ...

Error when compiling simple python program.

This script won't compile. I wanted to made a simple 21-style game for practice but I get an error: X@X:~/Desktop$ python 21.py File "21.py", line 18 int(ptotal) = ptotal + newcard SyntaxError: can't assign to function call Here's the code. Can anyone please help me? I'm obviously a beginner and the code is pretty sloppy. ...

encode Netbios name python

Hi alls, I would like to encode "ITSATEST" to it's netbios name value in python; The occurence table and explication are here: http://support.microsoft.com/kb/194203 I dont know how this could be done easily in python, someone can give me a hand ? Thanks ! ...

Python epoll.register threadsafe?

Does anyone know if I can call epoll.register from another thread safely? Here is what I am imagining: Thread 1: epoll.poll() Thread 2: adding some fd to the same epoll object with epoll.register http://docs.python.org/library/select.html ...

why my code run wrong ,it is about '@property'.

i used python 2.5,i want to know how can change the next code when the Platform is python2.5 or python2.6 class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.delete...

Change sound output

Is there a way in windows by which I can toggle the audio output between a built-in speaker and the headphone jack using a python library. I am thinking someone with .NET experience would be able to give me some pointers (I could use IronPython if there is a .NET library to do that). I have no idea where to start. Any hints would help....

Calculate time between time-1 to time-2?

enter time-1 // eg 01:12 enter time-2 // eg 18:59 calculate: time-1 to time-2 / 12 // i.e time between 01:12 to 18:59 divided by 12 How can it be done in Python. I'm a beginner so I really have no clue where to start. Edited to add: I don't want a timer. Both time-1 and time-2 are entered by the user manually. Thanks in advance fo...

File downloading using python with threads

I'm creating a python script which accepts a path to a remote file and an n number of threads. The file's size will be divided by the number of threads, when each thread completes I want them to append the fetch data to a local file. How do I manage it so that the order in which the threads where generated will append to the local file ...

Implementing a popularity algorithm in Django

I am creating a site similar to reddit and hacker news that has a database of links and votes. I am implementing hacker news' popularity algorithm and things are going pretty swimmingly until it comes to actually gathering up these links and displaying them. The algorithm is simple: Y Combinator's Hacker News: Popularity = (p - 1) / ...

2to3 not working

I'm converting a single module using 2to3. test_lib2to3.py is in /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/test/test_lib2to3.py File to be converted is in /Users/Nimbuz/Documents/python31/Excercise 1/time3.py Terminal Session: localhost:test Nimbuz$ 2to3 /Users/Nimbuz/Documents/python31/Excercise\ 1/time3.py Ref...

ImageFont's getsize() does not get correct text size?

I use the following two methods to to generate text preview image for a .ttf font file PIL method: def make_preview(text, fontfile, imagefile, fontsize=30): try: font = ImageFont.truetype(fontfile, fontsize) text_width, text_height = font.getsize(text) img = Image.new('RGBA', (text_width, text_height)) ...

What should a software engineer (web) start by learning - Erlang, Haskell, Python, C++, F#

What would you suggest what will be the next choice of language that could benefit an engineer in his career utmost? I am a Software Engineer and almost everything I engineered is for WWW. I have decided to learn a language and keep it learning parallel. I'm fluent in C# and JavaScript. But, apart from it I want to learn a language whic...

Remove all nested blocks, whilst leaving non-nested blocks alone via python

Source: [This] is some text with [some [blocks that are nested [in a [variety] of ways]]] Resultant text: [This] is some text with I don't think you can do a regex for this, from looking at the threads at stack overflow. Is there a simple way to to do this -> or must one reach for pyparsing (or other parsing library)? ...

How can I pass a variable in a decorator to function's argument in a decorated function?

Hello, I am in progress to learn Python. Hopefully someone points me to correct way. This is what I'd like to do below: def decorate(function): def wrap_function(*args, **kwargs): str = 'Hello!' # This is what I want return function(*args, **kwargs) return wrap_function @decorate def print_message(): # I'd ...

how to make a python array of particular objects

in java, the following code defines an array of the predefined class (myCls): myCls arr[] = new myCls how can I do that in python? I want to have an array of type (myCls)? thanks in advance ...

Python: error while checking IE state

Please, help me with Python 2.6 and win32com. I'm a newbie to Python and I got error when I start the next program: import pywintypes from win32com.client import Dispatch from time import sleep ie = Dispatch("InternetExplorer.Application") ie.visible=1 url='hotfile.com' ie.navigate(url) while ie.ReadyState !=4: sleep(1) print 'O...

How do I send attachments using SMTP?

I want to write a program that sends email using Python's smtplib. I searched through the document and the RFCs, but couldn't find anything related to attachments. Thus, I'm sure there's some higher-level concept I'm missing out on. Can someone clue me in on how attachments work in SMTP? ...