def update(self, doc, process=None):
        if process is None:
            pk = 'id'
            id = doc.id
            doc.synced = False
            set = doc
        elif process == UPDATE_PROCESS:
            if self.db.docs.find({'sid': doc.sid}).count() == 0:
                return self.insert(doc, synced=True)
            pk = 'sid'
            id = doc.sid
            doc.synced = not doc.merged() # must be commited if was merged
            doc.merged(False)
            set = {'tid': doc.tid, 'data': doc.data, 'user': doc.user,
                   'public': doc.public, 'synced': doc.synced}
        elif process == COMMIT_PROCESS:
            pk = 'id'
            id = doc.id
            doc.synced = True
            set = {'sid': doc.sid, 'tid': doc.tid, 'synced': doc.synced}
        else:
            raise DatabaseException('Incorrect update process')

        try:
            rs = self.db.docs.update({pk: id}, {'$set': set}, safe=True)
            assert rs['n'] == 1
        except Exception as e:
            raise DatabaseException(e)
        return doc
def winding_factors(wdg, n=8, ax=0):
    """plot winding factors"""
    ax = plt.gca()
    ax.set_title(f'Winding factors Q={wdg.Q}, p={wdg.p}, q={round(wdg.q,4)}')
    ax.grid(True)
    order, kwp, kwd, kw = np.array([(n, k1, k2, k3)
                                    for n, k1, k2, k3 in zip(wdg.kw_order(n),
                                                             wdg.kwp(n),
                                                             wdg.kwd(n),
                                                             wdg.kw(n))]).T
    try:
        markerline1, stemlines1, _ = ax.stem(order-1, kwp, 'C1:', basefmt=" ",
                                             markerfmt='C1.',
                                             use_line_collection=True, label='Pitch')
        markerline2, stemlines2, _ = ax.stem(order+1, kwd, 'C2:', basefmt=" ",
                                             markerfmt='C2.',
                                             use_line_collection=True, label='Distribution')
        markerline3, stemlines3, _ = ax.stem(order, kw, 'C0-', basefmt=" ",
                                             markerfmt='C0o',
                                             use_line_collection=True, label='Total')
        ax.set_xticks(order)
        ax.legend()
    except ValueError:  # empty sequence
        pass