python-3.x

Path of current Python instance?

I need to access the Scripts and tcl sub-directories of the currently executing Python instance's installation directory on Windows. What is the best way to locate these directories? ...

Python 3.3 syntax discussion.

Does anybody else find {:} and {} be a more natural way to construct empty dictionary and empty set as compared to {} and set()? Thanks. ...

Assignment into Python 3.x Buffers with itemsize > 1

I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so: int Image_get_buffer(PyObject* self, Py_buffer* view, int flags) { int img_len; void* img_bytes; // Do my image fetch magic get_ima...

Change list formatting in PyYAML output

This is how PyYAML behaves on my machine: >>> plan = {'Business Plan': ['Collect Underpants', '?', 'Profit']} >>> print(yaml.dump(plan)) Business Plan: [Collect Underpants, '?', Profit] What I want instead is this output (both is valid YAML): Business Plan: - Collect Underpants - '?' - Profit Is there some kind of option that would...

Variable Files with Python

Hi, I am trying to have a file path like 'C:\Programfiles\file.txt' but i would like to have file.txt be a variable that i can change whenever i need to. I am trying to compare 2 directories then copy files from one to another if they arent already there. i have this code so far. import os import shutil A= set(os.listdir(r"C:\Users\Mor...

Pass array of structs from Python to C

[Update: Problem solved! See bottom of the post] I need to allow python developers to pass an array of packed data (in this case vertices) into my API, which is a series of C++ interfaces exposed manually through the Python C API. My initial impression with this is to use the ctypes Structure class to allow for an interface like this: ...

Scheduling Python Programs

How would you go about a having a function check something every ten minutes? I would like to check a directory for new files every ten minutes. I know python has a time library but can it be used for this? ...

Parse JSON as easily as XML can be in Python 3

In Python 3 it is easy to find a given tag in an XML file via etree. Using JSON, I've found there isn't a function to do that. Is there a library that does so efficiently? Is it worth my time to have a tree constructed from some JSON? I'm having to use woeid = data["places"]["place"][0]["woeid"] where with XML I could simply use tree.f...

Efficiently reading a csv file with windows newline on linux in Python

The following is working under windows for reading csv files line by line. f = open(filename, 'r') for line in f: Though when copying the csv file to a linux server, it fails. It should be mentioned that performance is an issue as the csv files are huge. I am therefore concerned about the string copying when using things like strip....

How do I print to the OS's default printer in Python 3 (cross platform)?

I have a Python 3 script that is going to be doing some regex substitution on some Rich Text Files (rtf) and I would like to be able to print out a whole directory's files on Windows, Linux, and Mac. I have done quite a bit of searching to no avail. Thanks in advance. ...

Working with itertools.product and lists in python 3.

I'm trying to create a possible list of codons given a protein sequence. Basically, the script i'm trying to create will process a given string input and outputs a possible combinations of another set of strings the input represents. For example, the character 'F' represents either 'UUU' or 'UUC'; the character 'I' represents either 'A...

Py3k and IPython

I'm moving on up to Python 3, but can't seem to find an IPython release for it. The main IPython release page doesn't list anything appropriate. Any help in getting IPython working for Py3k would be much appreciated. ...

Python 3.1.1 string to hex

I am trying to use str.encode() but I get >>> "hello".encode(hex) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be string, not builtin_function_or_method I have tried a bunch of variations and they seem to all work in Python 2.5.2, so what do I need to do to get them to work in Python 3.1? ...

Python socket data returns <byte> object. How to regexp it?

I'm writing a basic html-proxy in python (3), and up to now I'm not using prebuild classes like http.server. I'm just starting a socket which accepts connection: self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listen_socket.bind((socket.gethostname(), 4321)) self.listen_socket.listen(5) (a, b) = self.listen_...

Socket shutdown and rebind - How to avoid long wait?

I'm working with socket in python, and being in development stage I need to kill and restart my program frequently. The issue is that once killed my python script, I've to wait long time to be able to rebind the listen socket. Here's a snippet to reproduce the problem: #!/usr/bin/env python3 ...

Python 3 object construction: which is the most Pythonic / the accepted way?

Having a background in Java, which is very verbose and strict, I find the ability to mutate Python objects as to give them with fields other than those presented to the constructor really "ugly". Trying to accustom myself to a Pythonic way of thinking, I'm wondering how I should allow my objects to be constructed. My instinct is to hav...

How can I implement a tree in Python? Are there any built in data structures in Python like in Java?

I am trying to construct a general tree. Are there any built in data structures in Python to implement a tree? ...

How do I unescape HTML entities in a string in Python 3.1?

I have looked all around and only found solutions for python 2.6 and earlier, NOTHING on how to do this in python 3.X. (I only have access to Win7 box.) I HAVE to be able to do this in 3.1 and preferably without external libraries. Currently, I have httplib2 installed and access to command-prompt curl (that's how I'm getting the source ...

Python win32crypt.CryptProtectData difference between 2.5 and 3.1?

I'm trying to hash a password to dump into a .rdp file. I found a site that (more or less) shows how to do that here but it doesn't work in 3.1. In 2.5.4 I get this: >>> import win32crypt >>> import binascii >>> pwdHash = win32crypt.CryptProtectData(u"password",u'psw',None,None,None,0) >>> print str(binascii.hexlify(pwdHash)).upper() 0...

TypeError: unsupported operand type(s) for -: 'str' and 'int'

New to python and programing how come I'm getting this error? def cat_n_times(s, n): while s != 0: print(n) s = s - 1 text = input("What would you like the computer to repeat back to you: ") num = input("How many times: ") cat_n_times(num, text) ...