snippets / python

All snippets tagged python (14)

  1. Changer du texte dans des fichiers recursivement

    un script en python pour remplacer du texte recursivement dans des fichiers.

     1 import os
    2 liste = os.listdir('c:/repertoire')
    3 for fichiers in liste :
    4 if os.path.isfile(fichiers):
    5 fichier=open(fichiers, "r")
    6 contenu=fichier.read()
    7 contenu = contenu.replace('\n<\?','<?')
    8 contenu = contenu.replace('\n\?>','?>')
    9 else:
    10 liste.append(os.listdir(fichiers))
  2. Cache any function (with its arguments)

    original snippet from http://www.djangosnippets.org/snippets/109/

     1 import cPickle as pickle
    2 import md5
    3
    4 def cache_function(length):
    5 """
    6 A variant of the snippet posted by Jeff Wheeler at
    7 http://www.djangosnippets.org/snippets/109/
    8
    9 Caches a function, using the function and its arguments as the key, and the return
    10 value as the value saved. It passes all arguments on to the function, as
    11 it should.
    12
    13 The decorator itself takes a length argument, which is the number of
    14 seconds the cache will keep the result around.
    15
    16 It will put in a MethodNotFinishedError in the cache while the function is
    17 processing. This should not matter in most cases, but if the app is using
    18 threads, you won't be able to get the previous value, and will need to
    19 wait until the function finishes. If this is not desired behavior, you can
    20 remove the first two lines after the ``else``.
    21 """
    22 def decorator(func):
    23 def inner_func(*args, **kwargs):
    24 from django.core.cache import cache
    25
    26 raw = [func.__name__, func.__module__, args, kwargs]
    27 pickled = pickle.dumps(raw, protocol=pickle.HIGHEST_PROTOCOL)
    28 key = md5.new(pickled).hexdigest()
    29 value = cache.get(key)
    30 if cache.has_key(key):
    31 return value
    32 else:
    33 # This will set a temporary value while ``func`` is being
    34 # processed. When using threads, this is vital, as otherwise
    35 # the function can be called several times before it finishes
    36 # and is put into the cache.
    37 class MethodNotFinishedError(Exception): pass
    38 cache.set(key, MethodNotFinishedError(
    39 'The function %s has not finished processing yet. This \
    40 value will be replaced when it finishes.' % (func.__name__)
    41 ), length)
    42 result = func(*args, **kwargs)
    43 cache.set(key, result, length)
    44 return result
    45 return inner_func
    46 return decorator
    Posted by benoitc to python cache django ... saved by 3 persons ... 0 comments ... 1 year
  3. Generate random password with given length from given chars.

    Generate random password with given length from given chars.

    1 def get_random_password(self, length=6, chars='1234567890'):
    2 """
    3 Generate random password
    4 length - length of the password.
    5 chars - allowed chars
    6 """
    7 return ''.join([random.choice(chars) for i in range(length)])
    Posted by prudis to python python password ... saved by 2 persons ... 0 comments ... 1 year, 1 month
  4. script en root

    permet d'executer le script en root - utilise sudo -

     1 #!/usr/bin/python
    2 # This script is under WTFPL - Do What The Fuck You Want To Public License
    3 # http://sam.zoy.org/wtfpl/
    4
    5 import os
    6 import sys
    7
    8 if os.geteuid()==0:
    9 print 'je suis dieu'
    10 else:
    11 print 'je suis un manant qui veut faire des choses'
    12 os.system('sudo "%s"' % sys.argv[0])
    13 print 'manant je suis, manant je meurs'
    Posted by BugMeNot to python python root ... saved by 1 person ... 0 comments ... 1 year, 1 month
  5. A function to enumerate the items of a set product.

    A function to enumerate the items of a set product. A recursive version is also provided for completeness, although it is much slower.

     1 """A function to enumerate the items of a set product.
    2 """
    3 __docformat__ = 'reStructuredText'
    4
    5
    6 def enumerate_set_product(*args):
    7 """Iterate over all elements in the cartesian product of its arguments.
    8 Each argument to this variadic function *must* be a sequence.
    9
    10 Examples::
    11 >>> list(enumerate_set_product())
    12 [[]]
    13 >>> list(enumerate_set_product([1]))
    14 [[1]]
    15 >>> list(enumerate_set_product([1],[1]))
    16 [[1, 1]]
    17 >>> list(enumerate_set_product([1,2],[1]))
    18 [[1, 1], [2, 1]]
    19 >>> list(enumerate_set_product([1,2],[1,2]))
    20 [[1, 1], [2, 1], [1, 2], [2, 2]]
    21 """
    22
    23 # `assert` guard against invocation with wrong arguments.
    24 def __all_items_are_sequences(s):
    25 from operator import and_, isSequenceType
    26 return reduce(and_,
    27 [ isSequenceType(item) for item in s],
    28 True)
    29 assert __all_items_are_sequences(args), \
    30 "All arguments to `enumerate_set_products` must be sequences."
    31
    32 if len(args) == 0:
    33 yield []
    34 else:
    35 L = len(args)
    36 M = [ len(s)-1 for s in args ]
    37 m = [0] * L
    38 i = 0
    39 while i < L:
    40 # return element corresponding to current multi-index
    41 yield [ s[m[i]] for (i,s) in enumerate(args) ]
    42 # advance multi-index
    43 i = 0
    44 while (i < L):
    45 if m[i] == M[i]:
    46 m[i] = 0
    47 i += 1
    48 else:
    49 m[i] += 1
    50 break
    51
    52
    53 def recursively_enumerate_set_product(*args):
    54 """Iterate over all elements in the cartesian product of its arguments.
    55 Each argument to this variadic function *must* be a sequence.
    56
    57 You would probably prefer `enumerate_set_product`, which is faster.
    58
    59 Examples::
    60 >>> list(recursively_enumerate_set_product())
    61 [[]]
    62 >>> list(recursively_enumerate_set_product([1]))
    63 [[1]]
    64 >>> list(recursively_enumerate_set_product([1],[1]))
    65 [[1, 1]]
    66 >>> list(recursively_enumerate_set_product([1,2],[1]))
    67 [[1, 1], [2, 1]]
    68 >>> list(recursively_enumerate_set_product([1,2],[1,2]))
    69 [[1, 1], [2, 1], [1, 2], [2, 2]]
    70 """
    71
    72 # `assert` guard against invocation with wrong arguments.
    73 def __all_items_are_sequences(s):
    74 from operator import and_, isSequenceType
    75 return reduce(and_,
    76 [ isSequenceType(item) for item in s],
    77 True)
    78 assert __all_items_are_sequences(args), \
    79 "All arguments to `enumerate_set_products` must be sequences."
    80
    81 if len(args) == 0:
    82 yield []
    83 else:
    84 for i in args[-1]:
    85 for js in recursively_enumerate_set_product(*args[:-1]):
    86 yield js+[i]
    87
    88
    89 ## main: run tests
    90
    91 if "__main__" == __name__:
    92 import doctest
    93 doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
    Posted by rmurri to python python mathematics algorithms ... saved by 1 person ... 0 comments ... 1 year, 1 month
  6. Various algorithms for computing the sign of a permutation.

    This is a Python version of the code (and associated commentary) from http://perplexus.info/show.php?pid=4895&op=sol You can run the source file through PyLit (http://pylit.berlios.de/) to get a nicely-formatted reStructuredText file.

      1 #! /usr/bin/env python
    2 #
    3 """Various algorithms for computing the sign of a permutation.
    4
    5 Ported from http://perplexus.info/show.php?pid=4895&op=sol
    6 """
    7 __docformat__ = 'reStructuredText'
    8
    9
    10 ## perm_sign by John Burkardt is an example that can be found online
    11 ## (see it among the collection at
    12 ## http://orion.math.iastate.edu/burkardt/f_src/subset/subset.f90 or
    13 ## see http://www.scs.fsu.edu/~burkardt/math2071/perm_sign.m). A
    14 ## similar method, that, however, avoids explicit searching, is
    15 ## illustrated by the following function::
    16
    17 def jbsign(p):
    18 """Compute sign of permutation `p` by counting the number of
    19 interchanges required to change the given permutation into the
    20 identity one.
    21
    22 Examples::
    23 >>> jbsign([0,1,2])
    24 1
    25 >>> jbsign([0,2,1])
    26 -1
    27 """
    28 n = len(p)
    29 s = +1
    30 # get elements back in their home positions
    31 for j in xrange(0,n):
    32 q=p[j]
    33 if q !=j:
    34 p[j],p[q] = p[q],q # interchange p[j] and p[p[j]]
    35 s = -s # and account for the interchange
    36 # note that q is now in its home position
    37 # whether or not an interchange was required
    38 return s
    39
    40
    41 ## A somewhat more sophisticated method follows the orbits of elements
    42 ## as they are permuted through cycles::
    43
    44 def sign_by_orbits(p):
    45 """Compute sign of permutation `p` by following orbit cycles.
    46
    47 Examples::
    48 >>> sign_by_orbits([0,1,2])
    49 1
    50 >>> sign_by_orbits([0,2,1])
    51 -1
    52 """
    53 n = len(p)
    54 s = +1
    55 # follow orbit cycles and tote up signs
    56 for j in xrange(0,n):
    57 if (p[j] >= 0):
    58 # new cycle, follow it, marking each place as "visited"
    59 q = j
    60 t = +1
    61 while (q >= 0):
    62 r = p[q]
    63 if (r >= 0):
    64 p[q] = -1
    65 t = -t
    66 q = r
    67 # factor in contribution of each cycle
    68 s *= t
    69 return s
    70
    71
    72 ## Lang's Algebra, First Ed. 1965, p. 53, gives an equivalent
    73 ## definition of the sign as simply `(-1)^m` where m=n-number of
    74 ## orbits. Thus the above can be modified to just count the number of
    75 ## orbits and do a little arithmetic at the end, as the following
    76 ## function illustrates::
    77
    78 def sign_lang_formula(p):
    79 """Compute sign of permutation `p` as `(-1)^{length of permutation
    80 - number of orbits}`.
    81
    82 Examples::
    83 >>> sign_lang_formula([0,1,2])
    84 1
    85 >>> sign_lang_formula([0,2,1])
    86 -1
    87 """
    88 n = len(p)
    89 c = 0
    90 # count the orbit cycles
    91 for j in xrange(0,n):
    92 if (p[j] >= 0):
    93 # new cycle, follow it, marking each place
    94 q = j
    95 while (q >= 0):
    96 r = p[q]
    97 if (r >= 0):
    98 p[q] = -1
    99 q = r
    100 # increment cycle count
    101 c += 1
    102 if (n-c) % 2 == 0:
    103 return +1
    104 else:
    105 return -1
    106
    107
    108 ## Another method counts inversions and leads to the following short
    109 ## program::
    110
    111 def sign_counting_inversions(p):
    112 """Compute sign of permutation `p` by counting the number of
    113 interchanges required to change the given permutation into the
    114 identity one.
    115
    116 Examples::
    117 >>> sign_counting_inversions([0,1,2])
    118 1
    119 >>> sign_counting_inversions([0,2,1])
    120 -1
    121 """
    122 n = len(p)
    123 v = 0
    124 # count inversions
    125 for i in xrange(1,n):
    126 for j in xrange(i+1, n):
    127 if (p[i] > p[j]):
    128 v += 1
    129 if (v % 2 == 0):
    130 return +1
    131 else:
    132 return -1
    133
    134
    135 ## And one more: Those with a sufficient background in algebra will
    136 ## realize that a simple algorithm exists based on the Laplace
    137 ## expansion of the determinant of a permutation matrix. However,
    138 ## while rather similar to the algorithm above that counts inversions,
    139 ## it turned out to be a bit more complicated to program::
    140
    141 def lpsign(p):
    142 """Compute sign of permutation `p` via Laplace expansion of the
    143 determinant of the permutation matrix.
    144
    145 Examples::
    146 >>> lpsign([0,1,2])
    147 1
    148 >>> lpsign([0,2,1])
    149 -1
    150 """
    151 # Laplace expansion of determinant of permutation
    152 # matrix with a 1 in in row p[j] of column j
    153 n = len(p)
    154 s = 1
    155 for j in xrange(1, n):
    156 q=p[j]
    157 # apply checkerboard sign
    158 if (q % 2 == 0):
    159 s = -s
    160 for k in xrange(j+1, n):
    161 r = p[k]
    162 # adjust numbers of remaining rows beyond row p[j]
    163 # to account for having deleted row p[j]
    164 if (r > q):
    165 p[k] = r-1
    166 return s
    167
    168
    169 def sign_recursive(p):
    170 """Recursively compute the sign of permutation `p`.
    171
    172 A permutation is represented as a linear list: `p` maps
    173 `i` to `p[i]`.
    174
    175 Examples:
    176 >>> sign_recursive([0,1,2])
    177 1
    178 >>> sign_recursive([0,2,1])
    179 -1
    180 """
    181 n = len(p)
    182 if 1 >= n:
    183 return 1
    184 # find highest-numbered element
    185 k = p.index(n-1)
    186 # remove highest-numbered element for recursion
    187 del p[k]
    188 # recursively compute
    189 if 0 == ((n+k) % 2):
    190 s = -1
    191 else:
    192 s = 1
    193 return s * sign_recursive(p)
    194
    195
    196 ## So, based on simplicity, counting inversions appears to have the
    197 ## edge over all the other algorithms discussed above. However, based
    198 ## on speed, efficiently returning elements to their homes, and
    199 ## counting orbits both seem to be quite fast.
    200 ##
    201 ## Indeed, running 100000 iterations of each function to compute the
    202 ## sign of a 5-element permutation, gives::
    203 ##
    204 ## | $ python2.4 profile.py ./permsign.py
    205 ## | ncalls tottime percall cumtime percall function
    206 ## | [...]
    207 ## | 100002 1.950 0.000 2.480 0.000 jbsign
    208 ## | 100002 2.550 0.000 3.140 0.000 sign_counting_inversions
    209 ## | 100002 2.810 0.000 3.350 0.000 sign_lang_formula
    210 ## | 100002 2.820 0.000 3.510 0.000 sign_by_orbits
    211 ## | 100002 3.470 0.000 4.190 0.000 lpsign
    212 ## | 100002 11.450 0.000 16.890 0.000 sign_recursive
    213 ## | [...]
    214 ##
    215
    216
    217
    218 ## main: run tests
    219
    220 if "__main__" == __name__:
    221 import doctest
    222 doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)
  7. Afficher la somme des tailles de plusieurs fichiers

    Ce script fonctionne comme la commande UNIX "du", mais pour les fichiers : il calcule la taille totale des fichiers donnés et l'affiche en o, Ko, Mo et Go.

     1 import os
    2 import sys
    3
    4 if len(sys.argv) == 1:
    5 print "Usage : %s files"%(sys.argv[0])
    6 sys.exit(0)
    7
    8 sizes_sum = 0.0
    9 for file in sys.argv[1:]:
    10 if not os.path.isfile(file):
    11 print "%s is not a regular file"%(file)
    12 sys.exit(1)
    13 sizes_sum = sizes_sum + os.path.getsize(file)
    14
    15 print "%d o"%(int(sizes_sum))
    16 print "%0.02f Ko"%(sizes_sum/1024)
    17 print "%0.02f Mo"%(sizes_sum/1024/1024)
    18 print "%0.02f Go"%(sizes_sum/1024/1024/1024)
    Posted by zebra3 to python taille ... saved by 3 persons ... 0 comments ... 1 year, 2 months
  8. Envoyer un mail html avec python

    Comment envoyer un mail au format html en python. Pratique pour de gros envois de mail.

     1 import smtplib
    2 import MimeWriter
    3 import mimetools
    4 import StringIO
    5
    6 def sendHtmlMail(to,frommail,text,html):
    7 encoding = "base64"
    8 charset = "iso-8859-15"
    9
    10 #déclaration des buffers
    11 out = StringIO.StringIO()
    12 htmlin = StringIO.StringIO(html)
    13 txtin = StringIO.StringIO(html)
    14
    15 #déclaration et initialisation du writer
    16 writer = MimeWriter.MimeWriter(out)
    17 writer.addheader("Subject", text)
    18 writer.addheader("MIME-Version", "1.0")
    19 writer.addheader("From", frommail)
    20 writer.addheader("To", to)
    21 writer.startmultipartbody("alternative")
    22 writer.flushheaders()
    23
    24 #ajout de la partie text
    25 textPart = writer.nextpart()
    26 textPart.addheader("Content-Transfer-Encoding", encoding)
    27 pout = textPart.startbody("text/plain", [("charset", charset)])
    28 mimetools.encode(txtin, pout, encoding)
    29 txtin.close()
    30
    31 #On ajoute la partie html
    32 htmlPart = writer.nextpart()
    33 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    34 pout = htmlPart.startbody("text/html", [("charset", charset)])
    35 mimetools.encode(htmlin, pout, encoding)
    36 htmlin.close()
    37
    38 #on clot le mail
    39 writer.lastpart()
    40 mail = out.getvalue()
    41 out.close()
    42 smtp = smtplib.SMTP("localhost")
    43 #smtp.connect()
    44 smtp.sendmail(frommail, [to], mail)
    45 smtp.close()
    Posted by Fufu to python python mail ... saved by 7 persons ... 0 comments ... 1 year, 2 months
  9. Création d'un mail html en python

    Cette classe permet d'envoyer un mail au format HTML en python. Très pratique pour de l'encoi de mail en masse.

     1 CLASSE MAIL.PY
    2
    3 import smtplib
    4 import MimeWriter
    5 import mimetools
    6 import StringIO
    7
    8 def sendHtmlMail(to,frommail,text,html):
    9 encoding = "base64"
    10 charset = "iso-8859-15"
    11
    12 #déclaration des buffers
    13 out = StringIO.StringIO()
    14 htmlin = StringIO.StringIO(html)
    15 txtin = StringIO.StringIO(html)
    16
    17 #déclaration et initialisation du writer
    18 writer = MimeWriter.MimeWriter(out)
    19 writer.addheader("Subject", text)
    20 writer.addheader("MIME-Version", "1.0")
    21 writer.addheader("From", frommail)
    22 writer.addheader("To", to)
    23 writer.startmultipartbody("alternative")
    24 writer.flushheaders()
    25
    26 #ajout de la partie text
    27 textPart = writer.nextpart()
    28 textPart.addheader("Content-Transfer-Encoding", encoding)
    29 pout = textPart.startbody("text/plain", [("charset", charset)])
    30 mimetools.encode(txtin, pout, encoding)
    31 txtin.close()
    32
    33 #On ajoute la partie html
    34 htmlPart = writer.nextpart()
    35 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    36 pout = htmlPart.startbody("text/html", [("charset", charset)])
    37 mimetools.encode(htmlin, pout, encoding)
    38 htmlin.close()
    39
    40 #on clot le mail
    41 writer.lastpart()
    42 mail = out.getvalue()
    43 out.close()
    44 smtp = smtplib.SMTP("localhost")
    45 #smtp.connect()
    46 smtp.sendmail(frommail, [to], mail)
    47 smtp.close()
    Posted by Fufu to python python mail html ... saved by 2 persons ... 0 comments ... 1 year, 2 months
  10. unicode2ascii() : conversion tout Unicode vers ASCII

    Convertit n'importe quelle chaîne de caractères Unicode en chaîne de caractère ASCII : supprime les accents et remplace les lettres spéciales par des caractères ASCII semblables.

      1 #!/usr/bin/python
    2 # -*- coding: utf-8 -*-
    3
    4 # This source code is distributed under GNU GPL v2 license
    5 # written by Victor Stinner <victor.stinner AT haypocalc.com>
    6 # http://www.haypocalc.com/
    7 # creatied: 2006-08-14 -- last change: 2007-08-17
    8
    9 # Convert any unicode string to ASCII string:
    10 # - Remove diacriticals
    11 # - Replace special letter with similar ASCII character (similar glyph)
    12 #
    13 # Support greek, cyrillic, some latin letters and some signs.
    14
    15 from unicodedata import normalize
    16
    17 UNICODE_TO_ASCII = {
    18 # Latin letters
    19 u"Æ": u"AE", # U+00C6 (latin capital ligature ae)
    20 u"Ø": u"O", # U+00D8 (latin capital letter o with stroke)
    21 u"ß": u"ss", # U+00DF (latin small letter sharp s)
    22 u"æ": u"ae", # U+00E6 (latin small ligature ae)
    23 u"ø": u"o", # U+00F8 (latin small letter o with stroke)
    24 u"?": u"l", # U+0142 (latin small letter l with stroke)
    25 u"Œ": u"OE", # U+0152 (latin capital ligature oe)
    26 u"œ": u"oe", # U+0153 (latin small ligature oe)
    27
    28 # Various signs
    29 u"¡": u"!", # U+00A1 (inverted exclamation mark)
    30 u"©": u"(c)", # U+00A9 (copyright sign)
    31 u"«": u'"', # U+00AB (left-pointing double angle quotation mark)
    32 u"®": u"(r)", # U+00AE (registred sign)
    33 u"²": u"2", # U+00B2 (superscript two)
    34 u"»": u'"', # U+00BB (right-pointing double angle quotation mark)
    35 u"?": u"/", # U+2044 (fraction slash)
    36
    37 # Greek
    38 u"?": u"A", # U+0391 (capital alpha)
    39 u"?": u"B", # U+0392 (capital beta)
    40 u"?": u"E", # U+0395 (capital epsilon)
    41 u"?": u"Z", # U+0396 (capital zeta)
    42 u"?": u"H", # U+0397 (capital eta)
    43 u"?": u"O", # U+0398 (captial theta)
    44 u"?": u"I", # U+0399 (capital iota)
    45 u"?": u"K", # U+039A (capital kappa)
    46 u"?": u"M", # U+039C (capital mu)
    47 u"?": u"N", # U+039D (capital nu)
    48 u"?": u"O", # U+039F (capital omicron)
    49 u"?": u"P", # U+03A1 (capital rho)
    50 u"?": u"T", # U+03A4 (capital tau)
    51 u"?": u"Y", # U+03A5 (capital upsilon)
    52 u"?": u"X", # U+03A7 (capital chi)
    53 u"?": u"a", # U+03B1 (small alpha)
    54 u"?": u"b", # U+03B2 (small beta)
    55 u"?": u"y", # U+03B2 (small gamma)
    56 u"?": u"e", # U+03B5 (small espilon)
    57 u"?": u"n", # U+03B7 (small eta)
    58 u"?": u"o", # U+03BF (small omicron)
    59 u"?": u"p", # U+03C1 (small rho)
    60 u"?": u"v", # U+03C1 (small upsilon)
    61
    62 # Cyrillic
    63 u"?": u"I", # U+0406 (capital byelorussian-ukrainian i)
    64 u"?": u"J", # U+0408 (capital je)
    65 u"?": u"B", # U+0412 (capital ve)
    66 u"?": u"E", # U+0415 (capital ie)
    67 u"?": u"N", # U+0418 (capital i)
    68 u"?": u"3", # U+0417 (capital ze)
    69 u"?": u"K", # U+041A (capital ka)
    70 u"?": u"M", # U+041C (capital em)
    71 u"?": u"H", # U+041D (capital en)
    72 u"?": u"O", # U+041E (capital o)
    73 u"?": u"P", # U+0420 (capital er)
    74 u"?": u"C", # U+0421 (capital es)
    75 u"?": u"T", # U+0422 (capital te)
    76 u"?": u"Y", # U+0423 (capital u)
    77 u"?": u"X", # U+0425 (capital ha)
    78 u"?": u"R", # U+042F (capital ya)
    79 u"?": u"a", # U+0430 (small a)
    80 u"?": u"b", # U+0432 (small ve)
    81 u"?": u"e", # U+0435 (small ie)
    82 u"?": u"3", # U+0437 (small ze)
    83 u"?": u"k", # U+043A (small ka)
    84 u"?": u"m", # U+043C (small em)
    85 u"?": u"h", # U+043D (small en)
    86 u"?": u"o", # U+043E (small o)
    87 u"?": u"p", # U+0440 (small er)
    88 u"?": u"c", # U+0441 (small es)
    89 u"?": u"T", # U+0442 (small te)
    90 u"?": u"y", # U+0443 (small u)
    91 u"?": u"x", # U+0445 (small ha)
    92 u"?": u"R", # U+044F (small ya)
    93 u"?": u"i", # U+0456 (small byelorussian-ukrainian i)
    94 u"?": u"j", # U+0458 (small je)
    95 }
    96
    97 def unicode2ascii(text, replace=False):
    98 """
    99 Convert an unicode string (type 'unicode') to ascii string (type 'str').
    100 Try to keep same visual result.
    101
    102 You can specify an ASCII character to replace non-ASCII character
    103 in 'replace' argument (eg. replace='?').
    104
    105 >>> unicode2ascii(unicode("¡ Hé hø « español » ! Pythøn", "UTF-8"))
    106 '! He ho " espanol " ! Python'
    107 >>> unicode2ascii(unicode("L'œuf de læticia", "UTF-8"))
    108 "L'oeuf de laeticia"
    109 >>> unicode2ascii(unicode("????????????????????????????", "UTF-8"), u'?')
    110 'IEOAB??EZHOIK?NM?OYanay?e?n?'
    111 >>> unicode2ascii(unicode("??????????????????????????????????????????", "UTF-8"), u'?')
    112 'EE??IIJKN?BE3NKMHOPCTYXabe3mho?pcTyxeeiijk'
    113 """
    114 assert isinstance(text, unicode)
    115 if replace:
    116 if isinstance(replace, str):
    117 replace = unicode(replace, "latin-1")
    118 if not isinstance(replace, unicode) \
    119 or len(replace) != 1 \
    120 or not (32 <= ord(replace) <= 127):
    121 raise ValueError(
    122 "invalid replace character (%r): "
    123 "need one ascii printable character" % replace)
    124
    125 ascii = []
    126 for char in text:
    127 # Remove diacriticals
    128 char = normalize("NFKD", char)[0]
    129
    130 # Known values
    131 if char in UNICODE_TO_ASCII:
    132 ascii.append(UNICODE_TO_ASCII[char])
    133 continue
    134
    135 if ord(char) <= 127:
    136 # Add valid ASCII
    137 ascii.append(char)
    138 elif replace:
    139 # non-ASCII character
    140 ascii.append(replace)
    141 # else: ignore it
    142
    143 text = ''.join(ascii)
    144 return text.encode("ascii", "strict")
    145
    146 if __name__ == "__main__":
    147 from doctest import testmod
    148 from sys import exit
    149 failure, total = testmod()
    150 if failure:
    151 print "%s failure on %s tests" % (failure, total)
    152 exit(1)
    153 else:
    154 print "All tests are OK (count=%s)" % total
    Posted by haypo to python unicode ascii accents characters ... saved by 7 persons ... 2 comments ... 1 year, 2 months
showing 10, 25, 50 items per pages

Pages : 1 2

Flux RSS friendsnippetLatest snippets


More...