Clayton's Tech Bits

Home

Contact

Resumé / C.V.

Links

Search this site:
Custom Search

Categories:

/ (224)
  Admin/ (86)
    Apache/ (7)
      HTTPS-SSL/ (4)
    Cherokee/ (1)
    LAN/ (4)
    LVM/ (3)
    Monitoring/ (2)
      munin/ (2)
    OpenVPN/ (1)
    SSH-Proxy/ (3)
    SSH-SSL/ (6)
    backups/ (16)
      SpiderOak/ (1)
      backuppc/ (5)
      dirvish/ (1)
      misc/ (6)
      rdiff-backup/ (1)
      rsync/ (1)
      unison/ (1)
    commandLine/ (11)
    crontab/ (1)
    databases/ (8)
      MSSQL/ (2)
      MySQL/ (5)
      PostgreSQL/ (1)
    dynamicDNS/ (2)
    email/ (9)
      Dovecot/ (1)
      deliverability/ (1)
      misc/ (1)
      postfix/ (6)
    iptables/ (2)
    virtualization/ (8)
      VMware/ (1)
      virtualBox/ (7)
  Coding/ (11)
    bash/ (1)
    gdb/ (1)
    git/ (2)
    php/ (4)
    python/ (3)
      Django/ (1)
  Education/ (1)
  Hosting/ (23)
    Amazon/ (14)
      EBS/ (3)
      EC2/ (11)
    Godaddy/ (2)
    NearlyFreeSpeech/ (3)
    Rackspace/ (1)
    vpslink/ (3)
  Linux/ (20)
    Awesome/ (3)
    CPUfreq/ (1)
    Chinese/ (1)
    Debian/ (5)
      WPA/ (1)
    audio/ (1)
    encryption/ (2)
    fonts/ (1)
    misc/ (4)
    router-bridge/ (2)
  SW/ (39)
    browser/ (2)
      Chrome/ (1)
      Firefox/ (1)
    business/ (25)
      Drupal/ (8)
      KnowledgeTree/ (6)
      Redmine/ (2)
      SugarCRM/ (6)
      WebERP/ (2)
      eGroupware/ (1)
    email/ (1)
    fileSharing/ (1)
      mldonkey/ (1)
    graphics/ (2)
    research/ (2)
    website/ (6)
      blog/ (6)
        blosxom/ (3)
        rss2email/ (1)
        webgen/ (1)
  Security/ (12)
    IMchat/ (1)
    circumvention/ (2)
    e-mail/ (4)
    greatFirewall/ (1)
    hacking/ (1)
    password/ (1)
    privacy/ (1)
    skype/ (1)
  Services/ (1)
    fileSharing/ (1)
  TechWriting/ (1)
  xHW/ (13)
    Lenovo/ (1)
    Motorola_A1200/ (2)
    Thinkpad_600e/ (1)
    Thinkpad_a21m/ (3)
    Thinkpad_i1300/ (1)
    Thinkpad_x24/ (1)
    USB_audio/ (1)
    scanner/ (1)
    wirelessCards/ (2)
  xLife/ (17)
    China/ (9)
      Beijing/ (5)
        OpenSource/ (3)
    Expatriation/ (1)
    Vietnam/ (7)

Archives:

  • 2012/03
  • 2012/01
  • 2011/12
  • 2011/11
  • 2011/10
  • 2011/09
  • 2011/08
  • 2011/07
  • 2011/06
  • 2011/05
  • 2011/04
  • 2011/02
  • 2010/12
  • 2010/11
  • 2010/10
  • 2010/09
  • 2010/08
  • 2010/07
  • 2010/06
  • 2010/05
  • 2010/04
  • 2010/03
  • 2010/02
  • 2010/01
  • 2009/12
  • 2009/11
  • 2009/10
  • 2009/09
  • 2009/08
  • 2009/07
  • 2009/06
  • 2009/05
  • 2009/04
  • 2009/03
  • 2009/02
  • 2009/01
  • 2008/12
  • 2008/11
  • 2008/10
  • 2008/09
  • Subscribe XML RSS Feed

    Thu, 07 May 2009


    /Coding/python: Python: How to Put Some of Your Code in a Subdirectory

    Splitting one's code up into logical sub-directories is a fairly normal practice. This seems to be a little more complicated then usual with Python. One uses the "import" command to access functions in other files / directories. Of course Python automatically searches the system path and your execution directory to satisfy an import command. But to pull in a subdirectory one must place an (empty? Mine is....) __init__.py file in any sub-directory containing Python code that one wants to access. Then the directory name is added to the Python name space and is treated the same as files and functions defined within files.

    A concrete example is most definitely in order. Suppose I want to place all my database connect code into a subdirectory called "db":

    db$ ll
    total 16
    -rw------- 1 1489 2009-02-23 15:18 armor-mail-cert.pem
    -rw------- 1 3243 2009-02-23 15:18 armor-mail-key.pem
    -rw------- 1 1009 2009-02-23 14:48 armorMailSSL-cacert.pem
    -rwxr-xr-x 1  436 2009-05-07 15:55 connectArmorMail.py
    -rw-r--r-- 1    0 2009-05-07 16:33 __init__.py
    

    My main code is executing one directory up. Any code there that wants to access the connect function must contain:

    import db.connectArmorMail
    db = db.connectArmorMail.connect_armor_mail()
    

    Note that we are importing db.connectArmorMail, *not* db/connectArmorMail, and then the reference in the next line also uses a ".", not a "/". "db" is a directory, "connectArmorMail" is a file name, and "connect_armor_mail" is a function within a file, but all are treated equally in Python name space, separated by periods. db.connectArmorMail contains:

    def connect_armor_mail():
    
       import MySQLdb
    
       ssl_settings = {'ca': 'db/armorMailSSL-cacert.pem',
           'cert': 'db/armor-mail-cert.pem',
           'key': 'db/armor-mail-key.pem'
       }
    
       try:
          db=MySQLdb.connect(host="www.armor-mail.com",
             user="username",
             passwd="password",
             db="databasename",
             ssl=ssl_settings
          )
       except:
          print "

    ERROR: could not connect to database." return db

    Note that the files referenced in ssl_settings are in the same directory as db.connectArmorMail, but they are referenced relative to the execution directory (db/*).

    posted at: 04:01 | path: /Coding/python | permanent link to this entry