??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲第一第二第三第四第五第六 ,亚洲精品乱码久久久久久久久久久久 ,亚洲人成电影在线天堂http://www.tkk7.com/kick191/登高者必自卑Q行q者必自迩Q在q个世界上,重要的不是你正站在哪里,而是你正朝什么方向移?/description>zh-cnSun, 11 May 2025 13:17:11 GMTSun, 11 May 2025 13:17:11 GMT60Python核心~程Q第二版Q第?ji)章习题Q?Q?的实?/title><link>http://www.tkk7.com/kick191/articles/Python_core_9-7.html</link><dc:creator>天独</dc:creator><author>天独</author><pubDate>Thu, 15 Jul 2010 04:47:00 GMT</pubDate><guid>http://www.tkk7.com/kick191/articles/Python_core_9-7.html</guid><wfw:comment>http://www.tkk7.com/kick191/comments/326183.html</wfw:comment><comments>http://www.tkk7.com/kick191/articles/Python_core_9-7.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/kick191/comments/commentRss/326183.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/kick191/services/trackbacks/326183.html</trackback:ping><description><![CDATA[9Q?习题是要求些一個解析配|文件的c,用来解析Win32QPOSIXQ或着其他q_下的配置文gQ我在这里只写了一個Win32文g的ini文g的解?br /> 下面是我的实C码:<br /> <div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 128);">  1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">coding=utf-8</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">  2</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">  3</span> <span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">  4</span> <span style="color: rgb(128, 0, 0);">Created on 2010-7-12<br /> </span><span style="color: rgb(0, 128, 128);">  5</span> <span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">  6</span> <span style="color: rgb(128, 0, 0);">@author: Innate Solitary<br /> </span><span style="color: rgb(0, 128, 128);">  7</span> <span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);">  8</span> <span style="color: rgb(128, 0, 0);">ini文g解析c?br /> </span><span style="color: rgb(0, 128, 128);">  9</span> <span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 10</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 11</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> ConfigParser<br /> </span><span style="color: rgb(0, 128, 128);"> 12</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 13</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 14</span> <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ConfigFile(object):<br /> </span><span style="color: rgb(0, 128, 128);"> 15</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 16</span> <span style="color: rgb(128, 0, 0);">    配置文g解析Q处理的单实现类<br /> </span><span style="color: rgb(0, 128, 128);"> 17</span> <span style="color: rgb(128, 0, 0);">    </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 18</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 19</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 20</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self, file_name):<br /> </span><span style="color: rgb(0, 128, 128);"> 21</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 22</span> <span style="color: rgb(128, 0, 0);">        Constructor<br /> </span><span style="color: rgb(0, 128, 128);"> 23</span> <span style="color: rgb(128, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 24</span> <span style="color: rgb(0, 0, 0);">        self.file_name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> file_name<br /> </span><span style="color: rgb(0, 128, 128);"> 25</span> <span style="color: rgb(0, 0, 0);">        self.file </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> file(file_name)<br /> </span><span style="color: rgb(0, 128, 128);"> 26</span> <span style="color: rgb(0, 0, 0);">        self.cfg </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ConfigParser.ConfigParser()<br /> </span><span style="color: rgb(0, 128, 128);"> 27</span> <span style="color: rgb(0, 0, 0);">        self.read_handle </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None<br /> </span><span style="color: rgb(0, 128, 128);"> 28</span> <span style="color: rgb(0, 0, 0);">        self.write_handle </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None<br /> </span><span style="color: rgb(0, 128, 128);"> 29</span> <span style="color: rgb(0, 0, 0);">        self.sections </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> []<br /> </span><span style="color: rgb(0, 128, 128);"> 30</span> <span style="color: rgb(0, 0, 0);">        self.items </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br /> </span><span style="color: rgb(0, 128, 128);"> 31</span> <span style="color: rgb(0, 0, 0);">        self.init_flag </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> False<br /> </span><span style="color: rgb(0, 128, 128);"> 32</span> <span style="color: rgb(0, 0, 0);">    <br /> </span><span style="color: rgb(0, 128, 128);"> 33</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> initialization(self):<br /> </span><span style="color: rgb(0, 128, 128);"> 34</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 35</span> <span style="color: rgb(128, 0, 0);">        初始化配|文?br /> </span><span style="color: rgb(0, 128, 128);"> 36</span> <span style="color: rgb(128, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 37</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">:<br /> </span><span style="color: rgb(0, 128, 128);"> 38</span> <span style="color: rgb(0, 0, 0);">            self.read_handle </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(self.file_name, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);"> 39</span> <span style="color: rgb(0, 0, 0);">            self.write_handle </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(self.file_name, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">w</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);"> 40</span> <span style="color: rgb(0, 0, 0);">            self.cfg.readfp(self.read_handle)<br /> </span><span style="color: rgb(0, 128, 128);"> 41</span> <span style="color: rgb(0, 0, 0);">            self.sections </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.cfg.sections()<br /> </span><span style="color: rgb(0, 128, 128);"> 42</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> section </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> self.sections:<br /> </span><span style="color: rgb(0, 128, 128);"> 43</span> <span style="color: rgb(0, 0, 0);">                self.items[section] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.cfg.items(section)<br /> </span><span style="color: rgb(0, 128, 128);"> 44</span> <span style="color: rgb(0, 0, 0);">            self.init_flag </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> True<br /> </span><span style="color: rgb(0, 128, 128);"> 45</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">except</span><span style="color: rgb(0, 0, 0);"> Exception, e:<br /> </span><span style="color: rgb(0, 128, 128);"> 46</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">初始化配|文仉?</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, str(e)<br /> </span><span style="color: rgb(0, 128, 128);"> 47</span> <span style="color: rgb(0, 0, 0);">            self.init_flag </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> False<br /> </span><span style="color: rgb(0, 128, 128);"> 48</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> self.init_flag<br /> </span><span style="color: rgb(0, 128, 128);"> 49</span> <span style="color: rgb(0, 0, 0);">    <br /> </span><span style="color: rgb(0, 128, 128);"> 50</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> close(self):<br /> </span><span style="color: rgb(0, 128, 128);"> 51</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 52</span> <span style="color: rgb(128, 0, 0);">        关闭配置文g<br /> </span><span style="color: rgb(0, 128, 128);"> 53</span> <span style="color: rgb(128, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 54</span> <span style="color: rgb(0, 0, 0);">        self.read_handle.close()<br /> </span><span style="color: rgb(0, 128, 128);"> 55</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.file<br /> </span><span style="color: rgb(0, 128, 128);"> 56</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.read_handle<br /> </span><span style="color: rgb(0, 128, 128);"> 57</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.file_name<br /> </span><span style="color: rgb(0, 128, 128);"> 58</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.cfg<br /> </span><span style="color: rgb(0, 128, 128);"> 59</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.sections<br /> </span><span style="color: rgb(0, 128, 128);"> 60</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> self.items<br /> </span><span style="color: rgb(0, 128, 128);"> 61</span> <span style="color: rgb(0, 0, 0);">    <br /> </span><span style="color: rgb(0, 128, 128);"> 62</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> get_value(self, section, key, default </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);">):<br /> </span><span style="color: rgb(0, 128, 128);"> 63</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 64</span> <span style="color: rgb(128, 0, 0);">        得到指定section下的key的|如果沒有i其一個空字符串做为默认|q加入文件中<br /> </span><span style="color: rgb(0, 128, 128);"> 65</span> <span style="color: rgb(128, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 66</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> self.init_flag:<br /> </span><span style="color: rgb(0, 128, 128);"> 67</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);"> Exception(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">未初始化配置文gQ请在处理文件前调用initialization(self)q行配置文g初始?/span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);"> 68</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 69</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 70</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> self.cfg.has_section(section):<br /> </span><span style="color: rgb(0, 128, 128);"> 71</span> <span style="color: rgb(0, 0, 0);">            self.cfg.add_section(section)<br /> </span><span style="color: rgb(0, 128, 128);"> 72</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> self.cfg.has_option(section, key):<br /> </span><span style="color: rgb(0, 128, 128);"> 73</span> <span style="color: rgb(0, 0, 0);">            self.cfg.set(section, key, default)<br /> </span><span style="color: rgb(0, 128, 128);"> 74</span> <span style="color: rgb(0, 0, 0);">                <br /> </span><span style="color: rgb(0, 128, 128);"> 75</span> <span style="color: rgb(0, 0, 0);">        value </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.cfg.get(section, key)<br /> </span><span style="color: rgb(0, 128, 128);"> 76</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 77</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 78</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> value<br /> </span><span style="color: rgb(0, 128, 128);"> 79</span> <span style="color: rgb(0, 0, 0);">    <br /> </span><span style="color: rgb(0, 128, 128);"> 80</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> set_value(self, section, key, value):<br /> </span><span style="color: rgb(0, 128, 128);"> 81</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 82</span> <span style="color: rgb(128, 0, 0);">        讄?br /> </span><span style="color: rgb(0, 128, 128);"> 83</span> <span style="color: rgb(128, 0, 0);">        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 84</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 85</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">not</span><span style="color: rgb(0, 0, 0);"> self.cfg.has_section(section):<br /> </span><span style="color: rgb(0, 128, 128);"> 86</span> <span style="color: rgb(0, 0, 0);">            self.cfg.add_section(section);<br /> </span><span style="color: rgb(0, 128, 128);"> 87</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 88</span> <span style="color: rgb(0, 0, 0);">        self.cfg.set(section, key, value)<br /> </span><span style="color: rgb(0, 128, 128);"> 89</span> <span style="color: rgb(0, 0, 0);">    <br /> </span><span style="color: rgb(0, 128, 128);"> 90</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> write_to_file(self, file </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None):<br /> </span><span style="color: rgb(0, 128, 128);"> 91</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> None </span><span style="color: rgb(0, 0, 255);">and</span><span style="color: rgb(0, 0, 0);"> file.mode </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br /> </span><span style="color: rgb(0, 128, 128);"> 92</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);"> Exception(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">文g模式不可?/span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> </span><span style="color: rgb(0, 128, 128);"> 93</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> file </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> None:<br /> </span><span style="color: rgb(0, 128, 128);"> 94</span> <span style="color: rgb(0, 0, 0);">            self.cfg.write(self.write_handle)<br /> </span><span style="color: rgb(0, 128, 128);"> 95</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br /> </span><span style="color: rgb(0, 128, 128);"> 96</span> <span style="color: rgb(0, 0, 0);">            self.cfg.write(file)<br /> </span><span style="color: rgb(0, 128, 128);"> 97</span> <span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 128, 128);"> 98</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);"> 99</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">100</span> <span style="color: rgb(0, 0, 0);">        <br /> </span><span style="color: rgb(0, 128, 128);">101</span> </div> 我在q里使用了ConfigParser模块Q这里我的这D代码因为工作的原因只实C一部分Qƈ沒有把我当初惛_的功能实玎ͼ但是习题9Q?要求的功能都实现了,代码很简单个沒有什么难得所以沒有加注释?br /> <br /> <img src ="http://www.tkk7.com/kick191/aggbug/326183.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/kick191/" target="_blank">天独</a> 2010-07-15 12:47 <a href="http://www.tkk7.com/kick191/articles/Python_core_9-7.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJBq接时异常Q解军_法!http://www.tkk7.com/kick191/articles/EJB.html天独天独Sat, 12 Jun 2010 08:03:00 GMThttp://www.tkk7.com/kick191/articles/EJB.htmlhttp://www.tkk7.com/kick191/comments/323440.htmlhttp://www.tkk7.com/kick191/articles/EJB.html#Feedback0http://www.tkk7.com/kick191/comments/commentRss/323440.htmlhttp://www.tkk7.com/kick191/services/trackbacks/323440.html 1.首先查你的Context.PROVIDER_URL配置是否正确Q例如Jboss4.0.2中是jnp://localhost:1099Q当然这?099端口也可能换Q如果不知道请到%JBOSS_HOME%\server\default\conf\下查找jboss-service.xml在里面找到如下代码:
 1 <mbean code="org.jboss.naming.NamingService"
 2       name="jboss:service=Naming"
 3       xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
 4       <!-- The call by value mode. true if all lookups are unmarshalled using
 5       the caller's TCL, false if in VM lookups return the value by reference.
 6       -->
 7       <attribute name="CallByValue">false</attribute>
 8       <!-- The listening port for the bootstrap JNP service. Set this to -1
 9         to run the NamingService without the JNP invoker listening port.
