Exemple #1
0
def xls_writer(list_of_list, sheet_name="Sheet1"):
    """
    将一个包含列表的列表写成xls文件

    [
        ["a0", "b0", ...], ...
        ["a1", "b1", ...], ...
    ]

    :param list_of_list: list of list
    :return: xls文件的二进制内容
    """
    xls_file_obj = xlwt.Workbook()
    the_sheet = xls_file_obj.add_sheet(sheet_name, cell_overwrite_ok=True)
    current_row = 0
    for row in list_of_list:
        current_column = 0
        for column in row:
            the_sheet.write(current_row, current_column, safe_unicode(column))
            current_column += 1
        current_row += 1

    filename = _generate_filename(x=False)
    xls_file_obj.save(filename)
    info("wrote xls to: " + filename)
    info("=========================")
    return _read_from_file(filename)
Exemple #2
0
def async_send_sms(tel, msg="", sms_type=SMS_TYPE_NORMAL, code=0):
    tel = safe_unicode(tel)
    msg = safe_unicode(msg)
    sms_data = {
        'tel': tel,
        'type': sms_type,
    }

    if sms_type is SMS_TYPE_NORMAL:
        sms_data['msg'] = msg
    elif sms_type is SMS_TYPE_VERIFICATION_CODE:
        sms_data['code'] = code
        sms_data['msg'] = '您的验证码是{code}(有效期10分钟)'.format(code=code)
    else:
        raise Exception('无效的短信类型')
    RabbitMqCtl.basic_publish(exchange=EXCHANGE_SEND_SMS,
                              body=pickle.dumps(sms_data))
Exemple #3
0
def redirect_get_express(number, **kwargs):
    """
    [HTTPRequest]一个运单的详情
    """
    url = settings.BL_DAS_API_PREFIX + "/express/expr/single/" + sstring.safe_unicode(
        number)
    url = url_concat(url, kwargs)
    return HTTPRequest(url, method="GET", **timeouts)
Exemple #4
0
 def _field_to_json(value):
     ret = value
     if isinstance(value, ObjectId):
         ret = safe_unicode(str(value))
     elif isinstance(value, datetime):
         ret = utc_to_local(value)
     elif isinstance(value, EmbeddedDocument):
         if hasattr(value, "pack"):
             ret = value.pack()
     elif isinstance(value, Document):
         if hasattr(value, "pack"):
             ret = value.pack()
         else:
             ret = safe_unicode(value.pk)
     elif isinstance(value, list):
         ret = [Node._field_to_json(_) for _ in value]
     elif isinstance(value, dict):
         ret = {k: Node._field_to_json(value[k]) for k in value}
     return ret
Exemple #5
0
 def _field_to_json(value):
     ret = value
     if isinstance(value, ObjectId):
         ret = safe_unicode(str(value))
     elif isinstance(value, datetime):  # 从mongodb返回的utc时间
         ret = arrow.get(value).to('local').isoformat()
     elif isinstance(value, EmbeddedDocument):
         if hasattr(value, "pack"):
             ret = value.pack()
     elif isinstance(value, Document):
         if hasattr(value, "pack"):
             ret = value.pack()
         else:
             ret = safe_unicode(value.pk)
     elif isinstance(value, list):
         ret = [Call._field_to_json(_) for _ in value]
     elif isinstance(value, dict):
         ret = {k: Call._field_to_json(value[k]) for k in value}
     return ret
Exemple #6
0
 def _field_to_json(value):
     ret = value
     if isinstance(value, ObjectId):
         ret = safe_unicode(str(value))
     elif isinstance(value, datetime):
         ret = utc_to_local(value)
     elif isinstance(value, EmbeddedDocument):
         if hasattr(value, "pack"):
             ret = value.pack()
     elif isinstance(value, Document):
         if hasattr(value, "pack"):
             ret = value.pack()
         else:
             ret = safe_unicode(value.pk)
     elif isinstance(value, list):
         ret = [Express._field_to_json(_) for _ in value]
     elif isinstance(value, dict):
         ret = {
             k: Express._field_to_json(v)
             for k, v in list(value.items())
         }
     elif isinstance(value, float):
         ret = round(ret, 10)
     return ret
