本章主要介绍了Python的基本数据类型以及基本语法,并介绍了一些python风格的类型操作的写法
Author Archives: Shang Erxin
Python基础教程,第一章 简介(Python Fundamental, Charpter 01 Introduction)
对Python编程语言做了基本的介绍
Make a basic introduction about python programming language.
主要内容:
Get required lasts lines from a huge text file in python
During my work, I need to parse the result from some huge log files. The result is always save at the end of the file. So I need find a way to get the required last lines to accelerate the parse speed. Here is the function I have written to achieve this requirement in python.
- def last_lines(filename, lines=1):
- '''
- return the last lines from the specify files
- '''
- with open(filename, 'rb') as fh:
- fh.seek(0, os.SEEK_END)
- if fh.tell() == 0:
- return []
- fh.seek(-2, os.SEEK_END)
- #while not BOF
- while fh.tell() > 0:
- cur_char = fh.read(1).decode()
- if cur_char == '\n':
- lines -= 1
- if lines > 0:
- fh.seek(-2, os.SEEK_CUR)
- else:
- break
- return [line.decode() for line in fh.readlines()]
Use oscilloscope Check the AC noise of a Power Source
How to use a oscilloscope display the wave shape of AC power source?
1. Check the oscilloscope is connected with attenuator probes, and adjust to the right attenuation factor. 1x, 10x 100x, … the more big the number the more attenuator value
2. Adjust the oscilloscope to the relative measure scope and set to the right channel
3. Connect the ground with the clamp to the ground of the source
4. Use one probe connecting with zero line, the other connected with the supply line
5. Adjust the sample time to make the wave shape looks better
6. The noise pulses are displayed during my measurement result
Use Python puka client with RabbitMQ
Send a message to RabbitMQ
- #!/usr/bin/env python
-
- import sys
- sys.path.append("..")
-
-
- import puka
-
- client = puka.Client("amqp://localhost/")
-
- promise = client.connect()
- client.wait(promise)
-
- promise = client.queue_declare(queue='test')
- client.wait(promise)
-
- promise = client.basic_publish(exchange='', routing_key='test',
- body="Hello world!")
- client.wait(promise)
-
- print " [*] Message sent"
-
- promise = client.queue_declare(queue='test', passive=True)
- print " [*] Queue size:", client.wait(promise)['message_count']
-
- promise = client.close()
- client.wait(promise)