10       -->
11       <attribute name="Port">1099</attribute>
12       <!-- The bootstrap JNP server bind address. This also sets the default
13       RMI service bind address. Empty == all addresses
14        -->
15       <attribute name="BindAddress">${jboss.bind.address}</attribute>
16       <!-- The port of the RMI naming service, 0 == anonymous -->
17       <attribute name="RmiPort">1098</attribute>
18       <!-- The RMI service bind address. Empty == all addresses
19        -->
20       <attribute name="RmiBindAddress">${jboss.bind.address}</attribute>
21       <!-- The thread pool service used to control the bootstrap lookups -->
22       <depends optional-attribute-name="LookupPool"
23          proxy-type="attribute">jboss.system:service=ThreadPool</depends>
24    </mbean>
 <attribute name="Port">1099</attribute> 看这里的端口P修改q里的端口号Zprovider_url配置的端口号Q或着反过来修改你的provider_url的端口号里的端口受?br />
2.如果1中配|沒有问题,h查你的防火墙Q杀毒Y件等安全工具是否止了端口,我用的mcafeeQ需要在mcafee的访问保?>属?>用户自定义规则添加一条规则,Ҏ(gu)提示写入需要排除的端口P然后在排除项中添加java.exe和javaw.exe定卛_Q在试试Q应该就可以了!


