Python Show-Me-the-Code 第 0003 题 Redis操作

第 0003 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中。

跟0002题相似,只不过是把关系型数据库换成了非关系型数据库。
步骤:

  • 首先安装Redis和库,用pip安装即可
  • 打开radis server
  • 然后了解下Redis的操作
  • 在代码中引入该库
  • 连接数据库,写入数据,保存

python连接redis很方便,操作也跟redis-cli差不多。

因为生成的激活码应该是不重复的,所以我选择用set这个数据结构保存

redis的set数据类型常用操作有:

  • sadd key member 往集合中key插入成员member,返回实际插入的成员
  • smembers key获取与该key关联的set中所有的成员。
  • sismember key member 检查member是否是集合key中的成员 是的话返回1
  • scard key 获取key中成员的数量

0003.Redis操作.py

#!/usr/bin/env python
#coding: utf-8
import redis
import gennerate_code

HOST =  'localhost'
PORT = 6379

#连接到数据库
r = redis.Redis(HOST,PORT)
#生成200组激活码
codelist = gennerate_code.generate(200)
#将生成的激活码存入数据库中
for i in xrange(200):
    r.sadd("code",codelist[i])
r.save()

其中gennerate_code是Python Show-Me-the-Code 第 0001 题 生成激活码 中的代码

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
myasm51,小型的51单片机汇编器源码。 基于Linux环境下编写的小型的51单片机汇编器,源码开放,采用lex和yacc两个扫描和分析工具创建,代码小巧,易于研读和分析。对汇编源程序2遍扫描完成汇编,可以生成列表文件,Intel的Hex格式的文件及.bin格式的映像文件,后两种文件可以直接下载到单片机上运行。源码程序包内包含若干示例汇编源程序(.asm),proteus的格式的数字种的仿真文件,用以测试编译结果,另有编译后的dos下的可执行文件myasm51.exe,可以在windows的命令窗口下运行。另外提供一个简明的用户手册以供参考。以下为程序包的README: What is Myasm51 =============== Myasm51 is an open source mini-assembler for the Intel MCS-51 family of microcontrollers or the compatible ones, distributed under the GPL license. By scanning the source file in two pass, Myasm51 translates a symbolic code in text file (assembly language source) into a machine executable object file. During the first pass, the assembler builds a symbol table from the symbols and labels used in the source file. In the second pass, the assembler maps the source file into machine code and generates the listing file through what it receives in the first pass. Myasm51 is an absolute assembler and only generates absolute object files in the plain binary file (with .bin extension) or the Intel Hex file (with .Hex extension) which can be read by any ROM programmer to burn the object code into the ROM space of microcontrollers. How to make =========== We assume that the UNIX utilities yacc and lex have been installed in you system, and following these steps to build Myasm51 by the super user 'root' in the Linux or the UNIX cloned system. # tar zxf myasm51-gk-20151208_121306.tar.gz # cd myasm51 # make # cp myasm51 /usr/local/bin done. How to use ========== [root@rh9 myasm51]# cd examples [root@rh9 examples]# myasm51 Myasm51 Assembler. Ver 0.01 Release 1, (20151231_165818) [email protected], Wed Sep 30 17:28:09 CST 2015 built: Dec 31 2015 - 17:04:44 Usage: myasm51 [-o] [-F] [-C] [-d] in.asm where -ob to output binary file 'in.bin' -oh to output hex file 'in.hx' (default format) -oH to output Intel Hex file 'in.Hex' -F to fill free bit with 0 or 1, (default 0) -C to turn on/off symbol case sensitive, (default on) -d to turn on/off the parser debug mode, (default off) [root@rh9 examples]# myasm51 dclk7seg2.asm Myasm51 Assembler. Ver 0.01 Release 1, (20151231_165818) [email protected], Wed Sep 30 17:28:09 CST 2015 built: Dec 31 2015 - 17:04:44 ;;;; Starting 1st Pass... ;;;; 1st Pass proceeded. ;;;; Starting 2nd Pass... ;;;; 2nd Pass proceeded. dclk7seg2.hx, 340(0x154) bytes assembled. [root@rh9 examples]# nl -ba dclk7seg2.hx |more ... 25 0000: | 25 .ORG 0 26 0000: 02 00 30 | 26 PowerON: LJMP Reset 27 | 27 28 0003: | 28 .ORG 0X0003 29 0003: 02 00 03 | 29 EXT_INT0_VECTOR: LJMP EXT_INT0_VECTOR 30 | 30 31 000B: | 31 .ORG 0X000B 32 000B: 02 00 7E | 32 TIMER_T0_VECTOR: LJMP TIMER_T0_INT 33 | 33 34 0013: | 34 .ORG 0X0013 ... [root@rh9 examples]# myasm51 -Cn pm51.asm Myasm51 Assembler. Ver 0.01 Release 1, (20151231_165818) [email protected], Wed Sep 30 17:28:09 CST 2015 built: Dec 31 2015 - 17:04:44 ;;;; Starting 1st Pass... ;;;; 1st Pass proceeded. ;;;; Starting 2nd Pass... ;;;; 2nd Pass proceeded. pm51.hx, 7760(0x1e50) bytes assembled. [root@rh9 examples]# nl -ba pm51.hx |more 1 | 1 ; PAULMON 8051 Debugger by Paul Stoffregen 2 | 2 ; Please distribute freely -- may not be sold, period. 3 | 3 4 | 4 ; .command +h58 ;set page height to 58 in listing file... 5 | 5 6 | 6 .equ start,0000h ;address for start of EPROM (0000h) 7 | 7 .equ program,2000h ;address for program loading location 8 | 8 9 0000: | 9 .ORG start 10 0000: 02 0B 08 | 10 rst: lJMP poweron 11 | 11 12 0003: | 12 .org start+3 ;ext int #0 13 0003: 02 20 03 | 13 LJMP program+3 14 000B: | 14 .org start+11 ;timer #0 15 000B: 02 20 0B | 15 LJMP program+11 16 0013: | 16 .org start+13h ;external interrupt routine #1 17 0013: 30 8A 03 | 17 jnb tcon.2,intr0 18 0016: 02 20 13 | 18 ljmp program+13h ;don't do ssrun if edge trigger'd 19 0019: 01 45 | 19 intr0: ajmp step ;but do ssrun if level trigger'd 20 001B: | 20 .org start+1bh ;timer #1 21 001B: 02 20 1B | 21 ljmp program+1bh 22 0023: | 22 .org start+23h ;serial port 23 0023: 02 20 23 | 23 ljmp program+23h 24 002B: | 24 .org start+2bh ;timer #2 (8052 only) 25 002B: 02 20 2B | 25 ljmp program+2bh ... [root@rh9 examples]# myasm51 -oH -Cn pm51.asm Myasm51 Assembler. Ver 0.01 Release 1, (20151231_165818) [email protected], Wed Sep 30 17:28:09 CST 2015 built: Dec 31 2015 - 17:04:44 ;;;; Starting 1st Pass... ;;;; 1st Pass proceeded. ;;;; Starting 2nd Pass... ;;;; 2nd Pass proceeded. pm51.Hex, 7760(0x1e50) bytes assembled. [root@rh9 examples]# nl -ba pm51.Hex |more 1 :03000000020B08E8 2 :03000300022003D5 3 :03000B0002200BC5 4 :03001300308A032D 5 :03001600022013B2 6 :0200190001459F 7 :03001B0002201BA5 8 :0300230002202395 9 :03002B0002202B85 10 :02003000A188A5 11 :02003200A180AB ... More about Myasm51 ================== For more information, see doc/myasm51_guide.pdf. Bug report ========== Please send email to [email protected] ========== Enjoy fun. [email protected] Thu Dec 31 17:43:48 CST 2015

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值