예제 #1
0
파일: parse.py 프로젝트: wing011203/Python
def parseContent():
	man = []
	other = []
	#result = []

	try:
		for line in open('sketch.txt'):
			try:
				(role, content) = line.split(":", 1)
				# print (role, end = '')
				# print(' says: ', end = '')
				# print(content, end = '')
				# result.append(role + ' said: ' + content)
				if (role.strip() == 'Man'):
					man.append(content.strip())
				elif (role.strip() == 'Other Man'):
					other.append(content.strip())
				
			except:
				pass
	except:
		print('File Error!')

	print('man count:', len(man))
	
	print ('other count:', len(other))
	man_data = open('man-data', 'w')
	other_data = open('other-data', 'w')
	import nester
	#print('man content:')
	nester.print_lol(man, output = man_data)
	#print('other content:')
	nester.print_lol(other, output = other_data)
예제 #2
0
def print_lol(the_list,level):
    for each_item in the_list:
        if isinstance(each_item,list):
           print_lol(each_item)
        else:
            for tab_stop in range(level):
                print("\t",end="")
            print(each_item)
예제 #3
0
#!/usr/sbin/env python3
from nester import print_lol
man = []
other = []

try:
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()

except IOError:
    print('The datafile is missing!')

try:
    with open('man_data.txt', 'w') as man_file, open('other_data.txt',
                                                     'w') as other_file:
        print_lol(man, fh=man_file)
        print_lol(other, fh=other_file)

except IOError as err:
    print('File error', str(err))