天独 2010-06-12 16:03 发表评论
]]>
JPA入门Q?http://www.tkk7.com/kick191/articles/319147.html天独天独Thu, 22 Apr 2010 12:01:00 GMThttp://www.tkk7.com/kick191/articles/319147.htmlhttp://www.tkk7.com/kick191/comments/319147.htmlhttp://www.tkk7.com/kick191/articles/319147.html#Feedback0http://www.tkk7.com/kick191/comments/commentRss/319147.htmlhttp://www.tkk7.com/kick191/services/trackbacks/319147.html 今天Q说CallBackQJPA为我们提供了一些在q行CURD前后的一些callback methodsQ这些方法可以在我们q行数据持久化和查询前后Q进行一些操作,比如U录日志?br /> 先来介绍写有几种callback methods annotationQ?br /> @PerPresist 持久化之前调用调?br /> @PostPersist 持久化之后(官方文档是在Object状态变?Persist 状态)
@PostLoad 查询之后
@PreUpdate和@PostUpdate q两个分别是在更C前和之后Q不q这两个必须是在PostLoad之后Q对查询出来的数据进行更斎ͼ才会触发
@PreRemove和@PostReomve 分别是删除前和删除后Q官Ҏ(gu)是Object状态变为deleted 状态)

下面是我的例子,当然q里只有关键代码Q?br /> 2010q??2?星期?br />
 1 @PrePersist
 2     public void logPrePersist() {
 3         logger.info("PrePersist " + this.getTitle());
 4     }
 5     
 6     @PostPersist
 7     public void logPostPersist() {
 8         logger.info("PostPersist " + this.getTitle());
 9     }
