Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

2007-07-19

File browser with tags

LJ user olpa asks himself: File browser with tags. Why it is needed?

IMHO if one uses such a browser then the functionality implemented in more specific programs a-la Windows Media Player or Picasa has become absolutely useless.

I myself started to dig in the similar direction. But first of all I'd like to files combine by tags be available in any program including the ill-stared dialog Open File.... And I do not like to implement yet another file manager.

The problem is easy solved at file system level and it seems to be all: sqlite base + fuse + console utility + nautilus plugin, it's rather easy and beautiful implemented. But damn, it will not work on Windows.

Update 23.07.2007 The modest operating system Emacs supports it for a long time.

2007-07-05

Google, google - save our soils.

There is a lot of computer dictionaries, there is a lot of programs, which easy translate a word when the mouse is moved to it, there is a lot of on-line dictionaries and translators, it's all great and wonderful.

But I'd like to have a list of everything I translate, to see the statistics in order to learn by heart the most often forgotten words after all. May be some flash cards should be formed by them, it seems there is such way of words learning already.

I need such web service, I need corresponding script for greasemonkey I need corresponding dictionary for my computer with the necessary possibility of quickly translation a word under the mouse, I need pdf dictionary integrated with a book reader... and so that all this has a common base of made requests.
Is there anything similar?

So far I've created a little script for we, using http://lingvo.yandex.ru. As least, I'll bind a database to it, it will be happiness yet.

The code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, urllib2, libxslt, libxml2
from html5lib import html5parser

# http://www.dbforums.com/archive/index.php/t-1419974.html
# by John J. Lee
class DumbProxyPasswordMgr:
def __init__(self):
self.user = self.passwd = None
def add_password(self, realm, uri, user, passwd):
self.user = user
self.passwd = passwd
def find_user_password(self, realm, authuri):
return self.user, self.passwd

def word2url(word):
return "http://lingvo.yandex.ru/en?text=%s"%word

def download(url):
proxy= urllib2.ProxyHandler({"http" : "http://proxy:8080"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(DumbProxyPasswordMgr ())
proxy_auth_handler.add_password(None, None, 'user', 'password')
opener = urllib2.build_opener(proxy,proxy_auth_handler)
urllib2.install_opener(opener)
src = urllib2.urlopen(url)
return src.read()

def html2xml(src):
p = html5parser.HTMLParser()
doc = p.parse(src,'utf-8')
return doc.toxml()


def decodeYaXML(src):
styledoc = libxml2.parseFile("decode.xslt")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseDoc(src)
result = style.applyStylesheet(doc, None)
content = style.saveResultToString(result)
style.freeStylesheet()
doc.freeDoc()
result.freeDoc()
return content

if __name__ == '__main__':
if len(sys.argv) < 2:
print "usage %s word"%sys.argv[0]
sys.exit()
word = sys.argv[1]
result = decodeYaXML(html2xml(download(word2url(word))))
print result.decode('utf-8','replace').encode('cp866','replace')



The contents of decode.xslt file is trivial everybody can write it to one's heart's content.

Monsieur being a good judge of perversion can appreciate the last row.

2007-05-08

Python behind proxy

Documentation on urllib2 is just a semblance.
So far the volume is too short, and the main thing is the working sample of how to download a file behind proxy using urllib2 in python.
Big Tnks to an unknown programmer

The code

import urllib2
# http://www.dbforums.com/archive/index.php/t-1419974.html
# by John J. Lee
class DumbProxyPasswordMgr:
def __init__(self):
self.user = self.passwd = None
def add_password(self, realm, uri, user, passwd):
self.user = user
self.passwd = passwd
def find_user_password(self, realm, authuri):
return self.user, self.passwd

url = "http://www.ya.ru"
proxy= urllib2.ProxyHandler({"http" : "http://proxy:8080"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(DumbProxyPasswordMgr ())
proxy_auth_handler.add_password(None, None, 'user', 'password')
opener = urllib2.build_opener(proxy,proxy_auth_handler)
urllib2.install_opener(opener)

src = urllib2.urlopen(url)
data = src.read()
dst = open("index.html","wb");
dst.write(data)

2007-04-28

DSL vs. Rich Native Syntax

It's interesting - there is so many different projects which authors declare as an advantage that a project does not use (strange) Domain Specific Language, but where everything is implemented in the same host language.

Sometimes it comes out more beautiful, sometimes it is not.

Tried ZestyParser.

Here is the banal parsing of a phrase "create or replace"
from ZestyParser import *

sp = Omit((RE('\s+')) + Skip((RE('#[^\n]*\n') | RE('\s+'))))
create_package = RawToken('create') + sp
+ ((RawToken('or') + sp + RawToken('replace')) | EmptyToken)
ZestyParser('create or replace').scan(create_package)

I have to dig it further, so far it comes out ugly enough.

2007-02-12

RuPyRu - Ruby & Python conference

I've visited a conference dedicated to Ruby & Python. As opposed to Microsoft speakers were developers themselves that's why they told about bugs, little discrepancies and so on:)

Reports archives are already available.

Impressions of reports:
Gusarov Mikhail "OOP principles in Python"


Hum... It seemed that the speaker desired to manage in five minutes and threw away the most interesting things, only a compressed description of what dynamical languages are was left. And all the principles of projecting were brought to the necessary automate testing and to restate of Liskov Substitution Principle.

Rastyagaev Vadim "Introduction to Ruby"


A little description of syntax — it was not interesting... in the discussion that took place later a little bit more interesting explanations of closures vs ruby-blocks and scopes of made changes were sound.

Artamonov Igor "Python and Ruby in Java-projects"

Speaker was concentrated on weak places — productivity and correspondence of java-clones and native implementations in ruby and python. As a result everybody was deeply disappointed...
About the speed he told for nothing... everybody was scared of jruby productivity failure, but by the was jyton looks worthy enough.
But for the scripting in java projects I would choose Scheme or Groovy

Tarancov Andrew "Dynamic Languages Toolkit"

The project is interesting, promises a lot, written in Java. Wish them good luck. Looks like their goal is equivalent to creation own language interpreter, as it actually happened with Eclipse JDT. May be, in this way a good implementation of python and ruby in java will appear :)

Okoneshnikov Andrew "Python in business applications"

It were our honorable guests — guys use python since 2002 year in business applications and they are satisfied with their choice... a drop of optimism :)
By the way, this is not the only business system, which uses python. This is at least one more

Baburov Yuri "Python in C/C++ and C/C++ in Python"

A good review of everything. Choice to any taste.

Lyfar Dmitry "Boost.Python, Python in GameDev"

One more good report. What, how and why. But may be one should use Lua?

Coffee-break

That everybody looked forward — it justified the expectancies :) However I would like it happen in one or two reports earlier.

