Python 非常奇怪的代码现象

  1. #!/usr/bin/python
    
    
    import os
    from subprocess import *
    import sys
    
    import yaml
    def local_run(cmd):
        if cmd:
            p = Popen(cmd, shell=True, stdout=PIPE,stderr=PIPE)
            out,err = p.communicate()
        return(p.returncode, out, err)
    
    
    if __name__ == '__main__':
        rst = local_run("salt 'dev*' state.apply test  --out=yaml")
        data=yaml.load(rst[1])
        if rst[0] == 0:
            print([ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ])
            print([ [data[k][cmd]['result'] for cmd in data[k].keys() ] for k in data.keys() ])
            # method 1
            #for k in data.keys():
            #    for cmd in data[k].keys():
            #        print data[k][cmd]['result']
            # method 2
        else:
            print rst[2]
            [ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ]
            sys.exit(rst[0])
    
    

    Traceback (most recent call last):

      File “test.py”, line 20, in <module>

        print([ data[k][cmd][‘result’] for cmd in data[k].keys() for k in data.keys() ])

    NameError: name ‘k’ is not defined

    2. 将两个Expression 对掉,却能生效

    #!/usr/bin/python
    
    
    import os
    from subprocess import *
    import sys
    
    import yaml
    def local_run(cmd):
        if cmd:
            p = Popen(cmd, shell=True, stdout=PIPE,stderr=PIPE)
            out,err = p.communicate()
        return(p.returncode, out, err)
    
    
    if __name__ == '__main__':
        rst = local_run("salt 'dev*' state.apply test  --out=yaml")
        data=yaml.load(rst[1])
        if rst[0] == 0:
            print([ [data[k][cmd]['result'] for cmd in data[k].keys() ] for k in data.keys() ])
            print([ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ])
            # method 1
            #for k in data.keys():
            #    for cmd in data[k].keys():
            #        print data[k][cmd]['result']
            # method 2
        else:
            print rst[2]
            [ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ]
            sys.exit(rst[0])

    [[True], [True]]

    [True, True]

    奇怪。

Working with Django Model

  1. Sub-queries used for query pk dee in  main talbe

Create your models here.
class projects(models.Model):
    #env = models.ForeignKey(deploy_env, on_delete=models.CASCADE)
    project_name = models.CharField(max_length=200)
    create_date = models.DateTimeField(default=0)
    owner = models.CharField(max_length=200)

    def __str__(self):
        return self.project_name
class AppEnv(models.Model):

    name = models.CharField(max_length=200, unique=True)
    def __str__(self):
        return self.name

class MGDeployEnv(models.Model):
    deploy_env = models.OneToOneField(minion_groups, on_delete=models.CASCADE)
    project_name = models.ForeignKey(projects, on_delete=models.CASCADE)
    env_name = models.ForeignKey(AppEnv, on_delete=models.CASCADE)
    pillar = models.TextField()
    state = models.TextField()
    current_version = models.CharField(max_length=200, blank=True)
    status = models.CharField(max_length=200, blank=True)
    comments = models.CharField(max_length=200, blank=True)
    update_on = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.deploy_env)
class tasks(models.Model): #question = models.ForeignKey(Question, on_delete=models.CASCADE) #project_name = models.ForeignKey(projects, on_delete=models.CASCADE) env = models.ForeignKey(deploy_env, on_delete=models.CASCADE) deploy_url = models.CharField(max_length=200) owner = models.CharField(max_length=200) date = models.DateTimeField(default=timezone.now) status = models.CharField(max_length=200, blank=True) result = models.TextField(blank=True) def __str__(self): return self.deploy_url

1.1 Command line tests:

>> env_id=deploy_env.objects.filter(project_name_id=5)
>>> qt=tasks.objects.filter(env_id__in=env_id)
>>> qt.values()