10     
11     @PostLoad
12     public void logPostLoad() {
13         logger.info("PostLoad " + this.getTitle());
14     }
15     
16     @PreUpdate
17     public void logPreUpdate() {
18         logger.info("PreUpdate " + this.getTitle());
19     }
20     
21     @PostUpdate
22     public void logPostUpdate() {
23         logger.info("PostUpdate " + this.getTitle());
24     }
25     
26     @PreRemove
27     public void logPreRemove() {
28         logger.info("PreRemove " + this.getTitle());
29     }
30     
31     @PostRemove
32     public void logPostRemove() {
33         logger.info("PostRemove " + this.getTitle());
34     }
当然也可以用Entity Listeners Q如Q?br /> 在实体类上加上这个注解:@EntityListeners(value = { MagazineLog.class })
然后写MagazineLog.java:
 1 public class MagazineLog {
 2     /**
 3      * Logger for this class
 4      */
 5     private static final Logger logger = LoggerFactory.getLogger(MagazineLog.class);
 6 
 7     @PrePersist
 8     public void logPrePersist(Object pc) {
 9         logger.info("PrePersist " + ((Magazine)pc).getTitle());
10     }
11     
12     @PostPersist
13     public void logPostPersist(Object pc) {
14         logger.info("PostPersist " + ((Magazine)pc).getTitle());
15     }
16     
17     @PostLoad
18     public void logPostLoad(Object pc) {
19         logger.info("PostLoad " + ((Magazine)pc).getTitle());
20     }
21     
22     @PreUpdate
23     public void logPreUpdate(Object pc) {
24         logger.info("PreUpdate " + ((Magazine)pc).getTitle());
25     }
26     
27     @PostUpdate
28     public void logPostUpdate(Object pc) {
29         logger.info("PostUpdate " + ((Magazine)pc).getTitle());
30     }
31     
32     @PreRemove
33     public void logPreRemove(Object pc) {
34         logger.info("PreRemove " + ((Magazine)pc).getTitle());
35     }
36     
37     @PostRemove
38     public void logPostRemove(Object pc) {
39         logger.info("PostRemove " + ((Magazine)pc).getTitle());
40     }
41 }
p些,q个比较?br />