Exemple #7
0
def dump_to_schedule(from_file, _mc):
    schedules = []
    count = 0
    with open(from_file, 'r') as f:
        for line in f:
            count += 1
            values = line.strip().replace(b'\"', b'').split(b',')
            parsed = []
            for v in values:
                if v:
                    parsed.append(safe_unicode(v))

            print(('%s: %s: %s' % (count, len(parsed), ', '.join(parsed))))
            # 人员基本信息行: 刘从志,18218711837,56c811a27f452563e439bcb6,bd74cd1296da48ed8e1a5248c62d6166,司机
            if len(parsed) == 5 and count != 1 and parsed[4] in ('司机', '收派员'):
                name = parsed[0]
                tel = parsed[1]
                man_id = parsed[2]
                client_id = parsed[3]
                m_type = ROLE_TAG_MAN if parsed[4] == '收派员' else ROLE_TAG_DRIVER
                s_basic = dict(
                    man={
                        'id': man_id,
                        'name': name,
                        'tel': tel,
                        'm_type': m_type,
                        'client_id': client_id
                    })
                schedules.append(s_basic)
                # 不是第一个站点基本信息行, 将上一个站点信息记入mongodb
                if len(schedules) > 1:
                    # sort schedule.run
                    schedule = schedules[-2]
                    run = sorted(
                        schedule['run'], lambda x, y: cmp(x['t'], y['t'])
                        if x['t'] != y['t'] else cmp(x['name'], y['name']))
                    schedule['run'] = run
                    schedules[-2] = schedule
                    # 记入db
                    _mc.insert_one(schedules[-2])
            # 时刻行: 09:10,西湖站,余杭塘路/学院路路口(诗曼油压养生),"30.3010580000,120.1346380000"
            elif len(parsed) == 5 and count != 1:
                t = parsed[0]
                name = parsed[1]
                address = parsed[2]
                lat = round(float(parsed[3]), 10)
                lng = round(float(parsed[4]), 10)
                loc = dict(lat=lat, lng=lng, address=address)
                run_row = {
                    't': t,
                    'name': name,
                    'loc': loc,
                }
                if 'run' not in schedules[-1]:
                    schedules[-1]['run'] = [run_row]
                else:
                    schedules[-1]['run'].append(run_row)
        # sort schedule.run
        schedule = schedules[-1]
        run = sorted(
            schedule['run'], lambda x, y: cmp(x['t'], y['t'])
            if x['t'] != y['t'] else cmp(x['name'], y['name']))
        schedule['run'] = run
        schedules[-1] = schedule
        _mc.insert_one(schedules[-1])
Exemple #8
0
15:20,姚磊,13575498576,56e17b28eed093310861162b,dae0929ec4b6417b9b1de8a27954f942
19:40,姚磊,13575498576,56e17b28eed093310861162b,dae0929ec4b6417b9b1de8a27954f942
11:20,章成龙,18668760225,56fb30b5eed093338d22a919,1c85f1b222084688bf60bbc0aadb9a5a
"""
if __name__ == '__main__':
    nodes = []
    count = 0

    with open('从csv拿node.csv', 'r') as f:
        for line in f:
            count += 1
            values = line.strip().replace(b'\"', b'').split(b',')
            parsed = []
            for v in values:
                if v:
                    parsed.append(safe_unicode(v))

            print(('%s: %s' % (len(parsed), parsed)))
            # 站点基本信息行: 萧山站,杭州萧山区建设一路金一路路口,,"30.2012140000,120.2662010000"
            if len(parsed) == 4:
                name = parsed[0]
                address = parsed[1]
                lat = round(float(parsed[2]), 10)
                lng = round(float(parsed[3]), 10)
                node_basic = dict(name=name,
                                  loc={
                                      'lat': lat,
                                      'lng': lng,
                                      'address': address
                                  })
                nodes.append(node_basic)