sys

What does “del sys.modules[module]” actually do?

会有一股神秘感。 提交于 2019-11-28 01:03:04
问题 As everyone knows, you can do del sys.modules[module] to delete an imported module. So I was thinking: how is this different from rewriting sys.modules ? An interesting fact is, rewriting sys.modules can't truely delete a module. # a_module.py print("a module imported") Then import sys def func1(): import a_module # del sys.modules['a_module'] sys.modules = { k: v for k, v in sys.modules.items() if 'a_module' not in k} print('a_module' not in sys.modules) # True def func2(): import a_module

Problem with sys.argv[1] when unittest module is in a script

China☆狼群 提交于 2019-11-27 16:31:10
问题 I have a script that does various things and access paramenters using sys.argv but when the script gets to the unittest part of the code it says there is no module for this. The script that I have is: class MyScript(): def __init__(self): self.value = sys.argv[1] def hello(self): print self.value def suite(self): modules_to_test = ('external_sanity_onvif', 'starttest') alltests = unittest.TestSuite() for module in map(__import__, modules_to_test): alltests.addTest(unittest.findTestCases

Cache-as-Ram (no fill mode) Executable Code

会有一股神秘感。 提交于 2019-11-27 15:08:55
问题 I have read about cache-as-ram mode (no-fill mode) numerous times and am wondering whether number one, can executable code be written and jumped to and if so is the executable code restricted to half of the level one cache (since the cache is really just sram). 回答1: Coreboot originally used CAR to save C stack in L1 data cache: http://rere.qmqm.pl/~mirq/cache_as_ram_lb_09142006.pdf http://www.coreboot.org/images/6/6c/LBCar.pdf To execute code, we should switch unified L2 to CAR mode, then L1i

How to finish sys.stdin.readlines() input?

妖精的绣舞 提交于 2019-11-27 12:06:14
This might be a silly question, but as I can't find an answer, I have to ask it. In interactive python I want to process a message which i get with: >>> message = sys.stdin.readlines() Everything works fine, but... how to stop it from getting an input and make it save into message variable? Stopping with ctrl+c stops whole process so there is no input to be saved anywhere. I guess there's an easy answer I just can't find... For unix based system : Hello, you can tape : Ctrl d Ctrl d closes the standard input (stdin) by sending EOF . Example : >>> import sys >>> message = sys.stdin.readlines()

What is the difference between a stack and a frame?

无人久伴 提交于 2019-11-27 09:23:49
问题 Under what situations would I want to use one over the other? What is the difference between: >>> import inspect >>> print(inspect.getouterframes(inspect.currentframe())) [(<frame object at 0x8fc262c>, '<stdin>', 1, '<module>', None, None)] And: >>> import traceback >>> traceback.extract_stack() [('<stdin>', 1, '<module>', None)] Update: Another: >>> import sys >>> print(sys._getframe().f_trace,sys._getframe().f_code) (None, <code object <module> at 0x8682a88, file "<stdin>", line 1>) I do

Import from sibling directory

徘徊边缘 提交于 2019-11-27 07:05:41
I have a Python class called "ClassA" and another Python class which is supposed to import ClassA which is "ClassB". The directory structure is as follows: MainDir ../Dir ..../DirA/ClassA ..../DirB/ClassB How would I use sys.path so that ClassB can use ClassA? You really should be using packages. Then MainDir is placed at a point in the file system on sys.path (e.g. .../site-packages), then you can say in ClassB: from MainDir.Dir.DirA import ClassA # which is actually a module You just have to place files named __init__.py in each directory to make it a package hierarchy. as a literal answer

Python memory usage of numpy arrays

亡梦爱人 提交于 2019-11-27 03:24:39
I'm using python to analyse some large files and I'm running into memory issues, so I've been using sys.getsizeof() to try and keep track of the usage, but it's behaviour with numpy arrays is bizarre. Here's an example involving a map of albedos that I'm having to open: >>> import numpy as np >>> import struct >>> from sys import getsizeof >>> f = open('Albedo_map.assoc', 'rb') >>> getsizeof(f) 144 >>> albedo = struct.unpack('%df' % (7200*3600), f.read(7200*3600*4)) >>> getsizeof(albedo) 207360056 >>> albedo = np.array(albedo).reshape(3600,7200) >>> getsizeof(albedo) 80 Well the data's still

Python: Which encoding is used for processing sys.argv?

我们两清 提交于 2019-11-27 02:05:51
问题 In what encoding are the elements of sys.argv , in Python? are they encoded with the sys.getdefaultencoding() encoding? sys.getdefaultencoding(): Return the name of the current default string encoding used by the Unicode implementation. PS : As pointed out in some of the answers, sys.stdin.encoding would indeed be a better guess . I would love to see a definitive answer to this question, though, with pointers to solid sources! PPS : As Wim pointed out, Python 3 solves this issue by putting

What does 'sys.argv' mean?

放肆的年华 提交于 2019-11-27 02:05:43
问题 I am learning from code, and I am get confused by one of its lines which is: things = [float(arg) for arg in sys.argv[1:]] Omega_a, Omega_b, Delta_a, Delta_b, \ init_pop_a, init_pop_b, tstep, tfinal = things I have searched online and tried to understand what sys.arg means, and here is my understanding: So sys.argv[0] is the file name, and sys.argv[1:] is the rest of the parameters which should given by users. I am not sure am I understood it right, and if it is, then I don't understand why

Set LD_LIBRARY_PATH before importing in python

送分小仙女□ 提交于 2019-11-27 02:02:59
问题 Python uses the PYTHONPATH environment-variable to determine in which folders it should look for modules. You can play around with it by modifying sys.path , which works nicely for pure Python-Modules. But when a module uses shared object files or static libraries, it looks for those in LD_LIBRARY_PATH (on linux), but this can't be changed as easily and is platform dependent as far as I know. The quick-fix for this problem is of course to set the environment-variable or invoke the script like