天独 2010-04-22 20:01 发表评论
]]>
JPA入门-2http://www.tkk7.com/kick191/articles/JPA2.html天独天独Tue, 06 Apr 2010 14:24:00 GMThttp://www.tkk7.com/kick191/articles/JPA2.htmlhttp://www.tkk7.com/kick191/comments/317597.htmlhttp://www.tkk7.com/kick191/articles/JPA2.html#Feedback1http://www.tkk7.com/kick191/comments/commentRss/317597.htmlhttp://www.tkk7.com/kick191/services/trackbacks/317597.html
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <!-- q个是自动创?nbsp;-->
<property name="openjpa.Log" value="DefaultLevel=WARN,SQL=TRACE" />  <!-- q是打印SQL语句 -->
q两个是比较常用的属?br /> 今天说的是一对多的关联,q里有三个类Q非别是UserQPostQComment他们之间的关pLQUser中有多个Post? CommentQPost有多个CommentQ而Comment有只有一个User和PostQ这里我׃列出来表l构了,因ؓ可以用JPA的自动生? ?br /> 下面是User的关键代?
 1 @Entity
 2 public class User {
 3   @Id
 4   @GeneratedValue(strategy = GenerationType.SEQUENCE)
 5   private int id;
 6   private String name;
 7   private String password;
 8   private String gender;
 9   @OneToMany(cascade = CascadeType.ALL)
10   private List<Comment> comments;
11   @OneToMany(cascade = CascadeType.ALL)
12   private List<Post> posts;
13   // ………………
14 }
没有什么可说的Q只说两?br /> 1.@GeneratedValue(strategy = GenerationType.SEQUENCE)q是说ؓid自动生成一个序列,q个选项湖自动在数据库中创徏一个用来生成序列的表,一共有四种
  • GeneratorType.AUTO: The default. Assign the field a generated value, leaving the details to the JPA vendor.

  • GenerationType.IDENTITY: The database will assign an identity value on insert.

  • GenerationType.SEQUENCE: Use a datastore sequence to generate a field value.

  • GenerationType.TABLE: Use a sequence table to generate a field value

