36 | ''')
37 |
--------------------------------------------------------------------------------
/samples/debug/mydict_test.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | import unittest
5 |
6 | from mydict import Dict
7 |
8 | class TestDict(unittest.TestCase):
9 |
10 | def test_init(self):
11 | d = Dict(a=1, b='test')
12 | self.assertEqual(d.a, 1)
13 | self.assertEqual(d.b, 'test')
14 | self.assertTrue(isinstance(d, dict))
15 |
16 | def test_key(self):
17 | d = Dict()
18 | d['key'] = 'value'
19 | self.assertEqual(d.key, 'value')
20 |
21 | def test_attr(self):
22 | d = Dict()
23 | d.key = 'value'
24 | self.assertTrue('key' in d)
25 | self.assertEqual(d['key'], 'value')
26 |
27 | def test_keyerror(self):
28 | d = Dict()
29 | with self.assertRaises(KeyError):
30 | value = d['empty']
31 |
32 | def test_attrerror(self):
33 | d = Dict()
34 | with self.assertRaises(AttributeError):
35 | value = d.empty
36 |
37 | if __name__ == '__main__':
38 | unittest.main()
39 |
40 |
--------------------------------------------------------------------------------
/docs/pyscript_plugin.js:
--------------------------------------------------------------------------------
1 | export default class LearnPython3Plugin {
2 | afterStartup(runtime) {
3 | console.log('[plugin] pyscript startup.');
4 | window.__pyscript_ready__ = true;
5 | }
6 |
7 | afterPyScriptExec(opt) {
8 | let
9 | tag = opt.pyScriptTag,
10 | outputId = tag.getAttribute('output'),
11 | $btn = $('button[outputId=' + outputId + ']'),
12 | $i = $btn.find('i');
13 | $i.removeClass('uk-icon-spinner');
14 | $i.removeClass('uk-icon-spin');
15 | $btn.removeAttr('disabled');
16 | let err = $(tag).find('pre.py-error').html();
17 | if (err) {
18 | let
19 | $out = $('#' + outputId),
20 | s = $out.html();
21 | s = s + err.replaceAll(' ', ' ');
22 | $out.html(s);
23 | $out.addClass('uk-alert-danger');
24 | }
25 | }
26 |
27 | onUserError(err) {
28 | console.log('Error >>>');
29 | console.error(err);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/samples/packages/pil/use_pil_draw.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from PIL import Image, ImageDraw, ImageFont, ImageFilter
5 |
6 | import random
7 |
8 | # 随机字母:
9 | def rndChar():
10 | return chr(random.randint(65, 90))
11 |
12 | # 随机颜色1:
13 | def rndColor():
14 | return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
15 |
16 | # 随机颜色2:
17 | def rndColor2():
18 | return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
19 |
20 | # 240 x 60:
21 | width = 60 * 4
22 | height = 60
23 | image = Image.new('RGB', (width, height), (255, 255, 255))
24 | # 创建Font对象:
25 | font = ImageFont.truetype('Arial.ttf', 36)
26 | # 创建Draw对象:
27 | draw = ImageDraw.Draw(image)
28 | # 填充每个像素:
29 | for x in range(width):
30 | for y in range(height):
31 | draw.point((x, y), fill=rndColor())
32 | # 输出文字:
33 | for t in range(4):
34 | draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
35 | # 模糊:
36 | image = image.filter(ImageFilter.BLUR)
37 | image.save('code.jpg', 'jpeg')
38 |
--------------------------------------------------------------------------------
/samples/multitask/task_worker.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | import time, sys
5 | from multiprocessing.managers import BaseManager
6 |
7 | # 创建类似的QueueManager:
8 | class QueueManager(BaseManager):
9 | pass
10 |
11 | # 由于这个QueueManager只从网络上获取Queue,所以注册时只提供名字:
12 | QueueManager.register('get_task_queue')
13 | QueueManager.register('get_result_queue')
14 |
15 | # 连接到服务器,也就是运行task_master.py的机器:
16 | server_addr = '127.0.0.1'
17 | print('Connect to server %s...' % server_addr)
18 | # 端口和验证码注意保持与task_master.py设置的完全一致:
19 | m = QueueManager(address=(server_addr, 5000), authkey=b'abc')
20 | # 从网络连接:
21 | m.connect()
22 | # 获取Queue的对象:
23 | task = m.get_task_queue()
24 | result = m.get_result_queue()
25 | # 从task队列取任务,并把结果写入result队列:
26 | for i in range(10):
27 | try:
28 | n = task.get(timeout=1)
29 | print('run task %d * %d...' % (n, n))
30 | r = '%d * %d = %d' % (n, n, n*n)
31 | time.sleep(1)
32 | result.put(r)
33 | except Queue.Empty:
34 | print('task queue is empty.')
35 | # 处理结束:
36 | print('worker exit.')
37 |
--------------------------------------------------------------------------------
/samples/debug/mydict2.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | class Dict(dict):
5 | '''
6 | Simple dict but also support access as x.y style.
7 |
8 | >>> d1 = Dict()
9 | >>> d1['x'] = 100
10 | >>> d1.x
11 | 100
12 | >>> d1.y = 200
13 | >>> d1['y']
14 | 200
15 | >>> d2 = Dict(a=1, b=2, c='3')
16 | >>> d2.c
17 | '3'
18 | >>> d2['empty']
19 | Traceback (most recent call last):
20 | ...
21 | KeyError: 'empty'
22 | >>> d2.empty
23 | Traceback (most recent call last):
24 | ...
25 | AttributeError: 'Dict' object has no attribute 'empty'
26 | '''
27 | def __init__(self, **kw):
28 | super(Dict, self).__init__(**kw)
29 |
30 | def __getattr__(self, key):
31 | try:
32 | return self[key]
33 | except KeyError:
34 | raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
35 |
36 | def __setattr__(self, key, value):
37 | self[key] = value
38 |
39 | if __name__=='__main__':
40 | import doctest
41 | doctest.testmod()
42 |
43 |
--------------------------------------------------------------------------------
/samples/test/dict_doctest.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | class Dict(dict):
5 | '''
6 | Simple dict but also support access as x.y style.
7 |
8 | >>> d1 = Dict()
9 | >>> d1['x'] = 100
10 | >>> d1.x
11 | 100
12 | >>> d1.y = 200
13 | >>> d1['y']
14 | 200
15 | >>> d2 = Dict(a=1, b=2, c='3')
16 | >>> d2.c
17 | '3'
18 | >>> d2['empty']
19 | Traceback (most recent call last):
20 | ...
21 | KeyError: 'empty'
22 | >>> d2.empty
23 | Traceback (most recent call last):
24 | ...
25 | AttributeError: 'Dict' object has no attribute 'empty'
26 | '''
27 | def __init__(self, **kw):
28 | super(Dict, self).__init__(**kw)
29 |
30 | def __getattr__(self, key):
31 | try:
32 | return self[key]
33 | except KeyError:
34 | raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
35 |
36 | def __setattr__(self, key, value):
37 | self[key] = value
38 |
39 | if __name__=='__main__':
40 | import doctest
41 | doctest.testmod()
42 |
--------------------------------------------------------------------------------
/samples/multitask/task_master.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | import random, time, queue
5 | from multiprocessing.managers import BaseManager
6 |
7 | # 发送任务的队列:
8 | task_queue = queue.Queue()
9 | # 接收结果的队列:
10 | result_queue = queue.Queue()
11 |
12 | # 从BaseManager继承的QueueManager:
13 | class QueueManager(BaseManager):
14 | pass
15 |
16 | # 把两个Queue都注册到网络上, callable参数关联了Queue对象:
17 | QueueManager.register('get_task_queue', callable=lambda: task_queue)
18 | QueueManager.register('get_result_queue', callable=lambda: result_queue)
19 | # 绑定端口5000, 设置验证码'abc':
20 | manager = QueueManager(address=('', 5000), authkey=b'abc')
21 | # 启动Queue:
22 | manager.start()
23 | # 获得通过网络访问的Queue对象:
24 | task = manager.get_task_queue()
25 | result = manager.get_result_queue()
26 | # 放几个任务进去:
27 | for i in range(10):
28 | n = random.randint(0, 10000)
29 | print('Put task %d...' % n)
30 | task.put(n)
31 | # 从result队列读取结果:
32 | print('Try get results...')
33 | for i in range(10):
34 | r = result.get(timeout=10)
35 | print('Result: %s' % r)
36 | # 关闭:
37 | manager.shutdown()
38 | print('master exit.')
39 |
--------------------------------------------------------------------------------
/samples/db/do_sqlalchemy.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from sqlalchemy import Column, String, create_engine
5 | from sqlalchemy.orm import sessionmaker
6 | from sqlalchemy.ext.declarative import declarative_base
7 |
8 | # 创建对象的基类:
9 | Base = declarative_base()
10 |
11 | # 定义User对象:
12 | class User(Base):
13 | # 表的名字:
14 | __tablename__ = 'user'
15 |
16 | # 表的结构:
17 | id = Column(String(20), primary_key=True)
18 | name = Column(String(20))
19 |
20 | # 初始化数据库连接:
21 | engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
22 | # 创建DBSession类型:
23 | DBSession = sessionmaker(bind=engine)
24 |
25 | # 创建session对象:
26 | session = DBSession()
27 | # 创建新User对象:
28 | new_user = User(id='5', name='Bob')
29 | # 添加到session:
30 | session.add(new_user)
31 | # 提交即保存到数据库:
32 | session.commit()
33 | # 关闭session:
34 | session.close()
35 |
36 | # 创建Session:
37 | session = DBSession()
38 | # 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行:
39 | user = session.query(User).filter(User.id=='5').one()
40 | # 打印类型和对象的name属性:
41 | print('type:', type(user))
42 | print('name:', user.name)
43 | # 关闭Session:
44 | session.close()
45 |
--------------------------------------------------------------------------------
/samples/basic/do_match.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | # 简单匹配:
5 | print('-- simple match --')
6 |
7 | score = 'A'
8 |
9 | match score:
10 | case 'A':
11 | print('score is A.')
12 | case 'B':
13 | print('score is B.')
14 | case 'C':
15 | print('score is C.')
16 | case _:
17 | print('score is ???.')
18 |
19 | # 复杂匹配:
20 | print('-- complex match --')
21 |
22 | age = 15
23 |
24 | match age:
25 | case x if x < 10:
26 | print('< 10 years old.')
27 | case 10:
28 | print('10 years old.')
29 | case 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18:
30 | print('11~18 years old.')
31 | case 19:
32 | print('19 years old.')
33 | case _:
34 | print('not sure.')
35 |
36 | # 列表匹配:
37 | print('-- match list --')
38 |
39 | args = ['gcc', 'hello.c', 'world.c']
40 | # args = ['clean']
41 | # args = ['gcc']
42 |
43 | match args:
44 | # 如果仅出现gcc,报错:
45 | case ['gcc']:
46 | print('gcc: missing source file(s).')
47 | # 出现gcc,且至少指定了一个文件:
48 | case ['gcc', file1, *files]:
49 | print('gcc compile: ' + file1 + ', ' + ', '.join(files))
50 | # 仅出现clean:
51 | case ['clean']:
52 | print('clean')
53 | case _:
54 | print('invalid command.')
55 |
--------------------------------------------------------------------------------
/samples/commonlib/use_datetime.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from datetime import datetime, timedelta, timezone
5 |
6 | # 获取当前datetime:
7 | now = datetime.now()
8 | print('now =', now)
9 | print('type(now) =', type(now))
10 |
11 | # 用指定日期时间创建datetime:
12 | dt = datetime(2015, 4, 19, 12, 20)
13 | print('dt =', dt)
14 |
15 | # 把datetime转换为timestamp:
16 | print('datetime -> timestamp:', dt.timestamp())
17 |
18 | # 把timestamp转换为datetime:
19 | t = dt.timestamp()
20 | print('timestamp -> datetime:', datetime.fromtimestamp(t))
21 | print('timestamp -> datetime as UTC+0:', datetime.utcfromtimestamp(t))
22 |
23 | # 从str读取datetime:
24 | cday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')
25 | print('strptime:', cday)
26 |
27 | # 把datetime格式化输出:
28 | print('strftime:', cday.strftime('%a, %b %d %H:%M'))
29 |
30 | # 对日期进行加减:
31 | print('current datetime =', cday)
32 | print('current + 10 hours =', cday + timedelta(hours=10))
33 | print('current - 1 day =', cday - timedelta(days=1))
34 | print('current + 2.5 days =', cday + timedelta(days=2, hours=12))
35 |
36 | # 把时间从UTC+0时区转换为UTC+8:
37 | utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
38 | utc8_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
39 | print('UTC+0:00 now =', utc_dt)
40 | print('UTC+8:00 now =', utc8_dt)
41 |
--------------------------------------------------------------------------------
/samples/functional/do_reduce.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from functools import reduce
5 |
6 | CHAR_TO_INT = {
7 | '0': 0,
8 | '1': 1,
9 | '2': 2,
10 | '3': 3,
11 | '4': 4,
12 | '5': 5,
13 | '6': 6,
14 | '7': 7,
15 | '8': 8,
16 | '9': 9
17 | }
18 |
19 | def str2int(s):
20 | ints = map(lambda ch: CHAR_TO_INT[ch], s)
21 | return reduce(lambda x, y: x * 10 + y, ints)
22 |
23 | print(str2int('0'))
24 | print(str2int('12300'))
25 | print(str2int('0012345'))
26 |
27 | CHAR_TO_FLOAT = {
28 | '0': 0,
29 | '1': 1,
30 | '2': 2,
31 | '3': 3,
32 | '4': 4,
33 | '5': 5,
34 | '6': 6,
35 | '7': 7,
36 | '8': 8,
37 | '9': 9,
38 | '.': -1
39 | }
40 |
41 | def str2float(s):
42 | nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
43 | point = 0
44 | def to_float(f, n):
45 | nonlocal point
46 | if n == -1:
47 | point = 1
48 | return f
49 | if point == 0:
50 | return f * 10 + n
51 | else:
52 | point = point * 10
53 | return f + n / point
54 | return reduce(to_float, nums, 0.0)
55 |
56 | print(str2float('0'))
57 | print(str2float('123.456'))
58 | print(str2float('123.45600'))
59 | print(str2float('0.1234'))
60 | print(str2float('.1234'))
61 | print(str2float('120.0034'))
62 |
--------------------------------------------------------------------------------
/samples/test/dict_unittest.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | import unittest
5 |
6 | class Dict(dict):
7 |
8 | def __init__(self, **kw):
9 | super(Dict, self).__init__(**kw)
10 |
11 | def __getattr__(self, key):
12 | try:
13 | return self[key]
14 | except KeyError:
15 | raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
16 |
17 | def __setattr__(self, key, value):
18 | self[key] = value
19 |
20 | class TestDict(unittest.TestCase):
21 |
22 | def setUp(self):
23 | print('setUp...')
24 |
25 | def tearDown(self):
26 | print('tearDown...')
27 |
28 | def test_init(self):
29 | d = Dict(a=1, b='test')
30 | self.assertEqual(d.a, 1)
31 | self.assertEqual(d.b, 'test')
32 | self.assertTrue(isinstance(d, dict))
33 |
34 | def test_key(self):
35 | d = Dict()
36 | d['key'] = 'value'
37 | self.assertEqual(d.key, 'value')
38 |
39 | def test_attr(self):
40 | d = Dict()
41 | d.key = 'value'
42 | self.assertTrue('key' in d)
43 | self.assertEqual(d['key'], 'value')
44 |
45 | def test_keyerror(self):
46 | d = Dict()
47 | with self.assertRaises(KeyError):
48 | value = d['empty']
49 |
50 | def test_attrerror(self):
51 | d = Dict()
52 | with self.assertRaises(AttributeError):
53 | value = d.empty
54 |
55 | if __name__ == '__main__':
56 | unittest.main()
57 |
--------------------------------------------------------------------------------
/samples/micropython/smallcar/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env pybricks-micropython
2 |
3 | from pybricks import ev3brick as brick
4 | from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)
5 | from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
6 | from pybricks.tools import print, wait, StopWatch
7 | from pybricks.robotics import DriveBase
8 |
9 | # Write your program here
10 |
11 | motor = Motor(Port.B)
12 | ultrasonic = UltrasonicSensor(Port.S4)
13 |
14 | brick.sound.beep()
15 | brick.light(None)
16 |
17 | speed = 0
18 | colors = [None, Color.GREEN, Color.YELLOW, Color.RED]
19 |
20 | def setSpeed(acc):
21 | global speed
22 | if acc < 0:
23 | speed = max(0, speed - 1)
24 | elif acc > 0:
25 | speed = min(3, speed + 1)
26 | else:
27 | speed = 0
28 | if speed > 0:
29 | motor.run(speed * 90)
30 | else:
31 | motor.stop()
32 | brick.light(colors[speed])
33 |
34 | while True:
35 | if not any(brick.buttons()):
36 | wait(10)
37 | else:
38 | if Button.LEFT in brick.buttons():
39 | setSpeed(-1)
40 | elif Button.RIGHT in brick.buttons():
41 | setSpeed(1)
42 | elif Button.CENTER in brick.buttons():
43 | setSpeed(0)
44 | elif Button.UP in brick.buttons():
45 | setSpeed(0)
46 | break
47 | wait(500)
48 | if ultrasonic.distance() < 200:
49 | setSpeed(0)
50 |
51 | brick.sound.beep()
52 |
--------------------------------------------------------------------------------
/samples/micropython/rccar/devices.py:
--------------------------------------------------------------------------------
1 | # list input devices
2 |
3 | class InputDevice():
4 | def __init__(self):
5 | self.name = ''
6 | self.handler = ''
7 |
8 | def __str__(self):
9 | return '' % (self.name, self.handler)
10 |
11 | def setName(self, name):
12 | if len(name) >= 2 and name.startswith('"') and name.endswith('"'):
13 | name = name[1:len(name)-1]
14 | self.name = name
15 |
16 | def setHandler(self, handlers):
17 | for handler in handlers.split(' '):
18 | if handler.startswith('event'):
19 | self.handler = handler
20 |
21 | def listDevices():
22 | devices = []
23 | with open('/proc/bus/input/devices', 'r') as f:
24 | device = None
25 | while True:
26 | s = f.readline()
27 | if s == '':
28 | break
29 | s = s.strip()
30 | if s == '':
31 | devices.append(device)
32 | device = None
33 | else:
34 | if device is None:
35 | device = InputDevice()
36 | if s.startswith('N: Name='):
37 | device.setName(s[8:])
38 | elif s.startswith('H: Handlers='):
39 | device.setHandler(s[12:])
40 | return devices
41 |
42 | def detectJoystick(joystickNames):
43 | for device in listDevices():
44 | for joystickName in joystickNames:
45 | if joystickName in device.name:
46 | return '/dev/input/%s' % device.handler
47 | return None
48 |
--------------------------------------------------------------------------------
/samples/micropython/tank/devices.py:
--------------------------------------------------------------------------------
1 | # list input devices
2 |
3 | class InputDevice():
4 | def __init__(self):
5 | self.name = ''
6 | self.handler = ''
7 |
8 | def __str__(self):
9 | return '' % (self.name, self.handler)
10 |
11 | def setName(self, name):
12 | if len(name) >= 2 and name.startswith('"') and name.endswith('"'):
13 | name = name[1:len(name)-1]
14 | self.name = name
15 |
16 | def setHandler(self, handlers):
17 | for handler in handlers.split(' '):
18 | if handler.startswith('event'):
19 | self.handler = handler
20 |
21 | def listDevices():
22 | devices = []
23 | with open('/proc/bus/input/devices', 'r') as f:
24 | device = None
25 | while True:
26 | s = f.readline()
27 | if s == '':
28 | break
29 | s = s.strip()
30 | if s == '':
31 | devices.append(device)
32 | device = None
33 | else:
34 | if device is None:
35 | device = InputDevice()
36 | if s.startswith('N: Name='):
37 | device.setName(s[8:])
38 | elif s.startswith('H: Handlers='):
39 | device.setHandler(s[12:])
40 | return devices
41 |
42 | def detectJoystick(joystickNames):
43 | for device in listDevices():
44 | for joystickName in joystickNames:
45 | if joystickName in device.name:
46 | return '/dev/input/%s' % device.handler
47 | return None
48 |
--------------------------------------------------------------------------------
/samples/advance/do_iter.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from collections.abc import Iterable, Iterator
5 |
6 | def g():
7 | yield 1
8 | yield 2
9 | yield 3
10 |
11 | print('Iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
12 | print('Iterable? \'abc\':', isinstance('abc', Iterable))
13 | print('Iterable? 123:', isinstance(123, Iterable))
14 | print('Iterable? g():', isinstance(g(), Iterable))
15 |
16 | print('Iterator? [1, 2, 3]:', isinstance([1, 2, 3], Iterator))
17 | print('Iterator? iter([1, 2, 3]):', isinstance(iter([1, 2, 3]), Iterator))
18 | print('Iterator? \'abc\':', isinstance('abc', Iterator))
19 | print('Iterator? 123:', isinstance(123, Iterator))
20 | print('Iterator? g():', isinstance(g(), Iterator))
21 |
22 | # iter list:
23 | print('for x in [1, 2, 3, 4, 5]:')
24 | for x in [1, 2, 3, 4, 5]:
25 | print(x)
26 |
27 | print('for x in iter([1, 2, 3, 4, 5]):')
28 | for x in iter([1, 2, 3, 4, 5]):
29 | print(x)
30 |
31 | print('next():')
32 | it = iter([1, 2, 3, 4, 5])
33 | print(next(it))
34 | print(next(it))
35 | print(next(it))
36 | print(next(it))
37 | print(next(it))
38 |
39 | d = {'a': 1, 'b': 2, 'c': 3}
40 |
41 | # iter each key:
42 | print('iter key:', d)
43 | for k in d.keys():
44 | print('key:', k)
45 |
46 | # iter each value:
47 | print('iter value:', d)
48 | for v in d.values():
49 | print('value:', v)
50 |
51 | # iter both key and value:
52 | print('iter item:', d)
53 | for k, v in d.items():
54 | print('item:', k, v)
55 |
56 | # iter list with index:
57 | print('iter enumerate([\'A\', \'B\', \'C\']')
58 | for i, value in enumerate(['A', 'B', 'C']):
59 | print(i, value)
60 |
61 | # iter complex list:
62 | print('iter [(1, 1), (2, 4), (3, 9)]:')
63 | for x, y in [(1, 1), (2, 4), (3, 9)]:
64 | print(x, y)
65 |
--------------------------------------------------------------------------------
/samples/micropython/tank/joystick.py:
--------------------------------------------------------------------------------
1 | # joystick control:
2 |
3 | import struct
4 |
5 | # define button code:
6 |
7 | BUTTON_A = 305
8 | BUTTON_B = 304
9 | BUTTON_X = 307
10 | BUTTON_Y = 306
11 | BUTTON_PLUS = 313
12 | BUTTON_MINUS = 312
13 | BUTTON_START = 317
14 | BUTTON_HOME = 316
15 |
16 | class JoyStick():
17 | def __init__(self, eventFile):
18 | self.eventFile = eventFile
19 | self.buttonHandler = None
20 | self.joyLeftHandler = None
21 | self.joyRightHandler = None
22 |
23 | def setButtonHandler(self, buttonHandler):
24 | self.buttonHandler = buttonHandler
25 |
26 | def setJoyLeftHandler(self, joyLeftHandler):
27 | self.joyLeftHandler = joyLeftHandler
28 |
29 | def setJoyRightHandler(self, joyRightHandler):
30 | self.joyRightHandler = joyRightHandler
31 |
32 | def startLoop(self):
33 | FORMAT = 'llHHI'
34 | EVENT_SIZE = struct.calcsize(FORMAT)
35 | with open(self.eventFile, 'rb') as infile:
36 | lx, ly, rx, ry = 0, 0, 0, 0
37 | while True:
38 | event = infile.read(EVENT_SIZE)
39 | _, _, t, c, v = struct.unpack(FORMAT, event)
40 | if t == 1 and v == 1:
41 | # button pressed:
42 | if self.buttonHandler:
43 | if not self.buttonHandler(c):
44 | return
45 | if t == 3:
46 | if c == 0 and self.joyLeftHandler:
47 | # left stick & horizontal:
48 | lx = v - 32768
49 | self.joyLeftHandler(lx, ly)
50 | elif c == 1 and self.joyLeftHandler:
51 | # left stick & vertical:
52 | ly = v - 32768
53 | self.joyLeftHandler(lx, ly)
54 | elif c == 3 and self.joyRightHandler:
55 | # right stick & horizontal:
56 | rx = v - 32768
57 | self.joyRightHandler(rx, ry)
58 | elif c == 4 and self.joyRightHandler:
59 | # right stick & vertical:
60 | ry = v - 32768
61 | self.joyRightHandler(rx, ry)
62 |
--------------------------------------------------------------------------------
/samples/commonlib/use_urllib.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from urllib import request, parse
5 |
6 | # get:
7 |
8 | with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
9 | data = f.read()
10 | print('Status:', f.status, f.reason)
11 | for k, v in f.getheaders():
12 | print('%s: %s' % (k, v))
13 | print('Data:', data.decode('utf-8'))
14 |
15 | # advanced get:
16 |
17 | req = request.Request('http://www.douban.com/')
18 | req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
19 | with request.urlopen(req) as f:
20 | print('Status:', f.status, f.reason)
21 | for k, v in f.getheaders():
22 | print('%s: %s' % (k, v))
23 | print('Data:', f.read().decode('utf-8'))
24 |
25 | # post:
26 |
27 | print('Login to weibo.cn...')
28 | email = input('Email: ')
29 | passwd = input('Password: ')
30 | login_data = parse.urlencode([
31 | ('username', email),
32 | ('password', passwd),
33 | ('entry', 'mweibo'),
34 | ('client_id', ''),
35 | ('savestate', '1'),
36 | ('ec', ''),
37 | ('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom&jumpfrom=weibocom')
38 | ])
39 |
40 | req = request.Request('https://passport.weibo.cn/sso/login')
41 | req.add_header('Origin', 'https://passport.weibo.cn')
42 | req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
43 | req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F%3Fjumpfrom%3Dweibocom')
44 |
45 | with request.urlopen(req, data=login_data.encode('utf-8')) as f:
46 | print('Status:', f.status, f.reason)
47 | for k, v in f.getheaders():
48 | print('%s: %s' % (k, v))
49 | print('Data:', f.read().decode('utf-8'))
50 |
51 | # with proxy and proxy auth:
52 |
53 | proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
54 | proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
55 | proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
56 | opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
57 | with opener.open('http://www.example.com/login.html') as f:
58 | pass
59 |
--------------------------------------------------------------------------------
/samples/oop_advance/orm.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | ' Simple ORM using metaclass '
5 |
6 | class Field(object):
7 |
8 | def __init__(self, name, column_type):
9 | self.name = name
10 | self.column_type = column_type
11 |
12 | def __str__(self):
13 | return '<%s:%s>' % (self.__class__.__name__, self.name)
14 |
15 | class StringField(Field):
16 |
17 | def __init__(self, name):
18 | super(StringField, self).__init__(name, 'varchar(100)')
19 |
20 | class IntegerField(Field):
21 |
22 | def __init__(self, name):
23 | super(IntegerField, self).__init__(name, 'bigint')
24 |
25 | class ModelMetaclass(type):
26 |
27 | def __new__(cls, name, bases, attrs):
28 | if name=='Model':
29 | return type.__new__(cls, name, bases, attrs)
30 | print('Found model: %s' % name)
31 | mappings = dict()
32 | for k, v in attrs.items():
33 | if isinstance(v, Field):
34 | print('Found mapping: %s ==> %s' % (k, v))
35 | mappings[k] = v
36 | for k in mappings.keys():
37 | attrs.pop(k)
38 | attrs['__mappings__'] = mappings # 保存属性和列的映射关系
39 | attrs['__table__'] = name # 假设表名和类名一致
40 | return type.__new__(cls, name, bases, attrs)
41 |
42 | class Model(dict, metaclass=ModelMetaclass):
43 |
44 | def __init__(self, **kw):
45 | super(Model, self).__init__(**kw)
46 |
47 | def __getattr__(self, key):
48 | try:
49 | return self[key]
50 | except KeyError:
51 | raise AttributeError(r"'Model' object has no attribute '%s'" % key)
52 |
53 | def __setattr__(self, key, value):
54 | self[key] = value
55 |
56 | def save(self):
57 | fields = []
58 | params = []
59 | args = []
60 | for k, v in self.__mappings__.items():
61 | fields.append(v.name)
62 | params.append('?')
63 | args.append(getattr(self, k, None))
64 | sql = 'insert into %s (%s) values (%s)' % (self.__table__, ','.join(fields), ','.join(params))
65 | print('SQL: %s' % sql)
66 | print('ARGS: %s' % str(args))
67 |
68 | # testing code:
69 |
70 | class User(Model):
71 | id = IntegerField('id')
72 | name = StringField('username')
73 | email = StringField('email')
74 | password = StringField('password')
75 |
76 | u = User(id=12345, name='Michael', email='test@orm.org', password='my-pwd')
77 | u.save()
78 |
--------------------------------------------------------------------------------
/samples/mail/fetch_mail.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 |
4 | from email.parser import Parser
5 | from email.header import decode_header
6 | from email.utils import parseaddr
7 |
8 | import poplib
9 |
10 | # 输入邮件地址, 口令和POP3服务器地址:
11 | email = input('Email: ')
12 | password = input('Password: ')
13 | pop3_server = input('POP3 server: ')
14 |
15 | def guess_charset(msg):
16 | charset = msg.get_charset()
17 | if charset is None:
18 | content_type = msg.get('Content-Type', '').lower()
19 | pos = content_type.find('charset=')
20 | if pos >= 0:
21 | charset = content_type[pos + 8:].strip()
22 | return charset
23 |
24 | def decode_str(s):
25 | value, charset = decode_header(s)[0]
26 | if charset:
27 | value = value.decode(charset)
28 | return value
29 |
30 | def print_info(msg, indent=0):
31 | if indent == 0:
32 | for header in ['From', 'To', 'Subject']:
33 | value = msg.get(header, '')
34 | if value:
35 | if header=='Subject':
36 | value = decode_str(value)
37 | else:
38 | hdr, addr = parseaddr(value)
39 | name = decode_str(hdr)
40 | value = u'%s <%s>' % (name, addr)
41 | print('%s%s: %s' % (' ' * indent, header, value))
42 | if (msg.is_multipart()):
43 | parts = msg.get_payload()
44 | for n, part in enumerate(parts):
45 | print('%spart %s' % (' ' * indent, n))
46 | print('%s--------------------' % (' ' * indent))
47 | print_info(part, indent + 1)
48 | else:
49 | content_type = msg.get_content_type()
50 | if content_type=='text/plain' or content_type=='text/html':
51 | content = msg.get_payload(decode=True)
52 | charset = guess_charset(msg)
53 | if charset:
54 | content = content.decode(charset)
55 | print('%sText: %s' % (' ' * indent, content + '...'))
56 | else:
57 | print('%sAttachment: %s' % (' ' * indent, content_type))
58 |
59 | # 连接到POP3服务器:
60 | server = poplib.POP3(pop3_server)
61 | # 可以打开或关闭调试信息:
62 | server.set_debuglevel(1)
63 | # 可选:打印POP3服务器的欢迎文字:
64 | print(server.getwelcome().decode('utf-8'))
65 | # 身份认证:
66 | server.user(email)
67 | server.pass_(password)
68 | # stat()返回邮件数量和占用空间:
69 | print('Messages: %s. Size: %s' % server.stat())
70 | # list()返回所有邮件的编号:
71 | resp, mails, octets = server.list()
72 | # 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
73 | print(mails)
74 | # 获取最新一封邮件, 注意索引号从1开始:
75 | index = len(mails)
76 | resp, lines, octets = server.retr(index)
77 | # lines存储了邮件的原始文本的每一行,
78 | # 可以获得整个邮件的原始文本:
79 | msg_content = b'\r\n'.join(lines).decode('utf-8')
80 | # 稍后解析出邮件:
81 | msg = Parser().parsestr(msg_content)
82 | print_info(msg)
83 | # 可以根据邮件索引号直接从服务器删除邮件:
84 | # server.dele(index)
85 | # 关闭连接:
86 | server.quit()
87 |
--------------------------------------------------------------------------------
/samples/micropython/rccar/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env pybricks-micropython
2 |
3 | import struct, threading
4 |
5 | from pybricks import ev3brick as brick
6 | from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)
7 | from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
8 | from pybricks.tools import print, wait, StopWatch
9 | from pybricks.robotics import DriveBase
10 | from devices import detectJoystick
11 |
12 | class Robot():
13 | def __init__(self):
14 | self.motor = Motor(Port.B)
15 | self.ultrasonic = UltrasonicSensor(Port.S4)
16 | self.active = True
17 | self.speed = 0
18 | self.colors = [None, Color.GREEN, Color.YELLOW, Color.RED]
19 |
20 | def setSpeed(self, acc):
21 | if acc < 0:
22 | self.speed = max(-3, self.speed - 1)
23 | elif acc > 0:
24 | self.speed = min(3, self.speed + 1)
25 | else:
26 | self.speed = 0
27 | if self.speed != 0:
28 | self.motor.run(self.speed * 90)
29 | else:
30 | self.motor.stop()
31 | brick.light(self.colors[abs(self.speed)])
32 |
33 | def inactive(self):
34 | self.active = False
35 | self.setSpeed(0)
36 | brick.sound.beep()
37 |
38 | def autoStopLoop(robot):
39 | while robot.active:
40 | if robot.speed > 0 and robot.ultrasonic.distance() < 200:
41 | robot.setSpeed(0)
42 | wait(100)
43 |
44 | def joystickLoop(robot, eventFile):
45 | FORMAT = 'llHHI'
46 | EVENT_SIZE = struct.calcsize(FORMAT)
47 | with open(eventFile, 'rb') as infile:
48 | while True:
49 | event = infile.read(EVENT_SIZE)
50 | _, _, t, c, v = struct.unpack(FORMAT, event)
51 | # button A, B:
52 | if t == 1 and v == 1:
53 | if c == 305:
54 | # press A:
55 | robot.setSpeed(1)
56 | elif c == 304:
57 | # press B:
58 | robot.setSpeed(-1)
59 | elif c == 307:
60 | # press X:
61 | return robot.inactive()
62 | elif t == 3:
63 | if c == 1:
64 | # Left stick & vertical:
65 | speed = 0
66 | if v < 32768:
67 | # up:
68 | speed = 1
69 | elif v > 32768:
70 | # down:
71 | speed = -1
72 | robot.setSpeed(speed)
73 |
74 | def buttonLoop(robot):
75 | while True:
76 | if not any(brick.buttons()):
77 | wait(10)
78 | else:
79 | if Button.LEFT in brick.buttons():
80 | robot.setSpeed(-1)
81 | elif Button.RIGHT in brick.buttons():
82 | robot.setSpeed(1)
83 | elif Button.CENTER in brick.buttons():
84 | robot.setSpeed(0)
85 | elif Button.UP in brick.buttons():
86 | return robot.inactive()
87 | wait(500)
88 |
89 | def main():
90 | brick.sound.beep()
91 | joystickEvent = detectJoystick(['Controller'])
92 | robot = Robot()
93 | t = threading.Thread(target=autoStopLoop, args=(robot,))
94 | t.start()
95 | if joystickEvent:
96 | joystickLoop(robot, joystickEvent)
97 | else:
98 | buttonLoop(robot)
99 |
100 | main()
101 |
--------------------------------------------------------------------------------
/samples/micropython/tank/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env pybricks-micropython
2 |
3 | import struct, threading
4 |
5 | from pybricks import ev3brick as brick
6 | from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor)
7 | from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align)
8 | from pybricks.tools import print, wait, StopWatch
9 | from pybricks.robotics import DriveBase
10 |
11 | from devices import detectJoystick
12 | from joystick import JoyStick, BUTTON_A, BUTTON_X
13 |
14 | SPEED = 100
15 | STEERING = 90
16 |
17 | STATUS_STOPPED = 0
18 | STATUS_FORWARD = 1
19 | STATUS_BACKWARD = 2
20 | STATUS_STEERING = 3
21 |
22 | COLORS = (None, Color.GREEN, Color.RED, Color.YELLOW)
23 |
24 | class Driver():
25 | def __init__(self, leftMotor, rightMotor, diameter, axle):
26 | self.driver = DriveBase(leftMotor, rightMotor, diameter, axle)
27 | self.x = 0
28 | self.y = 0
29 | self.speed = 0
30 | self.steering = 0
31 |
32 | def drive(self, speed, steering):
33 | self.speed = speed
34 | self.steering = steering
35 | if self.speed == 0:
36 | self.driver.stop()
37 | else:
38 | self.driver.drive(self.speed, self.steering)
39 |
40 | class Robot():
41 | def __init__(self, leftMotor, rightMotor, topMotor, diameter, axle, maxSpeed=300, maxSteering=180, port=Port.S4):
42 | self.driver = Driver(leftMotor, rightMotor, diameter, axle)
43 | self.cannon = topMotor
44 | self.ultrasonic = UltrasonicSensor(port)
45 | self.speedStep = 32767 // maxSpeed
46 | self.steeringStep = 32767 // maxSteering
47 | self.active = True
48 |
49 | def drive(self, x, y):
50 | # map y (-32768 ~ +32767) to speed (+maxSpeed ~ -maxSpeed):
51 | speed = -y // self.speedStep
52 | # map x (-32768 ~ +32767) to steering (-maxSteering ~ +maxSteering):
53 | steering = x // self.steeringStep
54 | self.driver.drive(speed, steering)
55 |
56 | def target(self, x):
57 | self.cannon.run(-x // 327)
58 |
59 | def fire(self):
60 | brick.sound.file('cannon.wav')
61 |
62 | def inactive(self):
63 | self.active = False
64 | self.drive(0, 0)
65 | brick.sound.beep()
66 |
67 | def autoStopLoop(robot):
68 | while robot.active:
69 | if robot.ultrasonic.distance() < 200:
70 | robot.drive(0, 0)
71 | wait(100)
72 |
73 | def main():
74 | brick.sound.beep()
75 | joystickEvent = detectJoystick(['Controller'])
76 | if joystickEvent:
77 | robot = Robot(Motor(Port.D), Motor(Port.A), Motor(Port.B), 55, 200)
78 | t = threading.Thread(target=autoStopLoop, args=(robot,))
79 | t.start()
80 |
81 | def onButtonPressed(code):
82 | if code == BUTTON_X:
83 | robot.inactive()
84 | return False
85 | if code == BUTTON_A:
86 | robot.fire()
87 | return True
88 |
89 | def onLeftJoyChanged(x, y):
90 | robot.drive(x, y)
91 |
92 | def onRightJoyChanged(x, y):
93 | robot.target(x)
94 |
95 | joystick = JoyStick(joystickEvent)
96 | joystick.setButtonHandler(onButtonPressed)
97 | joystick.setJoyLeftHandler(onLeftJoyChanged)
98 | joystick.setJoyRightHandler(onRightJoyChanged)
99 | joystick.startLoop()
100 | else:
101 | brick.sound.beep()
102 |
103 | main()
104 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
--------------------------------------------------------------------------------