<QuerySet [{‘deploy_url’: ‘testurl’, ‘id’: 11, ‘env_id’: 12, ….. etc

class tasks(models.Model):
    #question = models.ForeignKey(Question, on_delete=models.CASCADE)
    #project_name = models.ForeignKey(projects, on_delete=models.CASCADE)
    #env = models.ForeignKey(deploy_env, on_delete=models.CASCADE)
    env = models.ForeignKey(MGDeployEnv, on_delete=models.CASCADE)
    deploy_url = models.CharField(max_length=200)
    owner = models.CharField(max_length=200)
    date = models.DateTimeField(default=timezone.now)
    status = models.CharField(max_length=200, blank=True)
    result = models.TextField(blank=True)

    def __str__(self):
        return self.deploy_url

More Model Query:

There two methods to get the searching result, for example.

1.2 Foward query (query by main set)

if we would like get the all deploy environment of PROD,  or deploy environment of Projects test, we can do following query:

by project

pj=projects.objects.get(project_name=‘abc’)

pj.mgdeployenv_set.all()

or shorter 

projects.objects.get(project_name=‘cpg’).mgdeployenv_set.all()

<QuerySet [<MGDeployEnv: cpg_cons_be>, <MGDeployEnv: cpg_cons_all>]>

by environments

env = AppEnv.objects.get(name=’cons’)

env.mgdeployenv_set.all()

QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

1.3 BackForward query(query by sub set)

by project

MGDeployEnv.objects.filter(project_name__in=projects.objects.filter(project_name=‘abc’))

<QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

by environments

MGDeployEnv.objects.filter(env_name__in=AppEnv.objects.filter(name=‘cons’))

<QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

 

as you can see, this 4 queries are the same.

 

Reference Links:

http://scottlobdell.me/2015/01/sql-database-best-practices-django-orm/ 

 

2. Many to Many

https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/

Regular Expression to minimum(non-greedy) matching

If we would like to match multiple duplicate content in the same line, we would like to use minimum match by using RE.

for example:

filename: text

pillar[‘abc’]
pillar[‘abc’]
pillar[‘abc’]}pillar[‘wx’]||pillar[‘yz’]
pillar[‘abc’]

1. matched expression python

import re

f=open('./text','r').read()
# non-greddy result:

print(re.findall('(pillar\[.*?\])',f))

# ourput: ["pillar['abc']", 
"pillar['abc']", 
"pillar['abc']", 
"pillar['wx']", 
"pillar['yz']", 
"pillar['abc']"]

# greedy match result:

print(re.findall('(pillar\[.*\])',f))

#output: ["pillar['abc']", 
"pillar['abc']", 
"pillar['abc']}pillar['wx']||pillar['yz']", 
"pillar['abc']"]

 

2. matched expression using shell

grep -o -P "pillar\['.*?'\]" text

# output: 
pillar['abc']
pillar['abc']
pillar['abc']
pillar['wx']
pillar['yz']
pillar['abc']

Principle of Programming

http://www.cprogramming.com/how_to_learn_to_program.html

Learning to program isn’t something you can do in an afternoon, but it doesn’t have to be a life’s work, either. There are lots of things you can do to make it easier on yourself when you are learning to program. You already know about The 5 Most Common Problems New Programmers Face–And How You Can Solve Them. Now, discover how to get the most out of your learning.

One common theme across many of these tips is:

don’t go too fast; get it right before moving on.

When I was teaching C, there were always a few students who came into the class knowing a bit about programming. Inevitably, some of these students did great in the first few weeks only to fall further and further behind as the course went on. Why? They went too fast through the introductory part of the course, thinking they knew it all–but they rarely did. They knew some of the material, but not enough to have a strong grasp of the fundamentals.

At the same time, you must not stop making progress–you can go too slow as well as too fast. Don’t avoid a topic after you’ve mastered everything leading up to it. By facing more challenging ideas, you’ll help cement your grasp of the basics.
1. Look at the Example Code

