europython 2015 - instructions

17
1 / 17 Instructions Max Tepkeev 20 July 2015 Bilbao, Spain

Upload: max-tepkeev

Post on 18-Aug-2015

37 views

Category:

Software


2 download

TRANSCRIPT

Page 1: EuroPython 2015 - Instructions

1 / 17

Instructions

Max Tepkeev20 July 2015Bilbao, Spain

Page 2: EuroPython 2015 - Instructions

2 / 17

Page 3: EuroPython 2015 - Instructions

3 / 17

Page 4: EuroPython 2015 - Instructions

4 / 17

Page 5: EuroPython 2015 - Instructions

5 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada' 1, 2]>>> results = []

>>> for item in container:... if isinstance(item, str) and len(item) == 3:... results.append(item)

>>> print results['foo', 'bar']

Page 6: EuroPython 2015 - Instructions

6 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada' 1, 2]>>> results = [item for item in container if isinstance(item, str) and len(item) == 3]

>>> print results['foo', 'bar']

Page 7: EuroPython 2015 - Instructions

7 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada', ['olo', 3, [4, 'baz']], 1, 2]

>>> def finder(searchable):... results = []

... for item in searchable:

... if isinstance(item, str) and len(item) == 3:

... results.append(item)

... elif isinstance(item, list):

... results.extend(finder(item))

... return results

>>> print finder(container)['foo', 'bar', 'olo', 'baz']

Page 8: EuroPython 2015 - Instructions

8 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada' 1, 2]

>>> import instructions as i

>>> results = i.findstring__len(3).inside(container)

>>> print list(results)['foo', 'bar']

Page 9: EuroPython 2015 - Instructions

9 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada', ['olo', 3, [4, 'baz']], 1, 2]

>>> import instructions as i

>>> results = i.findstring__len(3).inside(container)

>>> print list(results)['foo', 'bar', 'olo', 'baz']

Page 10: EuroPython 2015 - Instructions

10 / 17

Task

Find all strings with a length of 3

>>> container = ['foo', 'bar', 'yada', ['olo', 3, [4, 'baz']], 1, 2]

>>> import instructions as i

>>> options = dict(level=1, limit=1, ignore=[tuple])>>> results = i.findstring__len(3, **options).inside(container)

>>> print list(results)['foo']

Page 11: EuroPython 2015 - Instructions

11 / 17

Concepts

• find – command• string – datatype• len – filter• 3 – filter argument• level, ignore - options

findstring__len

(3, level=1, ignore=[tuple])

Page 12: EuroPython 2015 - Instructions

12 / 17

Examples

• findstring__lengte(3)• findnumeric__between(5, 10)• countint__divisibleby(2)• firstlist__contains_all(['foo',

'baz'])• lasttuple__str_contains_str('ba')• existsset__issuperset(set(['bar']))• finddict__contains_all_keys(['a',

'b'])• finddict__key_contains_str('ab')• finddict__contains_any_values(['b',

'ba'])

Page 13: EuroPython 2015 - Instructions

13 / 17

Advanced Mode

>>> container = ['foobar', 'fooyadabar', 2]

>>> from instructions import commands, datatypes

>>> result = commands.count(... datatypes.string.startswith('foo') & ... datatypes.string.endswith('bar') & ... ~datatypes.string.contains('yada')... ).inside(container)

>>> print result1

Page 14: EuroPython 2015 - Instructions

14 / 17

Installation

From PyPi:

$ pip install instructions

or clone from github:

$ git clone git://github.com/maxtepkeev/instructions.git

Page 15: EuroPython 2015 - Instructions

15 / 17

Features

• Python 2.6 - 3.4, PyPy, PyPy3• No external dependencies• Documentation at Read the Docs• More than 100 instructions included• 100% test coverage• Library – 400 LOC• Tests – 3600 LOC

Page 16: EuroPython 2015 - Instructions

16 / 17

Supported Python Datatypes

bool longstr complexunicode listbytes tuplebytearray setint frozensetfloat dict

numeric iterable

Page 17: EuroPython 2015 - Instructions

17 / 17

Contacts

https://github.com/maxtepkeev/instructions

slides: http://slideshare.net/maxtepkeevemail: [email protected]: max.tepkeev