<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8717787531963527165</id><updated>2023-09-27T12:03:15.729-07:00</updated><category term="Python"/><category term="Python 3.x"/><category term="Pandas"/><category term="Python 2.7"/><category term="Matplotlib"/><category term="Performance"/><category term="Tkinter"/><category term="Windows"/><category term="Class"/><category term="Csv"/><category term="Dataframe"/><category term="Django"/><category term="Json"/><category term="Jupyter Notebook"/><category term="Numpy"/><category term="Regex"/><category term="Selenium"/><category term="Tensorflow"/><category term=".bash Profile"/><category term="Angularjs"/><category term="Arrays"/><category term="Attributes"/><category term="Button"/><category term="Conv Neural Network"/><category term="Correlation"/><category term="Data Processing"/><category term="Decimal"/><category term="Dictionary"/><category term="Django Queryset"/><category term="Django Urls"/><category term="Dynamic"/><category term="Eve"/><category term="Expansion"/><category term="Exponential"/><category term="Feedparser"/><category term="Floating Point"/><category term="Flurry"/><category term="Flurry Analytics"/><category term="For Loop"/><category term="Git"/><category term="Gitpython"/><category term="Html"/><category term="Hyperparameters"/><category term="Import"/><category term="Indexing"/><category term="Integer"/><category term="Io"/><category term="Java"/><category term="Jpype"/><category term="Jupyter"/><category term="Jython"/><category term="Keyerror"/><category term="Large Files"/><category term="List"/><category term="Long Integer"/><category term="Loops"/><category term="Macos"/><category term="Magic Methods"/><category term="Memory"/><category term="Module"/><category term="Multiprocessing"/><category term="Multithreading"/><category term="Mutex"/><category term="Mysql"/><category term="Oauth 2.0"/><category term="Object Detection Api"/><category term="Ocr"/><category term="Office365api"/><category term="Onedrive"/><category term="Opencv"/><category term="Optimization"/><category term="Osx Lion"/><category term="Path"/><category term="Post"/><category term="Profiling"/><category term="Py4j"/><category term="Pyinstaller"/><category term="Pyserial"/><category term="Python 3.4"/><category term="Python 3.6"/><category term="Python Idle"/><category term="Python Multithreading"/><category term="Python Requests"/><category term="Pyusb"/><category term="Rest"/><category term="Scalar"/><category term="Seaborn"/><category term="Select"/><category term="Selenium Webdriver"/><category term="Semaphore"/><category term="Series"/><category term="Spacy"/><category term="Sqlalchemy"/><category term="Sqlite"/><category term="Sublimerepl"/><category term="Sublimetext2"/><category term="Syntactic Sugar"/><category term="Tesseract"/><category term="Tkinter Entry"/><category term="Unicode"/><category term="Usb"/><category term="While Loop"/><category term="Wiki"/><title type='text'>100inoneyear</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default?start-index=26&amp;max-results=25'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>56</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-7582798403740160904</id><published>2023-09-27T12:02:00.001-07:00</published><updated>2023-09-27T12:02:44.511-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Matplotlib"/><category scheme="http://www.blogger.com/atom/ns#" term="Osx Lion"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Installing (build) Matplotlib In Mac Osx Lion</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I installed pynum and scipy (on osx Lion with python 2.7), but when I tried to build matplotlib  git clone https://github.com/matplotlib/matplotlib.git cd matplotlib python setup.p&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I always find this process more painful than it should be, but I&#39;ve done it a few times now and I believe that these steps should get you set up:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Get Xcode 4.3.2, it&#39;s required for some of the later steps.&lt;/li&gt;&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://python.org/download/&quot;&gt;Download&lt;/a&gt; the latest version of python for OSX from python.org&lt;/li&gt;&lt;li&gt;Grab the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://fonnesbeck.github.com/ScipySuperpack/&quot;&gt;Scipy superpack&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Uninstall any previous versions of numpy/matplotlib/scipy that you currently have. That includes doing &lt;code&gt;cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/&lt;/code&gt; and moving any numpy/matplotlib/scipy directories or eggs into a temp directory.&lt;/li&gt;&lt;li&gt;&lt;code&gt;cd ~/Downloads&lt;/code&gt;(or wherever you downloaded the superpack script to) and run &lt;code&gt;sh install_superpack.sh&lt;/code&gt;. &lt;strong&gt;Answer no&lt;/strong&gt; to the question &lt;code&gt;are you installing from a repository cloned to this machine&lt;/code&gt; or you&#39;ll be confused about why the script keeps failing.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;That should be it! You should now be able to boot up the python console and &lt;code&gt;import numpy, scipy, matplotlib&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/7582798403740160904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/installing-build-matplotlib-in-mac-osx.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/7582798403740160904'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/7582798403740160904'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/installing-build-matplotlib-in-mac-osx.html' title='Installing (build) Matplotlib In Mac Osx Lion'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-4785763545647310215</id><published>2023-09-27T11:08:00.001-07:00</published><updated>2023-09-27T11:08:59.033-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Feedparser"/><category scheme="http://www.blogger.com/atom/ns#" term="Keyerror"/><category scheme="http://www.blogger.com/atom/ns#" term="Pyserial"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Feedparser - Keyerror: &#39;fullcount&#39;</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have tried to follow this guide. It is about making a physical gmail notifier. When I entered the same code it found an error: Traceback (most recent call last):   File &#39;C:/Pytho&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Just took a look at that from a REPL. The code will work as-is for me. However, I was able to reproduce your error by entering an incorrect password.&lt;/p&gt;&lt;p&gt;This is what &lt;code&gt;feedparser.parse()[&#39;feed&#39;]&lt;/code&gt; looks like if you fail to authenticate:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;feedparser.parse(PROTO + USERNAME + &lt;span class=&quot;hljs-string&quot;&gt;&quot;:&quot;&lt;/span&gt; + INCORRECT_PASSWORD + &lt;span class=&quot;hljs-string&quot;&gt;&quot;@&quot;&lt;/span&gt; + SERVER + PATH)[&lt;span class=&quot;hljs-string&quot;&gt;&#39;feed&#39;&lt;/span&gt;]
{&lt;span class=&quot;hljs-string&quot;&gt;&#39;summary&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;&amp;lt;h1&amp;gt;Unauthorized&amp;lt;/h1&amp;gt;\n&amp;lt;h2&amp;gt;Error 401&amp;lt;/h2&amp;gt;&#39;&lt;/span&gt;}
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This is what it should look like if you do authenticate properly:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; pprint
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;pprint.pprint(feedparser.parse(PROTO + USERNAME + &lt;span class=&quot;hljs-string&quot;&gt;&quot;:&quot;&lt;/span&gt; + PASSWORD + &lt;span class=&quot;hljs-string&quot;&gt;&quot;@&quot;&lt;/span&gt; + SERVER + PATH)[&lt;span class=&quot;hljs-string&quot;&gt;&#39;feed&#39;&lt;/span&gt;])
{&lt;span class=&quot;hljs-string&quot;&gt;&#39;fullcount&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;0&#39;&lt;/span&gt;,
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;link&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;http://mail.google.com/mail&#39;&lt;/span&gt;,
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;links&#39;&lt;/span&gt;: [{&lt;span class=&quot;hljs-string&quot;&gt;&#39;href&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;http://mail.google.com/mail&#39;&lt;/span&gt;,
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;rel&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;alternate&#39;&lt;/span&gt;,
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;type&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;text/html&#39;&lt;/span&gt;}],
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;subtitle&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;New messages in your Gmail Inbox&#39;&lt;/span&gt;,
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;subtitle_detail&#39;&lt;/span&gt;: {&lt;span class=&quot;hljs-string&quot;&gt;&#39;base&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;https://mail.google.com/mail/feed/atom&#39;&lt;/span&gt;,
                     &lt;span class=&quot;hljs-string&quot;&gt;&#39;language&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;,
                     &lt;span class=&quot;hljs-string&quot;&gt;&#39;type&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;text/plain&#39;&lt;/span&gt;,
                     &lt;span class=&quot;hljs-string&quot;&gt;&#39;value&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;New messages in your Gmail Inbox&#39;&lt;/span&gt;},
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;title&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;Gmail - Inbox for xxxxxxx@gmail.com&#39;&lt;/span&gt;,
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;title_detail&#39;&lt;/span&gt;: {&lt;span class=&quot;hljs-string&quot;&gt;&#39;base&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;https://mail.google.com/mail/feed/atom&#39;&lt;/span&gt;,
                  &lt;span class=&quot;hljs-string&quot;&gt;&#39;language&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;,
                  &lt;span class=&quot;hljs-string&quot;&gt;&#39;type&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;text/plain&#39;&lt;/span&gt;,
                  &lt;span class=&quot;hljs-string&quot;&gt;&#39;value&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;Gmail - Inbox for xxxxxxx@gmail.com&#39;&lt;/span&gt;},
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;updated&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;u&#39;2013-03-01T20:11:03Z&#39;&lt;/span&gt;,
 &lt;span class=&quot;hljs-string&quot;&gt;&#39;updated_parsed&#39;&lt;/span&gt;: time.struct_time(tm_year=&lt;span class=&quot;hljs-number&quot;&gt;2013&lt;/span&gt;, tm_mon=&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;, tm_mday=&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, tm_hour=&lt;span class=&quot;hljs-number&quot;&gt;20&lt;/span&gt;, tm_min=&lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;, tm_sec=&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;, tm_wday=&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;, tm_yday=&lt;span class=&quot;hljs-number&quot;&gt;60&lt;/span&gt;, tm_isdst=&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)}
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You should print out the result of &lt;code&gt;feedparser.parse()&lt;/code&gt; to check if this is indeed the problem you have, but I suspect that simply making sure your username/password are correct will fix your problem&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/4785763545647310215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/feedparser-keyerror-fullcount.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/4785763545647310215'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/4785763545647310215'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/feedparser-keyerror-fullcount.html' title='Feedparser - Keyerror: &#39;fullcount&#39;'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-9022507379414357437</id><published>2023-09-27T10:09:00.001-07:00</published><updated>2023-09-27T10:09:57.629-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Import"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Ipython Importerror: No Module Named Display</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Trying to run python script but can&#39;t find module IPython.display.. Seems like it would be something simple but I can&#39;t find an answer anywhere! /vagrant/dreamify.py in &lt;module&amp;&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;According to the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://github.com/ipython/ipython&quot;&gt;IPython&#39;s repository&lt;/a&gt;, the &lt;code&gt;IPython.display&lt;/code&gt; module was added in commit &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://github.com/ipython/ipython/commit/5462c7efd6027ae36a2c89e369e82ba6d4eb76bf&quot;&gt;5462c7e&lt;/a&gt;, and it was released with IPython 0.13.&lt;/p&gt;&lt;p&gt;I think you have an older version of IPython.&lt;/p&gt;&lt;p&gt;You can check the IPython version with &lt;code&gt;ipython --version&lt;/code&gt;.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;It can be upgraded with &lt;code&gt;pip install --upgrade IPython&lt;/code&gt;. &lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;In Pycharm import module ipython. In Pycharm go to preferences and then project interpreter. Click on the &quot;+&quot; sign then search ipython and install package.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/9022507379414357437/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/ipython-importerror-no-module-named.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/9022507379414357437'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/9022507379414357437'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/ipython-importerror-no-module-named.html' title='Ipython Importerror: No Module Named Display'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-23950849043989714</id><published>2023-09-27T09:07:00.001-07:00</published><updated>2023-09-27T09:07:17.460-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Button"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tkinter"/><title type='text'>Tkinter Button Not Changing Global Variable</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have started learning python gui.Button function call doesn&#39;t update the global variable. Currently I am facing problem in below code. from tkinter import *  root=Tk()  root.titl&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Try this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; tkinter import *
root=Tk()  
root.title(&lt;span class=&quot;hljs-string&quot;&gt;&quot;learning&quot;&lt;/span&gt;)    
x=&lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;def &lt;span class=&quot;hljs-title&quot;&gt;change&lt;/span&gt;() :   
    &lt;span class=&quot;hljs-keyword&quot;&gt;global&lt;/span&gt; x
    x&lt;/span&gt;=&lt;span class=&quot;hljs-string&quot;&gt;&quot;program&quot;&lt;/span&gt;
    print(x)

&lt;span class=&quot;hljs-function&quot;&gt;def &lt;span class=&quot;hljs-title&quot;&gt;prt&lt;/span&gt;():
    &lt;span class=&quot;hljs-keyword&quot;&gt;global&lt;/span&gt; x
    &lt;span class=&quot;hljs-title&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;x&lt;/span&gt;)

&lt;span class=&quot;hljs-title&quot;&gt;Button&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;root,text=&lt;span class=&quot;hljs-string&quot;&gt;&quot;Click me&quot;&lt;/span&gt;, command=change&lt;/span&gt;).&lt;span class=&quot;hljs-title&quot;&gt;pack&lt;/span&gt;()
&lt;span class=&quot;hljs-title&quot;&gt;Button&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;root,text=&lt;span class=&quot;hljs-string&quot;&gt;&quot;Print x&quot;&lt;/span&gt;, command=prt&lt;/span&gt;).&lt;span class=&quot;hljs-title&quot;&gt;pack&lt;/span&gt;()

root.&lt;span class=&quot;hljs-title&quot;&gt;mainloop&lt;/span&gt;()     
&lt;span class=&quot;hljs-title&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;x&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Try to do this:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Click &#39;Print x&#39; first - This should print an empty space&lt;/li&gt;&lt;li&gt;Then click on &lt;code&gt;Click Me&lt;/code&gt; - &lt;code&gt;x&lt;/code&gt; changes to &lt;em&gt;program&lt;/em&gt;&lt;/li&gt;&lt;li&gt;And then again click on &#39;Print x&#39; - this should print &lt;em&gt;program&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Another error - &lt;code&gt;button1&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt; as you have used &lt;code&gt;.pack&lt;/code&gt; on the same line. So if you want to use &lt;code&gt;button1&lt;/code&gt; in your code ahead, use it as:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-bash&quot;&gt;button1 = Button(root, text=&lt;span class=&quot;hljs-string&quot;&gt;&#39;Click me&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;command&lt;/span&gt;=change)
button1.pack()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Try this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_8&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; tkinter &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; *
    
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;change&lt;/span&gt;():
        &lt;span class=&quot;hljs-keyword&quot;&gt;global&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;=&lt;span class=&quot;hljs-string&quot;&gt;&quot;program&quot;&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;) &lt;span class=&quot;hljs-comment&quot;&gt;# print function should be placed here&lt;/span&gt;
    
    root=Tk()
    root.title(&lt;span class=&quot;hljs-string&quot;&gt;&quot;learning&quot;&lt;/span&gt;)
    root.geometry(&lt;span class=&quot;hljs-string&quot;&gt;&quot;300x300&quot;&lt;/span&gt;)
    &lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;=&lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;/span&gt;
    button1=Button(root,text=&lt;span class=&quot;hljs-string&quot;&gt;&quot;Click me&quot;&lt;/span&gt;, command=change).pack()
    root.mainloop()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_8&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;when the button is clicked, immediately the function change() will be called so, the print function should be placed in change()&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/23950849043989714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tkinter-button-not-changing-global.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/23950849043989714'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/23950849043989714'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tkinter-button-not-changing-global.html' title='Tkinter Button Not Changing Global Variable'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2314561206632304377</id><published>2023-09-27T06:11:00.001-07:00</published><updated>2023-09-27T06:11:51.426-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Io"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Python: When Writing To A Large File, Keep The File Open Or To Open It And Append To It As Needed?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I am wondering how to best handle writing to a big file in python.  My python code loops many times running an external program (ancient Fortran with a weird input file format), re&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Opening and closing the files definitely has a cost. However if your legacy program takes one or more second to respond you propably won&#39;t notice. &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;func1&lt;/span&gt;():
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;):
        x = &lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;(x)
        &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;test1.txt&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;a&quot;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; k:
            k.write(x)