Reading is usually about the words on the page, but learning to program is about code. When you’re first learning to program, you should make sure to look at, and try to understand, every example. When I first learned to program, I would sometimes read the code examples before the text, and try to figure out what they did. It doesn’t always work, but it did force me to look at the example very carefully, and it often helped make the writeups clearer.

If you want to see what sample code looks like, you can read this site’s introductory programming tutorial. This tutorial spends a great deal of time talking about the sample code to help you work through exactly what the code does.
2. Don’t Just Read Example Code–Run It

But when you’re reading a programming tutorial (or book), it’s easy to look at the sample code and say “I get it, I get it, that makes sense”. Of course, you might get it, but you might not get it, and you just don’t know it. There’s only one way to find out–do something with that code.

If you haven’t already, get a compiler like Code::Blocks set up.

Then type the sample code into a compiler–if you type it, instead of copying and pasting it, you will really force yourself to go through everything that is there. Typing the code will force you to pay attention to the details of the syntax of the language–things like those funny semicolons that seem to go after every line.

Then compile it and run it. Make sure it does what you think it does.

Then change it. Software is the most easily changed machinery on the planet. You can experiment easily, try new things, see what happens; the changes will happen almost immediately, and there is no risk of death or mayhem. The easiest way to learn new language features is to take some code that works one way, and change it.
3. Write your Own Code as Soon as Possible

Once you understand something about the language–or even if you’re still getting your head around it–start writing sample programs that use it. Sometimes it’s hard to find good ideas for what programs to write. That’s OK, you don’t have to come up with every idea at the beginning.

You can find some programming challenges on this site.

You can also reimplement the examples from the book or tutorial you are reading. Try to do so without looking back at the sample code; it won’t be as easy as it seems. This technique can work especially well if you tweak the sample code.

If you can’t think of a small program to write, but you have in mind a larger program you want to implement, like a game, you could start building small pieces that you can later use for a game. Whether you use them later or not, you will get the same useful experience.
4. Learn to Use a Debugger

I already talked about the importance of debugging in The 5 Most Common Problems New Programmers Face–And How You Can Solve Them. But it bears repeating; the sooner you learn good debugging techniques, easier it will be to learn to program.

The first step in doing so is to learn how to use a tool called a debugger, which allows you to step through your code.

A debugger will allow you to step line by line through a piece of code. It will let you see the values of variables, and whether the code inside an if statement is executed.

