2018年3月28日 星期三

python練習

# from twisted.internet import reactor, protocol# from pymodbus.client.async import ModbusClientProtocol## def print_result(result):#     print("Result: %d" % result.bits[0])## def process(client):#     result = client.write_coil(1, True)#     result.addCallback(print_result)#     reactor.callLater(1, reactor.stop)### defer = protocol.ClientCreator(reactor, ModbusClientProtocol).connectTCP("localhost", 502)# defer.addCallback(process)
###############3
# from twisted.internet import reactor# from pymodbus.client.async import ModbusClientFactory## def process():#     factory = reactor.connectTCP("localhost", 502, ModbusClientFactory())#     reactor.stop()## if __name__ == "__main__":#    reactor.callLater(1, process)#    reactor.run()
###############

class unicode():
    def unicode_test(value):
        import unicodedata
        name = unicodedata.name(value)
        value2 = unicodedata.lookup(name)
        print('value="%s", name="%s", value2="%s"' %(value, name, value2))


unicode.unicode_test('A')

class writeFile():
    poem = '''Beautiful is the 'thank you'Wrapped with gratitude, Offered to peace prone peopleWho offer what is real-themselvesTo nurse with love and humilitynapalm asphyxiated victims in our stained world'''


    fout = open('relativity.txt', 'wt')
    print(poem, file=fout, sep='', end='')
    fout.close()

    print(len(poem))
    fin = open('relativity.txt', 'rt')
    poem = fin.read()
    fin.close()

    byteData = bytes(range(0, 256))
    print(len(byteData))
    with open('byteData.txt', 'wb') as fout:
        size = len(byteData)
        offset = 0        chunk = 50        while True:
            if offset > size:
                break            fout.write(byteData[offset:offset+chunk])
            offset += chunk
            print(offset)

    with open('byteData.txt', 'rb') as fout:
        bdata = fout.read()
        print(fout.tell())
        print(fout.seek(25))
        print(len(bdata))
        import os
        print(os.SEEK_SET)

    import csv
    villains = [['Doctor', 'No'], ['Doctor', 'No'], ['Doctor', 'No'],        ['Doctor', 'No'], ['Rosa', 'klebb']]
    with open('villains', 'wt') as fout:
        csvout = csv.writer(fout)
        csvout.writerows(villains)

    with open('villains', 'rt') as fin:
        cin = csv.reader(fin)
        newVillains = [row for row in cin]
        print(newVillains)

    with open('villains', 'rt') as fin:
        cin = csv.DictReader(fin, fieldnames=['first', 'second'])
        newVillains = [row for row in cin]
        print(newVillains)

    import shutil as st
    st.move('villains', 'copy_villains')

    path = os.path.abspath('villains')
    print(path)
    listdir = os.listdir('C:/python exercise/python_modbus/')
    print(listdir)

    import subprocess as sp
    date = sp.getoutput('date')
    print(date)

    import multiprocessing as mp
    import os

    def do_this(what):
        whoami(what)
    def whoami(what):
        print("Process %s says: %s" % (os.getpid(), what))


    if __name__ == "__main__":
        whoami("I'm the main program")
        for n in range(4):
            p = mp.Process(target=do_this, args=("I'm function %s" % n))
            p.start()

writeFile()

沒有留言:

張貼留言