Projekat

Općenito

Profil

Akcije

Podrška #21924

Zatvoren

python za talašike

Dodano od Ernad Husremović prije oko 14 godina. Izmjenjeno prije oko 13 godina.

Status:
Zatvoreno
Prioritet:
Normalan
Odgovorna osoba:
Početak:
23.03.2011
Završetak:
% završeno:

100%

Procjena vremena:
(Ukupno: 0.00 h)

Podzadaci 1 (0 otvoreno1 zatvoren)

Podrška #22893: python eggsOdbačenoErnad Husremović23.03.2011

Akcije
Akcije #1

Izmjenjeno od Ernad Husremović prije oko 14 godina

s obzirom da python godinama nisam ni pogledao, ovdje ću bilježiti nepoznate stvari na koje naletim u openerp kodu.

koristim sljedeće resurse:
  • quick python book second edition, vernon L. Ceder, manning publications
  • internet
Akcije #2

Izmjenjeno od Ernad Husremović prije oko 14 godina

lambda expressions

base_vat

def mult_add(i, j):
    """Sum each digits of the multiplication of i and j.""" 
    return reduce(lambda x, y: x + int(y), str(i*j), 0
Akcije #3

Izmjenjeno od Ernad Husremović prije oko 14 godina

evo nešto jednostavnije

>>> l1 = lambda deg_f: 273.15 + (deg_f - 32) * 5/9
>>> l1(32)
273.14999999999998

oni koji znaju clipper, lambda je najsličniji kodnom bloku.

Akcije #4

Izmjenjeno od Ernad Husremović prije oko 14 godina

reduce(function, iterable[, initializer])

    Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.

>>> l2 = lambda x, y: x+y
>>> l2(1,2)
3
>>> reduce(l2, [1,2,3,4,5])
15

aha kontam

prvo je bilo 1+2 = 3, pa je onda lambdi prosljeđeno to 3 + sljedeći argument 3 = 6, pa 6 + 4 = 10, pa 10 + 5 = 15

kontam cool stvar

Akcije #5

Izmjenjeno od Ernad Husremović prije skoro 14 godina

utf-8 encoding

Akcije #6

Izmjenjeno od Ernad Husremović prije skoro 14 godina

http://evanjones.ca/python-utf8.html

čitaj

>>> import codecs
>>> fileObj = codecs.open("test.txt", "r", "utf-8")
>>> u = fileObj.read()
>>> print u
Ernad Husremović

piši

>>> outFile = file( "test_2.txt", "w")
>>> outFile.write( codecs.BOM_UTF8)
>>> unicodeString=u'Ernad Husremovićć ŠŽĐ'
>>> outFile.write( unicodeString.encode( "utf-8" ))
>>> outFile.close()
>>> import os
>>> os.system("cat test_2.txt")
Ernad Husremovićć ŠŽĐ0
Akcije #7

Izmjenjeno od Ernad Husremović prije skoro 14 godina

unicode vs str

>>> str
'ernad husremovi\xc4\x87'
>>> u_str
u'ernad husremovi\u0107'
>>> str
'ernad husremovi\xc4\x87'
>>> u_str.__class__.__name__
'unicode'
>>> str.__class__.__name__
'str'
Akcije #8

Izmjenjeno od Ernad Husremović prije skoro 14 godina

>>> u_str
u'ernad husremovi\u0107'
>>> binascii.a2b_uu(binascii.b2a_uu(u_str.encode('utf-8'))).decode('utf-8')
u'ernad husremovi\u0107'
Akcije #9

Izmjenjeno od Ernad Husremović prije skoro 14 godina

encoding decoding

http://docs.python.org/library/codecs.html

Unicode strings are stored internally as sequences of codepoints (to be precise as Py_UNICODE arrays). Depending on the way Python is compiled (either via --enable-unicode=ucs2 or --enable-unicode=ucs4, with the former being the default) Py_UNICODE is either a 16-bit or 32-bit data type. Once a Unicode object is used outside of CPU and memory, CPU endianness and how these arrays are stored as bytes become an issue. Transforming a unicode object into a sequence of bytes is called encoding and recreating the unicode object from the sequence of bytes is known as decoding. There are many different methods for how this transformation can be done (these methods are also called encodings). The simplest method is to map the codepoints 0-255 to the bytes 0x0-0xff. This means that a unicode object that contains codepoints above U+00FF can’t be encoded with this method (which is called 'latin-1' or 'iso-8859-1'). unicode.encode() will raise a UnicodeEncodeError that looks like this: UnicodeEncodeError: 'latin-1' codec can't encode character u'\u1234' in position 3: ordinal not in range(256).

Akcije #10

Izmjenjeno od Ernad Husremović prije skoro 14 godina

>>> f=open('test.txt', 'rb')
>>> str=f.read()
>>> str
'\xc4\x8c\xc4\x87\n'
>>> type(str)
<type 'str'>
>>> str.decode('utf-8')
u'\u010c\u0107\n'
>>> print str.decode('utf-8')
Čć
Akcije #11

Izmjenjeno od Ernad Husremović prije skoro 14 godina

http://stackoverflow.com/questions/2611205/how-do-i-write-raw-binary-data-in-python

If you need a 1:1 mapping from arbitrary bytes to unicode:

Pass data.decode('latin1') to the codec. latin1 maps bytes 0-255 to unicode characters 0-255, which is kinda elegant.

The codec will, of course, encode your characters - 128-255 are encoded as 2 or 3 bytes in UTF-8 (surprisingly, the average overhead is 50%, more than base64!). This quite kills the "elegance" of having a 1:1 mapping.

Note also that unicode characters 0-255 include nasty invisible/control characters (newline, formfeed, soft hyphen, etc.) making your binary data annoying to view in text editors.

Considering these drawbacks, I do not recommend latin1 unless you understand exactly why you want it.
I'm just mentioning it as the other "natural" encoding that springs to mind.

Akcije #12

Izmjenjeno od Ernad Husremović prije skoro 14 godina

h2.sys.path

>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.6/dist-packages/psycopg2-2.3.0_beta1-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/reportlab-2.5-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/pydot-1.0.3-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pyparsing-1.5.5-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/pytz-2010o-py2.6.egg', '/usr/lib/python2.6/dist-packages/PIL', '/usr/local/lib/python2.6/dist-packages/vobject-0.8.1c-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/python_dateutil-1.5-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/lxml-2.3beta1-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/PyYAML-3.09-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/openerp_web-6.0.0_rc1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/FormEncode-1.2.3dev-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Babel-0.9.5-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/CherryPy-3.1.2-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/xlwt-0.7.2-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/numpy-1.5.1-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/matplotlib-1.0.0-py2.6-linux-x86_64.egg', '/usr/local/lib/python2.6/dist-packages/openerp_client-6.0.0_rc1_1-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']
Akcije #14

Izmjenjeno od Ernad Husremović prije više od 13 godina

  • Projekat promijenjeno iz 66 u 71
  • Status promijenjeno iz Dodijeljeno u Zatvoreno
Akcije #15

Izmjenjeno od Ernad Husremović prije oko 13 godina

  • Projekat promijenjeno iz 71 u python
Akcije

Također dostupno kao Atom PDF