- Last 7 days
-
python.plainenglish.io python.plainenglish.io
-
Holy xxx, it takes 3.37s to calculate merely 1,000,000 reciprocal numbers. The same logic in C takes just a blink: 9ms; C# takes 19ms; Nodejs takes 26ms; Java takes 5ms! and Python takes self-doubting 3.37 SECONDS.
But
numpy
takes just 2ms!
-
-
steelkiwi.com steelkiwi.com
-
Python by the Numbers: How Strong Is the Demand for Python on the Software Development Market in 2021?
Python is hot on the artificial intelligence and machine learning market. It was used by Apple to support Siri and Face ID and deployed by Amazon as a backbone of the personalized recommendation engine. So, how strong is Python demand on the market?
-
- Mar 2021
-
stackoverflow.com stackoverflow.com
-
As to why both is_a? and kind_of? exist: I suppose it's part of Ruby's design philosophy. Python would say there should only be one way to do something; Ruby often has synonymous methods so you can use the one that sounds better. It's a matter of preference.
-
-
docs.github.com docs.github.com
-
www.youtube.com www.youtube.com
-
I don't like notebooks.- Joel Grus (Allen Institute for Artificial Intelligence)
Because it teaches scientists bad programming habits:
- no modularity (but result depend on order of evaluation)
- no testabilty
- cannot view code without opening jupyter
- missing history (ok
%history
works)
-
-
stackoverflow.com stackoverflow.com
-
session.query(Book.author, Chapter.title).select_from(Book).join(Book.chapters) or session.query(Book).options(load_only("author"), joinedload("chapters").load_only("title"))
Very useful quick guide for how to only load certain foreign key fields.
-
-
www.maartenbreddels.com www.maartenbreddels.com
-
Advanced profile tricks for python
-
-
-
McCabe, Stefan, Leo Torres, Timothy LaRock, Syed Arefinul Haque, Chia-Hung Yang, Harrison Hartle, and Brennan Klein. ‘Netrd: A Library for Network Reconstruction and Graph Distances’. ArXiv:2010.16019 [Physics], 29 October 2020. http://arxiv.org/abs/2010.16019.
-
-
en.wikipedia.org en.wikipedia.orgPyPy1
-
PyPy uses a technique known as meta-tracing, which transforms an interpreter into a tracing just-in-time compiler.
-
-
pytorch.org pytorch.org
-
Default: 0
padding's defautl value ==0
-
- Feb 2021
-
sobolevn.me sobolevn.me
-
You can use container values, that wraps actual success or error value into a thin wrapper with utility methods to work with this value. That’s exactly why we have created @dry-python/returns project. So you can make your functions return something meaningful, typed, and safe.
-
-
-
I'm not a fan of listing exceptions functions can throw, especially here in Python, where it's easier to ask forgiveness than permission.
-
-
github.com github.com
-
Interesting project to safely patch 3rdp libs, by detecting any breaking changes if/when the lib gets upgraded in the future.
-
-
www.scrapingbee.com www.scrapingbee.com
-
Here is a quick recap table of every technology we discussed in this blog post.
Quick comparison of Python web scraping tools (socket, urlib3, requests, scrapy, selenium) [below this highlight]
-
- Jan 2021
-
pythonspeed.com pythonspeed.com
-
We recommend the Alpine image as it is tightly controlled and small in size (currently under 5 MB), while still being a full Linux distribution. This is fine advice for Go, but bad advice for Python, leading to slower builds, larger images, and obscure bugs.
Alipne Linux isn't the most convenient OS for Python, but fine for Go
-
-
-
Download the the code for this module as a zip file. Edit the setup.py file and change 'lxml>=3.4,<=4', to 'lxml>=3.4,<=5', save the change and then from within the directory that you downloaded the source run 'python setup.py install' and you should be good to go.
fix: pyinstaper lxml compile error
-
-
-
When It Makes Sense to Use Python to Build an eCommerce Website
-
-
towardsdatascience.com towardsdatascience.com
-
Did you know that everything you can do in VBA can also be done in Python? The Excel Object Model is what you use when writing VBA, but the same API is available in Python as well.See Python as a VBA Replacement from the PyXLL documentation for details of how this is possible.
We can replace VBA with Python
-
You can write Excel worksheet functions in your Jupyter notebook too. This is a really great way of trying out ideas without leaving Excel to go to a Python IDE.
We can define functions in Python to later use in Excel
-
Use the magic function “%xl_get” to get the current Excel selection in Python. Have a table of data in Excel? Select the top left corner (or the whole range) and type “%xl_get” in your Jupyter notebook and voila! the Excel table is now a pandas DataFrame.
%xl_get
lets us get the current Excel selection in Python -
to run Python code in Excel you need the PyXLL add-in. The PyXLL add-in is what lets us integrate Python into Excel and use Python instead of VBA
PyXLL lets us use Python/Jupyter Notebooks in Excel
-
- Dec 2020
-
steelkiwi.com steelkiwi.com
-
How to Hire an In-house Python & Django Developer: Python/Django Interview Questions, Tips, and Advice
The questions you ask during a Python and Django interview are important, since the right Django interview questions can help you define whether an applicant is the right fit for your company. In this article, we talk about the most crucial stage in the hiring process — the in-person interview — outline Python and Django interview questions, and conclude with dos and don’ts
-
-
selectedfirms.co selectedfirms.co
-
Python Development Trends
It’s highly essential for a developer to learn about the development trends to employ the latest trends in his/her next project, and it’s essential for the customers to know for exploring all the available features. Here are the top Python Development Trends that will also rule 2021.
-
-
stackoverflow.blog stackoverflow.blog
-
Turning my Jupyter-compatible Python code into Flask-compatible Python code took dozens of hours. I ran into small bugs and errors
That's how I always expected Python notebook development to be
-
- Nov 2020
-
github.com github.com
-
Comparison between pytest and unittes test frameworks
Detailed comparison table of pytest vs unittest modules (check below)
Tags
Annotators
URL
-
-
www.tutorialsteacher.com www.tutorialsteacher.com
-
The behaviour of the argument function is extended by the decorator without actually modifying it.
需要修饰的函数被装饰器(decorator)扩展,且不用修改原函数.
Tags
Annotators
URL
-
-
realpython.com realpython.com
-
As of Python 3.6, f-strings are a great new way to format strings.
Python 3.6 之后推出的 f-strings 语法
-
-
numpy.org numpy.org
-
A location into which the result is stored.
out 参数是用来存放结果的 这样可以达到就地计算的效果
-
- Oct 2020
-
docs.python.org docs.python.org
-
Text Sequence Type
文本序列类型
Tags
Annotators
URL
-
-
docs.python.org docs.python.org
-
和 Python 里的字符串和列表切片不同,你不能在 start, stop 或者 step 这些参数中使用负数。:
通过 itertools.islice() 可以实现 set dict 的切片操作。
-
line.strip()
去掉字符串头和尾的空格
-
# A recursive generator that generates Tree leaves in in-order.
用生成器来写中序遍历
-
Any function containing a yield keyword is a generator function;
函数体里包含 yield 关键字的函数都是生成器函数
-
Generators
生成器
-
if expression is creating a tuple, it must be surrounded with parentheses.
如果表达式创建了一个元组,那么必须被圆括号包裹.
-
list comprehensions
List Comprehensions 列表推导
Generator Expressions 生成器表达式
-
An object is called iterable if you can get an iterator for it.
言外之意就是,如果调用 iter 之后没有报 TypeError, 说明该对象是可迭代的(iterable)
-
An iterator is an object representing a stream of data;
迭代器是一个代表数据流的对象
Tags
Annotators
URL
-
-
docs.python.org docs.python.org
-
key 形参用来指定在进行比较前要在每个列表元素上调用的函数
与 C++ 不同,key 形参用来指定进行比较前在每个列表元素上调用的函数。
-
-
blog.prokulski.science blog.prokulski.science
-
Sprawdźmy który rodzaj modelu daje najlepszą skuteczność: Python sns.boxplot(data=models_df, x='score', y='model') 1 sns.boxplot(data=models_df, x='score', y='model')
After comparing the pipelined ML models, we can easily display a comparison boxplot.
Well working ML models: 1) XGBoost 2) LightGBM 3) CatBoost.
-
Przy tych danych wygląda, że właściwie nie ma większej różnicy (nie bijemy się tutaj o 0.01 punktu procentowego poprawy accuracy modelu). Może więc czas treningu jest istotny? Python sns.boxplot(data=models_df, x='time_elapsed', y='model') 1 sns.boxplot(data=models_df, x='time_elapsed', y='model')
Training time of some popular ML models. After considering the performance, it's worth using XGBoost and LightGBM.
-
Teraz w zagnieżdżonych pętlach możemy sprawdzić każdy z każdym podmieniając klasyfikatory i transformatory (cała pętla trochę się kręci):
Example (below) of when creating pipelines with scikit-learn makes sense. Basically, it's convenient to use it while comparing multiple models in a loop
-
-
-
hyperscript is more concise because it's just a function call and doesn't require a closing tag. Using it will greatly simplify your tooling chain.
I suppose this is also an argument that Python tries to make? That other languages have this con:
- cons: closing tags make it more verbose / increase duplication
and that Python is simpler / more concise because it uses indentation instead of closing delimiters like
end
or}
?
- cons: closing tags make it more verbose / increase duplication
and that Python is simpler / more concise because it uses indentation instead of closing delimiters like
-
-
-
Used by convention to avoid naming conflicts with Python keywords.
var_ 主要用来避免与关键字冲突
-
meant as a hint to the programmer only.
_var 这种单下划线对于解释器没有实际作用.程序员之间表示内部使用的公用做法
Tags
Annotators
URL
-
-
realpython.com realpython.com
-
The second is a “throwaway” variable that we don’t need just yet, denoted with an underscore.
用下划线来表示没有用的变量
Tags
Annotators
URL
-
-
realpython.com realpython.com
-
If <key> is not found, it returns None
.get(key) 如果键存在返回相应的值,否则返回 None
-
The len() function returns the number of key-value pairs in a dictionary
len 函数返回字典中键值对的个数
-
the in and not in operators
用来检查键值是否在字典中。
-
key values are simple strings
当键值都是简单字符串的时候,键可以不用引号包裹。
-
Restrictions on Dictionary Values
Python对 dict 的值没有限制, 可以是任意类型(包括可变类型和自定义对象)
-
it is not quite correct to say an object must be immutable to be used as a dictionary key.
一个对象必须是不可变的才能用作 dict 的键。
这句话不是很准确。 严谨的说,一个对象必须是可哈希(hashable) 的,才可以用作 dict 的键。
-
Restrictions on Dictionary Keys#
dict 键值的限制:
- 键不能重复
- 键必须是不可变量(列表、字典不可以)
-
a dictionary key must be of a type that is immutable.
dict 键必须是不可修改的(immutable)
-
the values contained in the dictionary don’t need to be the same type.
dict 键和值的类型不必相同
-
Python does guarantee that the order of items in a dictionary is preserved.
尽管 Python 中访问字典元素与顺序无关, 但是 Python 会保存字典中元素定义的顺序(Python 3.7 引入的新特性).
-
they have nothing to do with the order of the items in the dictionary.
Python 中字典同样可以通过数字来访问,但是与列表不同的是,数字大小与元素的顺序没有关系.
-
If you refer to a key that is not in the dictionary, Python raises an exception:
https://realpython.com/python-defaultdict 可以参看这篇文章. defaultdict 可以处理 missing key 的情况.
-
Dictionary elements are not accessed by numerical index
注意: Python 中字典不能通过数字下标访问元素.
-
A list of tuples works well for this:
可以通过元素列表初始化字典.
dd = dict([('a',1),('b',2)])
-
Dictionaries and lists share the following characteristics:
Python 中字典和列表的相同点是:
- 可以修改的
- 动态变化,支持增加和缩减.
- 支持嵌套. 列表中可以嵌套另一个列表, 字典可以包含另一个字典.字典也可以包含列表.
-
Dictionaries differ from lists primarily in how elements are accessed:
Python 中字典与列表的不同点:
- 列表通过下标访问元素
- 字典用过键值访问元素
-
-
realpython.com realpython.com
-
这篇文章主要介绍了 Python 中字典处理缺省键值的方法。
引入了一个新的数据类型 defaultdict,并介绍了它访问和修改不存在键值时的机制。
主要是重写了
.missing__()
使得在通过 subscription operation 访问修改缺省键值时自动调用该方法,从而避免了dict 的 TypeError。 -
the instance behaves like a standard dictionary.
如果初始化 defaultdict 没有参数, 该变量蜕变成一个标准 dict。
-
The first argument to the Python defaultdict type must be a callable that takes no arguments and returns a value.
不接受形参,并且返回一个值。
-
Using the Python defaultdict Type for Handling Missing Keys
用 Python 的 defaultdict 处理不存在的键
想要学习 defaultdict 的原因: 看到 up 主在实现 DFS 的时候用到了这个语法
可以看到的是作者用到了对缺省键的访问操作。
-
not to call it using the parentheses at initialization time.
注意 给
.default_factory
赋值的时候一定不要带括号。 -
the dictionary assigns it the default value that results from calling list().
当我们访问不存在键的时候,defaultdict 会自动将调用 default_factory 的值赋给该键。
-
if you call .setdefault() on an existing key, then the call won’t have any effect on the dictionary.
如果对已存在的键调用 setdefault ,不会修改原值。
如果是缺省键,就会创建新的键值对。
-
four available ways to handle missing keys
处理缺省键的四种方式:
- .setdefault()
- .get()
- key in dict
- try and except
-
if you try to access or modify a missing key, then defaultdict will automatically create the key and generate a default value for it.
如果你想要访问或者修改一个缺省键值, defaultdict 会自动创建这个键然后生成一个默认值
-
Decide when and why to use a Python defaultdict rather than a standard dict
这句子不错
Decide when and why to use a Python defaultdict rather than a standard dict.
-
augmented assignment operator
augemented assignment operators In Chinese: 增强赋值运算符
-
Sets are collections of unique objects,
Python 中的 set 是单一对象的集合,不存在重复项。 与 C++ 的 set , unordered_set 类似
-
for large datasets, it can also be a lot faster and more efficient.
defaultdict 速度和效率往往好于 dict
-
Diving Deeper Into defaultdict
深入分析 defaultdict
-
When and why to use a Python defaultdict rather than a regular dict
何时以及为什么用 Python 的 defaultdict 而不是常规的 dict
-
-
docs.python.org docs.python.org
-
Note that __missing__() is not called for any operations besides __getitem__().
missing() 不会被除 getitem() 之外的其他操作调用。
Tags
Annotators
URL
-
-
arogozhnikov.github.io arogozhnikov.github.io
-
use code to parameterize calls:
You can write Python code to parametrize calls:
python -c " from mymodule import set_dragon_feeding_schedule, Creatures, Date set_dragon_feeding_schedule( feeding_times=['10:00', '14:00', '18:00'], dishes={Creatures.Tiger: 2, Creatures.Human: 1}, start_day=Date('1020-03-01'), ) "
instead of:
python -m mymodule \ set_dragon_feeding_schedule \ --feeding-times ['10:00','14:00','18:00'] # hopefully this way it gets recognized \ # how will you define parsing a dict with enum to integer mapping? --dishes=Creatures.Tiger:2 \ --dishes=Creatures.Human:1 \ --start-day=1020-03-21 # BTW bash allows no comments in multiline calls
-
That’s it. Types are parsed, checked and converted. Defaults and description are picked from function itself. Even provides bash completions you can install. You wrote no code for that!
Good example of writing CLI interfaces in Python with typer:
import typer from pathlib import Path app = typer.Typer() @app.command() def find_dragon(name: str, path: Path, min_age_years: int = 200): <implementation goes here> @app.command() def feed_dragon(dragon_name: str, n_humans: int = 3): <implementation goes here> if __name__ == "__main__": app()
later we can call it that way:
python example.py find_dragon 'Drake' --path /on/my/planet
-
-
docs.python.org docs.python.org
-
Merge (|) and update (|=) operators have been added to the built-in dict class. Those complement the existing dict.update and {**d1, **d2} methods of merging dictionaries.
From Python 3.9 it's much more convenient to:
- merge dictionaries with the
|
(pipe) operator, e.g.x | y
- update them with
|=
- merge dictionaries with the
-
-
datarevenue.com datarevenue.com
-
Use Streamlit if you want to get going as quickly possible and don’t have strong opinions or many custom requirements.Use Dash if you need something more flexible and mature, and you don’t mind spending the extra engineering time.
Streamlit vs Dash
-
Here’s a table showing the tradeoffs:
Comparison of dashboard tech stack as of 10/2020:
-
-
igraph.discourse.group igraph.discourse.group
-
Python-igraph 0.8.3. (2020, October 8). Igraph Support Forum. https://igraph.discourse.group/t/python-igraph-0-8-3/473
-
- Sep 2020
-
www.bd-econ.com www.bd-econ.com
-
The Census FTP page contains the microdata and dictionaries identifying each variable name, location, value range, and whether it applies to a restricted sample. To follow this example, download the April 2017 compressed data file that matches your operating system and unpack it in the same location as the python code. Next download the January 2017 data dictionary text file and save it in the same location.
This is important
-
Tags
Annotators
URL
-
-
devguide.python.org devguide.python.org
-
3.5 PEP 478 security 2015-09-13 2020-09-13 Larry Hastings
All Python versions less than 3.6 are now EOL
-
-
github.com github.com
-
This command will give you the top 25 stocks that had the highest anomaly score in the last 14 bars of 60 minute candles.
Supriver - find high moving stocks before they move using anomaly detection and machine learning. Surpriver uses machine learning to look at volume + price action and infer unusual patterns which can result in big moves in stocks
Tags
Annotators
URL
-
-
greenteapress.com greenteapress.com
-
Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements or sometimes itemsThe syntax for accessing the elements of a list is the same as for accessing the characters of a string—the bracket operator. The expression inside the brackets specifies the index. Remember that the indices start at 0:
cheeses[0]
' Cheddar ' Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned. numbers = [42, 123] numbers[1] = 5 numbers [42, 5] The most common way to traverse the elements of a list is with a for loop. The syntax is the same as for strings: for cheese in cheeses: print(cheese) This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine the built-in functions range and len : for i in range(len(numbers)): numbers[i] = numbers[i] 2 This loop traverses the list and updates each element. len returns the number of elements in the list. range returns a list of indices from 0 to
n
1, where n is the length of the list. Each time through the loop i gets the index of the next element. The assignment statement in the body uses i to read the old value of the element and to assign the new value. The + operator concatenates lists: a = [1, 2, 3] b = [4, 5, 6] c = a + b c [1, 2, 3, 4, 5, 6] The operator repeats a list a given number of times: [0] 4 [0, 0, 0, 0] [1, 2, 3] 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times. *ython provides methods that operate on lists. For example, append adds a new element to the end of a list: t = [ ' a ' , ' b ' , ' c ' ] t.append( ' d ' ) t [ ' a ' , ' b ' , ' c ' , ' d ' ] extend takes a list as an argument and appends all of the elements: t1 = [ ' a ' , ' b ' , ' c ' ] t2 = [ ' d ' , ' e ' ] t1.extend(t2) t1 [ ' a ' , ' b ' , ' c ' , ' d ' , ' e ' ] This example leaves t2 unmodified. sort arranges the elements of the list from low to high: t = [ ' d ' , ' c ' , ' e ' , ' b ' , ' a ' ] t.sort() t [ ' a ' , ' b ' , ' c ' , ' d ' , ' e ' ] Most list methods are void; they modify the list and return None . If you accidentally write t = t.sort() , you will be disappointed with the result.
-
*A string is a sequence , which means it is an ordered collection of other values.
- You can access the characters one at a time with the
bracket operator:
fruit =
' banana ' letter = fruit[1] The second statement selects character number 1 from fruit and assigns it to letter . The expression in brackets is called an index .A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal A segment of a string is called a slice . Selecting a slice is similar to selecting a character: s = ' Monty Python ' s[0:5] ' Monty ' s[6:12] ' PythonIt is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example: greeting = ' Hello, world! ' greeting[0] = ' J ' TypeError: ' str ' object does not support item assignment The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later (Section 10.10). The reason for the error is that strings are immutable ,
- You can access the characters one at a time with the
bracket operator:
-
-
arxiv.org arxiv.org
-
Miller, J. C., & TIng, T. (2019). EoN (Epidemics on Networks): A fast, flexible Python package for simulation, analytic approximation, and analysis of epidemics on networks. Journal of Open Source Software, 4(44), 1731. https://doi.org/10.21105/joss.01731
-
- Aug 2020
-
cmdlinetips.com cmdlinetips.com
-
List Comprehensions
-
-
epistasislab.github.io epistasislab.github.io
-
Once TPOT is finished searching (or you get tired of waiting), it provides you with the Python code for the best pipeline it found so you can tinker with the pipeline from there.
After all, magically you get the right Python snippet (based on scikit-learn)
-
TPOT is a Python Automated Machine Learning tool that optimizes machine learning pipelines using genetic programming
TPOT automates the following:
- feature selection
- feature preprocessing
- feature construction
- model selection
- parameter optimisation
Tags
Annotators
URL
-
-
steelkiwi.com steelkiwi.com
-
What Is Python & Django and Why Are They Considered a Top Choice for Web Development?
-
-
hackernoon.com hackernoon.com
-
fastapi.tiangolo.com fastapi.tiangolo.com
-
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"}
Basic get api with fastapi
Tags
Annotators
URL
-
-
towardsdatascience.com towardsdatascience.com
-
barChart = pygal.Bar(height=400)[barChart.add(x[0], x[1]) for x in mean_per_state.items()]display(HTML(base_html.format(rendered_chart=barChart.render(is_unicode=True))))
How to display html finally in jupyter notebooks
-
from IPython.display import display, HTMLbase_html = """<!DOCTYPE html><html> <head> <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script> <script type="text/javascript" src="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js""></script> </head> <body> <figure> {rendered_chart} </figure> </body></html>"""
How to display html in jupyter notebooks
-
-
typer.tiangolo.com typer.tiangolo.com
-
def main(name: str = typer.Argument("World", hidden=True)):
Hide a CLI argument from the help text
-
def main(name: str = typer.Argument(..., help="The name of the user to greet")): typer.echo(f"Hello {name}")
Add a help text for a CLI argument You can use the help parameter to add a help text for a CLI argument. This example without default
Tags
Annotators
URL
-
-
typer.tiangolo.com typer.tiangolo.com
-
import random import typer def get_name(): return random.choice(["Deadpool", "Rick", "Morty", "Hiro"]) def main(name: str = typer.Argument(get_name)): typer.echo(f"Hello {name}") if __name__ == "__main__": typer.run(main)
Setting a dynamic default value. And we can even make the default value be dynamically generated by passing a function as the first function argument:
Tags
Annotators
URL
-
-
typer.tiangolo.com typer.tiangolo.com
-
def main(name: Optional[str] = typer.Argument(None)):
How to define an optional parameter in function definition in typer
Tags
Annotators
URL
-
-
nextjournal.com nextjournal.com
- Jul 2020
-
libradocs.github.io libradocs.github.io
-
Libra is a machine learning API designed for non-technical users. This means that it assumes that you have no background in ML whatsoever.
With Libra you can write your ML code much faster:
For example, that's how it compares to Keras.
Tags
Annotators
URL
-
-
hackersandslackers.com hackersandslackers.com
-
github.com github.com
Tags
Annotators
URL
-
-
github.com github.com
Tags
Annotators
URL
-
-
www.geeksforgeeks.org www.geeksforgeeks.org
-
Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory. Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
Simple explanation of a difference between return and yield in Python
-
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
Why don't you allow a range without end, like (1..)? There are two advantages. First, we can write ary[1..] instead of ary[1..-1]. The -1 is one of the most I dislike in Ruby. It is very magical, ugly, redundant, and disappointing. I envy Python's ary[1:]. I know that ary.drop(1) is slightly fast, but too long for such a common operation, IMO. Second, we can write (1..).each {|n| ... }.
-
-
github.com github.com
-
stackoverflow.com stackoverflow.com
-
from difflib import SequenceMatcher squeeze=SequenceMatcher( None, A, B ) print "A - B = [%s]"%( reduce( lambda p,q: p+q, map( lambda t: squeeze.a[t[1]:t[2]], filter(lambda x:x[0]!='equal', squeeze.get_opcodes() ) ) ) )
-
The mixed functional, OO and procedural nature of python has always been, IMO, one of its strengths.
-
However, this is not working in Python 3, as print has changed from a command to a function, and reduce, filter and map have been declared unpythonic.
-
- Jun 2020
-
-
Privacy Preserving Data Analysis of Personal Data (May 27, 2020). (n.d.). Retrieved June 25, 2020, from https://www.youtube.com/watch?time_continue=11&v=wRI84xP0cVw&feature=emb_logo
-
-
stackoverflow.com stackoverflow.com
-
# scenario-1: delete in session: SA might set category_id of all chilren Products to None c1 = session.query(Category).get(1) session.delete(c1) session.commit() # scenario-2: delete without loading an object into the session: SA will perform no additional logic session.query(Category).filter(Category.id == 2).delete() session.commit()
Weird sqlalchemy behavior but totally an accurate statement. When using postgres and not specifying how to handle deletes, if you delete via object instead of filter.delete, sqlalchemy will set all children with foreignkey id's to None.
-
-
flask-sqlalchemy.palletsprojects.com flask-sqlalchemy.palletsprojects.com
-
flask_sqlalchemy.get_debug_queries()
Dumps out query history-super helpful for debug!
-
-
stackoverflow.com stackoverflow.com
-
session_options={"autoflush": False, "autocommit": False, "expire_on_commit": False}
Disable autocommit and autoflush in sqlalchemy.
-
-
martinheinz.dev martinheinz.dev
-
in Python - setting up basic logger is very simple
Apart from printing the result, it is better to debug with logging.
Sample logger:
import logging logging.basicConfig( filename='application.log', level=logging.WARNING, format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s', datefmt='%H:%M:%S' ) logging.error("Some serious error occurred.") logging.warning('Function you are using is deprecated.')
the sample result:
[12:52:35] {<stdin>:1} ERROR - Some serious error occurred. [12:52:35] {<stdin>:1} WARNING - Function you are using is deprecated.
to find its location, type:
logging.getLoggerClass().root.handlers[0].baseFilename
Tags
Annotators
URL
-
-
alembic.sqlalchemy.org alembic.sqlalchemy.org
-
Integration of Naming Conventions into Operations, Autogenerate
Importance of naming conventions for sqlalchemy when running alembic db migrations.
-
-
www.educative.io www.educative.io
-
it allows each new key to be given a default value based on the type of dictionary being created
Difference between defaultdict and dict
-
Memoization can be achieved through Python decorators
Example of memoization in Python:
import timeit def memoize_fib(func): cache = {} def inner(arg): if arg not in cache: cache[arg] = func(arg) return cache[arg] return inner def fib(num): if num == 0: return 0 elif num == 1: return 1 else: return fib(num-1) + fib(num-2) fib = memoize_fib(fib) print(timeit.timeit('fib(30)', globals=globals(), number=1)) print(timeit.timeit('fib(30)', globals=globals(), number=1)) print(timeit.timeit('fib(30)', globals=globals(), number=1))
Output:
4.9035000301955733e-05 1.374000021314714e-06 1.2790005712304264e-06
-
A deep copy refers to cloning an object. When we use the = operator, we are not cloning the object; instead, we reference our variable to the same object (a.k.a. shallow copy).
Difference between a shallow copy (=) and a deep copy:
-
Python 2 is entrenched in the software landscape to the point that co-dependency between several softwares makes it almost impossible to make the shift.
Shifting from Python 2 to 3 isn't always straight to the point
-
The @property decorator allows a function to be accessed like an attribute.
@property decorator
-
var = true_val if condition else false_val
Example of a ternary operator (one-line version of if-else):
to_check = 6 msg = "Even" if to_check%2 == 0 else "Odd" print(msg)
Usual if-else:
msg = "" if(to_check%2 == 0): msg = "Even" else: msg = "Odd" print(msg)
-
This method is automatically called to allocate memory when a new object/ instance of a class is created.
__init__
method in Python (which essentially all classes have) -
decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure
Decorators
-
The philosophy behind Flask is that it gives only the components you need to build an app so that you have the flexibility and control. In other words, it’s un-opinionated. Some features it offers are a build-int dev server, Restful request dispatching, Http request handling, and much more.
Flask isn't as full of features as Django (which makes him lighter), but it still offers:
- build-int dev server
- Restful request dispatching
- HTTP request handling
- much more...
-
The only difference is that range returns a Python list object and xrange returns an xrange object. This means that xrange doesn’t actually generate a static list at run-time like range does.
Difference between range() and xrange()
-
NumPy is not just more efficient; it is also more convenient. You get a lot of vector and matrix operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently implemented.
Main advantage of NumPy arrays over (nested) Python lists
-
process of retrieving original Python objects from the stored string representation
Unpickling
-
Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function
Pickling
-
The beauty of lambda functions lies in the fact that they return function objects. This makes them helpful when used with functions like map or filter which require function objects as arguments.
When to use lambdas
-
In Python, the term monkey patch only refers to dynamic modifications of a class or module at run-time.
Monkey patching in Python
-
Python has a multi-threading package but if you want to multi-thread to speed your code up, then it’s usually not a good idea to use it. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread.
Multi-threading is available in Python but it;s often not a good idea
Tags
Annotators
URL
-
-
www.humblebundle.com www.humblebundle.com
-
content Gray Hat Python
PAY $1 OR MORE TO ALSO UNLOCK!
Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming MSRP: $23.95 Already had
Invent Your Own Computer Games with Python, 4th Edition MSRP: $23.95 Already had
Black Hat Python: Python Programming for Hackers and Pentesters MSRP: $27.95 https://www.amazon.com/Black-Hat-Python-Programming-Pentesters-ebook/dp/B00QL616DW/
Gray Hat Python: Python Programming for Hackers and Reverse Engineers MSRP: $31.95 https://www.amazon.com/Gray-Hat-Python-Programming-Engineers-ebook/dp/B007V2DNEK/
PAY $8 OR MORE TO ALSO UNLOCK!
Mission Python: Code a Space Adventure Game! MSRP: $23.95
Python for Kids: A Playful Introduction to Programming MSRP: $27.95
Cracking Codes with Python: An Introduction to Building and Breaking Ciphers MSRP: $23.95
Python Playground: Geeky Projects for the Curious Programmer MSRP: $23.95
Math Adventures with Python: An Illustrated Guide to Exploring Math with Code MSRP: $23.95
PAY $15 OR MORE TO ALSO UNLOCK!
Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming MSRP: $31.95
Automate the Boring Stuff with Python, 2nd Edition: Practical Programming for Total Beginners MSRP: $31.95
Python Flash Cards PDF ONLY MSRP: $27.95
Serious Python: Black-Belt Advice on Deployment, Scalability, Testing, and More MSRP: $27.95
Impractical Python Projects: Playful Programming Activities to Make You Smarter
-
- May 2020
-
github.com github.com
-
DataForScience/Epidemiology101. (2020). [Jupyter Notebook]. Data For Science. https://github.com/DataForScience/Epidemiology101 (Original work published 2020)
-
-
medium.com medium.com
-
Hot Reloading refers to the ability to automatically update a running web application when changes are made to the application’s code.
Hot Reloading is what provides a great experience with updating your Dash code inside the Jupyter Notebooks
-
JupyterDash supports three approaches to displaying a Dash application during interactive development.
3 display modes of Dash using Jupyter Notebooks:
- app.run_server(mode='external')
- app.run_server(mode='inline')
- app.run_server(mode='jupyterlab')
-
# Run app and display result inline in the notebookapp.run_server(mode='inline')
Moreover, you can display your Dash result inside a Jupyter Notebook using
IPython.display.IFrame
with this line:app.run_server(mode='inline')
-
If running the server blocks the main thread, then it’s not possible to execute additional code cells without manually interrupting the execution of the kernel.JupyterDash resolves this problem by executing the Flask development server in a background thread. This leaves the main execution thread available for additional calculations. When a request is made to serve a new version of the application on the same port, the currently running application is automatically shut down first. This makes is possible to quickly update a running application by simply re-executing the notebook cells that define it.
How Dash can run inside Jupyter Notebooks
-
You can also try it out, right in your browser, with binder.
Dash can be tried out inside a Jupyter Notebook right in your browser using binder.
-
Then, copy any Dash example into a Jupyter notebook cell and replace the dash.Dash class with the jupyter_dash.JupyterDash class.
To use Dash in Jupyter Notebooks, you have to import:
from jupyter_dash import JupyterDash
instead of:
import dash
Therefore, all the imports could look like that for a typical Dash app inside a Jupyter Notebook:
import plotly.express as px from jupyter_dash import JupyterDash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output
-
-
realpython.com realpython.com
-
there is a particularly unconventional mechanism by which these coroutines actually get run. Their result is an attribute of the exception object that gets thrown when their .send() method is called.
A generator signals its termination with an exception (
StopIteration
). This was already a feature of generators.The "trick" to make coroutines work, is that this exception is used as a real return value when the coroutine terminates (with a
return
statement).The return value is (somehow) incapsulated into the exception that's being raised, and the event loop handles it.
In depth explanation: http://www.dabeaz.com/coroutines/
-
If Python encounters an await f() expression in the scope of g(), this is how await tells the event loop, “Suspend execution of g() until whatever I’m waiting on—the result of f()—is returned. In the meantime, go let something else run.”
The event loop in python orchestrate the whole "simulated concurrency" among coroutines.
Deep down, python has a library
select
that talks very closely with the OS and gets data from sockets. This is actually how the orchestra works really at the bottom layer. https://docs.python.org/3/library/select.html#module-select -
A coroutine is a specialized version of a Python generator function
in fact, async in python was built on top of the generators (that existed in python since long before).
A generator is a function that can be suspended --yielding a value-- and then resumed.
A key functionality of the generators in python is that when they are resumed they can receive a value back from the code that stopped/resumed them. This translates into the syntax
new_value = await coroutine()
-
Asynchronous routines are able to “pause” while waiting on their ultimate result and let other routines run in the meantime. Asynchronous code, through the mechanism above, facilitates concurrent execution. To put it differently, asynchronous code gives the look and feel of concurrency.
Async routines collaborate each with the others by saying when they can be paused. This is why they are called coroutines.
The communication between coroutines happen thanks to the event loop
Tags
Annotators
URL
-
-
www.runoob.com www.runoob.com
-
Python 中的变量赋值不需要类型声明。 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息。 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。 等号(=)用来给变量赋值。 等号(=)运算符左边是一个变量名,等号(=)运算符右边是存储在变量中的值。
变量
Tags
Annotators
URL
-
-
www.runoob.com www.runoob.com
-
像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组。 我们将首行及后面的代码组称为一个子句(clause)。
代码组
-
Python语句中一般以新行作为语句的结束符。 但是我们可以使用斜杠( \)将一行的语句分为多行显示,如下所示:
多行语句
-
在 Python 里,标识符由字母、数字、下划线组成。 在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。 Python 中的标识符是区分大小写的。 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入。 以双下划线开头的 __foo 代表类的私有成员,以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数
Python标识符规则。
Tags
Annotators
URL
-
-
docs.python.org docs.python.org
-
python -m http.server 8000
quick web server from terminal using python3 http module
Tags
Annotators
URL
-
-
www.estimationstats.com www.estimationstats.com
-
You can create estimation plots here at estimationstats.com, or with the DABEST packages which are available in R, Python, and Matlab.
You can create estimation plots with:
Tags
Annotators
URL
-
-
mateuszgrzyb.pl mateuszgrzyb.pl
-
Kolejna zmienna - "Recency", czyli informacja o tym, jak dawno klient robił zakupy w sklepie.
To calculate RFM we need recency value. Firstly, we shall specify the most recent transaction as "today" and then to find the latest transaction of a specific client:
df['Recency'] = (today - df.InvoiceDate)/np.timedelta64(1,'D')
Calculating frequency and aggregating data of each client may be done with the
groupby
method:abt = df.groupby(['CustomerID']).agg({'Recency':'min', 'MonetaryValue':'sum', 'InvoiceNo':'count'})
lastly, we can update the column names and display RFM data:
abt = df.groupby(['CustomerID']).agg({'Recency':'min', 'MonetaryValue':'sum', 'InvoiceNo':'count'}) abt.rename(columns = {'InvoiceNo':'Frequency'}, inplace = True) abt = abt[['Recency', 'Frequency', 'MonetaryValue']] abt.head()
-
Usuwam brakujące wartości w zmiennej "CustomerID".
Deleting rows where value is null:
df = df[~df.CustomerID.isnull()]
Assigning different data types to columns:
df['CustomerID'] = df.CustomerID.astype(int)
Deleting irrelevant columns:
df.drop(['Description', 'StockCode', 'Country'], axis = 1, inplace = True)
-
Sprawdzam braki danych.
Checking the % of missing data:
print(str(round(df.isnull().any(axis=1).sum()/df.shape[0]*100,2))+'% obserwacji zawiera braki w danych.')
Sample output:
24.93% obserwacji zawiera braki w danych.
-
Buduję prosty data frame z podstawowymi informacjami o zbiorze.
Building a simple dataframe with a summary of our columns (data types, sum and % of nulls):
summary = pd.DataFrame(df.dtypes, columns=['Dtype']) summary['Nulls'] = pd.DataFrame(df.isnull().any()) summary['Sum_of_nulls'] = pd.DataFrame(df.isnull().sum()) summary['Per_of_nulls'] = round((df.apply(pd.isnull).mean()*100),2) summary.Dtype = summary.Dtype.astype(str) print(summary)
the output:
Dtype Nulls Sum_of_nulls Per_of_nulls InvoiceNo object False 0 0.000 StockCode object False 0 0.000 Description object True 1454 0.270 Quantity int64 False 0 0.000 InvoiceDate datetime64[ns] False 0 0.000 UnitPrice float64 False 0 0.000 CustomerID float64 True 135080 24.930 Country object False 0 0.000
-
-
muldoon.cloud muldoon.cloud
-
Programming languages These will probably expose my ignorance pretty nicely.
When to use different programming languages (advice from an Amazon employee):
- Java - enterprise applications
- C# - Microsoft's spin on Java (useful in the Microsoft's ecosystem)
- Ruby - when speed is more important then legibility or debugging
- Python - same as Ruby but also for ML/AI (don't forget to use type hinting to make life a little saner)
- Go/Rust - fresh web service where latency and performance were more important than community/library support
- Haskell/Erlang - for very elegant/mathematical functional approach without a lot of business logic
- Clojure - in situation when you love Lisp (?)
- Kotlin/Scala - languages compiling to JVM bytecode (preferable over Clojure). Kotlin works with Java and has great IntelliJ support
- C - classes of applications (operating systems, language design, low-level programming and hardware)
- C++ - robotics, video games and high frequency trading where the performance gains from no garbage collection make it preferable to Java
- PHP/Hack - testing server changes without rebuilding. PHP is banned at Amazon due to security reasons, but its successor, Hack, runs a lot of Facebook and Slack's backends
-
-
chase-seibert.github.io chase-seibert.github.io
-
Multi-level argparse in Python (parsing commands like git)
-
-
realpython.com realpython.com
-
Python String Formatting Best Practices
Tags
Annotators
URL
-
- Apr 2020
-
-
In Python, when trying to do a dubious operation, you get an error pretty soon. In JavaScript… an undefined can fly through a few layers of abstraction, causing an error in a seemingly unrelated piece of code.
Undefined nature of JavaScript can hide an error for a long time. For example,
function add(a,b) { return + (a + b) } add(2,2) add('2', 2)
will result in a number, but is it the same one?
-
-
towardsdatascience.com towardsdatascience.com
-
How to use the PPS in your own (Python) project
Using PPS with Python
- Download ppscore:
pip install ppscore
shell - Calculate the PPS for a given pandas dataframe:
import ppscore as pps pps.score(df, "feature_column", "target_column")
- Calculate the whole PPS matrix:
pps.matrix(df)
- Download ppscore:
-