snippets / Juzam51 / 

All Juzam51's snippets (5)

  1. 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)])
    first posted by prudis to python python password ... saved by 2 persons ... 0 comments ... 1 year, 1 month
  2. 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()
    first posted by Fufu to python python mail ... saved by 7 persons ... 0 comments ... 1 year, 2 months
  3. Utiliser le Drag'N Drop dans un Canvas

    Utiliser le Drag'N Drop dans un Canvas

     1 #!/usr/local/bin/python
    2
    3 import sys, os, string, time
    4 import Tkinter
    5 tk =Tkinter
    6
    7 # A Python example of drag and drop functionality within a single Tk widget.
    8 # The trick is in the bindings and event handler functions.
    9 # Tom Vrankar twv at ici.net
    10
    11 # empirical events between dropee and target, as determined from Tk 8.0
    12 # down.
    13 # leave.
    14 # up, leave, enter.
    15
    16 class CanvasDnD (tk.Frame):
    17 def __init__ (self, master):
    18 self.master =master
    19 self.loc =self.dragged =0
    20 tk.Frame.__init__ (self, master)
    21 canvas =tk.Canvas (self, width =256, height =256,
    22 relief =tk.RIDGE, background ="white", borderwidth =1)
    23 self.defaultcolor =canvas.itemcget (canvas.create_text (30, 25,
    24 font =("Helvetica", 14), text ="Item 1", tags ="DnD"), "fill")
    25 canvas.create_text (75, 75,
    26 font =("Helvetica", 14), text ="Item 2", tags ="DnD")
    27 canvas.create_text (125, 125,
    28 font =("Helvetica", 14), text ="Item 3", tags ="DnD")
    29 canvas.create_text (175, 175,
    30 font =("Helvetica", 14), text ="Item 4", tags ="DnD")
    31 canvas.create_text (225, 225,
    32 font =("Helvetica", 14), text ="Item 5", tags ="DnD")
    33 canvas.pack (expand =1, fill =tk.BOTH)
    34 canvas.tag_bind ("DnD", "<ButtonPress-1>", self.down)
    35 canvas.tag_bind ("DnD", "<ButtonRelease-1>", self.chkup)
    36 canvas.tag_bind ("DnD", "<Enter>", self.enter)
    37 canvas.tag_bind ("DnD", "<Leave>", self.leave)
    38
    39 def down (self, event):
    40 print "Click on %s" %event.widget.itemcget (tk.CURRENT, "text")
    41 self.loc =1
    42 self.dragged =0
    43 event.widget.bind ("<Motion>", self.motion)
    44
    45 def motion (self, event):
    46 root.config (cursor ="exchange")
    47 event.widget.itemconfigure (tk.CURRENT, fill ="blue")
    48 event.widget.unbind ("<Motion>")
    49
    50 def leave (self, event):
    51 self.loc =0
    52
    53 def enter (self, event):
    54 self.loc =1
    55 if self.dragged ==event.time:
    56 self.up (event)
    57
    58 def chkup (self, event):
    59 root.config (cursor ="")
    60 self.target =event.widget.find_withtag (tk.CURRENT)
    61 event.widget.itemconfigure (tk.CURRENT, fill =self.defaultcolor)
    62 if self.loc: # is button released in same widget as pressed?
    63 self.up (event)
    64 else:
    65 self.dragged =event.time
    66
    67 def up (self, event):
    68 event.widget.unbind ("<Motion>")
    69 if (self.target ==event.widget.find_withtag (tk.CURRENT)):
    70 print "Select %s" %event.widget.itemcget (tk.CURRENT, "text")
    71 else:
    72 event.widget.itemconfigure (tk.CURRENT, fill ="blue")
    73 self.master.update()
    74 time.sleep (.1)
    75 print "%s Drag-N-Dropped onto %s" \
    76 %(event.widget.itemcget (self.target, "text"),
    77 event.widget.itemcget (tk.CURRENT, "text"))
    78 event.widget.itemconfigure (tk.CURRENT, fill =self.defaultcolor)
    79
    80 root =tk.Tk()
    81 root.title ("Drag-N-Drop Demo")
    82 CanvasDnD (root).pack()
    83 root.mainloop()
    first posted by Juzam51 to python tk dragndrop ... saved by 1 person ... 0 comments ... 1 year, 2 months
  4. Comment utiliser SQLite3 ?

    Ce code montre comment créer une base de données SQLite3, et comment faire une requête sur une table.

     1 # -*- coding:Utf-8 -*-
    2
    3 import sqlite3
    4 import os
    5
    6 def main():
    7 if not os.path.isfile("juzam51.db"):
    8 conn = sqlite3.connect("juzam51.db")
    9 c = conn.cursor()
    10 """
    11 c.execute("create table employe (numero integer, nom text, prenom text)")
    12 for t in ((1, "Dupond", "La joie"), (2, "DERE", "Armel"), (3, "Duval", "Leroy")):
    13 c.execute("insert into employe (numero, nom, prenom) values (?, ?, ?)", t)
    14 conn.commit()
    15 """
    16 else:
    17 conn = sqlite3.connect("base.db")
    18 c = conn.cursor()
    19 c.execute("select * from employe")
    20 print c.fetchall()
    21
    22 if __name__ == "__main__":
    23 main()
    first posted by Juzam51 to python sqlite3 database ... saved by 1 person ... 0 comments ... 1 year, 2 months
  5. Remplace les caractères accentués par leurs équivalents

    Exemple d'utilisation Accents.clean("mämé") == "mame"

     1 class Accents:
    2 __reptable = {}
    3
    4 __corresp = [
    5 (u"A", [0x00C0,0x00C1,0x00C2,0x00C3,0x00C4,0x00C5,0x0100,0x0102,0x0104]),
    6 (u"AE", [0x00C6]),
    7 (u"a", [0x00E0,0x00E1,0x00E2,0x00E3,0x00E4,0x00E5,0x0101,0x0103,0x0105]),
    8 (u"ae", [0x00E6]),
    9 (u"C", [0x00C7,0x0106,0x0108,0x010A,0x010C]),
    10 (u"c", [0x00E7,0x0107,0x0109,0x010B,0x010D]),
    11 (u"D", [0x00D0,0x010E,0x0110]),
    12 (u"d", [0x00F0,0x010F,0x0111]),
    13 (u"E", [0x00C8,0x00C9,0x00CA,0x00CB,0x0112,0x0114,0x0116,0x0118,0x011A]),
    14 (u"e", [0x00E8,0x00E9,0x00EA,0x00EB,0x0113,0x0115,0x0117,0x0119,0x011B]),
    15 (u"G", [0x011C,0x011E,0x0120,0x0122]),
    16 (u"g", [0x011D,0x011F,0x0121,0x0123]),
    17 (u"H", [0x0124,0x0126]),
    18 (u"h", [0x0125,0x0127]),
    19 (u"I", [0x00CC,0x00CD,0x00CE,0x00CF,0x0128,0x012A,0x012C,0x012E,0x0130]),
    20 (u"i", [0x00EC,0x00ED,0x00EE,0x00EF,0x0129,0x012B,0x012D,0x012F,0x0131]),
    21 (u"IJ", [0x0132]),
    22 (u"ij", [0x0133]),
    23 (u"J", [0x0134]),
    24 (u"j", [0x0135]),
    25 (u"K", [0x0136]),
    26 (u"k", [0x0137,0x0138]),
    27 (u"L", [0x0139,0x013B,0x013D,0x013F,0x0141]),
    28 (u"l", [0x013A,0x013C,0x013E,0x0140,0x0142]),
    29 (u"N", [0x00D1,0x0143,0x0145,0x0147,0x014A]),
    30 (u"n", [0x00F1,0x0144,0x0146,0x0148,0x0149,0x014B]),
    31 (u"O", [0x00D2,0x00D3,0x00D4,0x00D5,0x00D6,0x00D8,0x014C,0x014E,0x0150]),
    32 (u"o", [0x00F2,0x00F3,0x00F4,0x00F5,0x00F6,0x00F8,0x014D,0x014F,0x0151]),
    33 (u"OE", [0x0152]),
    34 (u"oe", [0x0153]),
    35 (u"R", [0x0154,0x0156,0x0158]),
    36 (u"r", [0x0155,0x0157,0x0159]),
    37 (u"S", [0x015A,0x015C,0x015E,0x0160]),
    38 (u"s", [0x015B,0x015D,0x015F,0x01610,0x017F]),
    39 (u"T", [0x0162,0x0164,0x0166]),
    40 (u"t", [0x0163,0x0165,0x0167]),
    41 (u"U", [0x00D9,0x00DA,0x00DB,0x00DC,0x0168,0x016A,0x016C,0x016E,0x0170,0x172]),
    42 (u"u", [0x00F9,0x00FA,0x00FB,0x00FC,0x0169,0x016B,0x016D,0x016F,0x0171]),
    43 (u"W", [0x0174]),
    44 (u"w", [0x0175]),
    45 (u"Y", [0x00DD,0x0176,0x0178]),
    46 (u"y", [0x00FD,0x00FF,0x0177]),
    47 (u"Z", [0x0179,0x017B,0x017D]),
    48 (u"z", [0x017A,0x017C,0x017E])
    49 ]
    50 for __repchar,__codes in __corresp :
    51 for __code in __codes :
    52 __reptable[__code] = __repchar
    53
    54 @staticmethod
    55 def clean(s):
    56 """ enleve les accents de 's' (unicode ou utf8), et renvoi de l'unicode
    57 (marche pour maj et minuscules !!!)
    58 """
    59 if isinstance(s,str) :
    60 s = unicode(s,"utf8","replace")
    61 return s.translate(Accents.__reptable)
    first posted by manatlan to python characters accents ... saved by 11 persons ... 2 comments ... 1 year, 2 months
showing 10, 25, 50 items per pages

Pages : 1