q事Apache官方文档说的Q?英文不好我就不翻译了Q应该说的很明白Q也很容易懂
GenerationValueq有另外一个属性generatorq个我只在AUTO下实验了Q他有下面几U:
  • uuid-string: OpenJPA will generate a 128-bit type 1 UUID unique within the network, represented as a 16-character string. For more information on UUIDs, see the IETF UUID draft specification at: http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/

  • uuid-hex: Same as uuid-string, but represents the type 1 UUID as a 32-character hexadecimal string.

  • uuid-type4-string: OpenJPA will generate a 128-bit type 4 pseudo-random UUID, represented as a 16-character string. For more information on UUIDs, see the IETF UUID draft specification at: http://www1.ics.uci.edu/~ejw/authoring/uuid-guid/

  • uuid-type4-hex: Same as uuid-type4-string , but represents the type 4 UUID as a 32-character hexadecimal string.


2.@OneToMany(cascade = CascadeType.ALL),U联Q我q里是所有,U联也是?中(q是考的官方文Q:
  • CascadeType.PERSIST: When persisting an entity, also persist the entities held in this field. We suggest liberal application of this cascade rule, because if the EntityManager finds a field that references a new entity during flush, and the field does not use CascadeType.PERSIST, it is an error.

  • CascadeType.REMOVE: When deleting an entity, also delete the entities held in this field.

  • CascadeType.REFRESH: When refreshing an entity, also refresh the entities held in this field.

  • CascadeType.MERGE: When merging entity state, also merge the entities held in this field.

具体的还是要看官Ҏ(gu),说的很详l?br /> q里也可以用其中的几种Q?U或着多重Q例如:@ManyToOne(cascade= {CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH,CascadeType.MERGE})
下来是Post和Comment的关键代码:
 1 @Entity
 2 public class Post {
 3   @Id
 4   @GeneratedValue(strategy = GenerationType.SEQUENCE)
 5   private int id;
 6   private Date createDate;
 7   private String title;
 8   private String content;
 9   @ManyToOne(cascade = CascadeType.ALL)
10   private User author;
11   @OneToMany(cascade = CascadeType.ALL)
12   private List<Comment> comments;
13   // ………………
14 }
15 
16 @Entity
17 public class Comment {
18   @Id
19   @GeneratedValue(strategy = GenerationType.SEQUENCE)
20   private int id;
21   private Date createDate;
22   private String title;
23   private String content;
24   @ManyToOne(cascade = CascadeType.ALL)
25   private User author;
26   @ManyToOne(cascade = CascadeType.ALL)
27   private Post post;
28   // ………………
29 }
下来的应该没有什么可说的Q下面是我的试代码Q?br />
 1  EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
 2 EntityManager em = factory.createEntityManager();
 3 em.getTransaction().begin();
 4 User user = new User();
 5 user.setName("Innate");
 6 user.setPassword("Innate");
 7 user.setGender("?/span>");
 8 List<Post> posts = new ArrayList<Post>();
 9 
10 for (int i = 0; i < 5; i++) {
11   Post post = new Post();
12   post.setAuthor(user);
13   post.setContent("test");
14   post.setCreateDate(new Date());
15   post.setTitle("Innate");
16   posts.add(post);
17   List<Comment> comments = new ArrayList<Comment>();
18   for (int j = 0; j < 5; j++) {
19     Comment comment = new Comment();
20     comment.setAuthor(user);
21     comment.setContent("testtest");
22     comment.setCreateDate(new Date());
23     comment.setPost(post);
24     comment.setTitle("InnateInnate");
25     comments.add(comment);
26   }
27   post.setComments(comments);
28   user.setComments(comments);
29 }
30 user.setPosts(posts);
31 em.persist(user);
32 em.getTransaction().commit();
源代码(包含1Q?的):JTATest.7z


天独 2010-04-06 22:24 发表评论
]]>
JPA入门-1http://www.tkk7.com/kick191/articles/JPA1.html天独天独Sat, 03 Apr 2010 08:01:00 GMThttp://www.tkk7.com/kick191/articles/JPA1.htmlhttp://www.tkk7.com/kick191/comments/317394.htmlhttp://www.tkk7.com/kick191/articles/JPA1.html#Feedback0http://www.tkk7.com/kick191/comments/commentRss/317394.htmlhttp://www.tkk7.com/kick191/services/trackbacks/317394.html persistence.xml文g的配|?br />
 1 <persistence-unit name="test">
 2   <class>cn.is.test.Test</class>
 3 
 4   <properties>
 5     <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
 6     <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
 7     <property name="openjpa.ConnectionUserName" value="root" />
 8     <property name="openjpa.ConnectionPassword" value="root" />
 9   </properties>
