小编Art*_*rti的帖子

Python安全方法获取嵌套字典的值

我有一个嵌套字典.是否只有一种方法可以安全地获得价值?

try:
    example_dict['key1']['key2']
except KeyError:
    pass
Run Code Online (Sandbox Code Playgroud)

或者python有一个像get()嵌套字典的方法?

python methods dictionary except

111
推荐指数
11
解决办法
9万
查看次数

将列表解压缩到变量

我有一个清单:

row = ["Title", "url", 33, "title2", "keyword"]
Run Code Online (Sandbox Code Playgroud)

是否有更多pythonic方式来解压缩这些值,如:

title, url, price, title2, keyword = row[0], row[1], row[2], row[3], row[4]
Run Code Online (Sandbox Code Playgroud)

python list

15
推荐指数
3
解决办法
9078
查看次数

Selenium安装Marionette webdriver

我有这个问题与firefox版本47 https://github.com/seleniumhq/selenium/issues/2110

所以,我尝试添加木偶网络驱动程序来解决这个问题:https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

但:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/Users/myproject/geckodriver-0.8.0-OSX'
Run Code Online (Sandbox Code Playgroud)

返回错误:

selenium.common.exceptions.WebDriverException:消息:'wires'可执行文件需要在PATH中.

异常AttributeError:"'服务'对象在>忽略中没有属性'process'"

硒== 2.53.5

python firefox selenium firefox-marionette

12
推荐指数
2
解决办法
2万
查看次数

将多个参数传递给addTarget

在我的UITableViewCell我有一个按钮.我想通过在cellForRowAtIndexPath方法中传递多个参数来添加操作.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:
        indexPath) as! CartTableViewCell
    cell.buyButton.addTarget(self, action: self.buyButton(indexPath, 2, 3 ,4 , 5, 6), forControlEvents: .TouchUpInside)
}
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift

10
推荐指数
4
解决办法
2万
查看次数

CloudKit和Core在设备之间同步数据

我创建了一个Notes基于的简单应用程序Core data.现在我想在用户设备之间添加同步.

我读过的所有文章(这个,这个)说我应该完全摆脱我的核心数据库,我的所有代码都将丢失并重新编写所有内容ICloud kit

无法将数据同步Core DataICloud?如果我摆脱核心数据,我的应用程序如何脱机工作?

请建议使用ICloud同步的简单项目

PS我用Google搜索了大约一个小时.Icloud已弃用,因此不建议与其同步.我很困惑.现在我如何在设备之间同步数据?

core-data ios icloud swift

10
推荐指数
1
解决办法
820
查看次数

Swift单元测试mock类静态方法

我在课堂上有一个静态方法

class A {
    static func myStaticMethod() -> B {
        return B()
    }
}

class B {
    func getTitle() -> String {
        // some functionality here
    }
}
Run Code Online (Sandbox Code Playgroud)

在我想要测试的类方法中,我使用它:

func someBoolFunc() -> Bool {
    var b = A.myStaticMethod()
    if (b.getTitle() = “hello”) {
         return true
    }
    return false
}
Run Code Online (Sandbox Code Playgroud)

如何为此编写模拟类...我尝试过:

class MockA: A {
    var myTitle:String
    // this seems incorrect, because i didn't override static function
    func myStaticMethod() -> MockB {
        var b = MockB()
        b.title = myTitle
        return b …
Run Code Online (Sandbox Code Playgroud)

macos unit-testing ios swift

10
推荐指数
1
解决办法
3110
查看次数

返回SQLAlchemy结果作为dicts而不是列表

当我检查查询的结果时,它看起来像一个列表列表.我想返回一个dicts列表,将列名映射到结果值.如何将结果行转换为dicts?

results = db.session.query(
    PendingPost.campaign_id.label('campaign_id'),
    Campaign.title.label('title'),
    sqlalchemy.func.count(PendingPost.status).label('status_count'),
).join(
    Campaign, Campaign.id == PendingPost.campaign_id,
).join(
    Areas, Areas.id == PendingPost.area_id
).filter(
    sqlalchemy.func.month(PendingPost.creation_date) == datetime.datetime.utcnow().month
).group_by(
    PendingPost.status,
    PendingPost.campaign_id,
).all()

print(results)
[(3, 'campaign title', 1),
 (4, 'campaign title', 1)]
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy

9
推荐指数
2
解决办法
4509
查看次数

Django在内联表单管理中获取实例

有一个内联表单类:

class ItemColorSelectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ItemColorSelectForm, self).__init__(*args, **kwargs)
        #here i need current object
Run Code Online (Sandbox Code Playgroud)

内联类:

class ItemColorSelectInline(generic.GenericTabularInline):
    model = ColorSelect
    extra = 1
    form = ItemColorSelectForm
Run Code Online (Sandbox Code Playgroud)

管理员班

class ItemAdmin(admin.ModelAdmin):
    inlines = [ItemColorInline,]
Run Code Online (Sandbox Code Playgroud)

问题:如何获取当前对象ItemColorSelectForm.

print kwargs 返回:

{'auto_id': u'id_%s', 'prefix': u'catalog-colorselect-content_type-object_id-__prefix__', 'empty_permitted': True}
Run Code Online (Sandbox Code Playgroud)

django inline django-forms django-admin python-2.7

8
推荐指数
2
解决办法
4125
查看次数

USB设备发送/接收数据

我已经实现了检测USB设备的功能.它工作,现在我需要发送/读取数据.

我开始查看很多obj-c来源,发现苹果文档中只有一篇好文章,它描述了我们如何将包发送到我们的USB设备:

IOReturn WriteToDevice(IOUSBDeviceInterface **dev, UInt16 deviceAddress,
                        UInt16 length, UInt8 writeBuffer[])
{

    IOUSBDevRequest     request;
    request.bmRequestType = USBmakebmRequestType(kUSBOut, kUSBVendor,
                                                kUSBDevice);
    request.bRequest = 0xa0;
    request.wValue = deviceAddress;
    request.wIndex = 0;
    request.wLength = length;
    request.pData = writeBuffer;

    return (*dev)->DeviceRequest(dev, &request);
}
Run Code Online (Sandbox Code Playgroud)

但我没有找到如何创建和发送数据的方法Swift.结构Swift看起来像:

public struct IOUSBDevRequest {
    public var bmRequestType: UInt8
    public var bRequest: UInt8
    public var wValue: UInt16
    public var wIndex: UInt16
    public var wLength: UInt16
    public var pData: UnsafeMutableRawPointer!
    public var wLenDone: UInt32
    public …
Run Code Online (Sandbox Code Playgroud)

macos cocoa objective-c swift

8
推荐指数
2
解决办法
2045
查看次数

Chart.js 在线性图上拖动点

我有一个使用Chart.js库构建的简单线性图表。

我希望允许用户在图表上拖动点以动态更改它的数据。我绑定了chartjs-plugin-draggable但它只适用于我的注释。我需要完全像这样的图表:

https://www.rgraph.net/canvas/docs/adjusting-line.html

但是在项目中使用新的图形库并不是一个好的解决方案:(

我也尝试玩点事件。

更新

使用 angular 我创建了这样的东西。 在此处输入图片说明

也许如果没有办法在点上添加拖放,将会有一个技巧,可以在点位置上放置具有绝对位置的“滑块”。我也没有找到任何信息:(

javascript charts chart.js

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