Записки программиста, обо всем и ни о чем. Но, наверное, больше профессионального.
2014-07-31
Large-Scale Machine Learning & Example
Posted by
Valentin
at
11:30
0
comments
2014-07-30
Open Source vs Proprietary
Posted by
Valentin
at
11:30
0
comments
Labels: opensource
2014-07-29
webcamfixer
#!/usr/bin/env python | |
# -*- mode: python; coding: utf-8 -*- | |
# Copyright (c) Valentin Fedulov <vasnake@gmail.com> | |
''' This script intended to fix some webcam software problems. | |
It's job can be described in three points: | |
First step: find latest JPG file in SEARCH_IN folder and subfolders. | |
Next step: copy that file to WRITE_TO destination. | |
Last step: put a timestamp on lower right corner of the WRITE_TO image. | |
Tested on Linux CentOS 6. | |
You may need to install PIL: | |
yum install python-imaging | |
also you'll need FreeSans.ttf (http://ftp.gnu.org/gnu/freefont/), refer to FONT constant. | |
Usage | |
Edit FONT, SEARCH_IN and WRITE_TO constants and call script like: | |
sudo -u ftpsecure python -u /opt/webcamfixer.py | |
or set a crontab job: | |
nano /etc/crontab | |
SHELL=/bin/bash | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
MAILTO=root | |
HOME=/ | |
*/3 * * * * ftpsecure python -u /opt/webcamfixer.py | |
Ftpsecure came from my vsftpd setup, just forget about it. | |
''' | |
from __future__ import print_function | |
import time | |
import os | |
import shutil | |
from PIL import Image, ImageDraw, ImageFont | |
FONT = "/opt/FreeSans.ttf" | |
SEARCH_IN = u'/home/ftpsecure' | |
WRITE_TO = u'/home/ftpsecure/cam_1.jpg' | |
print(u"Clubwindsurf webcam picture fixer. {timestamp}".format(timestamp=time.strftime('%Y-%m-%d %H:%M:%S'))) | |
def main(): | |
fname = findLatestJpg(SEARCH_IN) | |
if fname == WRITE_TO: | |
print(u"File is fresh as it is, quit.") | |
else: | |
statinfo = os.stat(fname) | |
if statinfo.st_size > 0: | |
if copyFile(fname, WRITE_TO): | |
timeInfo = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(statinfo.st_mtime)) | |
addTimeStamp(WRITE_TO, timeInfo) | |
def findLatestJpg(dirName): | |
fileName = u'' | |
listFiles = findFiles(dirName, u'jpg') | |
def getMtime(fn): | |
res = 0 | |
try: | |
statinfo = os.stat(fn) | |
if statinfo.st_size > 0: | |
res = statinfo.st_mtime | |
except: | |
print(u"Problem with {fn}".format(fn=fn)) | |
return res | |
sortedLF = sorted(listFiles, key=getMtime, reverse=True) | |
if sortedLF: | |
fileName = sortedLF[0] | |
return fileName | |
def copyFile(fromName, toName): | |
if fromName and toName: | |
if not fromName.lower() == toName.lower(): | |
print(u"Copy from {fromName} to {toName}".format(fromName=fromName, toName=toName)) | |
shutil.copy(fromName, toName) | |
return True | |
return False | |
def findFiles(scandir, ext): | |
'''Returns list of file names with extension 'ext' | |
recursively finded inside 'scandir' | |
scandir, ext is a unicode strings | |
''' | |
ftype = u'.' + ext.lower() | |
lstFiles = [] | |
for root, dirs, files in os.walk(scandir): | |
for f in files: | |
fn = os.path.join(root, f) | |
if fn.lower().endswith(ftype): | |
lstFiles.append(unicode(fn)) | |
return lstFiles | |
def addTimeStamp(fileName, timeInfo): | |
'''80% of this function code came from | |
http://jepoirrier.org/2008/11/24/short-script-to-add-a-timestamp-on-pictures/ | |
https://github.com/jepoirrier/miscScripts/blob/master/timestampFiles.py | |
''' | |
fontSize = 15 | |
textPadding = 2 | |
boxWidth = 150 | |
boxHeight = 25 | |
im = Image.open(fileName) | |
topLeftWidth = int(im.size[0] - boxWidth) | |
topLeftHeight = int(im.size[1] - boxHeight) | |
imfont = ImageFont.truetype(FONT, fontSize) | |
draw = ImageDraw.Draw(im) | |
draw.rectangle([topLeftWidth, topLeftHeight, im.size[0], im.size[1]], | |
fill="black") | |
draw.text([topLeftWidth + textPadding, topLeftHeight + textPadding], | |
timeInfo, fill="lime", font=imfont) | |
del draw | |
im.save(fileName, 'JPEG', quality=90, optimize=True) | |
if __name__ == "__main__": | |
main() |
Posted by
Valentin
at
11:30
0
comments
Labels: python
2014-07-28
Import VirtualBox machine
<StorageControllers> <StorageController name="IDE Controller" type="PIIX3" PortCount="2" useHostIOCache="true"> <AttachedDevice type="HardDisk" port="0" device="0"> <Image uuid="{c8c7052e-926c-4419-93ac-46756167604f}"/> </AttachedDevice> <AttachedDevice type="HardDisk" port="0" device="1"> <Image uuid="{c8c7052e-926c-4419-93ac-46756167604f}"/> </AttachedDevice> <AttachedDevice passthrough="false" type="DVD" port="1" device="0"/> </StorageController> <StorageController name="Floppy Controller" type="I82078" PortCount="1" useHostIOCache="true"> <AttachedDevice type="Floppy" port="0" device="0"/> </StorageController> </StorageControllers> |
<DiskSection> <Info>List of the virtual disks used in the package</Info> <Disk ovf:capacity="10737418240" ovf:diskId="vmdisk1" ovf:fileRef="file1" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" vbox:uuid="2d8c0bbc-ed2a-424a-9cfa-e8e922eb42ac"/> <Disk ovf:capacity="107374182400" ovf:diskId="vmdisk2" ovf:fileRef="file2" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" vbox:uuid="c8c7052e-926c-4419-93ac-46756167604f"/> </DiskSection> |
<StorageControllers> <StorageController name="IDE Controller" type="PIIX3" PortCount="2" useHostIOCache="true"> <AttachedDevice type="HardDisk" port="0" device="0"> <Image uuid="{c8c7052e-926c-4419-93ac-46756167604f}"/> </AttachedDevice> <AttachedDevice type="HardDisk" port="0" device="1"> <Image uuid="{2d8c0bbc-ed2a-424a-9cfa-e8e922eb42ac}"/> </AttachedDevice> <AttachedDevice passthrough="false" type="DVD" port="1" device="0"/> </StorageController> <StorageController name="Floppy Controller" type="I82078" PortCount="1" useHostIOCache="true"> <AttachedDevice type="Floppy" port="0" device="0"/> </StorageController> </StorageControllers> |
Posted by
Valentin
at
11:30
0
comments
2014-07-25
NLP
Posted by
Valentin
at
11:30
3
comments
2014-07-24
Duck Jibe
Posted by
Valentin
at
11:30
3
comments
Labels: video, windsurfing
2014-07-23
2.5 тонны
Posted by
Valentin
at
11:30
0
comments
2014-07-22
fb2tools
Posted by
Valentin
at
11:30
0
comments
Labels: python
2014-07-21
10 распространенных ошибок
>>> def foo(bar=[]): # bar is optional and defaults to [] if not specified ... bar.append("baz") # but this line could be problematic, as we'll see... ... return bar
>>> class A(object): ... x = 1 ... >>> class B(A): ... pass ... >>> class C(A): ... pass ... >>> print A.x, B.x, C.x 1 1 1 >>> B.x = 2 >>> print A.x, B.x, C.x 1 2 1 >>> A.x = 3 >>> print A.x, B.x, C.x 3 2 3
>>> try: ... l = ["a", "b"] ... int(l[2]) ... except ValueError, IndexError: # To catch both exceptions, right? ... pass ... Traceback (most recent call last): File "<stdin>", line 3, in <module> IndexError: list index out of range
>>> try: ... l = ["a", "b"] ... int(l[2]) ... except (ValueError, IndexError) as e: ... pass
>>> x = 10 >>> def foo(): ... x += 1 ... print x ... >>> foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in foo UnboundLocalError: local variable 'x' referenced before assignment
>>> numbers = [n for n in range(10)] >>> for i in range(len(numbers)): ... if odd(numbers[i]): ... del numbers[i] # BAD: Deleting item from a list while iterating over it ... Traceback (most recent call last): File "<stdin>", line 2, in <module> IndexError: list index out of range
>>> def create_multipliers(): ... return [lambda x : i * x for i in range(5)] >>> for multiplier in create_multipliers(): ... print multiplier(2) ... You might expect the following output: 0 2 4 6 8 But you actually get: 8 8 8 8 8
# In a.py: import b def f(): return b.x print f() # And in b.py: import a x = 1 def g(): print a.f()
import sys def bar(i): if i == 1: raise KeyError(1) if i == 2: raise ValueError(2) def bad(): e = None try: bar(int(sys.argv[1])) except KeyError as e: print('key error') except ValueError as e: print('value error') print(e) bad()
UnboundLocalError: local variable 'e' referenced before assignment
import foo class Bar(object): ... def __del__(self): foo.cleanup(self.myhandle)
Posted by
Valentin
at
11:30
0
comments
2014-07-18
The Onion Movie 2008
Posted by
Valentin
at
11:30
0
comments
2014-07-17
Safe way to setup firewall
#!/bin/bash # disable_fw.sh - Reset (disable) firewall # --------------------------------------------------------------------------------------------------------------- # Initially Written by Vivek Gite <vivek@nixcraft.com> # Rewrited by Valentin Fedulov <vasnake@gmail.com> # Source: http://www.cyberciti.biz/faq/turn-on-turn-off-firewall-in-linux/ # https://gist.github.com/vasnake/de19b6162ed97b0fd92b # --------------------------------------------------------------------------------------------------------------- # You can copy / paste / redistribute this script under GPL version 2.0 or above # ============================================================= # set to true if it is CentOS / RHEL / Fedora box RHEL=false ### no need to edit below ### IPT=/sbin/iptables IPT6=/sbin/ip6tables main() { if [ "$RHEL" == "true" ]; then # reset firewall using redhat script /etc/init.d/iptables stop /etc/init.d/ip6tables stop else # for all other Linux distro use following rules to reset firewall reset_iptables ${IPT} "/proc/net/ip_tables_names" reset_iptables ${IPT6} "/proc/net/ip6_tables_names" fi } reset_iptables() { local ipt_bin="${1}" local tables="${2}" $ipt_bin -P INPUT ACCEPT $ipt_bin -P OUTPUT ACCEPT $ipt_bin -P FORWARD ACCEPT $ipt_bin -F $ipt_bin -X $ipt_bin -Z for table in $(<$tables) do $ipt_bin -t $table -F $ipt_bin -t $table -X $ipt_bin -t $table -Z done } main |
chmod +x /root/disable_fw.sh nano /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ */5 * * * * root /root/disable_fw.sh |
iptables -A INPUT -p tcp --dport ssh -j ACCEPT
watch -d "iptables -L --line-numbers -nv"
/etc/sysconfig/iptables *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -m recent --update --seconds 15 -j DROP -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -m recent --set -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT |
/etc/sysconfig/ip6tables *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p ipv6-icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -m recent --update --seconds 15 -j DROP -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -m recent --set -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT -A INPUT -j REJECT --reject-with icmp6-adm-prohibited -A FORWARD -j REJECT --reject-with icmp6-adm-prohibited COMMIT |
iptables-restore < /root/working.iptables.rules
iptables-save > /root/working.iptables.rules
chkconfig iptables off chkconfig iptables on
system-config-firewall-tui
Posted by
Valentin
at
11:30
0
comments
2014-07-16
PEP 20
http://docs.python-guide.org/en/latest/writing/style/
http://artifex.org/~hblanks/talks/2011/pep20_by_example.html
#!/usr/bin/env python | |
""" | |
===================================== | |
PEP 20 (The Zen of Python) by example | |
===================================== | |
Usage: %prog | |
:Author: Hunter Blanks, hblanks@artifex.org / hblanks@monetate.com | |
:Date: 2011-02-08 for PhillyPUG/philly.rb, revised 2011-02-10 | |
Sources: | |
- http://artifex.org/~hblanks/talks/2011/pep20_by_example.pdf | |
- http://artifex.org/~hblanks/talks/2011/pep20_by_example.html | |
- http://artifex.org/~hblanks/talks/2011/pep20_by_example.py.txt | |
Dependencies for PDF output: | |
- Pygments 1.4 | |
- pdflatex & the usual mess of latex packages | |
""" | |
from __future__ import with_statement | |
import sys | |
################################ preface ############################### | |
""" | |
"In his wisdom and in his Molisan poverty, Officer Ingravallo, | |
who seemed to live on silence... , in his wisdom, he sometimes | |
interrupted this silence and this sleep to enunciate some | |
theoretical idea (a general idea, that is) on the affairs of men, | |
and of women. At first sight, or rather, on first hearing, these | |
seemed banalities. They weren't banalities. And so, those rapid | |
declarations, which crackled on his lips like the sudden | |
illumination of a sulphur match, were revived in the ears of people | |
at a distance of hours, or of months, from their enunciation: as if | |
after a mysterious period of incubation. 'That's right!' the person | |
in question admitted, 'That's exactly what Ingravallo said to me.'" | |
- Carlo Emilio Gadda, *That Awful Mess on the Via Merulana* | |
""" | |
################################# text ################################# | |
""" | |
The Zen of Python, by Tim Peters | |
Beautiful is better than ugly. | |
Explicit is better than implicit. | |
Simple is better than complex. | |
Complex is better than complicated. | |
Flat is better than nested. | |
Sparse is better than dense. | |
Readability counts. | |
Special cases aren't special enough to break the rules. | |
Although practicality beats purity. | |
Errors should never pass silently. | |
Unless explicitly silenced. | |
In the face of ambiguity, refuse the temptation to guess. | |
There should be one-- and preferably only one --obvious way to do it. | |
Although that way may not be obvious at first unless you're Dutch. | |
Now is better than never. | |
Although never is often better than *right* now. | |
If the implementation is hard to explain, it's a bad idea. | |
If the implementation is easy to explain, it may be a good idea. | |
Namespaces are one honking great idea -- let's do more of those! | |
""" | |
################################### 1 ################################## | |
""" | |
Give me a function that takes a list of numbers and returns only the | |
even ones, divided by two. | |
""" | |
#----------------------------------------------------------------------- | |
halve_evens_only = lambda nums: map(lambda i: i/2, filter(lambda i: not i%2, nums)) | |
#----------------------------------------------------------------------- | |
def halve_evens_only(nums): | |
return [i/2 for i in nums if not i % 2] | |
#----------------------------------------------------------------------- | |
print 'Beautiful is better than ugly.' | |
################################## 2 ################################### | |
""" | |
Load the cat, dog, and mouse models so we can edit instances of them. | |
""" | |
def load(): | |
from menagerie.cat.models import * | |
from menagerie.dog.models import * | |
from menagerie.mouse.models import * | |
#----------------------------------------------------------------------- | |
def load(): | |
from menagerie.models import cat as cat_models | |
from menagerie.models import dog as dog_models | |
from menagerie.models import mouse as mouse_models | |
#----------------------------------------------------------------------- | |
print 'Explicit is better than implicit.' | |
################################### 3 ################################## | |
""" | |
Can you write out these measurements to disk? | |
""" | |
measurements = [ | |
{'weight': 392.3, 'color': 'purple', 'temperature': 33.4}, | |
{'weight': 34.0, 'color': 'green', 'temperature': -3.1}, | |
] | |
#----------------------------------------------------------------------- | |
def store(measurements): | |
import sqlalchemy | |
import sqlalchemy.types as sqltypes | |
db = sqlalchemy.create_engine('sqlite:///measurements.db') | |
db.echo = False | |
metadata = sqlalchemy.MetaData(db) | |
table = sqlalchemy.Table('measurements', metadata, | |
sqlalchemy.Column('id', sqltypes.Integer, primary_key=True), | |
sqlalchemy.Column('weight', sqltypes.Float), | |
sqlalchemy.Column('temperature', sqltypes.Float), | |
sqlalchemy.Column('color', sqltypes.String(32)), | |
) | |
table.create(checkfirst=True) | |
for measurement in measurements: | |
i = table.insert() | |
i.execute(**measurement) | |
#----------------------------------------------------------------------- | |
def store(measurements): | |
import json | |
with open('measurements.json', 'w') as f: | |
f.write(json.dumps(measurements)) | |
#----------------------------------------------------------------------- | |
print 'Simple is better than complex.' | |
################################### 4 ################################## | |
""" | |
Can you write out those same measurements to a MySQL DB? I think we're | |
gonna have some measurements with multiple colors next week, by the way. | |
""" | |
#----------------------------------------------------------------------- | |
def store(measurements): | |
import sqlalchemy | |
import sqlalchemy.types as sqltypes | |
db = create_engine( | |
'mysql://user:password@localhost/db?charset=utf8&use_unicode=1') | |
db.echo = False | |
metadata = sqlalchemy.MetaData(db) | |
table = sqlalchemy.Table('measurements', metadata, | |
sqlalchemy.Column('id', sqltypes.Integer, primary_key=True), | |
sqlalchemy.Column('weight', sqltypes.Float), | |
sqlalchemy.Column('temperature', sqltypes.Float), | |
sqlalchemy.Column('color', sqltypes.String(32)), | |
) | |
table.create(checkfirst=True) | |
for measurement in measurements: | |
i = table.insert() | |
i.execute(**measurement) | |
#----------------------------------------------------------------------- | |
def store(measurements): | |
import MySQLdb | |
db = MySQLdb.connect(user='user', passwd="password", host='localhost', db="db") | |
c = db.cursor() | |
c.execute(""" | |
CREATE TABLE IF NOT EXISTS measurements | |
id int(11) NOT NULL auto_increment, | |
weight float, | |
temperature float, | |
color varchar(32) | |
PRIMARY KEY id | |
ENGINE=InnoDB CHARSET=utf8 | |
""") | |
insert_sql = ( | |
"INSERT INTO measurements (weight, temperature, color) " | |
"VALUES (%s, %s, %s)") | |
for measurement in measurements: | |
c.execute(insert_sql, | |
(measurement['weight'], measurement['temperature'], measurement['color']) | |
) | |
#----------------------------------------------------------------------- | |
print 'Complex is better than complicated.' | |
################################### 5 ################################## | |
"""Identify this animal. """ | |
#----------------------------------------------------------------------- | |
def identify(animal): | |
if animal.is_vertebrate(): | |
noise = animal.poke() | |
if noise == 'moo': | |
return 'cow' | |
elif noise == 'woof': | |
return 'dog' | |
else: | |
if animal.is_multicellular(): | |
return 'Bug!' | |
else: | |
if animal.is_fungus(): | |
return 'Yeast' | |
else: | |
return 'Amoeba' | |
#----------------------------------------------------------------------- | |
def identify(animal): | |
if animal.is_vertebrate(): | |
return identify_vertebrate() | |
else: | |
return identify_invertebrate() | |
def identify_vertebrate(animal): | |
noise = animal.poke() | |
if noise == 'moo': | |
return 'cow' | |
elif noise == 'woof': | |
return 'dog' | |
def identify_invertebrate(animal): | |
if animal.is_multicellular(): | |
return 'Bug!' | |
else: | |
if animal.is_fungus(): | |
return 'Yeast' | |
else: | |
return 'Amoeba' | |
#----------------------------------------------------------------------- | |
print 'Flat is better than nested.' | |
################################### 6 ################################## | |
""" Parse an HTTP response object, yielding back new requests or data. """ | |
#----------------------------------------------------------------------- | |
def process(response): | |
selector = lxml.cssselect.CSSSelector('#main > div.text') | |
lx = lxml.html.fromstring(response.body) | |
title = lx.find('./head/title').text | |
links = [a.attrib['href'] for a in lx.find('./a') if 'href' in a.attrib] | |
for link in links: | |
yield Request(url=link) | |
divs = selector(lx) | |
if divs: yield Item(utils.lx_to_text(divs[0])) | |
#----------------------------------------------------------------------- | |
def process(response): | |
lx = lxml.html.fromstring(response.body) | |
title = lx.find('./head/title').text | |
links = [a.attrib['href'] for a in lx.find('./a') if 'href' in a.attrib] | |
for link in links: | |
yield Request(url=link) | |
selector = lxml.cssselect.CSSSelector('#main > div.text') | |
divs = selector(lx) | |
if divs: | |
bodytext = utils.lx_to_text(divs[0]) | |
yield Item(bodytext) | |
#----------------------------------------------------------------------- | |
print 'Sparse is better than dense.' | |
################################### 7 ################################## | |
""" Write out the tests for a factorial function. """ | |
#----------------------------------------------------------------------- | |
def factorial(n): | |
""" | |
Return the factorial of n, an exact integer >= 0. | |
>>> [factorial(n) for n in range(6)] | |
[1, 1, 2, 6, 24, 120] | |
>>> factorial(30) | |
265252859812191058636308480000000L | |
>>> factorial(-1) | |
Traceback (most recent call last): | |
... | |
ValueError: n must be >= 0 | |
""" | |
pass | |
if __name__ == '__main__' and '--test' in sys.argv: | |
import doctest | |
doctest.testmod() | |
#----------------------------------------------------------------------- | |
import unittest | |
def factorial(n): | |
pass | |
class FactorialTests(unittest.TestCase): | |
def test_ints(self): | |
self.assertEqual( | |
[factorial(n) for n in range(6)], [1, 1, 2, 6, 24, 120]) | |
def test_long(self): | |
self.assertEqual( | |
factorial(30), 265252859812191058636308480000000L) | |
def test_negative_error(self): | |
with self.assertRaises(ValueError): | |
factorial(-1) | |
if __name__ == '__main__' and '--test' in sys.argv: | |
unittest.main() | |
#----------------------------------------------------------------------- | |
print 'Readability counts.' | |
################################# 8 & 9 ################################ | |
""" | |
Write a function that returns another functions. Also, test floating point. | |
""" | |
#----------------------------------------------------------------------- | |
def make_counter(): | |
i = 0 | |
def count(): | |
""" Increments a count and returns it. """ | |
i += 1 | |
return i | |
return count | |
count = make_counter() | |
assert hasattr(count, '__name__') # No anonymous functions! | |
assert hasattr(count, '__doc__') | |
assert float('0.20000000000000007') == 1.1 - 0.9 # (this is platform dependent) | |
assert 0.2 != 1.1 - 0.9 # Not special enough to break the rules of floating pt. | |
assert float(repr(1.1 - 0.9)) == 1.1 - 0.9 | |
#----------------------------------------------------------------------- | |
def make_adder(addend): | |
return lambda i: i + addend # But lambdas, once in a while, are practical. | |
assert str(1.1 - 0.9) == '0.2' # as may be rounding off floating point errors | |
assert round(0.2, 15) == round(1.1 - 0.9, 15) | |
#----------------------------------------------------------------------- | |
print "Special cases aren't special enough to break the rules." | |
print 'Although practicality beats purity.' | |
################################ 10 & 11 ############################### | |
""" Import whatever json library is available. """ | |
try: | |
import json | |
except ImportError: | |
try: | |
import simplejson as json | |
except: | |
print 'Unable to find json module!' | |
raise | |
#----------------------------------------------------------------------- | |
print 'Errors should never pass silently' | |
print 'Unless explicitly silenced.' | |
################################## 12 ################################## | |
""" Store an HTTP request in the database. """ | |
def process(response): | |
db.store(url, response.body) | |
#----------------------------------------------------------------------- | |
def process(response): | |
charset = detect_charset(response) | |
db.store(url, response.body.decode(charset)) | |
print 'In the face of ambiguity, refuse the temptation to guess.' | |
################################## 13 ################################## | |
# Example 1 | |
assert hasattr(__builtins__, 'map') # ('map' in __builtins__) raises TypeError | |
assert not hasattr(__builtins__, 'collect') | |
# Example 2 | |
def fibonacci_generator(): | |
prior, current = 0, 1 | |
while current < 100: | |
yield prior + current | |
prior, current = current, current + prior | |
sequences = [ | |
range(20), | |
{'foo': 1, 'fie': 2}, | |
fibonacci_generator(), | |
(5, 3, 3) | |
] | |
for sequence in sequences: | |
for item in sequence: # all sequences iterate the same way | |
pass | |
#----------------------------------------------------------------------- | |
print 'There should be one, and preferably only one way to do it.' | |
print "Although that way may not be obvious at first unless you're Dutch." | |
################################## 14 ################################## | |
def obsolete_func(): | |
raise PendingDeprecationWarning | |
def deprecated_func(): | |
raise DeprecationWarning | |
print 'Now is better than never' | |
print 'Although never is often better than *right* now.' | |
################################## 15 ################################## | |
def hard(): | |
# Example 1 | |
try: | |
import twisted | |
help(twisted) # (this may not be as hard as I think, though) | |
except: | |
pass | |
# Example 2 | |
import xml.dom.minidom | |
document = xml.dom.minidom.parseString( | |
'''<menagerie><cat>Fluffers</cat><cat>Cisco</cat></menagerie>''') | |
menagerie = document.childNodes[0] | |
for node in menagerie.childNodes: | |
if node.childNodes[0].nodeValue== 'Cisco' and node.tagName == 'cat': | |
return node | |
def easy(maybe): | |
# Example 1 | |
try: | |
import gevent | |
help(gevent) | |
except: | |
pass | |
# Example 2 | |
import lxml | |
menagerie = lxml.etree.fromstring( | |
'''<menagerie><cat>Fluffers</cat><cat>Cisco</cat></menagerie>''') | |
for pet in menagerie.find('./cat'): | |
if pet.text == 'Cisco': | |
return pet | |
print "If the implementation is hard to explain, it's a bad idea." | |
print 'If the implementation is easy to explain, it may be a good idea.' | |
################################## 16 ################################## | |
def chase(): | |
import menagerie.models.cat as cat | |
import menagerie.models.dog as dog | |
dog.chase(cat) | |
cat.chase(mouse) | |
print "Namespaces are one honking great idea -- let's do more of those!" | |
############################### Readings ############################### | |
""" | |
- Peters, Tim. PEP 20, "The Zen of Python". | |
- Raymond, Eric. *The Art of Unix Programming*. | |
(http://www.catb.org/~esr/writings/taoup/) | |
- Alchin, Marty. *Pro Python*. | |
- Ramblings on | |
http://stackoverflow.com/questions/228181/the-zen-of-python | |
""" | |
############################## main block ############################## | |
from optparse import OptionParser | |
import os | |
import re | |
import subprocess | |
import sys | |
parser = OptionParser(usage=__doc__.strip()) | |
parser.add_option('-v', dest='verbose', action='store_true', | |
help='Verbose output') | |
header_pat = re.compile(r'^\\PY\{c\}\{' + (r'\\PYZsh\{\}' * 8)) | |
def yield_altered_lines(latex): | |
""" | |
Adds page breaks and page layout to our pygments file. Blah. | |
""" | |
for line in latex.splitlines(): | |
if line == r'\documentclass{article}': | |
yield line | |
yield r'\usepackage{geometry}' | |
yield r'\geometry{letterpaper,landscape,margin=0.25in}' | |
elif line == r'\begin{document}': | |
yield line | |
yield r'\large' | |
elif header_pat.search(line): | |
yield r'\end{Verbatim}' | |
yield r'\pagebreak' | |
yield r'\begin{Verbatim}[commandchars=\\\{\}]' | |
yield line | |
else: | |
yield line | |
if __name__ == '__main__': | |
options, args = parser.parse_args() | |
if options.verbose: | |
errout = sys.stderr | |
else: | |
errout = open('/tmp/pep20.log', 'w') | |
try: | |
# TODO: pygmentize in Python instead of farming it out | |
p = subprocess.Popen( | |
('pygmentize', '-f', 'latex', '-l', 'python', | |
'-O', 'full', sys.argv[0]), | |
stdout=subprocess.PIPE, stderr=errout) | |
output, err = p.communicate() | |
assert p.returncode == 0, 'pygmentize exited with %d' % p.returncode | |
p2 = subprocess.Popen( | |
('pygmentize', '-f', 'html', '-l', 'python', | |
'-O', 'full', '-o', 'pep20_by_example.html', sys.argv[0]), | |
stdout=errout, stderr=errout) | |
p2.communicate() | |
assert p2.returncode == 0, 'pygmentize exited with %d' % p2.returncode | |
except OSError, e: | |
print >> sys.stderr, 'Failed to run pygmentize: %s' % str(e) | |
except AssertionError, e: | |
print e | |
altered_output = '\n'.join(l for l in yield_altered_lines(output)) | |
try: | |
p = subprocess.Popen(('pdflatex',), | |
stdin=subprocess.PIPE, stdout=errout, stderr=errout) | |
p.communicate(altered_output) | |
assert p.returncode == 0, 'pdflatex exited with %d' % p.returncode | |
except OSError, e: | |
print >> sys.stderr, 'Failed to run pygmentize: %s' % str(e) | |
except AssertionError, e: | |
print e | |
os.rename('texput.pdf', 'pep20_by_example.pdf') | |
errout.close() |
Posted by
Valentin
at
11:30
0
comments
Labels: python
2014-07-15
Фантомы / Shutter 2008
Posted by
Valentin
at
11:30
0
comments
2014-07-14
Logging ProjectMate
Posted by
Valentin
at
11:30
0
comments
2014-07-11
Recommender Systems
Если же у фильма нет еще ни одной оценки, лучше убрать этот фильм из расчетов.
Posted by
Valentin
at
11:30
0
comments
Архив блога
-
▼
2014
(267)
-
▼
июля
(23)
- Large-Scale Machine Learning & Example
- Open Source vs Proprietary
- webcamfixer
- Import VirtualBox machine
- NLP
- Duck Jibe
- 2.5 тонны
- fb2tools
- 10 распространенных ошибок
- The Onion Movie 2008
- Safe way to setup firewall
- PEP 20
- Фантомы / Shutter 2008
- Logging ProjectMate
- Recommender Systems
- Anomaly Detection, Recommender Systems
- Автошкола-онлайн
- subprocess.Popen
- Автошкола-онлайн
- Pumping
- Водный старт в петлях
- -fno-delete-null-pointer-checks
- Еще про Автошколу-онлайн
-
▼
июля
(23)