python-module

re-import module-under-test to lose context

好久不见. 提交于 2019-11-30 02:44:45
问题 Many Python modules preserve an internal state without defining classes, e.g. logging maintains several loggers accessible via getLogger() . How do you test such a module? Using the standard unittest tools, I would like the various tests inside a TestCase class to re-import my module-under-test so that each time it loses its context. Can this be done? 回答1: import unittest import sys class Test(unittest.TestCase): def tearDown(self): try: del sys.modules['logging'] except KeyError: pass def

Simple data validation

别等时光非礼了梦想. 提交于 2019-11-29 23:04:01
I'm writing a python module that will contain some functions that will manipulate a mongodb database. How can I go about validating input data passed to that function before saving it in database? For example, lets say one of the function in module is createUser(user) which accepts a python dictionary as argument. This dictionary contains user information to save in the database. I want to create an automated validation routine which checks that the dictionary structure matches the database structure. I released "pyvaru" ( https://github.com/daveoncode/pyvaru ) a couple of days ago, it is a

Python: sharing common code among a family of scripts

不打扰是莪最后的温柔 提交于 2019-11-29 23:03:18
I'm writing a family of Python scripts within a project; each script is within a subdirectory of the project, like so: projectroot | |- subproject1 | | | |- script1.main.py | `- script1.merger.py | |- subproject2 | | | |- script2.main.py | |- script2.matcher.py | `- script2.merger.py | `- subproject3 | |- script3.main.py |- script3.converter.py |- script3.matcher.py `- script3.merger.py Now several of the scripts share some code. The shared code is best considered part of the project itself, and not something I would compile separately and make a library out of, or drop in a sitewide

ImportError: libSM.so.6: cannot open shared object file: No such file or directory

让人想犯罪 __ 提交于 2019-11-29 22:37:40
When trying to import OpenCV, using import cv2 I get the following error: /usr/local/lib/python2.7/dist-packages/cv2/__init__.py in <module>() 7 8 # make IDE's (PyCharm) autocompletion happy ----> 9 from .cv2 import * 10 11 # wildcard import above does not import "private" variables like __version__ ImportError: libSM.so.6: cannot open shared object file: No such file or directory Not sure how to fix this - trying to play around with Google's new Colaboratory tool. Notebook is here: https://drive.google.com/file/d/0B7-sJqBiyjCcRmFkMzl6cy1iN0k/view?usp=sharing This fixed the problem by having

A plethora of Python OSC modules - which one to use?

﹥>﹥吖頭↗ 提交于 2019-11-29 22:24:38
Open Sound Control (OSC) is a protocol for communication among computers, sound synthesizers, and other multimedia devices that is optimized for modern networking technology. It is particularly common to use OSC with MAX/MSP -- which in fact is what I am doing, using OSC with Python to talk to another subsystem in MAX. There are a bunch of python modules that support OSC. Great. And they all claim to be simple, useful, and perfect. At the risk of verging into subjective territory, what use cases does your experience suggest for the following modules? python-osc pyOSC SimpleOSC (though this

What is Python's heapq module?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 19:57:24
I tried "heapq" and arrived at the conclusion that my expectations differ from what I see on the screen. I need somebody to explain how it works and where it can be useful. From the book Python Module of the Week under paragraph 2.2 Sorting it is written If you need to maintain a sorted list as you add and remove values, check out heapq. By using the functions in heapq to add or remove items from a list, you can maintain the sort order of the list with low overhead. Here is what I do and get. import heapq heap = [] for i in range(10): heap.append(i) heap [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] heapq

What is the reason for using a wildcard import?

怎甘沉沦 提交于 2019-11-29 18:51:36
I have just learnt about importing modules, and I am a bit confused about the wildcard import. from module_name import * I do not understand the reason of using it at all, I see people saying not to use it at all. Could someone clarify what it really means, and why would one use it? According to [Python 3.Docs]: Modules - More on Modules ( emphasis is mine): There is even a variant to import all names that a module defines: >>> from fibo import * >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 This imports all names except those beginning with an underscore ( _ ). In most cases Python

Where are math.py and sys.py?

不羁的心 提交于 2019-11-29 18:03:21
问题 I found all the other modules in Python33/Lib, but I can't find these. I'm sure there are others "missing" too, but these are the only ones I've noticed. They work just fine when I import them, I just can't find them. I checked sys.path and they weren't anywhere in there. Are they built-in or something? 回答1: The math and sys modules are builtins -- for purposes of speed, they're written in C and are directly incorporated into the Python interpreter. To get a full list of all builtins, you can

Utilising bluetooth on Mac with Python

人走茶凉 提交于 2019-11-29 16:49:39
I've practically scoured the entire web (metaphorically speaking) trying to find a way to work with Bluetooth via Python on Mac. PyBluez is not compatible, Lightblue is not being maintained (tried regardless), installed the PyObC framework (incl Bluetooth Framework), and attempted in both my 2.7.6 and 3.6.1 environments with MacOS 10.10.5. I often get errors similar to the below: Traceback (most recent call last): File "/Users/***/PycharmProjects/Bluey/main.py", line 1, in <module> import bluetooth File "build/bdist.macosx-10.10-intel/egg/bluetooth/__init__.py", line 47, in <module> File

Export decorator that manages __all__

家住魔仙堡 提交于 2019-11-29 10:58:34
A proper Python module will list all its public symbols in a list called __all__ . Managing that list can be tedious, since you'll have to list each symbol twice. Surely there are better ways, probably using decorators so one would merely annotate the exported symbols as @export . How would you write such a decorator? I'm certain there are different ways, so I'd like to see several answers with enough information that users can compare the approaches against one another. You could simply declare the decorator at the module level like this: __all__ = [] def export(obj): __all__.append(obj._