&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; loops, best of &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;2.47&lt;/span&gt; s per loop

&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;func2&lt;/span&gt;():
    &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;test2.txt&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;a&quot;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; k:
        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;):
            x = &lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;(x)
            k.write(x)

&lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt; loops, best of &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;6.66&lt;/span&gt; ms per loop
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;However if your file get&#39;s really big it becomes slower: (800+mb)&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;func3&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;file&lt;/span&gt;):
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;):
        x = &lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;(x)
        &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;open&lt;/span&gt;(file, &lt;span class=&quot;hljs-string&quot;&gt;&quot;a&quot;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; k:
            k.write(x)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;12kb file:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-rust&quot;&gt;&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt; loops, best of &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;33.4&lt;/span&gt; ms per &lt;span class=&quot;hljs-keyword&quot;&gt;loop&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;800mb+ file:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-rust&quot;&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; loops, best of &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;24.5&lt;/span&gt; s per &lt;span class=&quot;hljs-keyword&quot;&gt;loop&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Keeping the file open will mainly cost you memory.&lt;/p&gt;&lt;p&gt;I would suggest using SQlite to store your data.&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2314561206632304377/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-when-writing-to-large-file-keep.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2314561206632304377'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2314561206632304377'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-when-writing-to-large-file-keep.html' title='Python: When Writing To A Large File, Keep The File Open Or To Open It And Append To It As Needed?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-6919958235877917679</id><published>2023-09-27T04:48:00.001-07:00</published><updated>2023-09-27T04:48:34.160-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Processing"/><category scheme="http://www.blogger.com/atom/ns#" term="Html"/><category scheme="http://www.blogger.com/atom/ns#" term="Pandas"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Render Pandas Dataframe To Html Table With Row Seperators</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I render a pandas multi-index into an html table.  When rendering, after I passed a specific index X I want to add a separator line into the table. I can do this manually by editin&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can &lt;code&gt;groupby&lt;/code&gt; index and loop through it while adding the separator:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;df&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-string&quot;&gt;&quot;col1&quot;&lt;/span&gt;:list(&lt;span class=&quot;hljs-string&quot;&gt;&quot;AABBCCA&quot;&lt;/span&gt;),&lt;span class=&quot;hljs-string&quot;&gt;&quot;unit&quot;&lt;/span&gt;:[1,2,1,3,4,4,6]})

&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; _, data &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; df.groupby(&lt;span class=&quot;hljs-string&quot;&gt;&quot;col1&quot;&lt;/span&gt;):
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt; (data.to_html())
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt; (&lt;span class=&quot;hljs-string&quot;&gt;&#39;&amp;lt;td colspan=&quot;20&quot; class=&quot;divider&quot;&amp;gt;&amp;lt;hr /&amp;gt;&amp;lt;/td&amp;gt;&#39;&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Result:&lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/3VJfD.png&quot;&gt;&lt;img alt=&quot;enter image description here&quot; src=&quot;https://i.stack.imgur.com/3VJfD.png&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/6919958235877917679/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/render-pandas-dataframe-to-html-table.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6919958235877917679'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6919958235877917679'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/render-pandas-dataframe-to-html-table.html' title='Render Pandas Dataframe To Html Table With Row Seperators'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-5602716066618711972</id><published>2023-09-27T03:47:00.001-07:00</published><updated>2023-09-27T03:47:02.894-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Combining Lists With Overlapping Elements</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a collection of lists, some of which have overlapping elements: coll = [[&#39;aaaa&#39;, &#39;aaab&#39;, &#39;abaa&#39;],         [&#39;bbbb&#39;, &#39;bbbb&#39;],          [&#39;aaaa&#39;, &#39;bbbb&#39;],          [&#39;dddd&#39;, &#39;ddd&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Here&#39;s a way to do it (assuming you want unique elements over the overlapping results):&lt;/p&gt;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;coll&lt;/span&gt;):
     &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Input is:\n&#39;&lt;/span&gt;, coll)
     &lt;span class=&quot;hljs-comment&quot;&gt;# gather the lists that do overlap &lt;/span&gt;
     overlapping = [x &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; coll &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;any&lt;/span&gt;(x_element &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; [y &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; coll &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; k != x &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; y &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; k] &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x_element &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; x)] 
     &lt;span class=&quot;hljs-comment&quot;&gt;# flatten and get unique &lt;/span&gt;
     overlapping = &lt;span class=&quot;hljs-built_in&quot;&gt;sorted&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;([z &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; overlapping &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; z &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; x]))) 
     &lt;span class=&quot;hljs-comment&quot;&gt;# get the rest&lt;/span&gt;
     non_overlapping = [x &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; x &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; coll &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;all&lt;/span&gt;(y &lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; overlapping &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; y &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; x)] 
     &lt;span class=&quot;hljs-comment&quot;&gt;# use the line bellow only if merged non-overlapping elements are desired&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# non_overlapping = sorted([y for x in non_overlapping for y in x]) &lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Output is&quot;\n&#39;&lt;/span&gt;,[overlapping, non_overlapping])

coll = [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;]]
over(coll)
coll = [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;]]
over(coll)

&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;output:&lt;/p&gt;&lt;pre class=&quot;lang-sh prettyprint-override&quot;&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-bash&quot;&gt;$ python3 over.py                                                                                                                                                              -- NORMAL --
Input is:
 [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;]]
