Tools
Записки программиста, обо всем и ни о чем. Но, наверное, больше профессионального.
2015-05-31
Параллельная парковка
Posted by Valentin at 02:17 0 comments
2015-05-30
Сборник о Python
Posted by Valentin at 18:26 0 comments
Labels: programming, python
2015-05-28
Навигация 2015
Posted by Valentin at 18:43 0 comments
Labels: driving, windsurfing
2015-05-26
Patterns
class DelegatedAttribute: def __init__(self, delegate_name, attr_name): self.attr_name = attr_name self.delegate_name = delegate_name def __get__(self, instance, owner): if instance is None: return self else: # return instance.delegate.attr return getattr(self.delegate(instance), self.attr_name) def __set__(self, instance, value): # instance.delegate.attr = value setattr(self.delegate(instance), self.attr_name, value) def __delete__(self, instance): delattr(self.delegate(instance), self.attr_name) def delegate(self, instance): return getattr(instance, self.delegate_name) def __str__(self): return "<delegated attribute, '" + self.attr_name + "' at " + str(id(self)) + '>' |
class Foo: def __init__(self): self.bar = 'bar in foo' def baz(self): return 'baz in foo' class Baz: def __init__(self): self.foo = Foo() bar = DelegatedAttribute('foo', 'bar') baz = DelegatedAttribute('foo', 'baz') x = Baz() print(x.bar) # prints 'bar in foo' print(x.baz()) # prints 'baz in foo' |
def delegate_as(delegate_cls): # gather the attributes of the delegate class to add to the decorated class attributes = set(delegate_cls.__dict__.keys()) def inner(cls): # create property for storing the delegate setattr(cls, 'delegate', SimpleProperty()) # don't bother adding attributes that the class already has attrs = attributes - set(cls.__dict__.keys()) # set all the attributes for attr in attrs: setattr(cls, attr, DelegatedAttribute('delegate', attr)) return cls return inner |
class Borg: _shared_state = {} def __init__(self): self.__dict__ = self._shared_state
Posted by Valentin at 20:30 0 comments
Labels: programming, python
2015-05-25
Lambda calculus
bands = [ {'name': 'sunset rubdown', 'country': 'UK', 'active': False}, {'name': 'women', 'country': 'Germany', 'active': False}, {'name': 'a silver mt. zion', 'country': 'Spain', 'active': True} ] def assoc(_d, key, value): from copy import deepcopy d = deepcopy(_d) d[key] = value return d def call(fn, key): def apply_fn(record): return assoc(record, key, fn(record.get(key))) return apply_fn def pipeline_each(data, fns): return reduce( lambda a, x: map(x, a), fns, data ) print pipeline_each( bands, [ call(lambda x: 'Canada', 'country'), call(lambda x: x.replace('.', ''), 'name'), call(str.title, 'name') ] ) |
def format_bands(bands): for band in bands: band['country'] = 'Canada' band['name'] = band['name'].replace('.', '') band['name'] = band['name'].title() |
Posted by Valentin at 18:37 0 comments
Labels: functional programming
2015-05-23
Markdown lint
Posted by Valentin at 17:33 0 comments
Labels: JS
Hancock
Posted by Valentin at 00:44 0 comments
Labels: movie
2015-05-22
Книги
С фокусом на многопоточность, которой раньше в стандарте не было.
Поэтому рекомендую эту книгу тем, кто решил начинать изучать STL. Обратите внимание на издание, вам нужно второе издание, там на обложке крупно написано С++11.
Вообще это относится ко всем книгам по С++. То, что издано до 2012 года можно считать устаревшим
Posted by Valentin at 17:04 0 comments
Labels: Book
Apache Drill
Posted by Valentin at 00:25 0 comments
2015-05-21
М2 / E105
Posted by Valentin at 03:05 0 comments
Labels: driving
2015-05-11
Kilo
Posted by Valentin at 11:30 0 comments
Labels: cloud
2015-05-10
A Family Thing
Posted by Valentin at 01:45 0 comments
Labels: movie
2015-05-08
Монада, да не та
Posted by Valentin at 02:47 0 comments
Labels: citation
Python introspection
class ListStream: def __init__(self): self.data = [] def write(self, s): self.data.append(s) def interrogate(item): """Print useful information about item. Returns list of strings. """ sys.stdout = x = ListStream() if hasattr(item, '__name__'): print "NAME: ", item.__name__ if hasattr(item, '__class__'): print "CLASS: ", item.__class__.__name__ print "ID: ", id(item) print "TYPE: ", type(item) print "VALUE: ", repr(item) print "CALLABLE:", if callable(item): print "Yes" else: print "No" if hasattr(item, '__doc__'): doc = getattr(item, '__doc__') doc = doc.strip() # Remove leading/trailing whitespace. firstline = doc.split('\n')[0] print "DOC: ", firstline print "ATTRIBUTES:", vars(item) print "NAMES: ", dir(item) sys.stdout = sys.__stdout__ return x.data |
NAME: ttt CLASS: HTMLDocument ID: 77748944 TYPE: <type 'ImplicitAcquirerWrapper'> VALUE: <HTMLDocument at /docs/storage/members/valik/test/ttt> CALLABLE: Yes DOC: Subclassed Document type ATTRIBUTES: {'contributors': (), 'nd_uid': '143058357876X4990437627', 'distribution_log': [], '_safety_belt': 'None', 'id': 'ttt', 'subject': (), 'category': 'BGClientCard', '_last_safety_belt': '', 'skip_catalogize': [], 'subscribed_users': {}, '_last_safety_belt_editor': 'zope', '_Delete_objects_Permission': ('Director', 'Manager'), 'text_format': 'html', 'version': <Products.CMFNauTools.ContentVersions.VersionsContainer object at 0x4bf86e0>, '_objects': ({'meta_type': 'Object Manager', 'id': 'version'},), 'followup': <Products.CMFNauTools.TaskItemContainer.TaskItemContainer object at 0x4bc9230>, '_version_tag': -8584773770338532410, 'portal_type': 'HTMLDocument', 'registry_data': {}, 'language': 'ru', 'rights': '', 'expiration_date': None, '_Manage_properties_Permission': ('Author', 'Owner', 'Writer'), '_owner': (['acl_users'], 'zope'), 'changes_log': [], '__ac_local_roles__': {'zope': ['Owner']}} NAMES: ['COPY', 'COPY__roles__', 'Category', 'CategoryAttributes', 'Contributors', 'Contributors__roles__', 'CookedBody', 'CookedBody__roles__', 'CreationDate', 'CreationDate__roles__', 'Creator', 'Creator__roles__', ... |
import inspect import example print inspect.getsource(example.A.get_name)
import inspect import example arg_spec = inspect.getargspec(example.module_level_function) print 'NAMES :', arg_spec[0] print '* :', arg_spec[1] print '** :', arg_spec[2] print 'defaults:', arg_spec[3] args_with_defaults = arg_spec[0][-len(arg_spec[3]):] print 'args & defaults:', zip(args_with_defaults, arg_spec[3])
Posted by Valentin at 02:32 0 comments
Labels: python
2015-05-05
Вакансия
Posted by Valentin at 19:19 0 comments
2015-05-02
A Bronx Tale
Posted by Valentin at 03:34 0 comments
Labels: movie
2015-05-01
The Remains of the Day
Posted by Valentin at 03:16 0 comments
Labels: movie
With Honors
Posted by Valentin at 02:52 0 comments
Labels: movie