python https://www.e-learn.cn/tag/python zh-hans python open() - access denied https://www.e-learn.cn/topic/4120842 <span>python open() - access denied</span> <span><span lang="" about="/user/147" typeof="schema:Person" property="schema:name" datatype="">浪子不回头ぞ</span></span> <span>2021-02-20 19:36:55</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>This is my first post on stackoverflow, so if somethings wrong I'm eager to learn!</p> <p>I'm programming a plugin for Cinema 4d with python. Everything works on Mac, but I'm having problems on Windows with what I'm about to explain. </p> <p>The plugin needs a path to a server, in case of rendering on a renderserver. I want the user to enter the path once, and to then store it in a .txt file. </p> <p>for c4d, plugins are being installed, by draging-and-droping the plugin into the plugin folder at for example:</p> <p>C:/programm files/Maxon/Cinema4D/Plugins/NewPluginGoesHere</p> <p>In Order to keep everything nice and clean, I want the pathToServer.txt (text file that only stores one line with the path) in the plugin folder as well. When Installing (droping) the plugin into said folder, the .txt file is already there -&gt; Not created by the plugin.</p> <p>Here comes the problem:</p> <p>I open and write to the file with </p> <pre><code>pathFile = open(pathToPathFile, "w") pathFile.write(pathToServerFolder) </code></pre> <p>Works fine on mac, but throws </p> <pre><code>IOError: [Errno 13] Permission denied: 'C:\\Program Files\\MAXON\\CINEMA 4D R14\\plugins\\MultiLayerRender\\Renderserverpath.txt' </code></pre> <p>on Windows.</p> <p>I'm pretty inexperienced with using python for such tasks (like file management).</p> <p>I then tried the following:</p> <pre><code>pathFile = subprocess.Popen(pathToPathFile, stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell = True) pathFile.communicate(input = pathToServerFolder) pathFile.stdin.close() </code></pre> <p>Which doesn't give me an error, but it also doesn't seem to do anything :/</p> <p>I read throung the python documentation of subprocess, but that realy didn't help me at all, if anything, it confused me.</p> <p>I noticed, that I also need to run my code editor in Admin Mode, in order for it to have permission to save directly into the plugin folder in said directory. It seems like files in the 'program files' folder are somehow protected. Also, this plugin will be installed on several different computers either mac or windows, and therefor changing permission on for example the programm files folder is not an option. In the end draging and droping should be all one has to do in order to install it.</p> <p>Does anyone know how to achieve that?</p> <br /><h3>回答1:</h3><br /><p>You need elevated permissions to write to Program Files on Windows, somehow your plugin does not have them. I would consider just using another directory.</p> <p>Many render solutions requires you to specify a directory somewhere, so I don't think it's a huge problem. modo 501 and 601 (and 701 presumably), for example needs a directory somewhere for data sharing. V-Ray for Maya more or less requires you to put the scene files on a shared drive in distributed mode.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/15990843/python-open-access-denied</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/file-io" hreflang="zh-hans">file-io</a></div> <div class="field--item"><a href="/tag/stdin" hreflang="zh-hans">stdin</a></div> </div> </div> Sat, 20 Feb 2021 11:36:55 +0000 浪子不回头ぞ 4120842 at https://www.e-learn.cn python open() - access denied https://www.e-learn.cn/topic/4120841 <span>python open() - access denied</span> <span><span lang="" about="/user/127" typeof="schema:Person" property="schema:name" datatype="">我怕爱的太早我们不能终老</span></span> <span>2021-02-20 19:36:13</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>This is my first post on stackoverflow, so if somethings wrong I'm eager to learn!</p> <p>I'm programming a plugin for Cinema 4d with python. Everything works on Mac, but I'm having problems on Windows with what I'm about to explain. </p> <p>The plugin needs a path to a server, in case of rendering on a renderserver. I want the user to enter the path once, and to then store it in a .txt file. </p> <p>for c4d, plugins are being installed, by draging-and-droping the plugin into the plugin folder at for example:</p> <p>C:/programm files/Maxon/Cinema4D/Plugins/NewPluginGoesHere</p> <p>In Order to keep everything nice and clean, I want the pathToServer.txt (text file that only stores one line with the path) in the plugin folder as well. When Installing (droping) the plugin into said folder, the .txt file is already there -&gt; Not created by the plugin.</p> <p>Here comes the problem:</p> <p>I open and write to the file with </p> <pre><code>pathFile = open(pathToPathFile, "w") pathFile.write(pathToServerFolder) </code></pre> <p>Works fine on mac, but throws </p> <pre><code>IOError: [Errno 13] Permission denied: 'C:\\Program Files\\MAXON\\CINEMA 4D R14\\plugins\\MultiLayerRender\\Renderserverpath.txt' </code></pre> <p>on Windows.</p> <p>I'm pretty inexperienced with using python for such tasks (like file management).</p> <p>I then tried the following:</p> <pre><code>pathFile = subprocess.Popen(pathToPathFile, stdin = subprocess.PIPE, stdout = subprocess.PIPE, shell = True) pathFile.communicate(input = pathToServerFolder) pathFile.stdin.close() </code></pre> <p>Which doesn't give me an error, but it also doesn't seem to do anything :/</p> <p>I read throung the python documentation of subprocess, but that realy didn't help me at all, if anything, it confused me.</p> <p>I noticed, that I also need to run my code editor in Admin Mode, in order for it to have permission to save directly into the plugin folder in said directory. It seems like files in the 'program files' folder are somehow protected. Also, this plugin will be installed on several different computers either mac or windows, and therefor changing permission on for example the programm files folder is not an option. In the end draging and droping should be all one has to do in order to install it.</p> <p>Does anyone know how to achieve that?</p> <br /><h3>回答1:</h3><br /><p>You need elevated permissions to write to Program Files on Windows, somehow your plugin does not have them. I would consider just using another directory.</p> <p>Many render solutions requires you to specify a directory somewhere, so I don't think it's a huge problem. modo 501 and 601 (and 701 presumably), for example needs a directory somewhere for data sharing. V-Ray for Maya more or less requires you to put the scene files on a shared drive in distributed mode.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/15990843/python-open-access-denied</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/file-io" hreflang="zh-hans">file-io</a></div> <div class="field--item"><a href="/tag/stdin" hreflang="zh-hans">stdin</a></div> </div> </div> Sat, 20 Feb 2021 11:36:13 +0000 我怕爱的太早我们不能终老 4120841 at https://www.e-learn.cn how do I dynamically resize a pixbuf cellrenderer in a treeview? https://www.e-learn.cn/topic/4120837 <span>how do I dynamically resize a pixbuf cellrenderer in a treeview?</span> <span><span lang="" about="/user/191" typeof="schema:Person" property="schema:name" datatype="">末鹿安然</span></span> <span>2021-02-20 19:33:22</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p><img alt="pic" class="b-lazy" data-src="https://i.imgur.com/RszhVAm.png" data-original="https://i.imgur.com/RszhVAm.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></p> <p>I'm using a Gtk3 TreeView that looks like above. The model is a Gtk.TreeStore</p> <ul><li>Gtk.TreeStore(str, GdkPixbuf.Pixbuf)</li> </ul><p>For the picture I could add to the model the correctly sized images via:</p> <ul><li>pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR)</li> </ul><p>However I'm also using the model elsewhere showing the pixbufs in a different manner and the pixbufs can also be a variety of sizes.</p> <p>What I would like to do is force the size of the picture displayed at run-time. The question is - how do I do that?</p> <p>I tried forcing the GtkCellRendererPixbuf to be a fixed size, but this just displays the correct size image - but only the portion of the image corresponding to the fixed-size</p> <pre><code>pixbuf = Gtk.CellRendererPixbuf() pixbuf.set_fixed_size(48,48) </code></pre> <p>I thought of using the <code>set_cell_data_func</code> of the TreeViewColumn:</p> <pre><code>col = Gtk.TreeViewColumn('', pixbuf, pixbuf=1) col.set_cell_data_func(pixbuf, self._pixbuf_func, None) def _pixbuf_func(self, col, cell, tree_model, tree_iter, data): cell.props.pixbuf = cell.props.pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR) </code></pre> <p>This does do the dynamic resizing of the image at runtime - BUT in the terminal I get hundreds of errors such as this:</p> <pre><code>sys:1: RuntimeWarning: Expecting to marshal a borrowed reference for &lt;Pixbuf object at 0x80acbe0 (GdkPixbuf at 0x87926d0)&gt;, but nothing in Python is holding a reference to this object. See: https://bugzilla.gnome.org/show_bug.cgi?id=687522 </code></pre> <p>I also tried the alternative by resizing treemodel pixbuf instead of the <code>cell.props.pixbuf</code> but this also gives the same error as above.</p> <pre><code>cell.props.pixbuf = tree_model.get_value(tree_iter,1).scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR) </code></pre> <p>So obviously this is not the correct way of doing this - so any thoughts how else to approach this? Any links to example code Gtk3 based C++/Python would be most welcome.</p> <p>I'm using Gtk+3.6 / python 2.7</p> <br /><h3>回答1:</h3><br /><p>Old question, but sadly still relevant - this bug in Gtk still exists. Fortunately there's a pretty easy workaround. All you need to do is to keep a reference to the scaled pixbuf.</p> <p>I've altered the <code>_pixbuf_func</code> function so that it accepts a dictionary in which it stores the small pixbufs. This gets rid of the annoying warning message and also prevents the pixbuf from being downscaled every single time <code>_pixbuf_func</code> is called.</p> <pre><code>def pixbuf_func(col, cell, tree_model, tree_iter, data): pixbuf= tree_model[tree_iter][1] # get the original pixbuf from the TreeStore. [1] is # the index of the pixbuf in the TreeStore. try: new_pixbuf= data[pixbuf] # if a downscaled pixbuf already exists, use it except KeyError: new_pixbuf= pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR) data[pixbuf]= new_pixbuf # keep a reference to this pixbuf to prevent Gtk warning # messages cell.set_property('pixbuf', new_pixbuf) renderer = Gtk.CellRendererPixbuf() col = Gtk.TreeViewColumn('', renderer) col.set_cell_data_func(renderer, pixbuf_func, {}) # pass a dict to keep references in </code></pre> <p>A problem with this solution is that you have to remove the stored Pixbufs from the dict whenever the contents of your TreeStore change, otherwise they'll stick around forever and your program will consume more memory than necessary.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/19778003/how-do-i-dynamically-resize-a-pixbuf-cellrenderer-in-a-treeview</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/python-27" hreflang="zh-hans">python-2.7</a></div> <div class="field--item"><a href="/tag/gtk" hreflang="zh-hans">gtk</a></div> <div class="field--item"><a href="/tag/gtk3" hreflang="zh-hans">gtk3</a></div> </div> </div> Sat, 20 Feb 2021 11:33:22 +0000 末鹿安然 4120837 at https://www.e-learn.cn Why is my superclass calling my subclass method? https://www.e-learn.cn/topic/4120836 <span>Why is my superclass calling my subclass method?</span> <span><span lang="" about="/user/39" typeof="schema:Person" property="schema:name" datatype=""> ̄綄美尐妖づ</span></span> <span>2021-02-20 19:33:09</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>When I call a method that was overrided from my constructor, I am getting an error and it says that it is missing an argument (due to the subclass requiring a second argument). However, I called the method in the super(), so why doesn't it call the super class version of that method?</p> <p>This is best illustrated with a short example:</p> <pre><code>class A: def __init__(self): self.do() def do(self): print("A") class B(A): def __init__(self): super().__init__() self.do("B") def do(self, arg2): super().do() print(arg2) b = B() </code></pre> <p>Here is the error I get:</p> <pre><code>Traceback (most recent call last): File "D:/Python/Advanced/randomStuff/supersub.py", line 19, in &lt;module&gt; b = B() File "D:/Python/Advanced/randomStuff/supersub.py", line 11, in __init__ super().__init__() File "D:/Python/Advanced/randomStuff/supersub.py", line 3, in __init__ self.do() TypeError: do() missing 1 required positional argument: 'arg2' </code></pre> <p>It seems to be calling the do() method in class B, but I want to call the do() method in Class A. Ideally, the code should print:</p> <pre><code>A A B </code></pre> <p>What am I doing wrong?</p> <br /><h3>回答1:</h3><br /><p>The answer is that a base class that calls a method that is defined on itself, but also overridden by a subclass, calls the <em>overridden method on the subclass</em> not the method on the base class. For further information see calling an overridden method from base class?. See the below variant of your code and follow the logic as described above. </p> <pre><code>class A: def __init__(self): self.do() def do(self): print("do A") class B(A): def __init__(self): super().__init__() self.do() def do(self): super().do() print("do B") b = B() </code></pre> <p>Result: A B A B</p> <br /><br /><br /><h3>回答2:</h3><br /><p>This is really curious. I don't have a solution but the below appears to work as intended. It is exactly the same except for super() is called explicitly with self.<strong>class</strong> as an argument, which I thought was implicit.</p> <p>Maybe a more skilled Pythonista can build on this observation.</p> <pre><code>class A: def __init__(self): self.do() def do(self): print("A") class B(A): def __init__(self): super(self.__class__).__init__() self.do("B") def do(self, arg2): super().do() print(arg2) print('***init A***') a = A() print('***init B***') b = B() print('***A.do()***') a.do() print('***B.do()***') b.do('test') </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/49268725/why-is-my-superclass-calling-my-subclass-method</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/inheritance" hreflang="zh-hans">inheritance</a></div> <div class="field--item"><a href="/tag/overriding" hreflang="zh-hans">overriding</a></div> <div class="field--item"><a href="/tag/subclass" hreflang="zh-hans">subclass</a></div> <div class="field--item"><a href="/tag/superclass" hreflang="zh-hans">superclass</a></div> </div> </div> Sat, 20 Feb 2021 11:33:09 +0000  ̄綄美尐妖づ 4120836 at https://www.e-learn.cn Find most frequent row or mode of a matrix of vectors - Python / NumPy https://www.e-learn.cn/topic/4120835 <span>Find most frequent row or mode of a matrix of vectors - Python / NumPy</span> <span><span lang="" about="/user/78" typeof="schema:Person" property="schema:name" datatype="">懵懂的女人</span></span> <span>2021-02-20 19:32:53</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have a numpy array of shape (?,n) that represents a vector of n-dimensional vectors.</p> <p>I want to find the most frequent row. </p> <p>So far it seems that the best way is to just iterate over all the entries and store a count, but it seems obscene that numpy or scipy wouldn't have something builtin to perform this task.</p> <br /><h3>回答1:</h3><br /><p>Here's an approach using <code>NumPy views</code>, which should be pretty efficient -</p> <pre><code>def mode_rows(a): a = np.ascontiguousarray(a) void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a.shape[1:]))) _,ids, count = np.unique(a.view(void_dt).ravel(), \ return_index=1,return_counts=1) largest_count_id = ids[count.argmax()] most_frequent_row = a[largest_count_id] return most_frequent_row </code></pre> <p>Sample run -</p> <pre><code>In [45]: # Let's have a random arrayb with three rows(2,4,8) and two rows(1,7) ...: # being duplicated. Thus, the most freequent row must be 2 here. ...: a = np.random.randint(0,9,(9,5)) ...: a[4] = a[8] ...: a[2] = a[4] ...: ...: a[1] = a[7] ...: In [46]: a Out[46]: array([[8, 8, 7, 0, 7], [7, 8, 2, 6, 1], [2, 2, 5, 7, 6], [6, 5, 8, 8, 5], [2, 2, 5, 7, 6], [5, 7, 3, 6, 3], [2, 8, 7, 2, 0], [7, 8, 2, 6, 1], [2, 2, 5, 7, 6]]) In [47]: mode_rows(a) Out[47]: array([2, 2, 5, 7, 6]) </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>The numpy_indexed package (dsiclaimer: I am its author) has functionality that does exactly this, that works on any number of dimensions:</p> <pre><code>import numpy_indexed as npi row = npi.mode(arr) </code></pre> <p>Under the hood, it is like Divakar's solution in terms of algorithmics and complexity, with a few more bells and whistles; see the 'weights' and 'return_indices' kwargs.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>If you're able to use Pandas, here's one approach, which draws heavily from this answer: </p> <pre><code>import numpy as np import pandas as pd # generate sample data ncol = 5 nrow = 20000 matrix = np.random.randint(0,ncol,ncol*nrow).reshape(nrow,ncol) df = pd.DataFrame(matrix) df.head() 0 1 2 3 4 0 3 0 4 4 4 1 4 0 0 2 0 2 3 3 2 0 0 3 0 3 4 3 3 4 1 1 3 3 3 # count duplicated rows (df.groupby(df.columns.tolist()) .size() .sort_values(ascending=False)) </code></pre> <p>Output:</p> <pre><code>0 1 2 3 4 4 2 2 1 1 17 2 2 4 2 3 16 3 2 1 2 2 15 1 2 4 3 15 .. 4 1 3 0 1 1 1 2 3 0 4 1 </code></pre> <p>The most frequent row is the top row of this output. The frequency count is the rightmost column.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/43554819/find-most-frequent-row-or-mode-of-a-matrix-of-vectors-python-numpy</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/numpy" hreflang="zh-hans">numpy</a></div> <div class="field--item"><a href="/tag/scipy" hreflang="zh-hans">scipy</a></div> </div> </div> Sat, 20 Feb 2021 11:32:53 +0000 懵懂的女人 4120835 at https://www.e-learn.cn Python Datetime: All Items From Yesterday https://www.e-learn.cn/topic/4120816 <span>Python Datetime: All Items From Yesterday</span> <span><span lang="" about="/user/211" typeof="schema:Person" property="schema:name" datatype="">夙愿已清</span></span> <span>2021-02-20 19:26:26</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>In Python, if I wanted to do a check for all items from yesterday would I do something like:</p> <pre><code>from datetime import datetime, timedelta if datetime.datetime.today() - timedelta(days=2) &lt; item_to_check &lt; datetime.datetime.today(): </code></pre> <p>Would this pull all items from yesterday and is this the best way to do it?</p> <br /><h3>回答1:</h3><br /><p>I'd try something easier ;-)</p> <pre><code>from datetime import date, timedelta yesterday = date.today() - timedelta(days=1) if item_to_check.date() == yesterday: # yup! </code></pre> <p>Note that your:</p> <pre><code>item_to_check &lt; datetime.datetime.today() </code></pre> <p>is true for any item that occurred before the second you called <code>datetime.datetime.today()</code>. My <code>date.today()</code> doesn't include hours, minutes or seconds (i.e., it has no "time" component).</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/19851301/python-datetime-all-items-from-yesterday</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/datetime" hreflang="zh-hans">datetime</a></div> <div class="field--item"><a href="/tag/python-27" hreflang="zh-hans">python-2.7</a></div> </div> </div> Sat, 20 Feb 2021 11:26:26 +0000 夙愿已清 4120816 at https://www.e-learn.cn Convert a list of dictionaries into a set of dictionaries https://www.e-learn.cn/topic/4120807 <span>Convert a list of dictionaries into a set of dictionaries</span> <span><span lang="" about="/user/120" typeof="schema:Person" property="schema:name" datatype="">て烟熏妆下的殇ゞ</span></span> <span>2021-02-20 19:22:08</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>How can i make a set of dictionaries from one list of dictionaries?</p> <p>Example:</p> <pre><code>import copy v1 = {'k01': 'v01', 'k02': {'k03': 'v03', 'k04': {'k05': 'v05'}}} v2 = {'k11': 'v11', 'k12': {'k13': 'v13', 'k14': {'k15': 'v15'}}} data = [] N = 5 for i in range(N): data.append(copy.deepcopy(v1)) data.append(copy.deepcopy(v2)) print data </code></pre> <p>How would you create a set of dictionaries from the list <code>data</code>?</p> <p>NS: One dictionary is equal to another when they are structurally the same. That means, they got <strong>exactly</strong> the same keys and same values (recursively) </p> <br /><h3>回答1:</h3><br /><p>A cheap workaround would be to serialize your dicts, for example:</p> <pre><code>import json dset = set() d1 = {'a':1, 'b':{'c':2}} d2 = {'b':{'c':2}, 'a':1} # the same according to your definition d3 = {'x': 42} dset.add(json.dumps(d1, sort_keys=True)) dset.add(json.dumps(d2, sort_keys=True)) dset.add(json.dumps(d3, sort_keys=True)) for p in dset: print json.loads(p) </code></pre> <p>In the long run it would make sense to wrap the whole thing in a class like <code>SetOfDicts</code>.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Dictionaries are mutable and therefore not hashable in python.</p> <p>You could either create a dict-subclass with a <code>__hash__</code> method. Make sure that the hash of a dictionary does not change while it is in the set (that probably means that you cannot allow modifying the members). See http://code.activestate.com/recipes/414283-frozen-dictionaries/ for an example implementation of frozendicts.</p> <p>If you can define a sort order on your (frozen) dictionaries, you could alternatively use a data structure based on a binary tree instead of a set. This boils down to the bisect solution provided in the link below.</p> <p>See also https://stackoverflow.com/a/18824158/5069869 for an explanation why sets without hash do not make sense.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>not exactly what you're looking for as this accounts for lists too but:</p> <pre><code>def hashable_structure(structure): if isinstance(structure, dict): return {k: hashable_structure(v) for k, v in structure.items()} elif isinstance(structure, list): return {hashable_structure(elem) for elem in structure)} else: return structure </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/39203516/convert-a-list-of-dictionaries-into-a-set-of-dictionaries</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/dictionary" hreflang="zh-hans">dictionary</a></div> </div> </div> Sat, 20 Feb 2021 11:22:08 +0000 て烟熏妆下的殇ゞ 4120807 at https://www.e-learn.cn pip is not installing my data_files. But easy_install does https://www.e-learn.cn/topic/4120798 <span>pip is not installing my data_files. But easy_install does</span> <span><span lang="" about="/user/200" typeof="schema:Person" property="schema:name" datatype="">余生长醉</span></span> <span>2021-02-20 19:16:20</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>This is going to be a short question.</p> <p>The source is here: https://github.com/exhuma/grc</p> <p>And the title explains my problem ;)</p> <p>I registered and uploaded the package to <code>pypi</code>. Now if I run <code>easy_install grc</code>, it works as expected. But if I run <code>pip install grc</code>, it installs properly, but does not install the data_files. I don't see why this is happening. Any ideas?</p> <br /><h3>回答1:</h3><br /><p>Maybe the files aren't installed in the path you expect. In Ubuntu 11.10, I see them under <code>/usr/local/configs</code> as it can be seen in <code>pip uninstall</code> output:</p> <pre><code>$ sudo pip uninstall grc Uninstalling grc: /usr/local/bin/grc /usr/local/configs/apache_access.yml /usr/local/configs/aptitude.yml /usr/local/configs/pysetup.yml /usr/local/lib/python2.7/dist-packages/grc /usr/local/lib/python2.7/dist-packages/grc-1.0b3.egg-info Proceed (y/n)? </code></pre> <p>In your system, I guess you can check also the output of <code>pip uninstall</code> to make sure.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8471507/pip-is-not-installing-my-data-files-but-easy-install-does</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/pip" hreflang="zh-hans">pip</a></div> <div class="field--item"><a href="/tag/easy-install" hreflang="zh-hans">easy-install</a></div> <div class="field--item"><a href="/tag/setuppy" hreflang="zh-hans">setup.py</a></div> </div> </div> Sat, 20 Feb 2021 11:16:20 +0000 余生长醉 4120798 at https://www.e-learn.cn Why is smtplib.SMTP().sendmail not sending a DKIM signed message https://www.e-learn.cn/topic/4120791 <span>Why is smtplib.SMTP().sendmail not sending a DKIM signed message</span> <span><span lang="" about="/user/49" typeof="schema:Person" property="schema:name" datatype="">帅比萌擦擦*</span></span> <span>2021-02-20 19:14:41</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have set up postfix on a server, along with openDKIM.</p> <p>When I run:</p> <pre><code>echo "Testing setup" | mail -s "Postfix test" {my_email_address} </code></pre> <p>I get the email, and in the mail headers there is a <code>DKIM-Signature</code> header.</p> <p>When, however I write a python script to send an email, using smtplib:</p> <pre><code>import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.utils import make_msgid msg = MIMEMultipart('alternative') part1 = MIMEText('Hello, world', 'plain') msg.attach(part1) msg['From'] = 'alert@{my_domain}' msg['To'] = '{my_email_address}' msg['Subject'] = 'Test Email' msg['Message-ID'] = make_msgid() mailer = smtplib.SMTP('localhost') mailer.sendmail('alert@{my_domain}', '{my_email_address}', msg.as_string()) mailer.quit() </code></pre> <p>The email that arrives in my inbox is missing the <code>DKIM-Signature</code> header, and in the <code>Authentication-Results</code> I see <code>dkim=none (no signatures found);</code></p> <p>So my question is: Do I need to sign my email manually (eg. with dkimpy), or is there some setting I can enable to have it signed for me?</p> <p>Let me know if there is any extra information you want/need.</p> <br /><h3>回答1:</h3><br /><p>It's hard to say for sure since you haven't included any configuration or log information. But the OpenDKIM information has this to say:</p> <blockquote> <p>A message will be verified unless it conforms to the signing criteria, which are: (1) the domain on the From: address (if present) must be listed by the <em>−d</em> command line switch or the <em>Domain</em> configuration file setting, and (2) (a) the client connecting to the MTA must have authenticated, or (b) the client connecting to the MTA must be listed in the file referenced by the <em>InternalHosts</em> configuration file setting (or be in the default list for that option), or (c) the client must be connected to a daemon port named by the <em>MTAs</em> configuration file setting, or (d) the MTA must have set one or more macros matching the criteria set by the <em>MacroList</em> configuration file setting.</p> </blockquote> <p>In your SMTP code, you have clearly not authenticated to the MTA. So then the question becomes, does your configuration allow OpenDKIM to notice your desire in any of the other ways, such as having localhost in InternalHosts? (I think that is the default, but maybe you've overridden it.)</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/49059606/why-is-smtplib-smtp-sendmail-not-sending-a-dkim-signed-message</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/email" hreflang="zh-hans">email</a></div> <div class="field--item"><a href="/tag/smtplib" hreflang="zh-hans">smtplib</a></div> <div class="field--item"><a href="/tag/postfix" hreflang="zh-hans">postfix</a></div> <div class="field--item"><a href="/tag/dkim" hreflang="zh-hans">dkim</a></div> </div> </div> Sat, 20 Feb 2021 11:14:41 +0000 帅比萌擦擦* 4120791 at https://www.e-learn.cn Why is smtplib.SMTP().sendmail not sending a DKIM signed message https://www.e-learn.cn/topic/4120789 <span>Why is smtplib.SMTP().sendmail not sending a DKIM signed message</span> <span><span lang="" about="/user/85" typeof="schema:Person" property="schema:name" datatype="">社会主义新天地</span></span> <span>2021-02-20 19:14:30</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have set up postfix on a server, along with openDKIM.</p> <p>When I run:</p> <pre><code>echo "Testing setup" | mail -s "Postfix test" {my_email_address} </code></pre> <p>I get the email, and in the mail headers there is a <code>DKIM-Signature</code> header.</p> <p>When, however I write a python script to send an email, using smtplib:</p> <pre><code>import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.utils import make_msgid msg = MIMEMultipart('alternative') part1 = MIMEText('Hello, world', 'plain') msg.attach(part1) msg['From'] = 'alert@{my_domain}' msg['To'] = '{my_email_address}' msg['Subject'] = 'Test Email' msg['Message-ID'] = make_msgid() mailer = smtplib.SMTP('localhost') mailer.sendmail('alert@{my_domain}', '{my_email_address}', msg.as_string()) mailer.quit() </code></pre> <p>The email that arrives in my inbox is missing the <code>DKIM-Signature</code> header, and in the <code>Authentication-Results</code> I see <code>dkim=none (no signatures found);</code></p> <p>So my question is: Do I need to sign my email manually (eg. with dkimpy), or is there some setting I can enable to have it signed for me?</p> <p>Let me know if there is any extra information you want/need.</p> <br /><h3>回答1:</h3><br /><p>It's hard to say for sure since you haven't included any configuration or log information. But the OpenDKIM information has this to say:</p> <blockquote> <p>A message will be verified unless it conforms to the signing criteria, which are: (1) the domain on the From: address (if present) must be listed by the <em>−d</em> command line switch or the <em>Domain</em> configuration file setting, and (2) (a) the client connecting to the MTA must have authenticated, or (b) the client connecting to the MTA must be listed in the file referenced by the <em>InternalHosts</em> configuration file setting (or be in the default list for that option), or (c) the client must be connected to a daemon port named by the <em>MTAs</em> configuration file setting, or (d) the MTA must have set one or more macros matching the criteria set by the <em>MacroList</em> configuration file setting.</p> </blockquote> <p>In your SMTP code, you have clearly not authenticated to the MTA. So then the question becomes, does your configuration allow OpenDKIM to notice your desire in any of the other ways, such as having localhost in InternalHosts? (I think that is the default, but maybe you've overridden it.)</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/49059606/why-is-smtplib-smtp-sendmail-not-sending-a-dkim-signed-message</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/email" hreflang="zh-hans">email</a></div> <div class="field--item"><a href="/tag/smtplib" hreflang="zh-hans">smtplib</a></div> <div class="field--item"><a href="/tag/postfix" hreflang="zh-hans">postfix</a></div> <div class="field--item"><a href="/tag/dkim" hreflang="zh-hans">dkim</a></div> </div> </div> Sat, 20 Feb 2021 11:14:30 +0000 社会主义新天地 4120789 at https://www.e-learn.cn