Nemytchenko Ivan "Introduction to Ruby-on-Rails"
A short retelling of standard tutorials. But those who followed carefully, obviously would like familiarize themselves with rails closer.

Yurevich Yuri "Python for Web"

Django, Twisted, Pylons — choose any. H'm, does Django _really_ so fast?? I have to try it. But I still have no doubts that for a very custom project Pylons is the right choice.

Yurevich Yuri "Using Unicode in Python"

Oh... why I have not thought about sity.py, since I have difficulties — just forget u in the line beginning and get errors :) I'm kidding — the report is absolutely correct.


In the end surprising news were sound - Iss Art is going to hold such conferences once in a three months. The next conference will be dedicated to the «extreme programming». Looking forward to it.

Update:
Photos are on flikr
With the «official» report and participants opinions are here

2007-01-31

Links: multiple inheritance

An interesting comparison of different approaches from CLOS, Dylan and Python(C3)
A Monotonic Superclass Linearization for Dylan

2006-06-19

Python-inotify on Ubuntu

By hook or by crook I've stuck into my system the package python-inotify.

There is a library in Python for building rmp packages. What's a pity that it's not for deb.

I've come across a strange roughness, fixed it by explicitly setting the folder with h files.

file: setup.py

module1 = Extension(
'_inotify',
sources = ['inotifymodule.c'],
include_dirs = ['folder where source has been unpacked'])

python setup.py bdist_rpm
fakeroot alien python-inotify-0.1.0-1.i386.rpm


I have to see if it is possible to build python libraries in Scons.

2006-06-06

PLSQL Procedure Call in Python

I had some trouble. In cx_Oracle documentation there is nothing. Ours is here.

How to call PL/SQL function returning an array in python?

result = cursor.callfunc('getFoo', [cx_Oracle.STRING, 20])

2006-05-30

Python: The encodings

To make the picture complete in python batch files the encoding should be set explicitly

@python2.4 -x %0 & GOTO EXIT
# -*- coding: cp1251 -*-

print("Превед!!!");

"""
:EXIT
@rem """


Ha... if I would live on Linux only I'd never know about it. UTF-8, after all.

2006-05-24

Python2Bat

I've come across a looong discussion of the problem of embedding python code to batch files. To click and run.

The discussion is long, solutions are bulky.

There was no need to make all that fuss:
python2.4 -x %0 & GOTO EXIT

print("Hello, from python!");

"""
:EXIT
@rem """