python-3.4

Should 3.4 enums use UPPER_CASE_WITH_UNDERSCORES?

筅森魡賤 提交于 2020-08-21 07:03:10
问题 As the documentation says, an enumeration is a set of symbolic names (members) bound to unique, constant values. The PEP8 says that constants are usually named as UPPER_CASE , should I use this notation in Python 3.4 enums? If yes, why the examples in the docs are using lower_case ? 回答1: Update The BDFL (Benevolent Dictator For Life) has spoken, and the Enum documentation has changed to reflect all upper-case member names. The examples in the [previous] docs are lower-case primarily because

How to extract service state via Systemd DBus API?

故事扮演 提交于 2020-08-08 06:45:05
问题 I'm trying to extract openvpn.service status using Systemd D-Bus API. In [1]: import dbus In [2]: sysbus = dbus.SystemBus() In [3]: systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') In [4]: manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager') In [5]: service = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Service') In [6]: unit = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Unit') In [7]: unit.ActiveState('openvpn.service') ----

How can I construct an enum.Enum from a dictionary of values?

≯℡__Kan透↙ 提交于 2020-08-04 04:26:19
问题 I'd like to generate some types at runtime from a config file. For simplity, let's assume I already have the data loaded as a python dictionary: color_values = dict(RED = 1, YELLOW = 2, GREEN = 3) How can I transform this into the type (using enum) class Color(enum.Enum): RED = 1 YELLOW = 2 GREEN = 3 The following doesn't work def make_enum(name, values): return type(name, (enum.Enum,), values) >>> Color = make_enum('Color', color_values) AttributeError: 'dict' object has no attribute '

How can I construct an enum.Enum from a dictionary of values?

大兔子大兔子 提交于 2020-08-04 04:26:17
问题 I'd like to generate some types at runtime from a config file. For simplity, let's assume I already have the data loaded as a python dictionary: color_values = dict(RED = 1, YELLOW = 2, GREEN = 3) How can I transform this into the type (using enum) class Color(enum.Enum): RED = 1 YELLOW = 2 GREEN = 3 The following doesn't work def make_enum(name, values): return type(name, (enum.Enum,), values) >>> Color = make_enum('Color', color_values) AttributeError: 'dict' object has no attribute '

Python3 no such file or directory

自古美人都是妖i 提交于 2020-07-18 07:10:08
问题 I am trying to make python3 executable scripts and run them from shell.I have python 3.4.0 installed on my system. So, I added '/home/spandan/python_codes' directory to PYTHONPATH , as I am planning to keep my scripts and modules here. However, while trying to execute these, the above error is thrown by the system, and the scripts wont execute unless I go into the python_codes directory and then execute them. Executing python program : Here I found out that PYTHONPATH is irrelevant while

Upload File to Google-drive Teamdrive folder with PyDrive

北城余情 提交于 2020-06-26 05:21:28
问题 I have been successfully uploading files to a google-drive-folder with PyDrive. But, when it comes to uploading files to a folder in a google-drive-teamdrive-folder which is shared with me, the following code is not working. from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) location_to_save = "D:\images" mImageLoc = location_to_save + "\\abcd.jpg" #[...Code to fetch and save the file as abcd.jpg

How to print out a numbered list in Python 3

故事扮演 提交于 2020-06-25 06:44:13
问题 How do I print out the index location of each of a python list so that it starts at 1, rather than 0. Here's an idea of what I want it to look like: blob = ["a", "b", "c", "d", "e", "f"] for i in blob: print(???) Output: 1 a 2 b 3 c 4 d 5 e What I need to know is how do I get those numbers to show up alongside what I'm trying to print out? I can get a-e printed out no problem, but I can't figure out how to number the list. 回答1: for a, b in enumerate(blob, 1): print '{} {}'.format(a, b) 回答2:

Export C++ function to python using ctypes: undefined symbol

会有一股神秘感。 提交于 2020-05-29 07:13:31
问题 Consider this file containing two similar functions: #include <iostream> int main() { std::cout << "main\n"; } int notmain() { std::cout << "notmain\n"; } I compiled this into a shared library: g++ -shared -Wl,-soname,code -o code.so -fPIC code.cpp I wish to call these from python, for main this works fine: import ctypes libc = ctypes.cdll.LoadLibrary("code.so") libc.main() Which prints main . However, notmain doesn't work: import ctypes libc = ctypes.cdll.LoadLibrary("code.so") libc.notmain(

python 3.4 multiprocessing does not work with unittest

我是研究僧i 提交于 2020-05-27 06:22:10
问题 I have an unittest which is using multiprocessing. After upgrading from Python 3.2 to Python 3.4 I get following error. I can't find a hint, what was changed inside Python and what I have to change, to make my code running. Thanks in advance. Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Python341_64\lib\multiprocessing\spawn.py", line 106, in spawn_main exitcode = _main(fd) File "C:\Python341_64\lib\multiprocessing\spawn.py", line 116, in _main self =

Error installing PyCrypto on Mac 10.9.5

。_饼干妹妹 提交于 2020-05-15 18:04:12
问题 i'm trying to install PyCrypto for Python 3.4 but i'm having problem with the C compiled programs. it says that "error: cannot run C compiled program". anyone have an idea how to fix it?? code: running build running build_py running build_ext running build_configure checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... configure: error: in `