Output is&lt;span class=&quot;hljs-string&quot;&gt;&quot;
 [[&#39;aaaa&#39;, &#39;aaab&#39;, &#39;abaa&#39;, &#39;bbbb&#39;, &#39;cccc&#39;], [[&#39;dddd&#39;, &#39;dddd&#39;]]]
Input is:
 [[&#39;aaaa&#39;, &#39;aaaa&#39;], [&#39;bbbb&#39;, &#39;bbbb&#39;]]
Output is&quot;&lt;/span&gt;
 [[], [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;], [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;]]]


&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can do this with sets using a successive merging approach:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-csharp&quot;&gt;coll = [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;eeee&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;eeef&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggi&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;hhhh&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;iiii&#39;&lt;/span&gt;]]

pooled = [&lt;span class=&quot;hljs-keyword&quot;&gt;set&lt;/span&gt;(subList) &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; subList &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; coll]
merging = True
&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;/span&gt; merging:
    merging=False
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i,&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;enumerate&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;pooled&lt;/span&gt;):
        merged&lt;/span&gt; = next((g &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; g &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; pooled[i+&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;:] &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; g.intersection(&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;)),None)
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt; merged: &lt;span class=&quot;hljs-keyword&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;.update(merged)
        pooled.&lt;span class=&quot;hljs-keyword&quot;&gt;remove&lt;/span&gt;(merged)
        merging = &lt;span class=&quot;hljs-function&quot;&gt;True

&lt;span class=&quot;hljs-title&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;pooled&lt;/span&gt;)
# [&lt;/span&gt;{&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;}, {&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;}, {&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;eeef&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;eeee&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;hhhh&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;gggi&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;iiii&#39;&lt;/span&gt;}]
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Working on a suggestion from &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://stackoverflow.com/users/5087436/alkasm&quot;&gt;alkasm&lt;/a&gt; in the comments, I used networkx:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; networkx &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; nx

coll = [[&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;], 
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;eeee&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;eeef&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggi&#39;&lt;/span&gt;],
        [&lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;hhhh&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;iiii&#39;&lt;/span&gt;]]

edges = []
&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(coll)):
    a = coll[i]
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; j &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(coll)):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; i != j:
            b = coll[j]
            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(a).intersection(&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(b)):
                edges.append((i,j))

G = nx.Graph()
G.add_nodes_from(&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(coll)))
G.add_edges_from(edges)

&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; c &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; nx.connected_components(G):
    combined_lists = [coll[i] &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; c]
    flat_list = [item &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; sublist &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; combined_lists &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; item &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; sublist]
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(flat_list))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Output:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-bash&quot;&gt;{&lt;span class=&quot;hljs-string&quot;&gt;&#39;cccc&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;bbbb&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaab&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;aaaa&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abaa&#39;&lt;/span&gt;}
{&lt;span class=&quot;hljs-string&quot;&gt;&#39;dddd&#39;&lt;/span&gt;}
{&lt;span class=&quot;hljs-string&quot;&gt;&#39;eeef&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;eeee&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;hhhh&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;gggg&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;gggi&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;iiii&#39;&lt;/span&gt;}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Undoubtedly this can be optimized but it seems to solve my problem for now.&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/5602716066618711972/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/combining-lists-with-overlapping.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5602716066618711972'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5602716066618711972'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/combining-lists-with-overlapping.html' title='Combining Lists With Overlapping Elements'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-3857127359371482206</id><published>2023-09-26T23:49:00.001-07:00</published><updated>2023-09-26T23:49:26.813-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 2.7"/><title type='text'>How To Use Python Map Function To Generate The First 100 Powers Of Two Starting At 0 And Exclusive Of 100?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;def map_twos():     def pwr(x):         return 2**x      map(pwr, range(0, 100))     print pwr.get[2]     return pwr  What is wrong. I don&#39;t know how do I use map function to get t&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&lt;code&gt;pwr&lt;/code&gt; is a function. Even after you map it over a list, &lt;code&gt;pwr&lt;/code&gt; is still a function. &lt;code&gt;map(pwr, range(0, 100))&lt;/code&gt; is the list you want. You can store it in a variable:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-go&quot;&gt;powers = &lt;span class=&quot;hljs-keyword&quot;&gt;map&lt;/span&gt;(pwr, &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt;))
&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt; powers[&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;]
&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; powers
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&lt;code&gt;map&lt;/code&gt; does not modify the existing list or function, but instead generates an &lt;em&gt;iterator&lt;/em&gt; over the result (in Python 3). That means you need to store the result of &lt;code&gt;map&lt;/code&gt; and eventually convert it to a &lt;code&gt;list&lt;/code&gt; of you need to get the n-th element (in Python 3).&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-python&quot;&gt;pwrs = &lt;span class=&quot;hljs-built_in&quot;&gt;map&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;lambda&lt;/span&gt; n: &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; &amp;lt;&amp;lt; n, xrange(&lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt;))
&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt; pwrs[&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;]
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;From the use of the print statement I figured you were using Python 2, so I left out the explicit conversion to a &lt;code&gt;list&lt;/code&gt; and used &lt;code&gt;xrange&lt;/code&gt; which returns an iterator (leading to better performance). In Python 3 &lt;code&gt;xrange&lt;/code&gt; does not exist anymore however and &lt;code&gt;range&lt;/code&gt; always returns an iterator. Also notice how I left out the lower boundary on the &lt;code&gt;range&lt;/code&gt; function, because it defaults to 0. Instead of predefining a function I used a &lt;code&gt;lambda&lt;/code&gt; here, which in turn uses a bitwise leftshift , because it is usually faster than using the &lt;code&gt;**&lt;/code&gt; (power) operator.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/3857127359371482206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-use-python-map-function-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3857127359371482206'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3857127359371482206'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-use-python-map-function-to.html' title='How To Use Python Map Function To Generate The First 100 Powers Of Two Starting At 0 And Exclusive Of 100?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2690375257528476708</id><published>2023-09-26T22:49:00.001-07:00</published><updated>2023-09-26T22:49:01.889-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Multithreading"/><category scheme="http://www.blogger.com/atom/ns#" term="Mutex"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python Multithreading"/><category scheme="http://www.blogger.com/atom/ns#" term="Semaphore"/><title type='text'>What Is The Difference Between .semaphore() And .boundedsemaphore()?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I know that threading.Lock() is equal to threading.Semaphore(1). Is also threading.Lock() equal to threading.BoundedSemaphore(1) ? And newly I saw threading.BoundedSemaphore(), wha&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;A &lt;code&gt;Semaphore&lt;/code&gt; can be released more times than it&#39;s acquired, and that will raise its counter above the starting value. A &lt;code&gt;BoundedSemaphore&lt;/code&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/3.6/library/threading.html#threading.BoundedSemaphore&quot;&gt;can&#39;t&lt;/a&gt; be raised above the starting value.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; threading &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Semaphore, BoundedSemaphore

&lt;span class=&quot;hljs-comment&quot;&gt;# Usually, you create a Semaphore that will allow a certain number of threads&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# into a section of code. This one starts at 5.&lt;/span&gt;
s1 = Semaphore(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)

&lt;span class=&quot;hljs-comment&quot;&gt;# When you want to enter the section of code, you acquire it first.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# That lowers it to 4. (Four more threads could enter this section.)&lt;/span&gt;
s1.acquire()

&lt;span class=&quot;hljs-comment&quot;&gt;# Then you do whatever sensitive thing needed to be restricted to five threads.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# When you&#39;re finished, you release the semaphore, and it goes back to 5.&lt;/span&gt;
s1.release()


&lt;span class=&quot;hljs-comment&quot;&gt;# That&#39;s all fine, but you can also release it without acquiring it first.&lt;/span&gt;
s1.release()

&lt;span class=&quot;hljs-comment&quot;&gt;# The counter is now 6! That might make sense in some situations, but not in most.&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(s1._value)  &lt;span class=&quot;hljs-comment&quot;&gt;# =&amp;gt; 6&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# If that doesn&#39;t make sense in your situation, use a BoundedSemaphore.&lt;/span&gt;

s2 = BoundedSemaphore(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)  &lt;span class=&quot;hljs-comment&quot;&gt;# Start at 5.&lt;/span&gt;

s2.acquire()  &lt;span class=&quot;hljs-comment&quot;&gt;# Lower to 4.&lt;/span&gt;

s2.release()  &lt;span class=&quot;hljs-comment&quot;&gt;# Go back to 5.&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt;:
    s2.release()  &lt;span class=&quot;hljs-comment&quot;&gt;# Try to raise to 6, above starting value.&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;except&lt;/span&gt; ValueError:
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;As expected, it complained.&#39;&lt;/span&gt;)    
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot; style=&quot;height: auto !important;&quot;&gt;&lt;p&gt;The threading module provides the simple &lt;code&gt;Semaphore&lt;/code&gt; class. &lt;/p&gt;&lt;p&gt;A &lt;code&gt;Semaphore&lt;/code&gt; provides a non-bounded counter which allows you to call &lt;code&gt;release()&lt;/code&gt; any number of times for incrementing. &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;However, to avoid programming errors, it’s usually a correct choice to use &lt;code&gt;BoundedSemaphore&lt;/code&gt; , which raises an error if a &lt;code&gt;release()&lt;/code&gt; call tries to increase the counter beyond its maximum size.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;EDIT&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;A semaphore has an internal counter rather than a lock flag (in case of Locks), and it only blocks if more than a given number of threads have attempted to hold the semaphore. Depending on how the semaphore is initialized, this allows multiple threads to access the same code section simultaneously.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2690375257528476708/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/what-is-difference-between-semaphore.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2690375257528476708'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2690375257528476708'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/what-is-difference-between-semaphore.html' title='What Is The Difference Between .semaphore() And .boundedsemaphore()?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-9017538804959778618</id><published>2023-09-26T19:43:00.001-07:00</published><updated>2023-09-26T19:43:36.988-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Magic Methods"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Syntactic Sugar"/><title type='text'>Why Are Explicit Calls To Magic Methods Slower Than &quot;sugared&quot; Syntax?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I was messing around with a small custom data object that needs to be hashable, comparable, and fast, when I ran into an odd-looking set of timing results. Some of the comparisons&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Two reasons:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;The API lookups look at the &lt;em&gt;type&lt;/em&gt; only. They don&#39;t look at &lt;code&gt;self.foo.__hash__&lt;/code&gt;, they look for &lt;code&gt;type(self.foo).__hash__&lt;/code&gt;. That&#39;s one less dictionary to look in.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;The C slot lookup is faster than the pure-Python attribute lookup (which will use &lt;code&gt;__getattribute__&lt;/code&gt;); instead looking up the method objects (including the descriptor binding) is done entirely in C, bypassing &lt;code&gt;__getattribute__&lt;/code&gt;.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;So you&#39;d have to cache the &lt;code&gt;type(self._foo).__hash__&lt;/code&gt; lookup locally, and even then the call would not be as fast as from C code. Just stick to the standard library functions if speed is at a premium.&lt;/p&gt;&lt;p&gt;Another reason to avoid calling the magic methods directly is that the comparison operators do &lt;em&gt;more&lt;/em&gt; than just call one magic method; the methods have reflected versions too; for &lt;code&gt;x &amp;lt; y&lt;/code&gt;, if &lt;code&gt;x.__lt__&lt;/code&gt; isn&#39;t defined or &lt;code&gt;x.__lt__(y)&lt;/code&gt; returns the &lt;code&gt;NotImplemented&lt;/code&gt; singleton, &lt;code&gt;y.__gt__(x)&lt;/code&gt; is consulted as well.&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/9017538804959778618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/why-are-explicit-calls-to-magic-methods.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/9017538804959778618'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/9017538804959778618'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/why-are-explicit-calls-to-magic-methods.html' title='Why Are Explicit Calls To Magic Methods Slower Than &quot;sugared&quot; Syntax?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-4463580283872363906</id><published>2023-09-26T17:38:00.001-07:00</published><updated>2023-09-26T17:38:36.767-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pyinstaller"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Pyusb"/><category scheme="http://www.blogger.com/atom/ns#" term="Usb"/><title type='text'>Pyusb On Windows 8.1 - No Backend Available - How To Install Libusb?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Working on pyinstaller 3.1, python 2.7.9, and tkinter. Try to use pyusb instead of pyserial, but no backend available. Looking into Pyusb on windows - no backend available, PyUSB V&lt;/div&gt;

&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;
&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I downloaded latest libusb windows binaries from here:
&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download&quot;&gt;http://sourceforge.net/projects/libusb/files/libusb-1.0/libusb-1.0.20/libusb-1.0.20.7z/download&lt;/a&gt;
then I copied libusb1.dll according to this: when you are on 64-bit Windows,
MS64\dll\libusb-1.0.dll must be copied into C:\Windows\System32 and
(for running 32-bit applications that use libusb)
MS32\dll\libusb-1.0.dll must be copied into C:\Windows\SysWOW64.
and then I copied libusb1.lib from binaries to lib dir in python directory and now pyusb works for me. hope it helps. &lt;/p&gt;&lt;/div&gt; 
&lt;br&gt;
</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/4463580283872363906/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/pyusb-on-windows-81-no-backend.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/4463580283872363906'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/4463580283872363906'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/pyusb-on-windows-81-no-backend.html' title='Pyusb On Windows 8.1 - No Backend Available - How To Install Libusb?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2638588902889630534</id><published>2023-09-26T15:30:00.001-07:00</published><updated>2023-09-26T15:30:55.535-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Conv Neural Network"/><category scheme="http://www.blogger.com/atom/ns#" term="Hyperparameters"/><category scheme="http://www.blogger.com/atom/ns#" term="Optimization"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tensorflow"/><title type='text'>Save Optimization Of Hyperparameter In A Convolutional Neural Net</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I am facing a problem regarding saving the training process of the hyperparameter in my Convolutional Neural Net. I have read couple of blog posts, but somehow I am unable to do th&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I do not have the exact answer to that question, however there is &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://medium.com/vooban-ai/hyperopt-tutorial-for-optimizing-neural-networks-hyperparameters-e3102814b919&quot;&gt;here&lt;/a&gt; a &quot;trick&quot; that might do it.&lt;/p&gt;&lt;p&gt;It proposes to print the content of every trial tested, at the end of the code. Maybe you can save the &quot;trials&quot; object as well in a pickle or something, so that you can parse and check for yourself later.
I do have the exact same question and i&#39;m very very suprise that there is no &quot;easy&quot; solution as with the keras callbacks to save the best trained model.&lt;/p&gt;&lt;p&gt;I also suggest you to change the title of your questiopn, adding at least &quot;Hyperopt&quot;, &quot;callbacks&quot; and &quot;saving model&quot; key words. With more attention maybe our question will be answered :)&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2638588902889630534/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/save-optimization-of-hyperparameter-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2638588902889630534'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2638588902889630534'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/save-optimization-of-hyperparameter-in.html' title='Save Optimization Of Hyperparameter In A Convolutional Neural Net'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-7823039275523417779</id><published>2023-09-26T13:22:00.001-07:00</published><updated>2023-09-26T13:22:01.953-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 3.x"/><category scheme="http://www.blogger.com/atom/ns#" term="Selenium"/><category scheme="http://www.blogger.com/atom/ns#" term="Selenium Webdriver"/><title type='text'>How To Create Page Counter With Shuffled Links</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I  am wanting to create page count down for shuffled links so easily informed of where my script is actually progressing.  I try: def page_counter():   for x in range(1000):&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;try this&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Replace&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;links = &lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;((&lt;span class=&quot;hljs-built_in&quot;&gt;next&lt;/span&gt;(count) + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, e) &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; e &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; elements)
desc_links = &lt;span class=&quot;hljs-built_in&quot;&gt;sorted&lt;/span&gt;(links.items(), key=operator.itemgetter(&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;))

&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; link &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; desc_links:
    driver.get(link[&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;])
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;f&#39;At Page: &lt;span class=&quot;hljs-subst&quot;&gt;{link[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;]}&lt;/span&gt;&#39;&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;With&lt;/strong&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; collections
links = &lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;((&lt;span class=&quot;hljs-built_in&quot;&gt;next&lt;/span&gt;(count) + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, e) &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; e &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; elements)

desc_links = collections.OrderedDict(&lt;span class=&quot;hljs-built_in&quot;&gt;sorted&lt;/span&gt;(links.items(), reverse=&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;))

&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; key, value &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; desc_links.items():
    driver.get(value)
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;At Page: &#39;&lt;/span&gt; + &lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;(key))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/7823039275523417779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-create-page-counter-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/7823039275523417779'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/7823039275523417779'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-create-page-counter-with.html' title='How To Create Page Counter With Shuffled Links'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-3483310313801093622</id><published>2023-09-26T12:13:00.001-07:00</published><updated>2023-09-26T12:13:48.683-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Correlation"/><category scheme="http://www.blogger.com/atom/ns#" term="Dataframe"/><category scheme="http://www.blogger.com/atom/ns#" term="Pandas"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Scalar"/><title type='text'>How To Correlate Scalar Values Of Two Pandas Dataframes</title><content type='html'>&lt;div class=&quot;question&quot;&gt;How do I correlate two pandas dataframes, find a single r value for all values? I don&#39;t want to correlate columns or rows, but all scalar values. One dataframe is the x axis, and t&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I&#39;ve simulated a setup that I think mimics yours--three dataframes with countries across rows and years across columns. I then concatenate the different sets of data into a single dataframe. And show how to compute the correlation between them. Let me know if something about this example doesn&#39;t match your setup.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-java&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; pandas as &lt;span class=&quot;hljs-type&quot;&gt;pd&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;set1&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; pd.DataFrame({&lt;span class=&quot;hljs-number&quot;&gt;1980&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;], &lt;span class=&quot;hljs-number&quot;&gt;1981&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;], &lt;span class=&quot;hljs-number&quot;&gt;1982&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;]},
    index=pd.Index([&lt;span class=&quot;hljs-string&quot;&gt;&#39;USA&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;UK&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Iran&#39;&lt;/span&gt;], name=&lt;span class=&quot;hljs-string&quot;&gt;&#39;country&#39;&lt;/span&gt;))
set1.columns.name = &lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;
set1
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-yaml&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1980  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;USA&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;UK&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Iran&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-perl&quot;&gt;set2 = pd.DataFrame({&lt;span class=&quot;hljs-number&quot;&gt;1981&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;], &lt;span class=&quot;hljs-number&quot;&gt;1982&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;15&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;12&lt;/span&gt;], &lt;span class=&quot;hljs-number&quot;&gt;1983&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;13&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;]},
    &lt;span class=&quot;hljs-keyword&quot;&gt;index&lt;/span&gt;=pd.Index([&lt;span class=&quot;hljs-string&quot;&gt;&#39;USA&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;UK&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Turkey&#39;&lt;/span&gt;], name=&lt;span class=&quot;hljs-string&quot;&gt;&#39;country&#39;&lt;/span&gt;))
set2.columns.name = &lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;
set2
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-yaml&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1983&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;USA&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;UK&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Turkey&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Notice that, like your setup, some countries/years are not present in different datasets.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-ini&quot;&gt;&lt;span class=&quot;hljs-attr&quot;&gt;set3&lt;/span&gt; = pd.DataFrame({&lt;span class=&quot;hljs-number&quot;&gt;1980&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;12&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;], &lt;span class=&quot;hljs-number&quot;&gt;1982&lt;/span&gt;:[&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;]},
    &lt;span class=&quot;hljs-attr&quot;&gt;index&lt;/span&gt;=pd.Index([&lt;span class=&quot;hljs-string&quot;&gt;&#39;USA&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;UK&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Turkey&#39;&lt;/span&gt;], name=&lt;span class=&quot;hljs-string&quot;&gt;&#39;country&#39;&lt;/span&gt;))
&lt;span class=&quot;hljs-attr&quot;&gt;set3.columns.name&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;We can turns these into multi-indexed series by stacking along &lt;code&gt;year&lt;/code&gt; and then concatenate these across columns using &lt;code&gt;pd.concat&lt;/code&gt;.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;df&lt;/span&gt; = pd.concat([set1.stack(&lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;), set2.stack(&lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;), set3.stack(&lt;span class=&quot;hljs-string&quot;&gt;&#39;year&#39;&lt;/span&gt;)],
    keys=[&lt;span class=&quot;hljs-string&quot;&gt;&#39;set1&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;set2&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;set3&#39;&lt;/span&gt;], names=[&lt;span class=&quot;hljs-string&quot;&gt;&#39;set&#39;&lt;/span&gt;], axis=1)
&lt;span class=&quot;hljs-built_in&quot;&gt;df&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code id=&quot;code_8&quot; class=&quot;hljs language-yaml&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;set1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;set2&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;set3&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;year&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Iran&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1980   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Turkey&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1980   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;12.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;11.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1983   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;UK&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1980  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;11.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;11.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981  &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;8.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1983   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;13.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;USA&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1980   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;12.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1981   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;5.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1982   &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;15.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1983   &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10.0&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;NaN&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_8&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;And we can compute a 3x3 correlation matrix across the three different sets.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-scss&quot;&gt;df&lt;span class=&quot;hljs-selector-class&quot;&gt;.corr&lt;/span&gt;()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code id=&quot;code_10&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;       set1      set2      set3
&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;                               
set1  1.000000 -0.723632  0.509902
set2 -0.723632  1.000000  0.606891
set3  0.509902  0.606891  1.000000
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_10&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot; style=&quot;height: auto !important;&quot;&gt;&lt;p&gt;Here&#39;s what I did, but it&#39;s still not as slick as if I had found a built-in pandas feature or package.&lt;/p&gt;&lt;p&gt;Because I ultimately wanted to do this with more than two tables, I put the tables (dataframes) into a dictionary.&lt;/p&gt;&lt;p&gt;Then, I changed each table into a one-column table that has a MultiIndex representing the original column names and index values. The field values are the original column values strung end to end.&lt;/p&gt;&lt;p&gt;Then, I merged these new tables into one full outer join on the MultiIndex. Now I can correlate any two of the original tables by correlating their respective columns in the final table.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_11&quot; class=&quot;hljs language-makefile&quot;&gt;import pandas as pd

gvtx_eiu_df = pd.read_csv(&#39;gvtx_eiu.csv&#39;,index_col=0,
                          skip_blank_lines=False)
gvtx_eiu_df.columns.name = &#39;year&#39;
polpartix_eiu_df = pd.read_csv(&#39;polpartix_eiu.csv&#39;,index_col=0,
                               skip_blank_lines=False)
polpartix_eiu_df.columns.name = &#39;year&#39;
clean_elec_idea_df = pd.read_csv(&#39;clean_elec_idea.csv&#39;, index_col=0,
                                 skip_blank_lines=False)
clean_elec_idea_df.columns.name = &#39;year&#39;

test_table_dict = {&#39;gvtx_eiu&#39;: gvtx_eiu_df,
                   &#39;polpartix_eiu&#39;: polpartix_eiu_df,
                   &#39;clean_elec_idea&#39;: clean_elec_idea_df}
&#39;&#39;&#39;
&lt;span class=&quot;hljs-comment&quot;&gt;# Updated to not use this anymore. Using stack now, thanks to @jtorca. So it&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# fits more neatly into one function.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Serialize df columns into MultiIndex df, index=(year, country), one column&lt;/span&gt;
def df_to_multidx_df(df: pd.DataFrame, cols_idx1_name: str = &#39;Previous Columns&#39;,
                     idx_idx2_name: str = &#39;Previous Index&#39;,
                     val_col_name: str = &#39;Values&#39;) -&amp;gt; pd.DataFrame:
    &lt;span class=&quot;hljs-comment&quot;&gt;#Takes 2d dataframe (df) with a single-level index and one or more&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#single-level columns. All df values must be the same type.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#Parameters:&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    df: 2d dataframe with single-level index and one or more&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#        single-level columns. All df values must be the same type.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    cols_idx1_name: 1st index title for returned dataframe; index is df&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#        column names.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    idx_idx2_name: 2nd index title for returned dataframe; index is df&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#        index.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#Returns:&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    a 2d dataframe with a MultiIndex constructed of table_df column&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    names and index values. Has a single column with field values that are&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#    all df columns strung end to end.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Create MultiIndex from product of index values and column names.&lt;/span&gt;
    mult_idx = pd.MultiIndex.from_product([df.columns, df.index],
                                          names=[cols_idx1_name, idx_idx2_name])
    &lt;span class=&quot;hljs-comment&quot;&gt;# 1D list of table values in same order as MultiIndex.&lt;/span&gt;
    val_list = [val for col in df for val in df[col]]
    
    return pd.DataFrame(val_list, index=mult_idx, columns=[val_col_name])
&#39;&#39;&#39;

def df_dict_to_multidx_df(df_dict: dict) -&amp;gt; pd.DataFrame:
&lt;span class=&quot;hljs-comment&quot;&gt;#     , cols_idx1_name: str = &#39;idx1&#39;,&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#     idx_idx2_name: str = &#39;idx2&#39;) -&amp;gt; pd.DataFrame:&lt;/span&gt;
    &#39;&#39;&#39;Converts a dictionary (df_dict) of 2d dataframes, each with single-level
    indices and columns, into a 2d dataframe (multidx_df) with each column
    containing the the values of one of df_dict&#39;s dataframes. The index of
    multidx_df is a MultiIndex of the input dataframes&#39; column names and index
    values. Dataframes are joined in full outer join on the MultiIndex.
        NOTE: each input dataframe&#39;s index and columns row must be named
        beforehand in order to name the columns in the multiindex and join on it.
    Parameters:
        df_dict: dictionary of 2d dataframes, each with single-level
            indices and columns.
    Returns:
        multidx_df = MultiIndex dataframe.&#39;&#39;&#39;
    
    df_dict_copy = df_dict.copy()
        
    &lt;span class=&quot;hljs-comment&quot;&gt;# Full outer join each table to multidx_df on MultiIndex.&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Start with first indicator to have a left df to merge.&lt;/span&gt;
    first_key = next(iter(df_dict_copy))
    multidx_df = pd.DataFrame(df_dict_copy.pop(first_key).stack(),
                                     columns=[first_key])
    for key, df in df_dict_copy.items():
        df = pd.DataFrame(df.stack(), columns=[key])
        multidx_df = multidx_df.merge(right=df, how=&#39;outer&#39;,
                                     on=multidx_df.index.names[:2])

        &lt;span class=&quot;hljs-comment&quot;&gt;# concat twice as fast as merge&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#         multidx_df = pd.concat([multidx_df, df], names=[&#39;indicator&#39;], axis=1)&lt;/span&gt;
    
    return multidx_df

&lt;span class=&quot;hljs-comment&quot;&gt;###Test Code&lt;/span&gt;

print(gvtx_eiu_df)

&lt;span class=&quot;hljs-comment&quot;&gt;#               2006    2007   2008   2009   2010   2011   2012   2013   2014  \&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# country                                                                       &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Afghanistan    NaN  0.0395  0.079  0.079  0.079  0.079  0.079  0.079  0.114   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Albania      0.507  0.5070  0.507  0.507  0.507  0.471  0.400  0.400  0.400   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Algeria      0.221  0.2210  0.221  0.221  0.221  0.221  0.221  0.221  0.221   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Angola       0.214  0.2680  0.321  0.321  0.321  0.321  0.321  0.321  0.321   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Argentina    0.500  0.5000  0.500  0.535  0.571  0.571  0.571  0.571  0.571   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# ...            ...     ...    ...    ...    ...    ...    ...    ...    ...   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Venezuela    0.364  0.3960  0.429  0.411  0.393  0.393  0.429  0.429  0.429   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Vietnam      0.429  0.4290  0.429  0.429  0.429  0.429  0.393  0.393  0.393   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Yemen        0.271  0.2610  0.250  0.214  0.179  0.036  0.143  0.143  0.143   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Zambia       0.464  0.4640  0.464  0.500  0.536  0.500  0.536  0.536  0.536   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Zimbabwe     0.079  0.0790  0.079  0.104  0.129  0.129  0.129  0.129  0.129   &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#               2015   2016   2017   2018  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# country                                  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Afghanistan  0.114  0.114  0.114  0.114  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Albania      0.436  0.436  0.471  0.471  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Algeria      0.221  0.221  0.221  0.221  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Angola       0.321  0.321  0.286  0.286  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Argentina    0.500  0.500  0.500  0.536  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# ...            ...    ...    ...    ...  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Venezuela    0.393  0.250  0.286  0.179  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Vietnam      0.393  0.321  0.321  0.321  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Yemen        0.036    NaN    NaN    NaN  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Zambia       0.536  0.536  0.500  0.464  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Zimbabwe     0.200  0.200  0.200  0.200  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# [164 rows x 13 columns]&lt;/span&gt;


test_serialized = df_to_multidx_df(df=gvtx_eiu_df, cols_idx1_name=&#39;Year&#39;,
                                   idx_idx2_name=&#39;Country&#39;,
                                   val_col_name=&#39;gvtx_eiu&#39;)
print(test_serialized)

&lt;span class=&quot;hljs-comment&quot;&gt;#                       gvtx_eiu&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Year Country                  &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# 2006 Afghanistan           NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Albania             0.507&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Algeria             0.221&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Angola              0.214&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Argentina           0.500&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# ...                        ...&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# 2018 Venezuela           0.179&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Vietnam             0.321&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Yemen                 NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Zambia              0.464&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Zimbabwe            0.200&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# [2132 rows x 1 columns]&lt;/span&gt;


test_multidx_df = table_dict_to_multidx_df(test_table_dict, &#39;Year&#39;, &#39;Country&#39;)

print(test_multidx_df)

&lt;span class=&quot;hljs-comment&quot;&gt;#                       gvtx_eiu       polpartix_eiu  clean_elec_idea&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# Year Country                                                       &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# 2006 Afghanistan           NaN               0.222            0.475&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Albania             0.507               0.444            0.541&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Algeria             0.221               0.222            0.399&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Angola              0.214               0.111              NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Argentina           0.500               0.556            0.778&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# ...                        ...                 ...              ...&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# 2017 Somalia               NaN                 NaN            0.394&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      South Sudan           NaN                 NaN              NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# 2018 Georgia               NaN                 NaN            0.605&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      Somalia               NaN                 NaN              NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;#      South Sudan           NaN                 NaN              NaN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# [6976 rows x 3 columns]&lt;/span&gt;

test_multidx_profile = ProfileReport(test_multidx_df, title=&#39;Test MultIdx Profile&#39;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_11&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The output is exactly what I was going for, but in addition to wishing for a one- or two-statement solution, I&#39;m not completely happy with iterating through an input dictionary of dataframes. I tried to make the input a dataframe of dataframes so I could apply(lambda) to save some memory I think, but no dice getting apply() to work right, and it&#39;s time to move on.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/3483310313801093622/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-correlate-scalar-values-of-two.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3483310313801093622'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3483310313801093622'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/how-to-correlate-scalar-values-of-two.html' title='How To Correlate Scalar Values Of Two Pandas Dataframes'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-6101329152876765076</id><published>2023-09-26T10:11:00.001-07:00</published><updated>2023-09-26T10:11:56.976-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tkinter"/><title type='text'>Tkinter Button In Class Can&#39;t Call Function</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Total noob, seriously and angrily struggling with Python... What I&#39;m trying to do SHOULD be simple:  Make a button. Connect that button go a function. Click button --&gt; run funct&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;If you want your functions to be designed outside of the class, but want to call it from a button defined in the class, the solution is to create a method in your class which takes the input values and then passes them to the external function. &lt;/p&gt;&lt;p&gt;This is called &lt;em&gt;decoupling&lt;/em&gt;. Your function is &lt;em&gt;decoupled&lt;/em&gt; from the implementation of the UI, and it means that you are free to completely change the implementation of the UI without changing the function, and vice versa. It also means that you can reuse the same function in many different programs without modification. &lt;/p&gt;&lt;p&gt;The overall structure of your code should look something like this:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# this is the external function, which could be in the same&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# file or imported from some other module&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;convert&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;kg&lt;/span&gt;):
    pounds = kg * &lt;span class=&quot;hljs-number&quot;&gt;2.2046&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; pounds

&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;Example&lt;/span&gt;(...):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;initUI&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):
        ...
        self.entry_kg = Entry(...)
        btn_convert=Button(..., command=self.do_convert)
        ...

    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;do_convert&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):
        kg = &lt;span class=&quot;hljs-built_in&quot;&gt;float&lt;/span&gt;(self.entry_kg.get())
        result = convert(kg)
        &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;%d kg = %d lb&quot;&lt;/span&gt; % (kg, result))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot; style=&quot;height: auto !important;&quot;&gt;&lt;p&gt;Here&#39;s a modified version of your code that works. The changes have been indicated with ALL CAPS line comments. I obviously misunderstood your question (which does say you could figure out how to make the &lt;code&gt;convert()&lt;/code&gt; function part of the class. However, you mentioned you wanted the opposite of that, so I&#39;m modified the code here accordingly.&lt;/p&gt;&lt;p&gt;Essentially the problem boils down to the &lt;code&gt;convert()&lt;/code&gt; function needing to access a &lt;code&gt;tkinter.Entry&lt;/code&gt; widget that&#39;s created somewhere else—inside your &lt;code&gt;Example&lt;/code&gt; class in this case.&lt;/p&gt;&lt;p&gt;One way of doing that would be to save the widget in a global variable and access it through the variable name assigned to it. Many folks do that because it&#39;s easiest to thing to do, but as you should know, global variables are considered a bad practice and are generally something to be avoided.&lt;/p&gt;&lt;p&gt;There&#39;s a common way to avoid needing one with &lt;code&gt;tkinter&lt;/code&gt;, which is sometimes called &quot;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/extra-args.html&quot;&gt;The extra arguments trick&lt;/a&gt;&quot;. To use it all you need to do is create a short anonymous function with an argument that has a default value defined—which in this case will be the &lt;code&gt;Entry&lt;/code&gt; widget you want passed to the now &quot;wrapped&quot; &lt;code&gt;convert()&lt;/code&gt; function. This can be done using what&#39;s called a &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/3/reference/expressions.html#lambda&quot;&gt;&lt;code&gt;lambda expression&lt;/code&gt;&lt;/a&gt;. The revised code below and the comments in it show and describe how to do this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; tkinter &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; *
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; tkinter.ttk &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Frame, Button, Style


&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;convert&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;entry_widget&lt;/span&gt;):  &lt;span class=&quot;hljs-comment&quot;&gt;# ADDED WIDGET ARGUMENT&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&quot; Some function outside class. &quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;clicked&quot;&lt;/span&gt;)
    kg = entry_widget.get()  &lt;span class=&quot;hljs-comment&quot;&gt;# REFERENCE ENTRY WIDGET PASSED AS ARGUMENT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(kg)


&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;Example&lt;/span&gt;(&lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;Frame&lt;/span&gt;):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):
        &lt;span class=&quot;hljs-built_in&quot;&gt;super&lt;/span&gt;().__init__()

        self.initUI()    &lt;span class=&quot;hljs-comment&quot;&gt;# initiate the GUI&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;initUI&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):
        self.master.title(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Weight Converter&quot;&lt;/span&gt;)
        self.pack(fill=BOTH, expand=&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;)

        frame_kg = Frame(self)   &lt;span class=&quot;hljs-comment&quot;&gt;# frame for Kilograms&lt;/span&gt;
        frame_kg.pack(fill=X)

        lbl_kg = Label(frame_kg, text=&lt;span class=&quot;hljs-string&quot;&gt;&quot;Kilograms&quot;&lt;/span&gt;, width=&lt;span class=&quot;hljs-number&quot;&gt;16&lt;/span&gt;)
        lbl_kg.pack(side=LEFT, padx=&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;, pady=&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)

        entry_kg = Entry(frame_kg)
        entry_kg.pack(fill=X, padx=(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;30&lt;/span&gt;), expand=&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;)

        frame_btn = Frame(self)    &lt;span class=&quot;hljs-comment&quot;&gt;# frame for buttons&lt;/span&gt;
        frame_btn.pack(fill=BOTH, expand=&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;, padx=&lt;span class=&quot;hljs-number&quot;&gt;20&lt;/span&gt;, pady=&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)

        btn_convert=Button(frame_btn, text=&lt;span class=&quot;hljs-string&quot;&gt;&quot;Convert&quot;&lt;/span&gt;,
                        &lt;span class=&quot;hljs-comment&quot;&gt;# DEFINE ANONYMOUS FUNCTION WITH DEFAULT ARGUMENT SO IT&#39;S&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# AUTOMATICALLY PASSED TO THE TARGET FUNCTION.&lt;/span&gt;
                        command=&lt;span class=&quot;hljs-keyword&quot;&gt;lambda&lt;/span&gt; entry_obj=entry_kg: convert(entry_obj))
        btn_convert.pack(side=LEFT, padx=&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;, pady=&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;)


&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;main&lt;/span&gt;():
    root = Tk()
    root.geometry(&lt;span class=&quot;hljs-string&quot;&gt;&quot;300x200+300+200&quot;&lt;/span&gt;)
    app = Example()
    root.mainloop()

&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; __name__ == &lt;span class=&quot;hljs-string&quot;&gt;&#39;__main__&#39;&lt;/span&gt;:
    main()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Your &lt;code&gt;entry_kg&lt;/code&gt; is not &lt;em&gt;known&lt;/em&gt; anywhere outside the scope of &lt;code&gt;initUI&lt;/code&gt; method. That is why. You could convert it from a method variable to instance attribute to be within reach for class methods by replacing:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_12&quot; class=&quot;hljs language-bash&quot;&gt;entry_kg = Entry(frame_kg)
entry_kg.pack(fill=X, padx=(5, 30), &lt;span class=&quot;hljs-built_in&quot;&gt;expand&lt;/span&gt;=True)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_12&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;with:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_13&quot; class=&quot;hljs language-php&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;self&lt;/span&gt;.entry_kg = Entry(frame_kg)
&lt;span class=&quot;hljs-built_in&quot;&gt;self&lt;/span&gt;.entry_kg.pack(fill=X, padx=(&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;30&lt;/span&gt;), expand=&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_13&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;hr&gt;&lt;p&gt;&lt;strong&gt;Only &lt;em&gt;then&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;&lt;p&gt;You can mention it in a class method like:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_14&quot; class=&quot;hljs language-python-repl&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;...&lt;/span&gt;
kg = self.entry_kg.get()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_14&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;That way you if you make your &lt;code&gt;convert&lt;/code&gt; a method under &lt;code&gt;Example&lt;/code&gt; again:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_17&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;initUI&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):
...
&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;convert&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self&lt;/span&gt;):           &lt;span class=&quot;hljs-comment&quot;&gt;# is defined under the same scope as initUI&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;clicked&quot;&lt;/span&gt;)
    kg = self.entry_kg.get() &lt;span class=&quot;hljs-comment&quot;&gt;# notice that it now refers to self.e...&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(kg)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_17&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;also don&#39;t forget to replace command option as well:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_18&quot; class=&quot;hljs language-ini&quot;&gt;&lt;span class=&quot;hljs-attr&quot;&gt;btn_convert&lt;/span&gt;=Button(..., command=self.convert)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_18&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;hr&gt;&lt;p&gt;&lt;strong&gt;Or only &lt;em&gt;then&lt;/em&gt;:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;When outside the class scope, by using dot notation on the object that the class creates:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_19&quot; class=&quot;hljs language-css&quot;&gt;def &lt;span class=&quot;hljs-selector-tag&quot;&gt;main&lt;/span&gt;():

    root = &lt;span class=&quot;hljs-built_in&quot;&gt;Tk&lt;/span&gt;()
    root.&lt;span class=&quot;hljs-built_in&quot;&gt;geometry&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;300x200+300+200&quot;&lt;/span&gt;)
    app = &lt;span class=&quot;hljs-built_in&quot;&gt;Example&lt;/span&gt;()
    kg = app.entry_kg.&lt;span class=&quot;hljs-built_in&quot;&gt;get&lt;/span&gt;() # This would return an empty string, but it would still be valid
    root.&lt;span class=&quot;hljs-built_in&quot;&gt;mainloop&lt;/span&gt;()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_19&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;to be used with global methods, such as the current state of &lt;code&gt;convert&lt;/code&gt; you need to make &lt;code&gt;app&lt;/code&gt; (the object it is an attribute of) global, or pass it explicitly to the method.&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/6101329152876765076/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tkinter-button-in-class-cant-call.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6101329152876765076'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6101329152876765076'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tkinter-button-in-class-cant-call.html' title='Tkinter Button In Class Can&#39;t Call Function'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-3459027028464538647</id><published>2023-09-26T07:58:00.001-07:00</published><updated>2023-09-26T07:58:26.757-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Memory"/><category scheme="http://www.blogger.com/atom/ns#" term="Profiling"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Python Line-by-line Memory Profiler?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m looking to generate, from a large Python codebase, a summary of heap usage or memory allocations over the course of a function&#39;s run. I&#39;m familiar with heapy, and it&#39;s served m&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I would use &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/library/sys.html#sys.settrace&quot;&gt;&lt;code&gt;sys.settrace&lt;/code&gt;&lt;/a&gt; at program startup to register a custom tracer function. The custom_trace_function will be called for each line of code. Then you can use that function to store information gathered by heapy or &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://launchpad.net/meliae&quot;&gt;meliae&lt;/a&gt; in a file for later processing. &lt;/p&gt;&lt;p&gt;Here is a very simple example which logs the output of hpy.heap() each second to a plain text file:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-java&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; sys
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; time
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; atexit
from guppy &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;hpy&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;_last_log_time&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; time.time()
_logfile = open(&lt;span class=&quot;hljs-string&quot;&gt;&#39;logfile.txt&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;w&#39;&lt;/span&gt;)

def &lt;span class=&quot;hljs-title function_&quot;&gt;heapy_profile&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(frame, event, arg)&lt;/span&gt;:
    currtime = time.time()
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; currtime - _last_log_time &amp;lt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;:
        &lt;span class=&quot;hljs-type&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;_last_log_time&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;currtime&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; frame.&lt;span class=&quot;hljs-type&quot;&gt;f_code&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; code.&lt;span class=&quot;hljs-type&quot;&gt;co_filename&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;lineno&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; code.&lt;span class=&quot;hljs-type&quot;&gt;co_firstlineno&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;idset&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; hpy().heap()
    logfile.write(&lt;span class=&quot;hljs-string&quot;&gt;&#39;%s %s:%s\n%s\n\n&#39;&lt;/span&gt; % (currtime, filename, lineno, idset))
    logfile.flush()

atexit.register(_logfile.close)
sys.settrace(heapy_profile)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You might be interested by &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://pypi.org/project/memory-profiler/&quot;&gt;memory_profiler&lt;/a&gt;.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/3459027028464538647/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-line-by-line-memory-profiler.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3459027028464538647'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/3459027028464538647'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-line-by-line-memory-profiler.html' title='Python Line-by-line Memory Profiler?'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-5347592066783609860</id><published>2023-09-26T05:45:00.001-07:00</published><updated>2023-09-26T05:45:05.935-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Object Detection Api"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tensorflow"/><title type='text'>Class Weights For Balancing Data In Tensorflow Object Detection Api</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m fine-tuning SSD object detector using TensorFlow object detection API on Open Images Dataset. My training data contains imbalanced classes, e.g.  top (5K images) dress (50K ima&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;the API expects a weight for each object (bbox) directly in the &lt;strong&gt;annotation files&lt;/strong&gt;. Due to this requirement the solutions to use class weights seem to be:&lt;/p&gt;&lt;p&gt;1) If you have a custom dataset you can modify the annotations of each object (bbox) to include the weight field as &#39;object/weight&#39;.&lt;/p&gt;&lt;p&gt;2) If you don&#39;t want to modify the annotations you can recreate &lt;strong&gt;only the tf_records&lt;/strong&gt; file in order to include the weights of the bboxes. &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;3) Modify the code of the API (seemed to me quite tricky)&lt;/p&gt;&lt;p&gt;I decided to go for #2, so I put here the code to generate such &lt;strong&gt;weighted&lt;/strong&gt; tf records file for a &lt;em&gt;custom&lt;/em&gt; dataset with two classes (&quot;top&quot;, &quot;dress&quot;) with weights (1.0, 0.1) given a folder of &lt;strong&gt;xml&lt;/strong&gt; annotations as:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; os
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; io
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; glob
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; hashlib
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; pandas &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; pd
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; xml.etree.ElementTree &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; ET
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; tensorflow &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; tf
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; random
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; PIL &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Image
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; object_detection.utils &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; dataset_util

&lt;span class=&quot;hljs-comment&quot;&gt;# Define the class names and their weight&lt;/span&gt;
class_names = [&lt;span class=&quot;hljs-string&quot;&gt;&#39;top&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;dress&#39;&lt;/span&gt;, ...]
class_weights = [&lt;span class=&quot;hljs-number&quot;&gt;1.0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0.1&lt;/span&gt;, ...]

&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;create_example&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;xml_file&lt;/span&gt;):

        tree = ET.parse(xml_file)
        root = tree.getroot()
        image_name = root.find(&lt;span class=&quot;hljs-string&quot;&gt;&#39;filename&#39;&lt;/span&gt;).text
        image_path = root.find(&lt;span class=&quot;hljs-string&quot;&gt;&#39;path&#39;&lt;/span&gt;).text
        file_name = image_name.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;)
        size=root.find(&lt;span class=&quot;hljs-string&quot;&gt;&#39;size&#39;&lt;/span&gt;)
        width = &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;(size[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;].text)
        height = &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;(size[&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;].text)
        xmin = []
        ymin = []
        xmax = []
        ymax = []
        classes = []
        classes_text = []
        truncated = []
        poses = []
        difficult_obj = []
        weights = [] &lt;span class=&quot;hljs-comment&quot;&gt;# Important line&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; member &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; root.findall(&lt;span class=&quot;hljs-string&quot;&gt;&#39;object&#39;&lt;/span&gt;):

           xmin.append(&lt;span class=&quot;hljs-built_in&quot;&gt;float&lt;/span&gt;(member[&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;].text) / width)
           ymin.append(&lt;span class=&quot;hljs-built_in&quot;&gt;float&lt;/span&gt;(member[&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;].text) / height)
           xmax.append(&lt;span class=&quot;hljs-built_in&quot;&gt;float&lt;/span&gt;(member[&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;].text) / width)
           ymax.append(&lt;span class=&quot;hljs-built_in&quot;&gt;float&lt;/span&gt;(member[&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;].text) / height)
           difficult_obj.append(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)

           class_name = member[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;].text
           class_id = class_names.index(class_name)
           weights.append(class_weights[class_id])

           &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; class_name == &lt;span class=&quot;hljs-string&quot;&gt;&#39;top&#39;&lt;/span&gt;:
               classes_text.append(&lt;span class=&quot;hljs-string&quot;&gt;&#39;top&#39;&lt;/span&gt;.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;))
               classes.append(&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
           &lt;span class=&quot;hljs-keyword&quot;&gt;elif&lt;/span&gt; class_name == &lt;span class=&quot;hljs-string&quot;&gt;&#39;dress&#39;&lt;/span&gt;:
               classes_text.append(&lt;span class=&quot;hljs-string&quot;&gt;&#39;dress&#39;&lt;/span&gt;.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;))
               classes.append(&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;)
           &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:
               &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;E: class not recognized!&#39;&lt;/span&gt;)

           truncated.append(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)
           poses.append(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Unspecified&#39;&lt;/span&gt;.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;))

        full_path = image_path 
        &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; tf.gfile.GFile(full_path, &lt;span class=&quot;hljs-string&quot;&gt;&#39;rb&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; fid:
            encoded_jpg = fid.read()
        encoded_jpg_io = io.BytesIO(encoded_jpg)
        image = Image.&lt;span class=&quot;hljs-built_in&quot;&gt;open&lt;/span&gt;(encoded_jpg_io)
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; image.&lt;span class=&quot;hljs-built_in&quot;&gt;format&lt;/span&gt; != &lt;span class=&quot;hljs-string&quot;&gt;&#39;JPEG&#39;&lt;/span&gt;:
           &lt;span class=&quot;hljs-keyword&quot;&gt;raise&lt;/span&gt; ValueError(&lt;span class=&quot;hljs-string&quot;&gt;&#39;Image format not JPEG&#39;&lt;/span&gt;)
        key = hashlib.sha256(encoded_jpg).hexdigest()

        &lt;span class=&quot;hljs-comment&quot;&gt;#create TFRecord Example&lt;/span&gt;
        example = tf.train.Example(features=tf.train.Features(feature={
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/height&#39;&lt;/span&gt;: dataset_util.int64_feature(height),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/width&#39;&lt;/span&gt;: dataset_util.int64_feature(width),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/filename&#39;&lt;/span&gt;: dataset_util.bytes_feature(file_name),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/source_id&#39;&lt;/span&gt;: dataset_util.bytes_feature(file_name),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/key/sha256&#39;&lt;/span&gt;: dataset_util.bytes_feature(key.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;)),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/encoded&#39;&lt;/span&gt;: dataset_util.bytes_feature(encoded_jpg),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/format&#39;&lt;/span&gt;: dataset_util.bytes_feature(&lt;span class=&quot;hljs-string&quot;&gt;&#39;jpeg&#39;&lt;/span&gt;.encode(&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf8&#39;&lt;/span&gt;)),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/bbox/xmin&#39;&lt;/span&gt;: dataset_util.float_list_feature(xmin),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/bbox/xmax&#39;&lt;/span&gt;: dataset_util.float_list_feature(xmax),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/bbox/ymin&#39;&lt;/span&gt;: dataset_util.float_list_feature(ymin),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/bbox/ymax&#39;&lt;/span&gt;: dataset_util.float_list_feature(ymax),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/class/text&#39;&lt;/span&gt;: dataset_util.bytes_list_feature(classes_text),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/class/label&#39;&lt;/span&gt;: dataset_util.int64_list_feature(classes),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/difficult&#39;&lt;/span&gt;: dataset_util.int64_list_feature(difficult_obj),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/truncated&#39;&lt;/span&gt;: dataset_util.int64_list_feature(truncated),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/view&#39;&lt;/span&gt;: dataset_util.bytes_list_feature(poses),
            &lt;span class=&quot;hljs-string&quot;&gt;&#39;image/object/weight&#39;&lt;/span&gt;: dataset_util.float_list_feature(weights) &lt;span class=&quot;hljs-comment&quot;&gt;# Important line&lt;/span&gt;
        })) 
        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; example  

&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;main&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_&lt;/span&gt;):

    weighted_tf_records_output = &lt;span class=&quot;hljs-string&quot;&gt;&#39;name_of_records_file.record&#39;&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# output file&lt;/span&gt;
    annotations_path = &lt;span class=&quot;hljs-string&quot;&gt;&#39;/path/to/annotations/folder/*.xml&#39;&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# input annotations&lt;/span&gt;

    writer_train = tf.python_io.TFRecordWriter(weighted_tf_records_output)
    filename_list=tf.train.match_filenames_once(annotations_path)
    init = (tf.global_variables_initializer(), tf.local_variables_initializer())
    sess=tf.Session()
    sess.run(init)
    &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt; = sess.run(filename_list)
    random.shuffle(&lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;)  

    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; xml_file &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;:
      &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;-&amp;gt; Processing {}&#39;&lt;/span&gt;.&lt;span class=&quot;hljs-built_in&quot;&gt;format&lt;/span&gt;(xml_file))
      example = create_example(xml_file)
      writer_train.write(example.SerializeToString())

    writer_train.close()
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;-&amp;gt; Successfully converted dataset to TFRecord.&#39;&lt;/span&gt;)


&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; __name__ == &lt;span class=&quot;hljs-string&quot;&gt;&#39;__main__&#39;&lt;/span&gt;:
    tf.app.run()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;If you have other kinds of annotations the code will be very similar but this one unfortunately will not work. &lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;The Object Detection API losses are defined in: &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://github.com/tensorflow/models/blob/master/research/object_detection/core/losses.py&quot;&gt;https://github.com/tensorflow/models/blob/master/research/object_detection/core/losses.py&lt;/a&gt;&lt;/p&gt;&lt;p&gt;In particular, the following loss classes have been implemented:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Classification losses:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;WeightedSigmoidClassificationLoss&lt;/li&gt;&lt;li&gt;SigmoidFocalClassificationLoss&lt;/li&gt;&lt;li&gt;WeightedSoftmaxClassificationLoss&lt;/li&gt;&lt;li&gt;WeightedSoftmaxClassificationAgainstLogitsLoss&lt;/li&gt;&lt;li&gt;BootstrappedSigmoidClassificationLoss&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;strong&gt;Localization losses:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;WeightedL2LocalizationLoss&lt;/li&gt;&lt;li&gt;WeightedSmoothL1LocalizationLoss&lt;/li&gt;&lt;li&gt;WeightedIOULocalizationLoss&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;The weight parameters are used to balance anchors (prior boxes) and are of size &lt;code&gt;[batch_size, num_anchors]&lt;/code&gt; in addition to hard negative mining. Alternatively, the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://arxiv.org/pdf/1708.02002.pdf&quot;&gt;focal loss&lt;/a&gt; down weighs well classified examples and focusses on the hard examples.&lt;/p&gt;&lt;p&gt;The primary class imbalance is due to many more negative examples (bounding boxes without objects of interest) in comparison to very few positive examples (bounding boxes with object classes). That seems to be the reason why class imbalance within positive examples (i.e. unequal distribution of positive class labels) is not implemented as part of object detection losses.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/5347592066783609860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/class-weights-for-balancing-data-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5347592066783609860'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5347592066783609860'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/class-weights-for-balancing-data-in.html' title='Class Weights For Balancing Data In Tensorflow Object Detection Api'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-8436169880179522999</id><published>2023-09-26T04:39:00.001-07:00</published><updated>2023-09-26T04:39:49.258-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Json"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Python - Updating Json Objects In A File</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a (valid) JSON file contaning JSON objects, each object taking up one file line.  These JSON objects map to Python dicts, and are in ascending order by numerical IDs. I am w&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You cannot do what you want, because you can only surgically replace &lt;em&gt;fixed length records&lt;/em&gt; in a file. You cannot replace the line with something that is longer or shorter, as files don&#39;t store data as separate lines.&lt;/p&gt;&lt;p&gt;In addition, your code doesn&#39;t take into account that on some platforms (like on Windows) lines in text files are separated by two characters, representable in Python as &lt;code&gt;\r\n&lt;/code&gt;, but these line endings are translated for you to the single character &lt;code&gt;\n&lt;/code&gt;. Your line length calculation doesn&#39;t take that into account.&lt;/p&gt;&lt;p&gt;You instead have to rewrite &lt;strong&gt;everything&lt;/strong&gt; following that line. You&#39;d be better of using a database; &lt;code&gt;sqlite3&lt;/code&gt; comes bundled with Python, take a look at that.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;update_JSON_file&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, file_path, obj_IDs, props &lt;/span&gt;):
    decoder = json.JSONDecoder()
    &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; io.&lt;span class=&quot;hljs-built_in&quot;&gt;open&lt;/span&gt;( file_path, &lt;span class=&quot;hljs-string&quot;&gt;&#39;r+&#39;&lt;/span&gt;, encoding=&lt;span class=&quot;hljs-string&quot;&gt;&#39;utf=8&#39;&lt;/span&gt; ) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; files:
        IDs_iter = &lt;span class=&quot;hljs-built_in&quot;&gt;iter&lt;/span&gt;( obj_IDs )
        fl = files.read().splitlines(&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;)
        ID = IDs_iter.&lt;span class=&quot;hljs-built_in&quot;&gt;next&lt;/span&gt;()
        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i,line &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;enumerate&lt;/span&gt;(fl):
            &lt;span class=&quot;hljs-comment&quot;&gt;# ID = IDs_iter.next()&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# I think the &#39;ID&#39; should be initialized out of the loop&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;# and iterated only if the condition is true&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;str&lt;/span&gt;(ID) &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; line:
                JSON_obj = decoder.raw_decode(line)[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;]
                &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; key &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; props.keys():
                    JSON_obj[key] = props[key] 
                JSON_obj = json.dumps( JSON_obj, ensure_ascii=&lt;span class=&quot;hljs-literal&quot;&gt;False&lt;/span&gt; )
                &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;},\n&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; line: JSON_obj += &lt;span class=&quot;hljs-string&quot;&gt;&#39;,\n&#39;&lt;/span&gt;
                JSON_obj = unicode( JSON_obj )
                fl[i] = JSON_obj
                &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt;:
                    ID = IDs_iter.&lt;span class=&quot;hljs-built_in&quot;&gt;next&lt;/span&gt;()
                &lt;span class=&quot;hljs-keyword&quot;&gt;except&lt;/span&gt; StopIteration:
                    &lt;span class=&quot;hljs-keyword&quot;&gt;break&lt;/span&gt;
        files.seek(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)
        files.write(&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;.join(fl))
        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/8436169880179522999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-updating-json-objects-in-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/8436169880179522999'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/8436169880179522999'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-updating-json-objects-in-file.html' title='Python - Updating Json Objects In A File'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2991928776661094069</id><published>2023-09-26T02:26:00.001-07:00</published><updated>2023-09-26T02:26:14.905-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 2.7"/><title type='text'>Comparing Two Lists And Only Printing The Differences? (xoring Two Lists)</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m trying to create a function that takes in 2 lists and returns the list that only has the differences of the two lists. Example:  a = [1,2,5,7,9] b = [1,2,4,8,9]  The result sho&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You basically want to add an element to your new list if it is present in one and not present in another. Here is a compact loop which can do it. For each element in the two lists (concatenate them with &lt;code&gt;list1+list2&lt;/code&gt;), we add element if it is not present in one of them:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-csharp&quot;&gt;[&lt;span class=&quot;hljs-meta&quot;&gt;a for a in list1+list2 if (a not in list1) or (a not in list2)&lt;/span&gt;]
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can easily transform it into a more unPythonic code with explicit looping through elements as you have now, but honestly I don&#39;t see a point (not that it matters):&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;xor&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;list1, list2&lt;/span&gt;):
    outputlist = []
    list3 = list1 + list2
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;range&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(list3)):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; ((list3[i] &lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; list1) &lt;span class=&quot;hljs-keyword&quot;&gt;or&lt;/span&gt; (list3[i] &lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; list2)) &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; (list3[i] &lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; outputlist):
             outputlist[&lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(outputlist):] = [list3[i]]
    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; outputlist
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Use set is better&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-python-repl&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;language-python&quot;&gt;a = [&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;]&lt;/span&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;language-python&quot;&gt;b = [&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;]&lt;/span&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;language-python&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(a).symmetric_difference(b)&lt;/span&gt;
{4, 5, 7, 8}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Thanks to @DSM, a better sentence is:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-python-repl&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;language-python&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(a)^&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(b)&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;These two statements are the same. But the latter is clearer.&lt;/p&gt;&lt;p&gt;Update: sorry, I did not see the last requirement: cannot use set. As far as I see, the solution provided by @sashkello is the best.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Note: This is really unpythonic and should only be used as a homework answer :)&lt;/p&gt;&lt;p&gt;After you have sorted both lists, you can find duplicates by doing the following:&lt;/p&gt;&lt;p&gt;1) Place iterators at the start of A and B&lt;/p&gt;&lt;p&gt;2) If Aitr is greater than Bitr, advance Bitr after placing Bitr&#39;s value in the return list&lt;/p&gt;&lt;p&gt;3) Else if Bitr is greater than Aitr, advance Aiter after placing Aitr&#39;s value in the return list&lt;/p&gt;&lt;p&gt;4) Else you have found a duplicate, advance Aitr and Bitr&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;This code works assuming you&#39;ve got sorted lists. It works in linear time, rather than quadratic like many of the other solutions given.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;diff&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;sl0, sl1&lt;/span&gt;):
    i0, i1 = &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;/span&gt; i0 &amp;lt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(sl0) &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; i1 &amp;lt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(sl1):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; sl0[i0] == sl1[i1]:
            i0 += &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;
            i1 += &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;elif&lt;/span&gt; sl0[i0] &amp;lt; sl1[i1]:
            &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; sl0[i0]
            i0 += &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:
            &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; sl1[i1]
            i1 += &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; xrange(i0, &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(sl0)):
        &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; sl0[i]
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; xrange(i1, &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(sl1)):
        &lt;span class=&quot;hljs-keyword&quot;&gt;yield&lt;/span&gt; sl1[i]

&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;(diff([&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;], [&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;]))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_5&quot;&gt;Solution 5:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Try this,&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_6&quot; class=&quot;hljs language-bash&quot;&gt;    a = [1,2,5,7,9]
    b = [1,2,4,8,9]
 &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(a).symmetric_difference(&lt;span class=&quot;hljs-built_in&quot;&gt;set&lt;/span&gt;(b))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_6&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2991928776661094069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/comparing-two-lists-and-only-printing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2991928776661094069'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2991928776661094069'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/comparing-two-lists-and-only-printing.html' title='Comparing Two Lists And Only Printing The Differences? (xoring Two Lists)'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-5912419085493892269</id><published>2023-09-25T23:23:00.001-07:00</published><updated>2023-09-25T23:23:32.384-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python 3.x"/><category scheme="http://www.blogger.com/atom/ns#" term="Spacy"/><title type='text'>Spacy Custom Sentence Spliting</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I using Spacy for custom sentence spliting and i need to parametrized the custom_delimeter/word for sentence spiting but i didnt find how to pass as an arugument here is the functi&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You could turn your component into a class that can be initialized with a list of delimiters? For example:&lt;/p&gt;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;MyCustomBoundary&lt;/span&gt;(&lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;object&lt;/span&gt;):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self, delimiters&lt;/span&gt;):
        self.delimiters = delimiters

    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__call__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self, doc&lt;/span&gt;):  &lt;span class=&quot;hljs-comment&quot;&gt;# this is applied when you call it on a Doc&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; token &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; doc[:-&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;]:
            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; token.text &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; self.delimiters:
                doc[token.i+&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;].is_sent_start = &lt;span class=&quot;hljs-literal&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; doc
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can then add it to your pipeline like this:&lt;/p&gt;&lt;pre class=&quot;lang-py prettyprint-override&quot;&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-python&quot;&gt;mycustom_boundary = MyCustomBoundary(delimiters=[&lt;span class=&quot;hljs-string&quot;&gt;&#39;...&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;---&#39;&lt;/span&gt;])
nlp.add_pipe(mycustom_boundary, before=&lt;span class=&quot;hljs-string&quot;&gt;&#39;parser&#39;&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/5912419085493892269/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/spacy-custom-sentence-spliting.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5912419085493892269'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/5912419085493892269'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/spacy-custom-sentence-spliting.html' title='Spacy Custom Sentence Spliting'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-1513607520050090593</id><published>2023-09-25T22:23:00.001-07:00</published><updated>2023-09-25T22:23:29.468-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dictionary"/><category scheme="http://www.blogger.com/atom/ns#" term="Json"/><category scheme="http://www.blogger.com/atom/ns#" term="List"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 2.7"/><title type='text'>Python Chained Get() Method With List Element Inside Json</title><content type='html'>&lt;div class=&quot;question&quot;&gt;[Python 2.7] I have a JSON source that doesn&#39;t always return the full list of expected keys. I&#39;m using chained gets() to address this. d = {&#39;a&#39;: {&#39;b&#39;: 1}}  print(d.get(&#39;a&#39;, {}).get&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I agree with @stovfl that writing your own lookup function is the way to go. Although, I don&#39;t think a recursive implementation is necessary. The following should work well enough:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-perl&quot;&gt;def nested_lookup(obj, &lt;span class=&quot;hljs-keyword&quot;&gt;keys&lt;/span&gt;, default=&lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;):
    current = obj
    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; key in &lt;span class=&quot;hljs-keyword&quot;&gt;keys&lt;/span&gt;:
        current = current &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; isinstance(current, list) &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; [current]
        try:
            current = &lt;span class=&quot;hljs-keyword&quot;&gt;next&lt;/span&gt;(&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;sub&lt;/span&gt;[&lt;span class=&quot;hljs-title&quot;&gt;key&lt;/span&gt;] &lt;span class=&quot;hljs-title&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;sub&lt;/span&gt;)
        &lt;span class=&quot;hljs-title&quot;&gt;except&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;StopIteration&lt;/span&gt;:
            &lt;span class=&quot;hljs-title&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;d&lt;/span&gt; = &lt;/span&gt;{&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;: {&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;: [{&lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;}, {&lt;span class=&quot;hljs-string&quot;&gt;&#39;d&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;}]}}

&lt;span class=&quot;hljs-keyword&quot;&gt;print&lt;/span&gt; nested_lookup(d, (&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;))  &lt;span class=&quot;hljs-comment&quot;&gt;# 2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;print&lt;/span&gt; nested_lookup(d, (&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;d&#39;&lt;/span&gt;))  &lt;span class=&quot;hljs-comment&quot;&gt;# 3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;print&lt;/span&gt; nested_lookup(d, (&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;))       &lt;span class=&quot;hljs-comment&quot;&gt;# NA&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The class approach doesn&#39;t seem great because you&#39;re going to be creating a lot of unnecessary objects and if you&#39;re ever trying to lookup a node that isn&#39;t a leaf, then you&#39;re going to wind up with a custom object rather than the actual node object.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;: I can&#39;t use a get() ... because lists don&#39;t support the get(). &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/blockquote&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;You can implement your own &lt;code&gt;list.get()&lt;/code&gt;, for instance:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;myGET&lt;/span&gt;(&lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;object&lt;/span&gt;):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self, data&lt;/span&gt;):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(data, &lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;):
            self.__dict__.update(data)
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(data, &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;):
            &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; d &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; data:
                self.__dict__.update(d)

    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;get&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;self, key, default=&lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;&lt;/span&gt;):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;hasattr&lt;/span&gt;(self, key):
            _attr = &lt;span class=&quot;hljs-built_in&quot;&gt;object&lt;/span&gt;.__getattribute__(self, key)
            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(_attr, (&lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;)):
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; myGET(_attr)
            &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; _attr
        &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:
            &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; default

d = {&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;: {&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;: [{&lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;}]}}
myGET(d).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;, {}).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;, []).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;

myGET(d).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;, {}).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;, []).get(&lt;span class=&quot;hljs-string&quot;&gt;&#39;d&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;NA
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;A &lt;em&gt;recursive&lt;/em&gt; Solution, no need to &lt;em&gt;chaining&lt;/em&gt;, for instance:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;get&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, subkey, default&lt;/span&gt;):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;_get&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, key, deep&lt;/span&gt;):
        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; key == subkey[deep]:
            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; deep == &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;/span&gt;(subkey) - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;:
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; _&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:
                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; _get(_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;, deep + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)

        &lt;span class=&quot;hljs-keyword&quot;&gt;elif&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;):
            &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; k &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; _&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;:
                match = _get(_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;[k], k, deep)
                &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; match: &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; match

        &lt;span class=&quot;hljs-keyword&quot;&gt;elif&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;):
            &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; e &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; _&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;:
                match = _get(e, &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;, deep)
                &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; match: &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; match

    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;isinstance&lt;/span&gt;(subkey, (&lt;span class=&quot;hljs-built_in&quot;&gt;tuple&lt;/span&gt;, &lt;span class=&quot;hljs-built_in&quot;&gt;list&lt;/span&gt;)):
        subkey = (subkey)

    _r = _get(_&lt;span class=&quot;hljs-built_in&quot;&gt;dict&lt;/span&gt;, &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt; _r: &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; default
    &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;:      &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; _r

get(d, &lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;

get(d, &lt;span class=&quot;hljs-string&quot;&gt;&#39;d&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;NA

&lt;span class=&quot;hljs-comment&quot;&gt;# get a inside b&lt;/span&gt;
d = {&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;: {&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;: [{&lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;}, {&lt;span class=&quot;hljs-string&quot;&gt;&#39;c&#39;&lt;/span&gt;: &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;}]}}
get(d, (&lt;span class=&quot;hljs-string&quot;&gt;&#39;b&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;), &lt;span class=&quot;hljs-string&quot;&gt;&#39;NA&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-meta&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Tested with Python: 3.4.2 and 2.7.9&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/1513607520050090593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-chained-get-method-with-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/1513607520050090593'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/1513607520050090593'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/python-chained-get-method-with-list.html' title='Python Chained Get() Method With List Element Inside Json'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-8017298209748646812</id><published>2023-09-25T21:19:00.001-07:00</published><updated>2023-09-25T21:19:37.236-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 3.4"/><category scheme="http://www.blogger.com/atom/ns#" term="Python Idle"/><category scheme="http://www.blogger.com/atom/ns#" term="Unicode"/><category scheme="http://www.blogger.com/atom/ns#" term="Windows"/><title type='text'>Private Unicode Character Displays Differently In Python 3 Interpreter</title><content type='html'>&lt;div class=&quot;question&quot;&gt;So I created a unicode character privately using Private Character Editor on Windows 10. The character was saved with the code E000. I copied it from the Character Map and pasted i&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Python isn&#39;t really the interesting part of this, rather the shell or terminal is. In our case, Windows uses special code points to represent private character encodings. To get those, you need to get a hex dump of the character on a shell in Windows, then you can render the character in Python.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Use Unicode points &lt;code&gt;E021&lt;/code&gt; or &lt;strong&gt;higher&lt;/strong&gt;, since lower number code points are usually used for control, and it seems that the Windows shell that the python interpreter and IDLE use doesn&#39;t let you override those with private characters.&lt;/p&gt;&lt;h2&gt;Demonstration&lt;/h2&gt;&lt;p&gt;I tested your issue by generating a private character of my own. I will put an image of my test here since it wouldn&#39;t be rendered properly in text here on Stack Overflow.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/f7DZt.png&quot;&gt;&lt;img alt=&quot;enter image description here&quot; src=&quot;https://i.stack.imgur.com/f7DZt.png&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Explanation&lt;/h2&gt;&lt;p&gt;I used the &lt;code&gt;Character Map&lt;/code&gt; program in Windows 10 to copy the symbol and paste it into my python environment. The environment may truncate it on the right since it is a wide character and the environment didn&#39;t seem to like that. (I moved the cursor around to get it to render full-width.)&lt;/p&gt;&lt;p&gt;Then I proceeded to get the hexdump of the code point by encoding the character using the default utf-8 encoding, which turned out to be &lt;code&gt;\xee\x80\xa1&lt;/code&gt; as a &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/3/library/functions.html#bytes&quot;&gt;&lt;code&gt;bytes&lt;/code&gt;&lt;/a&gt; object.&lt;/p&gt;&lt;p&gt;Next I printed the data as a string to show you a common error, and what would be printed if you attempted to print a string of those bytes.&lt;/p&gt;&lt;p&gt;Then, I printed &lt;code&gt;b&#39;\xee\x80\xa1&#39;&lt;/code&gt;, which is how you would actually use the symbol in your software.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can use the &lt;code&gt;\u&lt;/code&gt; escape sequence in your Python source code, like so:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_6&quot; class=&quot;hljs language-bash&quot;&gt;my_unicode_string = &lt;span class=&quot;hljs-string&quot;&gt;&#39;This is my Unicode character: \ue000&#39;&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;(my_unicode_string)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_6&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/8017298209748646812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/private-unicode-character-displays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/8017298209748646812'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/8017298209748646812'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/private-unicode-character-displays.html' title='Private Unicode Character Displays Differently In Python 3 Interpreter'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2137942230720632073</id><published>2023-09-25T19:17:00.001-07:00</published><updated>2023-09-25T19:17:51.490-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Looking For A More Pythonic Way To Access The Database</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a bunch of python methods that follow this pattern: def delete_session(guid):     conn = get_conn()     cur = conn.cursor()      cur.execute(&#39;delete from sessions where guid&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You could write a context manager and use the with statement. For example, see this blog post:&lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;http://jessenoller.com/2009/02/03/get-with-the-program-as-contextmanager-completely-different/&quot;&gt;http://jessenoller.com/2009/02/03/get-with-the-program-as-contextmanager-completely-different/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Also the python documentation has a sample that pretty much matches your needs. See section 8.1 on this page, in particular the snippet that begins:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;db_connection = DatabaseConnection()
&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; db_connection &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; cursor:
    cursor.execute(&lt;span class=&quot;hljs-string&quot;&gt;&#39;insert into ...&#39;&lt;/span&gt;)
    cursor.execute(&lt;span class=&quot;hljs-string&quot;&gt;&#39;delete from ...&#39;&lt;/span&gt;)
    &lt;span class=&quot;hljs-meta&quot;&gt;# ... more operations ...&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/2.5/whatsnew/pep-343.html&quot;&gt;https://docs.python.org/2.5/whatsnew/pep-343.html&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Careful about that &lt;code&gt;execute&lt;/code&gt;, the second argument needs to be [guid] (a list with just one item). As for your question, I normally just use a class encapsulating connection and cursor, but it looks like you may prefer to use an &lt;em&gt;execution context&lt;/em&gt; object whose &lt;code&gt;__enter__&lt;/code&gt; method gives you a cursor while &lt;code&gt;__leave__&lt;/code&gt; commits or rollbacks depending on whether the termination was normal or by exception; this would make your code&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;def &lt;span class=&quot;hljs-title&quot;&gt;delete_session&lt;/span&gt;():
    &lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;get_cursor&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; cur:
        cur.&lt;span class=&quot;hljs-title&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;etc etc&lt;/span&gt;)
&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;If you like this style, let us know and I&#39;ll show you how to write &lt;code&gt;get_cursor&lt;/code&gt;. Others will no doubt propose a decorator instead, so you&#39;d write:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_6&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;@withcursor&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;delete_session&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;cur&lt;/span&gt;):
    cur.execute(etc etc)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_6&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;but I think this makes commit/rollback, among other issues, a bit murkier. Still, if &lt;em&gt;this&lt;/em&gt; is your preference, again let us know and I can show you how to write that form, too.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&quot;I have a bunch of python methods that follow this pattern:&quot;&lt;/p&gt;&lt;p&gt;This is confusing.&lt;/p&gt;&lt;p&gt;Either you have a bunch of functions, or you have a bunch of methods of a class.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Bunch of Functions&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Do this instead.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;SQLFunction&lt;/span&gt;( &lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;object&lt;/span&gt; ):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, connection &lt;/span&gt;):
        self.connection = connection
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__call__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, args=&lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;&lt;/span&gt;):
        self.cursor= self.connection.cursor()
        self.run( args )
        self.cursor.commit()
        self.cursor.close()

&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;DeleteSession&lt;/span&gt;( &lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;SQLFunction&lt;/span&gt; ):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;run&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, args &lt;/span&gt;):
        self.cursor.execute( &lt;span class=&quot;hljs-string&quot;&gt;&quot;statement&quot;&lt;/span&gt; )

delete_session = DeleteSession( connection )
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Your function declarations are two lines longer, but essentially the same.
You can do &lt;code&gt;func1( args )&lt;/code&gt; because it&#39;s a callable object. The rest of
your program should remain unchanged.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Bunch of Methods in One Class&lt;/strong&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;SomeClass&lt;/span&gt;( &lt;span class=&quot;hljs-title class_ inherited__&quot;&gt;object&lt;/span&gt; ):
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;__init__&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, connection &lt;/span&gt;):
        self.connection= connection
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;sql_execute&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self, statement, args= &lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;&lt;/span&gt;)
        self.cursor= self.connection.cursor() 
        self.cursor.execute( statement, args &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; args &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; [] )
        self.connection.commit()
        self.cursor.close()
    &lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title function_&quot;&gt;delete_session&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt; self &lt;/span&gt;):
        self.sql_execute( &lt;span class=&quot;hljs-string&quot;&gt;&quot;statement&quot;&lt;/span&gt; )
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;All your methods can look like delete_session and make use of a common &lt;code&gt;sql_execute&lt;/code&gt; method. &lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;A decorator?&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_11&quot; class=&quot;hljs language-ruby&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;SqlExec&lt;/span&gt;:&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;, f)&lt;/span&gt;&lt;/span&gt;:
      &lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.f = f
   &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;, *args)&lt;/span&gt;&lt;/span&gt;:
      conn = get_conn() 
      cur = conn.cursor()
      cur.execute(&lt;span class=&quot;hljs-keyword&quot;&gt;self&lt;/span&gt;.f (*args))
      conn.commit()
      conn.close()

&lt;span class=&quot;hljs-variable&quot;&gt;@SqlExec&lt;/span&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;delete_session&lt;/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(guid)&lt;/span&gt;&lt;/span&gt;:
      &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;delete from sessions where guid=%s&quot;&lt;/span&gt; % guid
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_11&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_5&quot;&gt;Solution 5:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;According to the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.python.org/2/library/sqlite3.html#using-shortcut-methods&quot;&gt;docs&lt;/a&gt;, if you were using SQLite3, you wouldn&#39;t even &lt;em&gt;need&lt;/em&gt; a &lt;code&gt;Cursor&lt;/code&gt; which, as the docs say, is &quot;often superfluous&quot;.&lt;/p&gt;&lt;p&gt;Instead you can use the shortcut methods &lt;code&gt;execute&lt;/code&gt;&lt;code&gt;executemany&lt;/code&gt; and &lt;code&gt;executescript&lt;/code&gt; directly on the connection object:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_16&quot; class=&quot;hljs language-lua&quot;&gt;import sqlite3

persons = [
    (&lt;span class=&quot;hljs-string&quot;&gt;&quot;Hugo&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;Boss&quot;&lt;/span&gt;),
    (&lt;span class=&quot;hljs-string&quot;&gt;&quot;Calvin&quot;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;Klein&quot;&lt;/span&gt;)
    ]

con = sqlite3.connect(&lt;span class=&quot;hljs-string&quot;&gt;&quot;:memory:&quot;&lt;/span&gt;)

# Create the &lt;span class=&quot;hljs-built_in&quot;&gt;table&lt;/span&gt;
con.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;create table person(firstname, lastname)&quot;&lt;/span&gt;)

# Fill the &lt;span class=&quot;hljs-built_in&quot;&gt;table&lt;/span&gt;
con.executemany(&lt;span class=&quot;hljs-string&quot;&gt;&quot;insert into person(firstname, lastname) values (?, ?)&quot;&lt;/span&gt;, persons)

# Print the &lt;span class=&quot;hljs-built_in&quot;&gt;table&lt;/span&gt; contents
&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; row &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; con.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;select firstname, lastname from person&quot;&lt;/span&gt;):
    &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt; row

&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;I just deleted&quot;&lt;/span&gt;, con.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;delete from person&quot;&lt;/span&gt;).rowcount, &lt;span class=&quot;hljs-string&quot;&gt;&quot;rows&quot;&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_16&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2137942230720632073/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/looking-for-more-pythonic-way-to-access.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2137942230720632073'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2137942230720632073'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/looking-for-more-pythonic-way-to-access.html' title='Looking For A More Pythonic Way To Access The Database'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-6596816436087498968</id><published>2023-09-25T15:34:00.001-07:00</published><updated>2023-09-25T15:34:35.286-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Ocr"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Python 2.7"/><category scheme="http://www.blogger.com/atom/ns#" term="Tesseract"/><title type='text'>Tesseractnotfound - Pytesser</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m trying to do OCR using pytesser downloaded from HERE. Here is the code of pytesser.py try:     import cv2.cv as cv     OPENCV_AVAILABLE = True except ImportError:     OPENCV_AV&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Following changes in &lt;code&gt;pytesser.py&lt;/code&gt; solved my problem, there is no problem with the paths that are set.&lt;/p&gt;&lt;p&gt;Changes are as follows:&lt;/p&gt;&lt;p&gt;&lt;code&gt;PROG_NAME=tesseract&lt;/code&gt; changed to &lt;code&gt;PROG_NAME=tesseract.exe&lt;/code&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;In the function &lt;code&gt;image_to_string()&lt;/code&gt; added &lt;code&gt;f.close()&lt;/code&gt; after &lt;code&gt;txt=f.read()&lt;/code&gt;&lt;/p&gt;&lt;p&gt;That&#39;t it :)&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/6596816436087498968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tesseractnotfound-pytesser.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6596816436087498968'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/6596816436087498968'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/tesseractnotfound-pytesser.html' title='Tesseractnotfound - Pytesser'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8717787531963527165.post-2112295510391404016</id><published>2023-09-25T14:47:00.001-07:00</published><updated>2023-09-25T14:47:37.633-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Matplotlib"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Axes3d.plot_wireframe(x,y,z) Error</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m trying to learn Python through a tutorial on youtube and I&#39;m having some difficulies working with 3D graphs. Long stories short, I continuously get (if  Z.ndim != 2: AttributeE&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I walked around this problem by doing two things.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;import numpy as np&lt;/li&gt;&lt;li&gt;making the z-axis a multidimensional array&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;snippet&quot; data-babel=&quot;false&quot; data-console=&quot;true&quot; data-hide=&quot;false&quot; data-lang=&quot;js&quot;&gt;&lt;div class=&quot;snippet-code&quot;&gt;&lt;pre class=&quot;snippet-code-html lang-html prettyprint-override&quot;&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-xml&quot;&gt;#My 3d graph

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

figure = plt.figure()
axis = figure.add_subplot(111, projection = &#39;3d&#39;)

x = [1,2,3,4,5,6,7,8,9,10]
y = [5,6,7,8,2,5,6,3,7,2]
z = np.array([[1,2,6,3,2,7,3,3,7,2],[1,2,6,3,2,7,3,3,7,2]])

axis.plot_wireframe(x, y, z)

axis.set_xlabel(&#39;x-axis&#39;)
axis.set_ylabel(&#39;y-axis&#39;)
axis.set_zlabel(&#39;z-axis&#39;)

plt.show()&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;p&gt;Take special note of the z variable. If z is not multidimensional, it will throw an error.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;strong&gt;Hope it solves your problem&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Running your code with either Python 2.7.10 or Python 3.6.0, with matplotlib version 2.0.2, yields the same image with no error:&lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/tZSoY.png&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://i.stack.imgur.com/tZSoY.png&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;This is not a wireframe though, and a simple &lt;code&gt;ax.plot(X, Y, Z)&lt;/code&gt; would have generated it. As &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://stackoverflow.com/users/5851928/davidg&quot;&gt;DavidG&lt;/a&gt; and &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://stackoverflow.com/users/4124317/importanceofbeingernest&quot;&gt;ImportanceOfBeingErnest&lt;/a&gt; cleverly mentioned, it makes no sense to pass 1D lists to the wireframe function, as X, Y and Z should be two-dimensional. &lt;/p&gt;&lt;p&gt;The following code (an example taken from the matplotlib official documentation) shows exactly how the parameters of the &lt;code&gt;plot_wireframe&lt;/code&gt; function should be (using &lt;code&gt;numpy&lt;/code&gt; arrays):&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; mpl_toolkits.mplot3d &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; axes3d
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; matplotlib.pyplot &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; plt
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; numpy &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; np

&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&#39;
def get_test_data(delta=0.05):

    from matplotlib.mlab import  bivariate_normal
    x = y = np.arange(-3.0, 3.0, delta)
    X, Y = np.meshgrid(x, y)

    Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    Z = Z2 - Z1

    X = X * 10
    Y = Y * 10
    Z = Z * 500
    return X, Y, Z

&#39;&#39;&#39;&lt;/span&gt;


fig = plt.figure()
ax = fig.add_subplot(&lt;span class=&quot;hljs-number&quot;&gt;111&lt;/span&gt;, projection=&lt;span class=&quot;hljs-string&quot;&gt;&#39;3d&#39;&lt;/span&gt;)

x, y, z = axes3d.get_test_data(&lt;span class=&quot;hljs-number&quot;&gt;0.05&lt;/span&gt;)

ax.plot_wireframe(x,y,z, rstride=&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, cstride=&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;)

plt.show()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The output image is a true wireframe:&lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/19uFS.png&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;https://i.stack.imgur.com/19uFS.png&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Printing &lt;code&gt;x.shape&lt;/code&gt;, for instance, yields you (120, 120), showing that the array is two-dimensional and have 120 positions in the first dimension and 120 positions in the second one.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I had the exact problem (example from video not working though exactly copied). Without looking into the source code I&#39;m assuming a reality check was added to matplotlib 2.1.0 that NOW stops 1D arrays from being used in plot_wireframe. Changing that method call to simply &quot;plot&quot; did indeed fix the problem.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;The command
&lt;code&gt;ax.plot_wireframe(x,y,z, rstride=2, cstride=2)&lt;/code&gt;
is creating the problems with the latest versions.
Try using: &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-scss&quot;&gt;ax&lt;span class=&quot;hljs-selector-class&quot;&gt;.plot&lt;/span&gt;(x,y,z)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This will definitely solve your issues.
Python has been known for being inconsistent with the older libraries.
I am getting this image as the output:
&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/Jisvu.png&quot;&gt;This is the 3d Image I am getting&lt;/a&gt;&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://100inoneyear.blogspot.com/feeds/2112295510391404016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/axes3dplotwireframexyz-error.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2112295510391404016'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/8717787531963527165/posts/default/2112295510391404016'/><link rel='alternate' type='text/html' href='https://100inoneyear.blogspot.com/2023/09/axes3dplotwireframexyz-error.html' title='Axes3d.plot_wireframe(x,y,z) Error'/><author><name>Dr. Theresa O&#39;Kon</name><uri>http://www.blogger.com/profile/10070996635680996835</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>