python-module

re-import module-under-test to lose context

依然范特西╮ 提交于 2019-11-30 18:51:38
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? import unittest import sys class Test(unittest.TestCase): def tearDown(self): try: del sys.modules['logging'] except KeyError: pass def test_logging(self): import logging logging.foo=1 def test_logging2(self): import logging print(logging.foo) if

python module for nslookup

空扰寡人 提交于 2019-11-30 17:04:51
Is there a python-module that's doing the same stuff as nslookup does? I am planning to use nslookup on digging some information regarding the domain of a URL to be scrapped. I know I can use os.sys to call nslookup but I am just wondering if there is a python-module for this already. Thanks in advance! PajE I'm using the following code: import socket ip_list = [] ais = socket.getaddrinfo("www.yahoo.com",0,0,0,0) for result in ais: ip_list.append(result[-1][0]) ip_list = list(set(ip_list)) Or using a comprehension as: ip_list = list({addr[-1][0] for addr in socket.getaddrinfo(name, 0, 0, 0, 0)

How to use __init__.py in (sub-)modules to define namespaces?

南楼画角 提交于 2019-11-30 15:36:40
my question is about writing Python (3.x) packages and (sub-)modules and the correct usage of __init__.py files to declare namespaces. I used to code in C++, so I like to use a lot of separate files to organize projects. For example, (imo) if a module contains several classes, each of these should be in a separate file. As I am inexperienced developing in Python, it is hard to formulate my thoughts in a simple question. So let's consider the following small python package as an example. Directory layout dir | +-- example | | | +-- script.py | +-- package | +-- __init__.py | +-- foo.py | +--

Reload a Module in Python 3.4 [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 13:43:11
问题 This question already has an answer here : Python3 - reload() can not be called on __import__ object? (1 answer) Closed 4 years ago . I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2.7), I would simply type in reload(the_module) and it would reload my module that includes the changes that I have made. However, when I am reloading the module

How do I document a module in Python?

拜拜、爱过 提交于 2019-11-30 13:36:59
问题 That's it. If you want to document a function or a class, you put a string just after the definition. For instance: def foo(): """This function does nothing.""" pass But what about a module? How can I document what a file.py does? 回答1: For the packages, you can document it in __init__.py . For the modules, you can add a docstring simply in the module file. All the information is here: http://www.python.org/dev/peps/pep-0257/ 回答2: Add your docstring as the first statement in the module. """

Where are math.py and sys.py?

自古美人都是妖i 提交于 2019-11-30 12:10:09
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? 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 run: >>> import sys >>> sys.builtin_module_names On my machine, that results in the following list: _

Reload a Module in Python 3.4 [duplicate]

人走茶凉 提交于 2019-11-30 08:15:43
This question already has an answer here: Python3 - reload() can not be called on __import__ object? 1 answer I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2.7), I would simply type in reload(the_module) and it would reload my module that includes the changes that I have made. However, when I am reloading the module here (on windows python v. 3.4), it simply gives me this: >>> reload(instfile) Traceback (most recent call last): File "<pyshell

why __builtins__ is both module and dict

岁酱吖の 提交于 2019-11-30 08:11:35
问题 I am using the built-in module to insert a few instances, so they can be accessed globally for debugging purposes. The problem with the __builtins__ module is that it is a module in a main script and is a dict in modules, but as my script depending on cases can be a main script or a module, I have to do this: if isinstance(__builtins__, dict): __builtins__['g_frame'] = 'xxx' else: setattr(__builtins__, 'g_frame', 'xxx') Is there a workaround, shorter than this? More importantly, why does _

Trouble Setting Up MySQLdb Module

两盒软妹~` 提交于 2019-11-30 07:44:29
I'm pulling my hair out over here trying to set up MySQLdb on my Mac in order to connect to a remote MySQL server. I have installed the latest C MySQL client libraries I have installed XCode 4 I did naively try to install the module before installing XCode or the client libraries I am attempting to set up the module by running the following commands in terminal: $ sudo python setup.py build $ sudo python setup.py install Both of these commands fail with similar error messages. Here is the first stack trace: running build running build_py copying MySQLdb/release.py -> build/lib.macosx-10.6

Intellij/Pycharm can't debug Python modules

强颜欢笑 提交于 2019-11-30 06:42:41
I use PyCharm / IntelliJ community editions from a wile to write and debug Python scripts, but now I'm trying to debug a Python module , and PyCharm does a wrong command line instruction parsing, causing an execution error, or maybe I'm making a bad configuration. This is my run/debug configuration: And this is executed when I run the module (no problems here): /usr/bin/python3.4 -m histraw But when I debug, this is the output in the IntelliJ console: /usr/bin/python3.4 -m /opt/apps/pycharm/helpers/pydev/pydevd.py --multiproc --client 127.0.0.1 --port 57851 --file histraw /usr/bin/python3.4: