<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Morlandi's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 22:05:44 GMT</pubDate>
    <description>DZone Snippets: Morlandi's Code Snippets</description>
    <item>
      <title>Packing ZODB from command line</title>
      <link>http://snippets.dzone.com/posts/show/5477</link>
      <description>Packing ZODB from command line&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$wget.exe --delete-after --user=admin --password=***** "http://localhost:8090/Control_Panel/manage_pack?days:float=0"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"D:\services\support\curl-7.17.0\curl" -d -v -f -u admin:password http://localhost:8090/Control_Panel/manage_pack?days:float=0&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 May 2008 14:13:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5477</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>send django queries to OutputDebugString (Win32)</title>
      <link>http://snippets.dzone.com/posts/show/5476</link>
      <description>Modify django/db/backends/util.py as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import win32api&lt;br /&gt;&lt;br /&gt;class CursorDebugWrapper(object):&lt;br /&gt;    def __init__(self, cursor, db):&lt;br /&gt;        self.cursor = cursor&lt;br /&gt;        self.db = db # Instance of a BaseDatabaseWrapper subclass&lt;br /&gt;&lt;br /&gt;    def debug_trace(self,category,message):&lt;br /&gt;        win32api.OutputDebugString('|django              |%s|%s\n' % (category,message))&lt;br /&gt;&lt;br /&gt;    def trace_entry (self, sql, elapsed): &lt;br /&gt;        self.debug_trace('Q',sql)&lt;br /&gt;        if elapsed &gt;= 0.01:&lt;br /&gt;            self.debug_trace(' ','elapsed: %.3f' % elapsed)&lt;br /&gt;&lt;br /&gt;    def execute(self, sql, params=()):&lt;br /&gt;        start = time()&lt;br /&gt;        try:&lt;br /&gt;            return self.cursor.execute(sql, params)&lt;br /&gt;        finally:&lt;br /&gt;            stop = time()&lt;br /&gt;            sql = self.db.ops.last_executed_query(self.cursor, sql, params)&lt;br /&gt;            self.db.queries.append({&lt;br /&gt;                'sql': sql,&lt;br /&gt;                'time': "%.3f" % (stop - start),&lt;br /&gt;            })&lt;br /&gt;            self.trace_entry(sql, stop - start) &lt;br /&gt;&lt;br /&gt;    def executemany(self, sql, param_list):&lt;br /&gt;        start = time()&lt;br /&gt;        try:&lt;br /&gt;            return self.cursor.executemany(sql, param_list)&lt;br /&gt;        finally:&lt;br /&gt;            stop = time()&lt;br /&gt;            self.db.queries.append({&lt;br /&gt;                'sql': '%s times: %s' % (len(param_list), sql),&lt;br /&gt;                'time': "%.3f" % (stop - start),&lt;br /&gt;            })&lt;br /&gt;            self.trace_entry(sql, stop - start) &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 08 May 2008 13:18:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5476</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>Fill combobox with enum values</title>
      <link>http://snippets.dzone.com/posts/show/3469</link>
      <description>&lt;code&gt;&lt;br /&gt;public enum CardDataType&lt;br /&gt;{&lt;br /&gt;    Unknown,&lt;br /&gt;    Raw,&lt;br /&gt;    RowChp&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;ctlDataType.DataSource = Enum.GetValues(typeof(CardDataType));&lt;br /&gt;ctlDataType.SelectedItem = CardDataType.Unknown;&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;...&lt;br /&gt;string strType = ctlDataType.SelectedItem.ToString();&lt;br /&gt;&lt;br /&gt;or&lt;br /&gt;&lt;br /&gt;CardDataType dataType;&lt;br /&gt;string strDataType = "Raw";&lt;br /&gt;dataType = (CardDataType)Enum.Parse(typeof(CardDataType), strDataType, true);&lt;br /&gt;&lt;br /&gt;and eventually&lt;br /&gt;&lt;br /&gt;ctlDataType.SelectedItem = dataType; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;// a volte da' problemi in fase di inizializzazione della UI; &lt;br /&gt;// in questi casi si puo' ricorrere a Enum.GetNames()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;foreach ( string name in Enum.GetNames(typeof(CardDataType)) ) &lt;br /&gt;{&lt;br /&gt;    ctlDataType.Items.Add(name);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;string strDataType = "Raw";&lt;br /&gt;ctlDataType.SelectedItem = strDataType; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 07 Feb 2007 18:21:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3469</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>IPython shell</title>
      <link>http://snippets.dzone.com/posts/show/3415</link>
      <description>// "%PYTHON%" -i -c ..."&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from Zope2 import configure; &lt;br /&gt;import os; &lt;br /&gt;configure(os.environ['ZOPE_CONFIG_FILE']); &lt;br /&gt;&lt;br /&gt;import Zope2; &lt;br /&gt;app=Zope2.app(); &lt;br /&gt;&lt;br /&gt;ns={'__name__':'blah','app':app};&lt;br /&gt;&lt;br /&gt;import IPython; &lt;br /&gt;IPython.Shell.IPShell(user_ns=ns).mainloop(sys_exit=1);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 05 Feb 2007 20:57:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3415</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>review_state</title>
      <link>http://snippets.dzone.com/posts/show/3331</link>
      <description>// retrieve review state&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from Products.CMFCore.utils import getToolByName&lt;br /&gt;wtool = getToolByName(context, "portal_workflow")&lt;br /&gt;wf_state = wtool.getInfoFor(obj,'review_state',None)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Jan 2007 14:52:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3331</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>object_delete</title>
      <link>http://snippets.dzone.com/posts/show/3281</link>
      <description>// object_delete&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from AccessControl import Unauthorized&lt;br /&gt;from Products.CMFPlone.utils import transaction_note&lt;br /&gt;from Products.CMFPlone import PloneMessageFactory as _&lt;br /&gt;&lt;br /&gt;REQUEST = context.REQUEST&lt;br /&gt;if REQUEST.get('REQUEST_METHOD', 'GET').upper() == 'GET':&lt;br /&gt;    raise Unauthorized, 'This method can not be accessed using a GET request'&lt;br /&gt;&lt;br /&gt;parent = context.aq_inner.aq_parent&lt;br /&gt;parent.manage_delObjects(context.getId())&lt;br /&gt;&lt;br /&gt;message = _(u'${title} has been deleted.',&lt;br /&gt;            mapping={u'title' : context.title_or_id()})&lt;br /&gt;transaction_note('Deleted %s' % context.absolute_url())&lt;br /&gt;&lt;br /&gt;context.plone_utils.addPortalMessage(message)&lt;br /&gt;return state.set(status = 'success')&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 14 Jan 2007 12:18:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3281</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>ArrayList</title>
      <link>http://snippets.dzone.com/posts/show/3255</link>
      <description>&lt;code&gt;&lt;br /&gt;using System.Collections;&lt;br /&gt;&lt;br /&gt;ArrayList arrList = new ArrayList();&lt;br /&gt;&lt;br /&gt;arrList.Add("one");&lt;br /&gt;arrList.Add("two");&lt;br /&gt;arrList.Add("three");&lt;br /&gt;&lt;br /&gt;string[] strArray = arrList.ToArray(Type.GetType("System.String")) as string[];&lt;br /&gt;return strArray;&lt;br /&gt;       &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 08 Jan 2007 12:39:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3255</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>autoscroll text</title>
      <link>http://snippets.dzone.com/posts/show/3254</link>
      <description>&lt;code&gt;&lt;br /&gt;    textBox1.SelectionStart = textBox1.Text.Length;&lt;br /&gt;    textBox1.SelectionLength = 0;&lt;br /&gt;    textBox1.ScrollToCaret();    &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 08 Jan 2007 12:34:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3254</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>Abilitazione accesso remoto per PostgreSQL</title>
      <link>http://snippets.dzone.com/posts/show/3199</link>
      <description>// in "pg_hba.conf"&lt;br /&gt;&lt;code&gt;&lt;br /&gt;host    all         all         192.168.98.0/24       md5&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;// in "postgresql.conf"&lt;br /&gt;&lt;code&gt;&lt;br /&gt;listen_addresses = '*'		# what IP address(es) to listen on; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 22 Dec 2006 15:51:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3199</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>Result Object Methods</title>
      <link>http://snippets.dzone.com/posts/show/3011</link>
      <description>// Assuming that we have set result to being a result object we can use the following methods:&lt;br /&gt;&lt;br /&gt;len(result)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;     this will show the number rows returned (which would be 3 in the example above).&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.names()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    a list of all the column headings, returning a list containing emp_id, first, last and salary&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.tuples()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    returns a list of tuples in our example:&lt;br /&gt;&lt;br /&gt;    [(43, 'Bob', 'Roberts', 50000),&lt;br /&gt;     (101, 'Cheeta', 'leCat', 100000),&lt;br /&gt;     (99, 'Jane', 'Junglewoman', 100001)]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.dictionaries()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    will return a list of dictionaries, with one dictionary for each row:&lt;br /&gt;&lt;br /&gt;    [{'emp_id': 42, 'first': 'Bob','last': 'Roberts', 'salary': 50000},&lt;br /&gt;     {'emp_id': 101, 'first: 'Cheeta', 'last': 'leCat', 'salary': 100000},&lt;br /&gt;     {'emp_id': 99, 'first': 'Jane', 'last': 'Junglewoman', 'salary': 100001}]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.data_dictionary()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    returns a dictionary describing the structure of the results table. &lt;br /&gt;&lt;br /&gt;The dictionary has the key name, type, null and width. &lt;br /&gt;Name and type are self explanatory, null is true if that field may contain a null value and width is the width in characters of the field. &lt;br /&gt;Note that null and width may not be set by some Database Adapters.&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.asRDB()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    displays the result in a similar way to a relational database. &lt;br /&gt;&lt;br /&gt;The DTML below displays the result below:&lt;br /&gt;    &lt;pre&gt;&lt;br /&gt;      &lt;dtml-var "list_all_employees().asRDB()"&gt;&lt;br /&gt;    &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;    ... displays ...&lt;br /&gt;&lt;br /&gt;    emp_id first last salary&lt;br /&gt;    42 Bob Roberts 50000&lt;br /&gt;    101 Cheeta leCat 100000&lt;br /&gt;    99 Jane Junglewoman 100001&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result[0][1]&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    return row 0, column 1 of the result, bob in this example. &lt;br /&gt;&lt;br /&gt;Be careful using this method as changes in the schema will cause unexpected results. &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Nov 2006 17:11:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3011</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
  </channel>
</rss>
