Menu

array.array: why? (and how2, exactly?)

Help
2006-06-02
2012-09-19
  • Profiliferator

    Profiliferator - 2006-06-02

    ...I honestly did a search 1st;
    In a couple days, it'll be an anniversary of the only post here regarding array (id=3196155).

    Anyway; this is just my insatiable curiosity, so let me ask:
    why BLOBs are converted to array.array, exactly? What's so special that array can hold what a native barebones Python string cannot handle?

    For some example/discussion:

    mysql> desc some_strings;
    +-------+--------------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | col1 | varchar(255) | YES | | NULL | |
    | col2 | blob | YES | | NULL | |
    | col3 | text | YES | | NULL | |
    +-------+--------------+------+-----+---------+-------+
    3 rows in set (0.01 sec)

    mysql> select * from some_strings;
    +------+------+------+
    | col1 | col2 | col3 |
    +------+------+------+
    | AAA | AAA | AAA |
    +------+------+------+
    1 row in set (0.00 sec)

    mysql> Bye

    :~> <some-shell-script.py> raw_sql 'select * from some_strings'

    ---- + ----------------- + ----
    col1 | col2 | col3
    ---- + ----------------- + ----
    AAA | array('c', 'AAA') | AAA
    ---- + ----------------- + ----

    (I honestly never used array before, never saw anybody using it until MySQLdb, and that's why am curious. Besides, (despite the advertised " efficiently represent an array of basic values"), some basic performance tests, like:
    string.append
    vs.
    array.array.append
    show that array.array sucks big time.).

    ======

    2nd part of the question -- regarding conversions, namely:

    ...
    FIELD_TYPE.BLOB:
    (FLAG.BINARY, char_array),
    (None, None),
    ,

    My guess is there's a loop somewhere on conversions[FIELD_TYPE.BLOB], but... not quite clear on what exactly, how it terminates, etc. And strictly speaking, the statement "...you can make your own... make copies...and then pass them to MySQL.connect()" doesn't quite hold water.

    (What if I pass, say:
    {...
    FIELD_TYPE.BLOB:
    ("Yes", 18, foo),
    ("Nope", lambda x: 18/0),
    ("Mabooka-Mabooka", "Mbe-Mbe"),
    ,
    } # // ?..

    THANKS in advance.

     
    • Andy Dustman

      Andy Dustman - 2006-06-02

      array objects are mutable. string objects are not. PEP-249 says:

      * The preferred object type for Binary objects are the
        buffer types available in standard Python starting with
        version 1.5.2. Please see the Python documentation for
        details. For information about the the C interface have a
        look at Include/bufferobject.h and
        Objects/bufferobject.c in the Python source
        distribution.
      

      However buffer objects are somewhat deprecated in favor of array objects.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.