A debugger can help you quickly answer questions about what your code is doing.
int main()
{
int x;
int y;
if( x > 4 ) // <-- what is the value of x here? { y = 5; // <-- did this line of code execute? } } A final word about debuggers: the first time you learn about a debugger, it will take you longer to fix the problems with your code. After the tenth or so bug, it will really start to pay off. And believe me, you will have way more than ten bugs in your programming career. I often saw students unwilling to use a debugger. These students really made life hard on themselves, taking ages to find very simple bugs. The sooner you learn to use a debugger, the sooner it will pay off. 5. Seek out More Sources If you don't understand something, there's a good possibility the way it was explained just didn't click. First, look for alternative explanations. The internet is filled with information about programming, and some explanations work better for different people; you might need pictures, someone else might not. There are also lots of good books with detailed explanations. But if that doesn't work, the easiest way to figure out where your misunderstanding lies is to ask someone else. But try to go beyond saying, "I don't understand. Please explain." You're likely to get a link back to the same text you didn't understand. Instead, rephrase your understanding of the text in your words. The more your question reveals about what you are thinking, the easier it will be for a knowledgeable expert to answer it. Programmers sometimes have a reputation for being grumpy about answering questions, but I think the reason is that they want to make progress in a conversation, and that requires both sides to put in effort. If you ask a smart, detailed question that shows you are thinking, you will generally get good results. There are plenty of places you can go to ask questions. You can always email me, or post on our message board, or ask an expert. In the coming days, I'll be writing more about how to learn to program effectively. Stay tuned, by subscribing to our RSS feed, signing up for email notifications, or following @alexallain on twitter.

Set Proxy of env using python

Use http://www.someproxy.com:3128 for http proxying
proxies = {‘http’: ‘http://www.someproxy.com:3128’}
filehandle = urllib.urlopen(some_url, proxies=proxies)
# Don’t use any proxies
filehandle = urllib.urlopen(some_url, proxies={})
# Use proxies from environment – both versions are equivalent
filehandle = urllib.urlopen(some_url, proxies=None)
filehandle = urllib.urlopen(some_url)

Nginx auxiliary server shcadule[shell scripts]

 

Recently, we are planning to use two nginx server for load balancing’s hot backup. Here is the scripts I wrote that used to switch virtual IP automatically for servers when either one of the two servers got crash.

#!/bin/bash                                                                                

# This scripts is used to check nginx status and startup automatically.
# Written by Libo.ma @ Oct 20, 2011 17:16

Ver=0.9 Continue reading "Nginx auxiliary server shcadule[shell scripts]" 

喜欢这样的小程序 李少宏 python简明教程练习–命令行 地址簿 程序

reference link:
李少宏
python简明教程练习–命令行 地址簿 程序

在这个程序中,你可以添加、修改、删除和搜索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码)。这些详细信息应该被保存下来以便以后提取。

由于上个程序中的代码没有把信息从文件中读取出来,我们可以把相同的信息保存在info.txt文件中去,这使得info.txt文件会有多余的东西。以下是我修改后的代码

代码:

”’

Created on 2011-10-16

@author: Descusr

”’

# coding:utf-8

import sys

import os

import cPickle

class Person:

filename = ‘info.txt’

def __init__(self, name, phone):

self.name = name

self.phone = phone

self.personlist = {self.name:self.phone}

def add(self):

self.name = raw_input(“Enter name:”)

if self.name in self.personlist.keys():

print “The name is exist!”

else:

self.phone=raw_input(“Enter phone:”)

self.personlist[self.name] = self.phone

print “Contact saved!”

def Modify(self):

self.name = raw_input(“Enter name:”)

if self.name in self.personlist.keys():

self.phone = raw_input(“Enter new phone:”)

self.personlist[self.name] = self.phone

print “Contact saved!”

else:

print “The name is not in here!!”

def delete(self):

self.name = raw_input(“Enter name:”)

if self.name in self.personlist.keys():

del self.personlist[self.name]

print “Deleted!!”

else:

print “The name is not in here!!”

def search(self):

self.name = raw_input(“Enter name:”)

if self.name in self.personlist.keys():

print ‘Name:%s,Phone:%s’%(self.name,self.phone)

else:

print “The name is not in here!!”

def save(self):

f = file(self.filename,’w’)

cPickle.dump(self.personlist,f)

f.close()

print “Your contacts list has been saved to file:%s successfully~” %self.filename

def load(self):

if os.path.exists(self.filename):

f = file(self.filename)

self.personlist = cPickle.load(f)

f.close()

def show(self):

for self.name,self.phone in self.personlist.items():

print “Name:%s Phone number:%s” %(self.name,self.phone)

if __name__ == ‘__main__’:

os.system(‘clear’)

command = [‘add’,’modify’,’search’,’delete’,’quit’ ,’show’]

person = Person(”,”)

person.load()

while True:

print “The contact person:”

str = raw_input(‘What are you going to do(add/modify/search/delete/show/quit)?’)

if str in command:

if str == ‘add’:

person.add()

elif str == ‘modify’:

person.Modify()

elif str == ‘search’:

person.search()

elif str == ‘delete’:

person.delete()

elif str == ‘show’:

person.show()

else:

ch = raw_input(“Your contacts list hasn’t been saved,save it now?(Y/N)”)

if ch == ‘y’:

person.save()

sys.exit()

else:

sys.exit()

print “Exit the System”

break

else:

print “Please input the command!”