10 </persistence-unit>

q里有个问题Q我一般用mysql驱动的org.gjt.mm.mysql.Driverq个驱动c,但是在这里,q行的时候报驱动cL着q接URL错误


下面的是实体c?br />
 1 package cn.is.test;
 2 
 3 import java.io.Serializable;
 4 import javax.persistence.Entity;
 5 import javax.persistence.Id;
 6 
 7 @Entity
 8 public class Test implements Serializable {
 9   @Id
10   private String id;
11 
12   private String userName;
13 
14   private String password;
15 
16   private String sex;
17 
18   private static final long serialVersionUID = 1L;
19 
20   public Test() {
21     super();
22   }
23 
24   public String getId() {
25     return this.id;
26   }
27 
28   public void setId(String id) {
29     this.id = id;
30   }
31 
32   public String getUserName() {
33     return this.userName;
34   }
35 
36   public void setUserName(String username) {
37     this.userName = username;
38   }
39 
40   public String getPassword() {
41     return this.password;
42   }
43 
44   public void setPassword(String password) {
45     this.password = password;
46   }
47 
48   public String getSex() {
49     return this.sex;
50   }
51 
52   public void setSex(String sex) {
53     this.sex = sex;
54   }
55 
56 
57 
58 }
59 

下来的是试代码Q?br />
 1 EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
 2 EntityManager em = factory.createEntityManager();
 3 Test test = new Test();
 4 test.setId(UUID.randomUUID().toString());
 5 test.setSex("?/span>");
 6 test.setUserName("Innate");
 7 test.setPassword("solitary");
 8 
 9 em.getTransaction().begin();
10 
11 em.persist(test);
12 
13 em.getTransaction().commit();
14 
15 em.close();
16 
17 em = factory.createEntityManager();
18 
19 Query query = em.createQuery("select t from Test t");
20 List<Test> testList = query.getResultList();
21 
22 for (Test _test : testList) {
23   System.out.println(_test.getUserName());
24 }
25 
26 em.close();
27 factory.close();



天独 2010-04-03 16:01 发表评论
]]>
վ֩ģ壺 ٸ17p| Ƭ߹ۿ| av볱߹ۿ | žŮվ| ޵һĻ| +ɫ++| 91ۺ| 19ѹۿ| ҳַȫѹۿ12վ| AV˾þԭ| Ʒϵ߹ۿ| ޾ƷһƷƷƷ| þ99ȾƷѹۿţţ| ӰԺ߲| պѹۿƵ| պaëƬѹۿ| Ʒ޾Ʒ| һëƬ| ޾Ʒ| Ů޾Ʒþþۺ| 95ѹۿƵ| ۺϼ¶| ޳AƬ߹ۿ3D| aëƬѲ| ޽Сɫxxxx| Ƶ߾Ʒ| hƵ߹ۿ| ޾Ʒŷ߹ۿ| AVרAëƬ| 114һëƬ| Ƶѹۿ| þ޾Ʒվ| ߹ۿƵ| ȫƵ߹ۿ| ɫվWWW| ޾Ʒ߹ۿ㶮| Ʒһʽֱ | Ƶһ| ޹91Ʒ| ޾ƷƬ߹ۿ| ëƬ߲|