小编nna*_*ski的帖子

kwargs解析最佳实​​践

有更紧凑/有效的方法吗?

    for key in kwargs:
        if key == 'log':
            self.log = kwargs[key]
        elif key == 'bin':
            self.bin = kwargs[key]
        elif key == 'pid':
            self.pid = kwargs[key]
        elif key == 'conf':
            self.conf = kwargs[key]
Run Code Online (Sandbox Code Playgroud)

python kwargs

22
推荐指数
3
解决办法
1万
查看次数

django auth ldap

我一直试图让LDAP - > Django组映射工作没有成功.除了组映射部分外,一切似乎都能正常工作.我的LDAP后端是Active Directory.我正在使用django-auth-ldap 1.0.10.

settings.py:

import ldap, logging
from django_auth_ldap.config import LDAPSearch, ActiveDirectoryGroupType

logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG) 

AUTH_LDAP_SERVER_URI = "ldap://sub.domain.com"
AUTH_LDAP_BIND_DN = 'CN=Bind Account,OU=Users,OU=Users,OU=Chicago,DC=sub,DC=domain,DC=com'
AUTH_LDAP_BIND_PASSWORD = 'passwd'
AUTH_LDAP_USER_SEARCH = LDAPSearch('OU=Users,OU=Users,OU=Chicago,DC=sub,DC=domain,DC=com', ldap.SCOPE_SUBTREE, "(uid=%(user)s)",)
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("OU=Groups,OU=Chicago,DC=sub,DC=domain,DC=com", ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)")

AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType()
AUTH_LDAP_FIND_GROUP_PERMS = True
#AUTH_LDAP_CACHE_GROUPS = True
#AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
AUTH_LDAP_GLOBAL_OPTIONS = {
    ldap.OPT_X_TLS_REQUIRE_CERT: False,
    ldap.OPT_REFERRALS: False,
} 

AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_staff":  "CN=SomeGroup,OU=Groups,OU=Chicago,DC=sub,DC=domain,DC=com",
}

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend', …
Run Code Online (Sandbox Code Playgroud)

authentication django ldap active-directory django-auth-ldap

16
推荐指数
1
解决办法
8092
查看次数

django星期几的模特代表

我有这个"乔布斯服务器"模型,我正在建设.我想要包含一个字段,该字段将保存此作业将在一周中的哪几天运行.最终在用户界面中,我希望用户能够拥有一系列可以选择的复选框(每天一个).在我的模式中表示这个"星期几"数据的最佳方法是什么?

class Job(models.Model):
    name = models.CharField(max_length=32, unique=True)
    package = models.ForeignKey(Package)
    binary = models.ForeignKey(Binary)
    host = models.ForeignKey(Host)
    colo = models.ForeignKey(Location)
    group = models.ForeignKey(Group)
    type = models.ForeignKey(Type)
    start = models.TimeField()
    end = models.TimeField()
    days = ?
Run Code Online (Sandbox Code Playgroud)

django model django-models days

11
推荐指数
3
解决办法
1万
查看次数

在python中获取给定时区的DST边界

是否有可能使用pytz获得给定时区的DST边界?

python pytz

11
推荐指数
1
解决办法
2436
查看次数

导入python模块的问题

我正在尝试bitstring在脚本中使用python的模块,并且导致导入错误.从交互模式运行时不会发生此错误.

这是代码:

import bitstring
b = bitstring.BitArray(bin='001001111')
Run Code Online (Sandbox Code Playgroud)

当像这样运行时:

python test.py
Run Code Online (Sandbox Code Playgroud)

我明白了:

AttributeError: 'module' object has no attribute 'BitArray'
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bitstring
>>> b = bitstring.BitArray(bin='001001111')
>>> print b
0b001001111
Run Code Online (Sandbox Code Playgroud)

它工作得很好!它是由同一个用户运行的相同解释器.有什么指针吗?

python import module

7
推荐指数
2
解决办法
4043
查看次数

python bitarray来往于文件

我正在使用此代码向文件写一个大的bitarray:

import bitarray
bits = bitarray.bitarray(bin='0000011111') #just an example

with open('somefile.bin', 'wb') as fh:
    bits.tofile(fh)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用以下方法读取此数据时:

import bitarray
a = bitarray.bitarray()
with open('somefile.bin', 'rb') as fh:
    bits = a.fromfile(fh)
    print bits
Run Code Online (Sandbox Code Playgroud)

它失败,'bits'是NoneType.我究竟做错了什么?

python bitarray fromfile

4
推荐指数
1
解决办法
4203
查看次数

从本地转换为utc时区

我正在尝试制作一个带有时间对象并将其转换为UTC时间的函数.下面的代码似乎关闭了一个小时.当我通过转换器中午运行时,我回到18:00:00.但是当我通过在线转换器运行相同的数据时,我得到17:00:00.

我在这做错了什么?任何帮助将不胜感激.

import pytz, datetime

def convert_to_utc(time, tz):
    now_dt = datetime.datetime.utcnow()
    #get a date object
    date_dt = now_dt.date()
    #combine the current date object with our given time object
    dt = datetime.datetime.combine(date_dt, time)
    #get an timezone object for the source timezone
    src_tz = pytz.timezone(str(tz))
    #stamp the source datetime object with the src timezone 
    src_dt = dt.replace(tzinfo=src_tz)
    #get the offset from utc to given timezone
    offset = str(int(src_dt.strftime("%z"))).rstrip('0')
    #convert the source datetime object to
    utc_dt = src_dt.astimezone(pytz.utc)
    #return the converted time and …
Run Code Online (Sandbox Code Playgroud)

python timezone pytz

4
推荐指数
1
解决办法
2554
查看次数

python datetime,将工作日的abbrv转换为数字?

我如何使用python datetime模块将工作日缩写(%a)转换为工作日数字(%u)?

python datetime

2
推荐指数
1
解决办法
3898
查看次数