As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I’m doing it in a way that seems simple but naive — by taking the average of each set of points, as in: where x, y and z are arrays of floating-point numbers. I seem to recall
Tag: python
How can I import a module dynamically given the full path?
How do I load a Python module given its full path? Note that the file can be anywhere in the filesystem where the user has access rights. See also: How to import a module given its name as string? Answer For Python 3.5+ use (docs): For Python 3.3 and 3.4 use: (Although this has been deprecated in Python 3.4.) For
Sending mail from Python using SMTP
I’m using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I’m missing ? Answer The script I use is quite similar; I post it here as an example of how to use the email.* modules to generate MIME messages; so this script can be easily modified to
Python dictionary from an object’s fields
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I’d like to do something like this: NOTE: It should not include methods. Only fields. Answer Note that best practice in Python 2.7 is to use new-style classes (not needed with Python 3), i.e. Also, there’s a difference between an ‘object’ and a
How to retrieve an element from a set without removing it?
Suppose the following: How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it – something I can only be sure of after an asynchronous call to another host. Quick and dirty: But do you know of a better way?
How do I check if a list is empty?
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. For example, if passed the following: How do I check to see if a is empty? Answer Using the implicit booleanness of the empty list is quite Pythonic.
Best way to extract text from a Word doc without using COM/automation?
Is there a reasonable way to extract plain text from a Word file that doesn’t depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform – that’s non-negotiable in this case.) Antiword seems like it might be a reasonable option, but it seems like it might be abandoned. A Python solution would
Using Django time/date widgets in custom form
How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view? I have looked through the Django forms documentation, and it briefly mentions django.contrib.admin.widgets, but I don’t know how to use it? Here is my template that I want it applied on. Also, I think it should be noted that I
Validate (X)HTML in Python
What’s the best way to go about validating that a document follows some version of HTML (prefereably that I can specify)? I’d like to be able to know where the failures occur, as in a web-based validator, except in a native Python app. Answer XHTML is easy, use lxml. HTML is harder, since there’s traditionally not been as much interest
How do you run a Python script as a service in Windows?
I am sketching the architecture for a set of programs that share various interrelated objects stored in a database. I want one of the programs to act as a service which provides a higher level interface for operations on these objects, and the other programs to access the objects through that service. I am currently aiming for Python and the