예제 #4
0
            (role,line_spoken) = each_line.split(':',1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('the date file is missing!')
try:
    man_file = open('man_data.txt','w')
    other_file = open('other_data.txt','w')
    print(man,file=man_file)
    print(other,file=other_file)
except IOError:
    print('file error')
finally:
    if 'man_file' in locals():
        man_file.close()
    if 'other_file' in locals():
        other_file.close()
try:
    with open('man_data.txt','w') as man_file:
        nester.print_lol(man,fh=man_file)
    with open('other_data.txt','w') as other_file:
        nester.print_lol(other,fh=other_file)
except IOError as err:
    print('file error:' + str(err))
예제 #5
0
                other.append(role_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('The data file is missing!')
'''使用pickle存储'''
try:
    with open('man_data.txt', 'wb') as man_file, open('other_data.txt',
                                                      'wb') as other_file:
        pickle.dump(man, man_file)
        pickle.dump(other, other_file)
except IOError as err:
    print('File error:' + str(err))
except pickle.PickleError as perr:
    print('Pickle error:' + str(pickle))
'''使用pickle读取'''
new_man_file = []

try:
    with open('man_data.txt', 'rb') as man_file:
        new_man_file = pickle.load(man_file)
except IOError as err:
    print('File error:' + str(err))
except pickle.PickleError as perr:
    print('Pick error:' + str(err))

nester.print_lol(new_man_file)
# 检查第一行和最后一行与上面是否一致
print(new_man_file[0])
print(new_man_file[-1])
예제 #6
0
import nester

cast = ["Palin", "Cleese", "Idle", "Jones", "Gilliam", "Chapman"]

nester.print_lol(cast)
예제 #7
0
파일: cast.py 프로젝트: gzg0612/myPython
import nester
cast = ["Palin", "Cleese",["dd", "dd",[1,1,1]], "Idle", "Jones", "Gillian", "Chapman"]
nester.print_lol(cast, 0)
예제 #8
0
import nester


CAST = [
    'Palin',
    'Cleese',
    'ldle',
    'Jones',
    'Gilliam',
    ['Jack', 1273, 32,
     [
         'Stven',
         1927]],
    'Chapman'
]

# nester.print_lol(cast)
# print_lol(cast)
# print_lol(cast, True)
nester.print_lol(CAST, True)
예제 #9
0
try:
    with open('A_file.txt', 'wb') as A_file:  # 以wb(可写二进制方式)方式打开,
        # print(A, file=A_file)  ##用print语句写入到A_file中
        # nester.print_lol(A,fh=A_file)#调用nester模块的print_lol方法
        pickle.dump(A, A_file)
    with open('B_file.txt', 'wb') as B_file:  # 以wb(可写二进制方式)方式打开
        # print(B, file=B_file)
        # nester.print_lol(B, fh=B_file)#调用nester模块的print_lol方法
        pickle.dump(B, B_file)
except IOError:
    print('File error')
except pickle.PickleError as perr:
    print('Pickling error:' + str(perr))

#新增一个列表,使用pickle进行存储、读取
new_A = []
try:
    with open('A_file.txt', 'rb') as A_file:  #rb,以二进制读方式打开
        new_A = pickle.load(A_file)
except IOError as err:
    print('File error:' + str(err))
except pickle.PickleError as perr:
    print('Pickling error:' + str(perr))

#打印第一行和最后一行
print(new_A[0])
print(new_A[-1])

#条用print_lol方法,打印所有行
nester.print_lol(new_A)
예제 #10
0
                other.append(line_spoken)
        except ValueError:
            pass

    data.close()

except IOError:
    print('The data file is missing')

try:
    with open("data_man.txt", "wb") as file_man:
        pickle.dump(man, file_man)
    with open("data_other.txt", "wb") as file_other:
        pickle.dump(other, file_other)

except IOError as err:
    print('File error: ' + str(err))

try:
    with open('data_man.txt', 'rb') as file_man:
        new_man = pickle.load(file_man)
    with open('data_other.txt', 'rb') as file_other:
        new_other = pickle.load(file_other)
except IOError as err:
    print('File error: ' + err)
except pickleError as perr:
    print('Pickling error: ' + perr)

nester.print_lol(new_man)
nester.print_lol(new_other)
예제 #11
0
파일: hello.py 프로젝트: hexingb/raw
import nester
movies = ['hello', 'world', 91, ['good', 23, ['morning', 23], 'yes', [23, 45, 0.2, 'large'], 32], ['yesterday']]
nester.print_lol(movies, 0)
nester.print_lol(movies, 1)
nester.print_lol(movies, 2)
예제 #12
0
import os
import nester

target = ["danile", "michael", ["aj","dk"]]
#print(target)
nester.print_lol(target, True, 3)

print(os.getcwd())

###
data = open("sketch.txt")
for each_line in data:
    try:
        (role, line_spoken) = each_line.split(":")
        #print(each_line, end="")
        print(role, end="")
        print(" said: ", end="")
        print(line_spoken, end="")
    except:
        pass
    
data.close()
예제 #13
0
#
# Created:     06-01-2013
# Copyright:   (c) Administrator 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------

def main():
    pass

if __name__ == '__main__':
    main()
import nester
print(dir(nester))
print(nester.__file__)
cast=['Palin','Cleese','idle','Jones','Gilliam','Chapman']
nester.print_lol(cast)
print("####################################################")
from nester import print_lol
##print(dir(nester))
##print(nester.__file__)
cast=['Palin','Cleese','idle','Jones','Gilliam','Chapman']
print_lol(cast)
print("####################################################")
from nester import *
##print(dir(nester))
##print(nester.__file__)
cast=['Palin','Cleese','idle','Jones','Gilliam','Chapman']
print_lol(cast)
print("####################################################")

예제 #14
0
import nester
cast = ['Palin','Cleese','Idle','Jone','Gilliam']
try:
     with open ('test.txt','w') as out1:
          nester.print_lol(cast,fh = out1)
except IOError as err:
     print('Error: '+str(err))
예제 #15
0
파일: sketch7.py 프로젝트: kudou/Python
import nester
man = []
woman = []

try:
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'man':
                man.append(line_spoken)
            elif role == 'woman':
                woman.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('The data file is missing!')

try:
#    with open('man_data.txt', 'w') as man_file:
#        print(man, file=man_file)
#    with open('woman_data.txt', 'w') as woman_file:
#        print(woman, file=woman_file)
    with open('man_data.txt', 'w') as man_file, open('woman_data.txt', 'w') as woman_file:
        nester.print_lol(man, fn=man_file)
        nester.print_lol(woman, fn=woman_file)
except IOError as err:
    print('File error: ' + str(err))
    data = open('sketch.txt')  #打开文件
    for each_line in data:  #遍历文件
        try:
            (role, line_spoken) = each_line.split(':', 1)  #将每行按照:分成2部分
            line_spoken = line_spoken.strip()  #去除多余空格
            if role == 'Man':  #如果role是man, 把line_spken 压入man list
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()  #关闭I/O
except IOError:
    print('The data file is missing!')

try:
    with open('man_data3.txt', 'w') as out_man:
        print_lol(man, fh=out_man)
    with open('other_data3.txt', 'w') as out_other:
        print_lol(other, fh=out_other)
except IOError as err:
    print('File Error: ' + str(err))

try:
    with open('man_data3.txt', 'w') as out_man, open('other_data3.txt',
                                                     'w') as out_other:
        print_lol(man, fh=out_man)
        print_lol(other, fh=out_other)
except IOError as err:
    print('File Error: ' + str(err))
예제 #17
0
파일: ch04.02.py 프로젝트: dlete/python_lab
other = []

try:
    os.chdir('/home/dlete/python_lab/ch04')
    data = open('sketch.txt')

    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(":", 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass

    data.close()

except IOError:
    print('The data file is missing')

try:
    with open("data_man.txt", "w") as file_man:
        nester.print_lol(man, fh=file_man)
    with open("data_other.txt", "w") as file_other:
        nester.print_lol(other, fh=file_other)

except IOError as err:
    print('File error: ' + str(err))
예제 #18
0
import nester

langs=['java', 'python', 'ruby', 'php', 'c#']

nester.print_lol(langs)
예제 #19
0
파일: cat.py 프로젝트: xinjianhou/python
import nester
cast = [
    'hehe', 123, 'miaomiao',
    [
        'shit', 1987,
        [
            'hello', 8769, 'hehe', ['good', 'hello world', 2017], 2020,
            'haoxiao'
        ], 'haode', 'buhao'
    ], 'zainali', 8848
]
nester.print_lol(cast, False, 0)
print("---------------------------------")
nester.print_lol(cast, True)
print("---------------------------------")
nester.print_lol(cast, True, 1)
예제 #20
0
        #print_lol(man, fn = man_file)
        #print_lol(other, fn = other_file)

except IOError as err1:
    print('File error: ' + str(err1))

except pickle.PickleError as perr:
    print('Pickle Error: ' + str(perr))

try:
    with open('man_data_1.txt', 'rb') as man_file:
        new_man = pickle.load(man_file)

except IOError as err1:
    print('File error: ' + str(err1))

except pickle.PickleError as perr:
    print('Pickle Error: ' + str(perr))

print_lol(new_man)

# Git practice








예제 #21
0
파일: cast.py 프로젝트: songxowe/PythonApp
import nester

cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman']

# 命名空间限定函数名
nester.print_lol(cast)

movies = [
    "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
    [
        "Graham Chapman",
        [
            "Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle",
            "Terry Jones"
        ]
    ]
]
nester.print_lol(movies, True, 0)
예제 #22
0
from nester import print_lol
cast = ['A', 'B', 'C', 'D', 'E']
print_lol(cast)
예제 #23
0
# module优化
import nester

movies = [
    "hello1", "hello2", 'hello3', ['hello5', 1978, "hello6", [123, 'hello7']]
]
nester.print_lol(movies, 2)
예제 #24
0
파일: write3.py 프로젝트: speark/Study
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass

man = []
other = []

try:
    data = open('sketch.txt')
    processData(data)
    data.close()
except IOError:
    print('The data file is missing!')

try:
    with open('man_data.txt', 'w') as man_file, open("other_data.txt", 'w') as other_file:
            
        nester.print_lol(man,fh=man_file)#print >> man_file, man      #python 2.7
        nester.print_lol(other,fh=other_file)#print >> other_file, other  #python 3 print(other, file=other_data)

except IOError as err:
    print("File error: " + str(err))

예제 #25
0
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)

        except ValueError:
            pass

    data.close()
        
except IOError:
    print('The datafile is missing!')

import nester
try:
    with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
        nester.print_lol(man, indent=True, level=0, fh=man_file)
        nester.print_lol(other, indent=True, level=0, fh=other_file)   
    
   
except IOError:
    print('File error.')


        

예제 #26
0
count = 0
while count < len(movies):
    print(movies[count])
    count += 1

movies.pop()
movies.pop()
movies.remove("The Life of Brain")
movies.remove(1979)
print(movies)

movies.extend(["Terry Jones & Terry Gilliam", 91])
"""extend在列表末尾添加一个数据项集合"""
print(movies)  # 打印列表

charpman = [
    "Graham chapman",
    [
        "Michael Plain", "John Cleese", "Terry Gilliam", "Eric Idle",
        "Terry Jones"
    ]
]
movies.extend(charpman)
for each_item in movies:
    print(each_item)

nester.print_lol(movies, True, 8)  # 模块名.函数名
nester.print_lol(movies)
nester.print_lol(movies, True)
예제 #27
0
except IOError:
    print('The data file is missing!')
'''文件打开写入的常规方式'''
# try:
#     man_file = open('man_data.txt','w')
#     other_file = open('other_data.txt','w')

#     print(man,file=man_file)
#     print(other,file=other_file)
# except IOError:
#     print('File error.')
# finally:
#     if man_file in locals():
#         man_file.close()
#     if other_file in locals():
#         other_file.close()
''' 使用with处理文件打开与写入'''
try:
    with open('man_data.txt', 'w') as man_file:
        nester.print_lol(man, fn=man_file)
    with open('other_data.txt', 'w') as other_file:
        nester.print_lol(other, False, 0, other_file)
except IOError as err:
    print('File error:' + str(err))
'''简化成一行'''
# try:
#     with open('man_data.txt','w') as man_file, open('other_data.txt','w') as other_file:
#         print(man,file = man_file)
#         print(other,file = other_file)
# except IOError as err:
#     print('File error:' + str(err))
예제 #28
0
# def print_lol(the_list):
#     for each_item in the_list:
#         if isinstance(each_item, list):
#             print_lol(each_item)
#         else:
#             print(each_item)

import nester

from nester import print_lol

m_array = ['a','b','c',['d',['e','f']]]

# namespace
# nester.print_lol(m_array)
#
# print_lol(m_array)
# nester.print_lol(m_array,3);

nester.print_lol(m_array);
예제 #29
0
파일: sketch.py 프로젝트: GracyD/HDPython
		try:
			(role, line_spoken) = line.split(":", 1)
			line_spoken = line_spoken.strip()
			if role == 'Man':
				man.append(line_spoken)
			elif role == 'Other Man':
				other.append(line_spoken)
		except ValueError:
			pass
	data.close()	
except IOError:
	print('The data file is missing!')
	
try:
        with open('man_data1.txt', 'w') as manout:
                #print(man, file=manout)
                print_lol(man, output=manout)

        with open('other_data1.txt', 'w') as otherout:
                #print(other, file=otherout)
                print_lol(other, output=otherout)
#        manout = open("man_data.txt", "w")
#        otherout = open("other_data.txt", "w")
#        print(man, file=manout)
#        print(other, file=otherout)
except IOError as err:
        print("Out file error: ", + str(err))
#finally:
#       manout.close()
#        otherout.close()
import nester

movies = [
    "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
    [
        "Graham Chapman",
        ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle"]
    ]
]

nester.print_lol(movies, 0)
예제 #31
0
import nester

cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman', ["Silicon"]]
nester.print_lol(cast, True)
    data = open('sketch.txt') #打开文件
    for each_line in data:#遍历文件
        try:
            (role,line_spoken) = each_line.split(':',1) #将每行按照:分成2部分
            line_spoken = line_spoken.strip()#去除多余空格
            if role == 'Man':#如果role是man, 把line_spken 压入man list
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()#关闭I/O
except IOError:
    print('The data file is missing!')

try:
    with open('man_data3.txt','w') as out_man:
        print_lol(man,fh=out_man)
    with open('other_data3.txt','w') as out_other:
        print_lol(other,fh=out_other)
except IOError as err:
    print('File Error: '+str(err))


try:
    with open('man_data3.txt','w') as out_man,open('other_data3.txt','w') as out_other:
        print_lol(man, fh=out_man)
        print_lol(other,fh=out_other)
except IOError as err:
    print('File Error: '+str(err))    
예제 #33
0
in Python is associated with a namespace. The code in the Python files are linked to the __main__ namespace, the builtin 
functions such as the `print()` and the `isinstance()` BIFs are a part of `__builtins__` namespace, but when we 
create our own custom modules, they're  put in a namespace with the same name as the module. Thus, the function needs to
be invoked with the name of the namespace (and consequently module) that contains it. So, we use `nester.print_lol()` 
instead of just `print_lol()`. This is called Namespace Qualification. 

An important distinction is the use of `from <module> import <functionName>` statements, which remove the necessity for 
namespace qualifications. However, if there's a function with the same name in the file as well, the imported one will
override the local function. 

"""

import nester

cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman']
nester.print_lol(cast)

"""
To share the code with the PyPI community, we used to have to do a commandline registration (telling the command line 
utilities the login credentials required). The command for this is `python3 setup.py register`. Pre-registration is no 
longer supported (or required), and hence the files can be directly uploaded with `python3 setup.py sdist upload`.  

Python, just like java, has the concept of a bytecode file. So, it generates a .pyc file (like a java's .class file) 
that it can execute directly and skip the compilation as long as the source code doesn't change. However, this is a 
run-time optimization performed by the python interpreter, and the original .py file is still the one we need the users 
to have. 
"""

# Usage of an extra argument passed to the function.

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
예제 #34
0
import nester

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, 
                ["Graham Chapman", ["Michael Palin", "John Cleese",
                        "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

nester.print_lol(movies)
print('')
nester.print_lol(movies,0)
print('')
nester.print_lol(movies,3)
예제 #35
0
from nester import print_lol
cast = ['Palin','Cleese','Idle','Jones','Gillian','Chapman']
print_lol(cast)
예제 #36
0
man=[]
other=[]

try:
        data=open('sketch.txt')
        for each_line in data:
                try:
                        (role,line_spoken) = each_line.split(':',1)
                        line_spoken=line_spoken.strip()
                        if role=='Man':
                                man.append(line_spoken)
                        if role=='Other Man':
                                other.append(line_spoken)
                except ValueError:
                        pass
        data.close()
	
except IOError:
        print('the data file is missing!')
        

try:
        with open('man_data.txt','w')as man_file:
                print_lol(man,fh=man_file)
        with open('other_data.txt','w')as other_file:
                print_lol(other,fh=other_file)
                
except IOError as err:
        print('File error:'+str(err)) 
예제 #37
0
# encoding:utf-8

import nester

# 列表的多层嵌套,打印Eric Idle
movies = [
    "The Holy Grail",
    1975,
    "Terry Jones & Terry Gilliam",
    91,
    ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]],
]

nester.print_lol(movies)
예제 #38
0
#from nester import print_lol
import nester

movies = [
    "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
    [
        "Graham Chapman",
        [
            "Michael Palin", "Jon Cleese", "Terry Gilliam", "Eric Idle",
            "Terry Jones", "Robert Deniro", ["Intern", "Heat", "God Father"]
        ]
    ]
]

nester.print_lol(movies, True, 2)
예제 #39
0
import pickle
import nester
fileName = 'pickledata.txt'
try:
    with open(fileName, 'wb') as mysavedata:
        pickle.dump([1, 2, "three"], mysavedata)
    with open(fileName, 'rb') as myrestoredata:
        data = pickle.load(myrestoredata)
        nester.print_lol(data, indent=True, level=0)
except IOError as error:
    print("The data file is missing!" + str(error))
예제 #40
0
파일: plol_hd.py 프로젝트: macnc/PythonL
本文件中使用之前章节建立的print_lol方法来目前的信息保存到txt文件中
'''
try:
    with open('sketch.txt') as data:
        for each_line in data:
            try:
                (role, line_spoken) = each_line.split(':', 1)
                line_spoken = line_spoken.strip()
                if role == 'Man':
                    man.append(line_spoken)
                elif role == 'Other Man':
                    other.append(line_spoken)
            except ValueError as er:
                # print("The error detail: <" + str(er) + '>')
                pass
            

    # 有了with,不用再加入关闭文件流操作了,因为with会自动处理这些东西
    # data.close()
except IOError:
    print('The datafile is missing!')

try:
    with open('man_data.txt', 'w') as man_data, open('other_data.txt', 'w') \
        as other_data:
        print_lol(man, fn=man_data)
        print_lol(other, fn=other_data)

except IOError as err:
    print('File Error: <' + str(err) + '>')
예제 #41
0
try:
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('the datafile is missing')
                
print(man)
print(other)
import nester
try:
    with open('man_data.txt', 'w') as man_file:#'命名文件名并赋值一个文件对象'
        nester.print_lol(man,True, fh = man_file)#'调用发布的模块函数将指定的列表保存到磁盘文件上'
    with open('other_data.txt', 'w') as other_file:#'同上'
        nester.print_lol(other,True, fh = other_file)#'同上'
except IOError as err:
    print('File error:' + str(err))

    


예제 #42
0
파일: load.py 프로젝트: yiyijie/testcode
import os
os.getcwd()
os.chdir('/SoftWare/python3/libs/headfirstpython/chapter3')
import pickle#'导入标准模块"pickle"'
import nester#'导入发布模块"nester"'
new_man = []#'创建空列表'
try:
    with open('man_dats.txt', 'rb') as man_file:
        new_man = pickle.load(man_file)
except IOError as err:
    print('File error:' + str(err))
except pickle.PickleError as perr:
          print('Pickling error:' +str(perr))


nester.print_lol(new_man)




print(new_man[0])
print(new_man[-1])
예제 #43
0
import nester

test_data1 = ['test1', 'test2', 'test3', 'test4', 'test5',]

print('Test 1 of test_data1')
nester.print_lol(test_data1, False, 0)

print('\nTest 2 of test_data1')
nester.print_lol(test_data1, True)


test_data2 = ['test1', 'test2', 'test3', 'test4', 'test5',
              ['inner list1', 'inner list2', 'inner list3', 'inner list4', 'inner list5'],
            ]

print('\nTest 1 of test_data2')
nester.print_lol(test_data2, False, 0)

print('\nTest 2 of test_data2')
nester.print_lol(test_data2, True, 1)
예제 #44
0
파일: Student.py 프로젝트: hhyifeng/Python
def test():
    std1 = Student('he', 22, 99)
    std2 = Student('yi', 18, 88)
    std1.study()
    std1.print_score()
    print(std1)
    gs = goodStudent('good', 18, 100)
    gs.study()
    print(gs)
    bs = badStudent('bad', 18, 10)
    bs.study()
    print(bs)
    print('##########################')
    if isinstance(std1, list):
        print('std1 is list')
    else:
        print("std1 is not list")
    if isinstance(std1, Student):
        print('std1 is Student')
    else:
        print("std1 is not Student")
    if isinstance(std1, goodStudent):
        print('std1 is goodStudent')
    else:
        print("std1 is not goodStudent")
    if isinstance(bs, Student):
        print('bs is Student')
    else:
        print("bs is not Student")
    print('##########################')
    study_twice(Student('he', 22, 50))
    study_twice(badStudent('yi', 22, 19))
    study_twice(goodStudent('feng', 22, 99))
    print('###########################')
    print(type(123))
    print(type(bs))
    print(type(std1))
    print(type(gs))
    print(type(None))
    # 函数类型
    print(type(abs))
    print(type(test))
    # type()可用来判断相等
    if type(test) == types.FunctionType:
        print('test() is function')
    if type(abs) == types.BuiltinFunctionType:
        print('abs() is builtinfunction')
    if type(lambda x: x) == types.LambdaType:
        print('lambda x:x is lambda')
    if type((x for x in range(100))) == types.GeneratorType:
        print(' is GeneratorType')
    # isinstance(1,(int,float))可能判断类型范围
    # dir()获取一个对象的方法和属性
    print_lol(dir(Student))
    print_lol(dir('ABC'))
    # getattr(),setattr(),hasattr()
    # getattr(object,property,defaultvalue),也可获取对象方法
    if hasattr(bs, 'x'):
        print('bs has property x')
    else:
        print('bs has not property x')
        setattr(bs, 'x', 19)
        if hasattr(bs, 'x'):
            print('bs has property x')
            print("bs.x equal %d" % bs.x)
    # 可以创建对象后临时绑定实例变量,如果有同名类变量则覆盖,如果没有(如果原来有后来实例删除了此变量(del object.name))则值为同名类变量的值
    # 实例属性会屏蔽类属性,但删除后会恢复类属性的值
    std1.gender = 'man'
    if hasattr(std1, 'gender'):
        print('bs gender is %s' % getattr(std1, 'gender', 'unknown'))
    # 给实例绑定方法

    def set_tempage(self, age):
        self.__age = age
    # 绑定后并不能像类方法一样以内部__age名称访问age变量
    std1.set_tempage = types.MethodType(set_tempage, std1)
    # 对另一个实例不起作用
    std1.set_tempage(100)
    print(std1.age())
    # 给类绑定方法

    def set_classstr(self, classs):
        self.classstr = classs
    Student.set_classstr = types.MethodType(set_classstr, Student)
    bs.set_classstr('badstudent')
    print(bs.classstr)
    print(std1.classstr)
###One way to do it
##import nester
##
##starwars = ['the last jedi','the force awakens']
##
##movies = [starwars,'black panther','avengers']
##
##nester.print_lol(movies)

#another way to do it
from nester import print_lol

starwars = ['the last jedi', 'the force awakens']

movies = [starwars, 'black panther', 'avengers']

print_lol(movies)
"""
you need to be careful. If you already have
a function called print_lol defined in your current namespace, the
specific import statement (from nester import print_lol)
overwrites your function with the imported
one, which might not be the behavior you want
"""
예제 #46
0
import nester

cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman', ["zhang", "myton"]]

nester.print_lol(cast, 0)
예제 #47
0
import nester

cast = ['Palin', ['Cleese', 'Hola'], 'Idle', 'Jones', 'Gilliam', 'Chapman']
nester.print_lol(cast, True)
예제 #48
0
import pickle
import nester

new_man = []
new_othe = []

try:
    with open('man_data.txt', 'rb') as man_file:
        new_man = pickle.load(man_file)
    with open('other_data.txt', 'rb') as other_file:
        new_other = pickle.load(other_file)
except IOError as err:
    print('File error: ' + str(err))
except pickle.pickleError as perr:
    print('pickling error: ' + str(perr))

nester.print_lol(new_man)
print(new_man[0])
print(new_man[-1])
import nester

lists = ["Palin", "Cleese", "idle", "Jones", "Gillian", "Chapman"]

nester.print_lol(lists)
예제 #50
0
cast = ['Palin', 'Cleese', 'Idle', 'Jones', 'Gilliam', 'Chapman']
print("----------")

movies = [
    "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
    [
        "Graham Chapman",
        [
            "Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle",
            "Terry Jones"
        ]
    ]
]

nester.print_lol(movies, 2)

names = ['John', 'Eric', ['Cleese', 'Idle'], 'Michael', ['Palin']]
print("----------")

#nester.print_lol(names, -9)
nester.print_lol(names, True)
"""
NOTE: thuc hien cac cau lenh sau truoc khi thay doi package
*python3 = c:python/python.exe

python3 setup.py sdist 
python3 setup.py install

python3 setup.py sdist register upload // OR
twine upload dist/*
				'''
		except ValueError:
			pass

	data.close()

except IOError:
	print ("The data file is not exist!!")



#print (man)

#fengyongming.fengexian()

#print (other)

#fengyongming.fengexian()

try:
	with open("man_data.txt" , "w") as man_file:
		nester.print_lol(man , fn = man_file)
	with open("other_data.txt" , "w") as other_file:
		nester.print_lol(man , fn = other_file)

except IOError:
	print('File error')



예제 #52
0
#!/usr/bin/python

import nester;


mylist = ["dave","dtormey","has","an","impressive","six-pack" ] 


nester.print_lol(mylist)
예제 #53
0
파일: test.py 프로젝트: sqler21c/Python
'''
Created on 2015. 11. 2.

@author: User
'''
import nester

exlist=['plain', 'clese', 'idel', 'gillen', 'eidlfgks', 'chapman']

nester.print_lol(exlist)
예제 #54
0
import nester

movies = [
    "The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
    ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam"]]
]

nester.print_lol(movies)
예제 #55
0
#!/usr/bin/env python3
from nester import print_lol
lista = ["bucetra", ["cu","pica"]]
print_lol(lista, True, 2)
예제 #56
0
man = []
other = []
try:
    data = open('../sketch.txt')
    for line in data:
        try:
            (role, line_spoken) = line.split(':', 1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print('The data file is missing!')

try:
    # man_file = open('man_data.txt', 'w')
    # other_file = open('other_data.txt', 'w')

    with open('man_data2.txt', 'w') as man_file:
        nester.print_lol(man, fh=man_file)

    with open('other_data2.txt', 'w') as other_file:
        nester.print_lol(other, fh=other_file)

except IOError as err:
    print('File error: ' + str(err))
예제 #57
0
from nester import print_lol

movies = ["shawshank redemption", "morgan freeman", 65, "cast away", ["tom hanks", ["julia roberts", "nicole kidman"]]]

print_lol(movies, True, 3)