<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Python on 村边的池塘</title>
    <link>https://jerrywang1981.github.io/tags/python/</link>
    <description>Recent content in Python on 村边的池塘</description>
    <generator>Hugo</generator>
    <language>zh-CN</language>
    <lastBuildDate>Tue, 09 Jun 2020 09:08:07 +0800</lastBuildDate>
    <atom:link href="https://jerrywang1981.github.io/tags/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>18 Matplotlib Demo</title>
      <link>https://jerrywang1981.github.io/post/python/18-matplotlib-demo/</link>
      <pubDate>Tue, 09 Jun 2020 09:08:07 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/18-matplotlib-demo/</guid>
      <description>the code in jupyter notebook matplot_demo</description>
    </item>
    <item>
      <title>17 Pandas Demo</title>
      <link>https://jerrywang1981.github.io/post/python/17-pandas-demo-2/</link>
      <pubDate>Tue, 09 Jun 2020 09:05:34 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/17-pandas-demo-2/</guid>
      <description>the code in jupyter notebook pandas_demo.ipynb</description>
    </item>
    <item>
      <title>16 Pandas Demo part 1</title>
      <link>https://jerrywang1981.github.io/post/python/16-pandas-demo-1/</link>
      <pubDate>Wed, 03 Jun 2020 16:30:03 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/16-pandas-demo-1/</guid>
      <description>the code in jupyter notebook refer to file pandas_demo.ipynb</description>
    </item>
    <item>
      <title>15 Numpy Demo</title>
      <link>https://jerrywang1981.github.io/post/python/15-numpy-demo/</link>
      <pubDate>Wed, 03 Jun 2020 16:28:51 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/15-numpy-demo/</guid>
      <description>the code in jupyter notebook the file numpy_demo.ipynb</description>
    </item>
    <item>
      <title>14 Anaconda</title>
      <link>https://jerrywang1981.github.io/post/python/14-anaconda/</link>
      <pubDate>Wed, 03 Jun 2020 16:27:28 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/14-anaconda/</guid>
      <description>How to install new package conda install xxx list env conda env list activate one env conda activate xx deactivate env conda deactivate </description>
    </item>
    <item>
      <title>13 Python Context Manager</title>
      <link>https://jerrywang1981.github.io/post/python/13-python-context-manager/</link>
      <pubDate>Sun, 31 May 2020 13:58:44 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/13-python-context-manager/</guid>
      <description>python context manager use contextlib/yield @contextmanager def send_mail_context(): &amp;#39;&amp;#39;&amp;#39;context manager with send_mail_context() &amp;#39;&amp;#39;&amp;#39; try: # 1 doing work BEFORE the code goes into with block print(&amp;#39;before yield&amp;#39;) server = smtplib.SMTP(SMTP_SERVERS[0], 25) # 2 yield back to with/caller yield server except Exception as e: print(e) finally: # 4. doing cleanup work server.quit() print(&amp;#39;in finally&amp;#39;) use class __enter__, __exit__ # 2 implement contextmanager with class class SMTPCT(object): &amp;#39;&amp;#39;&amp;#39; 1. override __enter__ __exit__ 2. in __enter__, return value &amp;#39;&amp;#39;&amp;#39; def __init__(self): self.smtp_server = None def __enter__(self): # 1 doing work BEFORE the code goes into with block print(&amp;#39;in enter&amp;#39;) self.smtp_server = smtplib.SMTP(SMTP_SERVERS[0], 25) # 2 return back to with/caller return self.smtp_server def __exit__(self, type, value, traceback): # 4. doing cleanup work print(&amp;#39;in exit&amp;#39;) self.smtp_server.quit() </description>
    </item>
    <item>
      <title>12 Python Smtp</title>
      <link>https://jerrywang1981.github.io/post/python/12-python-smtp/</link>
      <pubDate>Sun, 31 May 2020 13:52:03 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/12-python-smtp/</guid>
      <description>how to send mail in python #!/usr/bin/env python3 # -*- coding: utf-8 -*- # smtplib -&amp;gt; https://docs.python.org/3/library/smtplib.html import smtplib # contextlib -&amp;gt; https://docs.python.org/3/library/contextlib.html from contextlib import contextmanager # email -&amp;gt; https://docs.python.org/3/library/email.examples.html#email-examples from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr SMTP_SERVERS = [&amp;#39;your smtp servers&amp;#39;] def _format_addr(s): &amp;#39;&amp;#39;&amp;#39; format the address &amp;#39;&amp;#39;&amp;#39; name, addr = parseaddr(s) return formataddr((Header(name, &amp;#39;utf-8&amp;#39;).encode(), addr)) def generate_mail(from_addr, to_addr, cc_addr, subject, body, mime_type=&amp;#39;plain&amp;#39;): &amp;#39;&amp;#39;&amp;#39;plain/text text/html &amp;#39;&amp;#39;&amp;#39; msg = MIMEText(body, mime_type, &amp;#39;utf-8&amp;#39;) msg[&amp;#39;From&amp;#39;] = _format_addr(from_addr) msg[&amp;#39;To&amp;#39;] = &amp;#34;,&amp;#34;.join([_format_addr(x) for x in to_addr]) msg[&amp;#39;Cc&amp;#39;] = &amp;#34;,&amp;#34;.join([_format_addr(x) for x in cc_addr]) msg[&amp;#39;Subject&amp;#39;] = Header(subject, &amp;#39;utf-8&amp;#39;).encode() return msg def send_mail(from_addr, to_addr, cc_addr, subject, body, mime_type=&amp;#39;plain&amp;#39;): server = smtplib.SMTP(SMTP_SERVERS[0], 25) server.set_debuglevel(1) msg = generate_mail(from_addr, to_addr, cc_addr, subject, body, mime_type) server.sendmail(from_addr, to_addr + cc_addr, msg.as_string()) server.quit() # 1. to implement context manager, use decorator @contextmanager # and yield @contextmanager def send_mail_context(): &amp;#39;&amp;#39;&amp;#39;context manager with send_mail_context() &amp;#39;&amp;#39;&amp;#39; try: # 1 doing work BEFORE the code goes into with block print(&amp;#39;before yield&amp;#39;) server = smtplib.</description>
    </item>
    <item>
      <title>11 Python Mro</title>
      <link>https://jerrywang1981.github.io/post/python/11-python-mro/</link>
      <pubDate>Sun, 31 May 2020 13:50:50 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/11-python-mro/</guid>
      <description>method resolution order #!/usr/bin/env python3 # -*- coding: utf-8 -*- # method resolution order # mro class A(object): def hello(self): print(&amp;#39;class A says hello&amp;#39;) class B(A): pass class C(A): # class variable BP_URL = &amp;#39;https://xxxxx&amp;#39; def __init__(self): self.name = &amp;#39;ok&amp;#39; @staticmethod def print_hello(name, age): &amp;#39;&amp;#39;&amp;#39; staticmethod cannot access any class or self variabel/method &amp;#39;&amp;#39;&amp;#39; pass @classmethod def say(cls): &amp;#39;&amp;#39;&amp;#39;class method can access class level variable/method &amp;#39;&amp;#39;&amp;#39; print(cls.BP_URL) def hello(self): print(&amp;#39;class C says hello&amp;#39;) class D(C, B): pass if __name__ == &amp;#39;__main__&amp;#39;: d = D() d.hello() # to print the mro of one class print(d.__class__.__mro__) </description>
    </item>
    <item>
      <title>10 Dunder Methods</title>
      <link>https://jerrywang1981.github.io/post/python/10-dunder-methods/</link>
      <pubDate>Sun, 31 May 2020 13:49:16 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/10-dunder-methods/</guid>
      <description>dunder methods #!/usr/bin/env python3 # -*- coding: utf-8 -*- # double underscore variable/function # dunder class A(object): def __init__(self, number): self.number = number def __eq__(self, value): &amp;#39;&amp;#39;&amp;#39; a == b &amp;#39;&amp;#39;&amp;#39; return super().__eq__(value) def __ge__(self, value): &amp;#39;&amp;#39;&amp;#39; a &amp;gt;= b &amp;#39;&amp;#39;&amp;#39; return super().__ge__(value) def __gt__(self, value): &amp;#39;&amp;#39;&amp;#39; a &amp;gt; b &amp;#39;&amp;#39;&amp;#39; return super().__gt__(value) def __le__(self, value): &amp;#39;&amp;#39;&amp;#39; a &amp;lt; b &amp;#39;&amp;#39;&amp;#39; return super().__le__(value) def __ne__(self, value): &amp;#39;&amp;#39;&amp;#39; a != b &amp;#39;&amp;#39;&amp;#39; return super().__ne__(value) def __str__(self): &amp;#39;&amp;#39;&amp;#39;print(a) &amp;#39;&amp;#39;&amp;#39; return super().__str__() def __repr__(self): return super().__repr__() def __len__(self): &amp;#39;&amp;#39;&amp;#39; len(a) &amp;#39;&amp;#39;&amp;#39; return self.number * 10 def __lt__(self, other): &amp;#39;&amp;#39;&amp;#39;less than a &amp;lt; b &amp;#39;&amp;#39;&amp;#39; return self.number &amp;lt; other.number def __getattribute__(self, name): &amp;#39;&amp;#39;&amp;#39; print(a[the_key]) &amp;#39;&amp;#39;&amp;#39; return super().__getattribute__(name) def __setattr__(self, name, value): &amp;#39;&amp;#39;&amp;#39; a[the_key] = 5 &amp;#39;&amp;#39;&amp;#39; return super().__setattr__(name, value) def __delattr__(self, name): &amp;#39;&amp;#39;&amp;#39; delete a[&amp;#39;the_key&amp;#39;] &amp;#39;&amp;#39;&amp;#39; return super().__delattr__(name) if __name__ == &amp;#39;__main__&amp;#39;: &amp;#39;&amp;#39;&amp;#39; &amp;#39;&amp;#39;&amp;#39; # a = A(5) # b = A(6) # print(a &amp;lt; b) # print(len(a)) # a[&amp;#39;b&amp;#39;] = 10 # c = a[&amp;#39;c&amp;#39;] pass </description>
    </item>
    <item>
      <title>09 Python Decorators</title>
      <link>https://jerrywang1981.github.io/post/python/09-python-decorators/</link>
      <pubDate>Wed, 27 May 2020 10:17:38 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/09-python-decorators/</guid>
      <description>无参装饰器 #1. decorator without arguments def log_time_always(func): @wraps(func) def wrapped(*args, **kwargs): #1. get current time t1 = datetime.now() #2. run the function func(*args, **kwargs) #3. get current time t2 = datetime.now() print(f&amp;#39;in {func.__name__}, it took {t2-t1}&amp;#39;) return wrapped 带参数的装饰器 #2. decorator with arguments, flag is True or False def log_time(flag): def wrapper(func): @wraps(func) def wrapped(*args, **kwargs): # if flag is true, we need to log the time if flag: #1. get current time t1 = datetime.now() #2. run the function func(*args, **kwargs) #3. get current time t2 = datetime.now() print(f&amp;#39;in {func.__name__}, it took {t2-t1}&amp;#39;) else: func(*args, **kwargs) return wrapped return wrapper 本质 装饰器需要能够处理一个函数 无参 @decorator_1 def f(): pass 等同于 def f(): pass f =</description>
    </item>
    <item>
      <title>08 Python Virtual Environment</title>
      <link>https://jerrywang1981.github.io/post/python/08-python-virtual-environment/</link>
      <pubDate>Tue, 19 May 2020 21:00:00 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/08-python-virtual-environment/</guid>
      <description>Details refer to offical document&#xA;Virtual environment To create new virtual environment usually in folder venv&#xA;python -m venv yourfoldername enter virtual environment source venv/bin/activate exit virtual environment deactivate Pip usage install package pip install packagename list installed packages pip list save installed packages to requirements.txt pip freeze &amp;gt; requirements.txt install packages from requirements.txt pip install -r requirements.txt </description>
    </item>
    <item>
      <title>07 Python 函数</title>
      <link>https://jerrywang1981.github.io/post/python/07-python-function/</link>
      <pubDate>Sat, 16 May 2020 09:17:08 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/07-python-function/</guid>
      <description>函数 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # def func, snake case # return value # no args, no return def add_two_number(): print(&amp;#39;a&amp;#39;) # 1 arg def add_two_number_1(x): return x + 5 # return value # return multiple value def two_number_2(x, y): c = x + y d = x - y return c, d # args def add_number(x, y, *args): print(f&amp;#39;x = {x}&amp;#39;) print(f&amp;#39;y = {y}&amp;#39;) print(f&amp;#39;args = {args} &amp;#39;) # kwargs # def add_number_1(x, y, *args, **kwargs): print(f&amp;#39;x = {x}&amp;#39;) print(f&amp;#39;y = {y}&amp;#39;) # f-string f&amp;#39;this is string, {x}&amp;#39; print(f&amp;#39;args = {args} &amp;#39;) print(f&amp;#39;kwargs = {kwargs} &amp;#39;) # wrong # def add_number_2(**kwargs, *args, x, y) # pass # default def add_number_3(x, y, c=10, d = True): print(c) if __name__ == &amp;#39;__main__&amp;#39;: # add_two_number() # print(add_two_number_1(6)) # print(two_number_2(10, 5)) # add_number(1,2, 3, 4, 5, 6) # add_number_1(1,2,3,4,5,</description>
    </item>
    <item>
      <title>06 Python 控制，循环</title>
      <link>https://jerrywang1981.github.io/post/python/06-condition-loop/</link>
      <pubDate>Sat, 16 May 2020 09:14:28 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/06-condition-loop/</guid>
      <description>条件控制 #!/usr/bin/env python3 # -*- coding: utf-8 -*- if __name__ == &amp;#39;__main__&amp;#39;: a = 5 if a &amp;gt; 0: print(&amp;#39;a &amp;gt; 0&amp;#39;) else: print(&amp;#39;a &amp;lt; 0&amp;#39;) if a &amp;gt; 0: pass elif a &amp;lt; 5: pass elif a &amp;lt; 10: pass else: pass if a &amp;gt; 0: if a &amp;lt; 5: print(a) else: print(&amp;#39;a &amp;gt; 5&amp;#39;) 循环控制 #!/usr/bin/env python3 # -*- coding: utf-8 -*- if __name__ == &amp;#39;__main__&amp;#39;: # while condition a = 5 while a &amp;gt; 0: # print(a) a = a -1 # for in for i in range(10): print(i) # break the loop print(&amp;#39;------------------&amp;#39;) for i in range(10): print(i) else: # print(&amp;#39;done&amp;#39;) # continue the loop print(&amp;#39;------------------&amp;#39;) for i in range(10): if i == 5: continue print(i) count = 0 while count &amp;lt; 5: print (count, &amp;#34; 小于 5&amp;#34;) count</description>
    </item>
    <item>
      <title>转换Pdf成图片</title>
      <link>https://jerrywang1981.github.io/post/go/convert-pdf-to-images/</link>
      <pubDate>Mon, 11 May 2020 21:51:52 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/convert-pdf-to-images/</guid>
      <description>背景 在做文字检测和文字识别的时候，有时候客户提供的是pdf格式的文件，而不是jpg/png格式，这时候就需要把pdf里面多个页面 保存成图片。 代码库pdf2image 实现 Python 参考readme docker 参考readme golang 参考readme 也可以 直接下载可执行文件直接执行 -&amp;gt; https://github.com/jerrywang1981/pdf2images/blob/master/pdf2image</description>
    </item>
    <item>
      <title>Golang 中通过gRPC调用Python实现的功能</title>
      <link>https://jerrywang1981.github.io/post/go/python-go-rpc/</link>
      <pubDate>Mon, 04 May 2020 00:11:12 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/go/python-go-rpc/</guid>
      <description>背景 有时候，我们在python中实现了一个功能，这功能如果用golang重新写呢，会比较麻烦，如果要在golang中调用python中的功能。 方式有很多，主要就是两个程序如果沟通的问题，那方式就各种各样了，可以通过http协议，json/xml等格式，或者tcp, 当然还有个 选择就</description>
    </item>
    <item>
      <title>Class Object Method</title>
      <link>https://jerrywang1981.github.io/post/python/class-object-method/</link>
      <pubDate>Sat, 02 May 2020 21:59:33 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/class-object-method/</guid>
      <description>对象中通过类或者实例的方法调用方式的不同 如果通过类中取得的方法，需要传入实例 class A(object): def test(self, *args): print(*args) if __name__ == &amp;#39;__main__&amp;#39;: a = A() a.test(7, 8, 9) # method from class object m1 = A.test m1(a, 7, 8, 9, 10) # method from instance object m2 = a.test m2(7, 8, 9, 10) 也就是说, 实例方法的第一个参数是实例本身，如果写decorator的时候，要特别注意 比如print_args这个decor</description>
    </item>
    <item>
      <title>05 Python Dictionary</title>
      <link>https://jerrywang1981.github.io/post/python/05-Python-Dictionary/</link>
      <pubDate>Sat, 25 Apr 2020 18:35:30 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/05-Python-Dictionary/</guid>
      <description>Dictionary ###创建方式 a = dict() a = {} a = {&amp;#39;a&amp;#39;: 1, &amp;#39;b&amp;#39;: 2} a = dict(a=1,b=2) a = [(&amp;#39;a&amp;#39;, 1), (&amp;#39;b&amp;#39;, 2)] b = dict(a) a = zip(&amp;#39;abc&amp;#39;, [1,2,3]) a = { x:x*2 for x in range(10) } a = dict.fromkeys(range(3), &amp;#39;y&amp;#39;)</description>
    </item>
    <item>
      <title>04 Python Tuple</title>
      <link>https://jerrywang1981.github.io/post/python/04-python-tuple/</link>
      <pubDate>Fri, 24 Apr 2020 21:54:58 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/04-python-tuple/</guid>
      <description>Tuple Python 的元组与列表类似，不同之处在于元组的元素不能修改。 不可变 空元组, 一个元素元组 元组运算 元组在参数中的应用 创建tuple的方式</description>
    </item>
    <item>
      <title>03 Python List</title>
      <link>https://jerrywang1981.github.io/post/python/03-python-list/</link>
      <pubDate>Fri, 24 Apr 2020 21:05:59 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/03-python-list/</guid>
      <description>列表 序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置，或索引，第一个索引是0，第二个索引是1，依此类推 索引访问 长度 空或者False 创建列表的方式 a = [] a = [1,2,3] a = list() a = list(&amp;#39;abcdefg&amp;#39;) a = list(range(10)) a = [ x for x in range(20) ] append In [17]: a=[1,2,3] In [18]: a.append(4) In [19]: a Out[19]: [1, 2, 3, 4] In [20]: extend In [21]: a Out[21]: [1, 2, 3, 4] In [22]: a.extend([5,6])</description>
    </item>
    <item>
      <title>02 Python Variable</title>
      <link>https://jerrywang1981.github.io/post/python/02-python-variable/</link>
      <pubDate>Thu, 23 Apr 2020 14:23:52 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/02-python-variable/</guid>
      <description>变量 命名 变量名必须是大小写英文、数字和_的组合，且不能用数字开头 允许的变量名：a, a1, B_5, ____5, _C 静态语言，动态语言 大小写敏感 Dunder 规范 snake_case e.g. a_b, total_count 变量 函数名 模块 包 CamelCase e.g. Person, TeamMember 类名 常量 一般用大写, PI=3.14</description>
    </item>
    <item>
      <title>01 Python Data Type</title>
      <link>https://jerrywang1981.github.io/post/python/01-python-data-type/</link>
      <pubDate>Thu, 23 Apr 2020 13:51:39 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/01-python-data-type/</guid>
      <description>基本数据类型 整数 int 十进制： -1, 0, 1000, -8000, 88 八进制：0o10, 0o27 十六进制：0x10, 0xff 数学运算 + - * / // % 浮点数 float 比如：1.23, 2.6e3 字符串 str 字符串是以单引号&amp;rsquo;或双引号&amp;quot;括起来的任意文本 如果字符串内部包含&amp;rsquo;,&amp;quot;, 可以用转义字符\来标识 转义 \ 不转义</description>
    </item>
    <item>
      <title>Python Nest</title>
      <link>https://jerrywang1981.github.io/post/python/python-nest/</link>
      <pubDate>Sat, 11 Apr 2020 21:15:14 +0800</pubDate>
      <guid>https://jerrywang1981.github.io/post/python/python-nest/</guid>
      <description>背景 最近有做一个nestjs的项目，用的nestjs实现的微服务，如果所有的功能都用node/nestjs写，那倒也没有什么问题了。可是有一个功能是需要用到机器学习，代码是用python写的，需要用python实现一个微服务，供nest app来调用，同时，python代码也需要调</description>
    </item>
  </channel>
</rss>
