python-3.x

Python 3: timestamp to datetime: where does this additional hour come from?

I'm using the following functions: # The epoch used in the datetime API. EPOCH = datetime.datetime.fromtimestamp(0) def timedelta_to_seconds(delta): seconds = (delta.microseconds * 1e6) + delta.seconds + (delta.days * 86400) seconds = abs(seconds) return seconds def datetime_to_timestamp(date, epoch=EPOCH): # Ensure w...

Python 3: unpack inner lists in list comprehension

I'm running the following code on a list of strings to return a list of its words: words = [re.split('\\s+', line) for line in lines] However, I end up getting something like: [['import', 're', ''], ['', ''], ['def', 'word_count(filename):', ''], ...] As opposed to the desired: ['import', 're', '', '', '', 'def', 'word_count(filen...

What does a b prefix before a python string means ?

In a python source code I stumbled upon I've seen a small b before a string like in: b"abcdef" I know of u prefix that means unicode and r prefix that means raw. What does the b stand for and in which kind of source code is it useful as it seems to be exactly like a plain string without any prefix ? ...

Regex to split on successions of newline characters

I'm trying to split a string on newline characters (catering for Windows, OS X, and Unix text file newline characters). If there are any succession of these, I want to split on that too and not include any in the result. So, for when splitting the following: "Foo\r\n\r\nDouble Windows\r\rDouble OS X\n\nDouble Unix\r\nWindows\rOS X\nUni...

Regex to match words and those with an apostrophe

Update: As per comments regarding the ambiguity of my question, I've increased the detail in the question. (Terminology: by words I am refering to any succession of alphanumerical characters.) I'm looking for a regex to match the following, verbatim: Words. Words with one apostrophe at the beginning. Words with any number of non-con...

error in python d not defined.

Hi I am learning python and have this error . I can figure out where\what the error is in the code. File "<string>", line 1, in <module>. Name = "" Desc = "" Gender = "" Race = "" # Prompt user for user-defined information Name = input('What is your Name? ') Desc = input('Describe yourself: ') When i run the program it outputs Wha...

How to enter decimal/binary numbers when creating byte objects in python?

I'm using python 3.1.1. I know that I can create byte objects using the byte literal in the form of b'...'. In these byte objects, each byte can be represented as a character(in ascii code if I'm not wrong) or as a hexadecimal/octal number. Hexadecimal and octal numbers can be entered using an escape of \x for hexadecimal numbers and ju...

Help me finish this Python 3.x self-challenge.

This is not homework. I saw this article praising Linq library and how great it is for doing combinatorics stuff, and I thought to myself: Python can do it in a more readable fashion. After half hour of dabbing with Python I failed. Please finish where I left off. Also, do it in the most Pythonic and efficient way possible please. fro...

pexpect install problem

i have installed pexpect with the following command "python setup.py install" but when i try to run a program it says "AttributeError: 'module' object has no attribute 'spawn'". how to settle the matter? ...

Moving a turtle to the center of a circle.

I've just started using the turtle graphics program, but I can't figure out how to move the turtle automatically to the center of a circle (no matter where the circle is located) without it drawing any lines. I thought I could use the goto.() function but it's too specific and I need something general. ...

List attributes of an object

Is there a way to grab a list of attributes that exist on instances of a class? (This class is just an bland example, it is not my task at hand.) class new_class(): def __init__(self, number): self.multi = int(number) * 2 self.str = str(number) a = new_class(2) print(', '.join(a.SOMETHING)) The desired result is ...

cant download youtube video

I'm having trouble retrieving the youtube video automatically, heres the code. The problem is the last part. download = urllib.request.urlopen(download_url).read() # Youtube video download script # 10n1z3d[at]w[dot]cn import urllib.request import sys print("\n--------------------------") print (" Youtube Video ...

Type hinting in Python

I'm studying Python after a lot of PHP experience and it would be handy to have type-hinting in Python. Looks like eclipse + pydev doesn't support this. Any suggestions? For example, I want my IDE to show function docstrings and types, when I use it, like: def f(x: int) -> int: r"""Adds 3 to x""" return x + 3 f( #and now IDE...

Make the Move to Python 3 - Best practices

We think about whether we should convert a quite large python web application to Python 3 in the near future. All experiences, possible challenges or guidelines are highly appreciated. ...

Convert Python 3.x snippet to C#/LINQ.

I want to sort elements of a HashSet<string> and join them by a ; character. Python interpreter version: >>> st = {'jhg', 'uywer', 'nbcm', 'utr'} >>> strng = ';'.join(sorted(s)) >>> strng 'ASD;anmbh;ashgg;jhghjg' C# signature of a method I seek: private string getVersionsSorted(HashSet<string> versions); I can do this without usin...

How to create ASCII animation in a console application using Python 3.x?

I would like to port this question to Python (Windows + Linux + Mac Os) http://stackoverflow.com/questions/2725529/how-to-create-ascii-animation-in-windows-console-application-using-c Thank you! ...

Passing parameter to base class constructor or using instance variable?

All classes derived from a certain base class have to define an attribute called "path". In the sense of duck typing I could rely upon definition in the subclasses: class Base: pass # no "path" variable here def Sub(Base): def __init__(self): self.path = "something/" Another possiblity would be to use the base class c...

Replacement for htmllib module in Python 3.0

I want to use the htmllib module but it's been removed from Python 3.0. Does anyone know what's the replacement for this module? ...

Convert date, datetime to timestamp

How can I produce a timestamp to millisecond accuracy from a date or datetime in Python? There are an overwhelming number of methods and ways of doing this, and I'm wondering which is the most Pythonic way. ...

Getting syntax errors when building with python 3.1 and not with python 2.6

Hi All, Using Mac os X 10.6, python 3.1 and gcc 4.0 trying to build pyfsevent implemented in python2.6 by converting it to python 3.1. Wondering when getting the following errors while building in python 3.1 alone. Why not when building with python 2.6? error: syntax error before ‘CFFileDescriptorRef’ warning: no semicolon at end o...