10月 20

swift2 弹出窗口

xcode7 弹出提醒等简单窗口
最简单方法
UIAlertView(title: “提醒”, message:”登陆失败,请确认账号和密码信息”, delegate: nil, cancelButtonTitle: “确定”).show()

如弹出窗口还有其他操作。

func showAlert(){
        let alertController = UIAlertController(title: "系统提示",
            message: "账号错误", preferredStyle: UIAlertControllerStyle.Alert)
        let tryAction = UIAlertAction(title: "继续重试", style: UIAlertActionStyle.Cancel, handler: nil)
        let exitAction = UIAlertAction(title: "退出", style: UIAlertActionStyle.Default,
            handler: {
                ACTION in exit(0)
        })
        alertController.addAction(tryAction)
        alertController.addAction(exitAction)
        self.presentViewController(alertController, animated: true, completion: nil)
}
9月 25

python 字符串型二维数组转换

  问题场景:需要rsync一批文件,每个rsync信息又是一个list。想将这部分信息以字符串的形式存到mysql中。使用pickle模块做持久化,更为智能,详见http://www.simonzhang.net/?p=598。我想方便手动在数据库里手动编写。就写了以下部分。此次测试虽然实现字符串转列表功能,但是不能处理字符串、数字型等高级问题,只是自己用着方便(重新造轮子)。

#!/bin/env python
# -*- coding:utf-8 -*-
# Date:        2015-09-23
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
import sys
from string import strip

# 获得参数
a= sys.argv[1]
# 初始化一个列表
rsync_info = []
# 父级列表之间用"?"分割
# 循环处理父级的
f_list = a.split('?')
for f_num in xrange(0, len(f_list)):
    # 每次循环增加一个子列表,如果但是单个数据则直接添加
    if f_list[f_num][0] == "[":
        # 两级列表,要现增加一个新的
        rsync_info.append([])
        for s_one in f_list[f_num][1:-1].split(','):
            rsync_info[f_num].append(strip(s_one))
    else:
        rsync_info.append(f_list[f_num])
print rsync_info

执行测试成功
python test.py “[aa, 123| 4.56]?[bb, 78|9/10]?cc”

9月 22

python获取 需要的 随机数格式

要产生自己需要的随机数格式。python2.7.

# 产生随机数,状态有三种,num为数字,let为字母,mix为混合
def create_random(rand_type, rand_len):
    import random
    list_num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    list_let = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "g", \
                "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", \
                "u", "v", "w", "x", "y", "z"]
    rand_str = ""
    if rand_type == "num":
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_num, 1)[0])
    elif rand_type == "let":
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_let, 1)[0])
    elif rand_type == "mix":
        list_all = list_num + list_let
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_all, 1)[0])
    else:
        rand_str =""
    return rand_str
9月 10

python格式化n天前日期

需要获取n天前,或者n天后的信息。比如获取一天前,格式化为2015-10-09的格式。

导入time,time.time()为unixtime,60*60*24就是60秒乘60分钟乘24小时,减去一天就是一天前,加上一天就是后一天。

import time
time.strftime("%Y-%m-%d", time.localtime(time.time()-60*60*24))

可以自己计算n年,n月,n小时,n秒。