464 Matching Annotations
  1. Last 7 days
    1. 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!

    1. 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?

  2. Mar 2021
    1. 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.
    1. 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) untitled24.ipynb
    1. 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.

  3. en.wikipedia.org en.wikipedia.org
    1. PyPy uses a technique known as meta-tracing, which transforms an interpreter into a tracing just-in-time compiler.
  4. Feb 2021
    1. 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.
    1. Interesting project to safely patch 3rdp libs, by detecting any breaking changes if/when the lib gets upgraded in the future.

    1. 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]

  5. Jan 2021
    1. 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

    1. 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

    1. 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

    2. 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

    3. 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

    4. 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

  6. Dec 2020
    1. 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

    1. 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.

    1. 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

  7. Nov 2020
    1. Comparison between pytest and unittes test frameworks

      Detailed comparison table of pytest vs unittest modules (check below)

    1. The behaviour of the argument function is extended by the decorator without actually modifying it.


    1. As of Python 3.6, f-strings are a great new way to format strings.

      Python 3.6 之后推出的 f-strings 语法

    1. A location into which the result is stored.

      out 参数是用来存放结果的 这样可以达到就地计算的效果

  8. Oct 2020
    1. 和 Python 里的字符串和列表切片不同,你不能在 start, stop 或者 step 这些参数中使用负数。:

      通过 itertools.islice() 可以实现 set dict 的切片操作。

    2. line.strip()


    3. # A recursive generator that generates Tree leaves in in-order.


    4. Any function containing a yield keyword is a generator function;

      函数体里包含 yield 关键字的函数都是生成器函数

    5. Generators


    6. if expression is creating a tuple, it must be surrounded with parentheses.


    7. list comprehensions

      List Comprehensions 列表推导

      Generator Expressions 生成器表达式

    8. An object is called iterable if you can get an iterator for it.

      言外之意就是,如果调用 iter 之后没有报 TypeError, 说明该对象是可迭代的(iterable)

    9. An iterator is an object representing a stream of data;


    1. key 形参用来指定在进行比较前要在每个列表元素上调用的函数

      与 C++ 不同,key 形参用来指定进行比较前在每个列表元素上调用的函数。

    1. 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.

    2. 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.

    3. 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

    1. 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 } ?
    1. Used by convention to avoid naming conflicts with Python keywords.

      var_ 主要用来避免与关键字冲突

    2. meant as a hint to the programmer only.

      _var 这种单下划线对于解释器没有实际作用.程序员之间表示内部使用的公用做法

    1. The second is a “throwaway” variable that we don’t need just yet, denoted with an underscore.


    1. If <key> is not found, it returns None

      .get(key) 如果键存在返回相应的值,否则返回 None

    2. The len() function returns the number of key-value pairs in a dictionary

      len 函数返回字典中键值对的个数

    3. the in and not in operators


    4. key values are simple strings


    5. Restrictions on Dictionary Values

      Python对 dict 的值没有限制, 可以是任意类型(包括可变类型和自定义对象)

    6. it is not quite correct to say an object must be immutable to be used as a dictionary key.

      一个对象必须是不可变的才能用作 dict 的键。

      这句话不是很准确。 严谨的说,一个对象必须是可哈希(hashable) 的,才可以用作 dict 的键。

    7. Restrictions on Dictionary Keys#

      dict 键值的限制:

      1. 键不能重复
      2. 键必须是不可变量(列表、字典不可以)
    8. a dictionary key must be of a type that is immutable.

      dict 键必须是不可修改的(immutable)

    9. the values contained in the dictionary don’t need to be the same type.

      dict 键和值的类型不必相同

    10. Python does guarantee that the order of items in a dictionary is preserved.

      尽管 Python 中访问字典元素与顺序无关, 但是 Python 会保存字典中元素定义的顺序(Python 3.7 引入的新特性).

    11. they have nothing to do with the order of the items in the dictionary.

      Python 中字典同样可以通过数字来访问,但是与列表不同的是,数字大小与元素的顺序没有关系.

    12. If you refer to a key that is not in the dictionary, Python raises an exception:

      https://realpython.com/python-defaultdict 可以参看这篇文章. defaultdict 可以处理 missing key 的情况.

    13. Dictionary elements are not accessed by numerical index

      注意: Python 中字典不能通过数字下标访问元素.

    14. A list of tuples works well for this:


      dd = dict([('a',1),('b',2)])
    15. Dictionaries and lists share the following characteristics:

      Python 中字典和列表的相同点是:

      1. 可以修改的
      2. 动态变化,支持增加和缩减.
      3. 支持嵌套. 列表中可以嵌套另一个列表, 字典可以包含另一个字典.字典也可以包含列表.
    16. Dictionaries differ from lists primarily in how elements are accessed:

      Python 中字典与列表的不同点:

      1. 列表通过下标访问元素
      2. 字典用过键值访问元素
    1. 这篇文章主要介绍了 Python 中字典处理缺省键值的方法。

      引入了一个新的数据类型 defaultdict,并介绍了它访问和修改不存在键值时的机制。

      主要是重写了 .missing__() 使得在通过 subscription operation 访问修改缺省键值时自动调用该方法,从而避免了dict 的 TypeError。

    2. the instance behaves like a standard dictionary.

      如果初始化 defaultdict 没有参数, 该变量蜕变成一个标准 dict。

    3. The first argument to the Python defaultdict type must be a callable that takes no arguments and returns a value.


    4. Using the Python defaultdict Type for Handling Missing Keys

      用 Python 的 defaultdict 处理不存在的键

      想要学习 defaultdict 的原因: 看到 up 主在实现 DFS 的时候用到了这个语法 code


    5. not to call it using the parentheses at initialization time.

      注意.default_factory 赋值的时候一定不要带括号。

    6. the dictionary assigns it the default value that results from calling list().

      当我们访问不存在键的时候,defaultdict 会自动将调用 default_factory 的值赋给该键。

    7. if you call .setdefault() on an existing key, then the call won’t have any effect on the dictionary.

      如果对已存在的键调用 setdefault ,不会修改原值。


    8. four available ways to handle missing keys


      1. .setdefault()
      2. .get()
      3. key in dict
      4. try and except
    9. 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 会自动创建这个键然后生成一个默认值

    10. 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.

    11. augmented assignment operator

      augemented assignment operators In Chinese: 增强赋值运算符

    12. Sets are collections of unique objects,

      Python 中的 set 是单一对象的集合,不存在重复项。 与 C++ 的 set , unordered_set 类似

    13. for large datasets, it can also be a lot faster and more efficient.

      defaultdict 速度和效率往往好于 dict

    14. Diving Deeper Into defaultdict

      深入分析 defaultdict

    15. When and why to use a Python defaultdict rather than a regular dict

      何时以及为什么用 Python 的 defaultdict 而不是常规的 dict

    1. Note that __missing__() is not called for any operations besides __getitem__().

      missing() 不会被除 getitem() 之外的其他操作调用。

    1. use code to parameterize calls:

      You can write Python code to parametrize calls:

      python -c "
      from mymodule import set_dragon_feeding_schedule, Creatures, Date
          feeding_times=['10:00', '14:00', '18:00'],
          dishes={Creatures.Tiger: 2, Creatures.Human: 1},

      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
    2. 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()
      def find_dragon(name: str, path: Path, min_age_years: int = 200):
          <implementation goes here>
      def feed_dragon(dragon_name: str, n_humans: int = 3):
          <implementation goes here>
      if __name__ == "__main__":

      later we can call it that way:

      python example.py find_dragon 'Drake' --path /on/my/planet
    1. 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 |=
    1. 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

    2. Here’s a table showing the tradeoffs:

      Comparison of dashboard tech stack as of 10/2020:

  9. Sep 2020
    1. 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

    1. 3.5 PEP 478 security 2015-09-13 2020-09-13 Larry Hastings

      All Python versions less than 3.6 are now EOL

    1. 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

    1. 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:


      ' 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


      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.

    2. *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 ,

  10. Aug 2020
    1. 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)

    2. 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
    1. from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"}

      Basic get api with fastapi

    1. 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

    2. 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

    1. def main(name: str = typer.Argument("World", hidden=True)):

      Hide a CLI argument from the help text

    2. 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

    1. 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:

    1. def main(name: Optional[str] = typer.Argument(None)):

      How to define an optional parameter in function definition in typer

  11. Jul 2020
    1. 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.

    1. 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

    1. 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| ... }.
    1. 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() ) ) ) )
    2. The mixed functional, OO and procedural nature of python has always been, IMO, one of its strengths.
    3. 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.
  12. Jun 2020
    1. # 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.

    1. 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
          format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)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:

    1. Integration of Naming Conventions into Operations, Autogenerate

      Importance of naming conventions for sqlalchemy when running alembic db migrations.

    1. it allows each new key to be given a default value based on the type of dictionary being created

      Difference between defaultdict and dict

    2. 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
              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))


    3. 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:

    4. 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

    5. The @property decorator allows a function to be accessed like an attribute.

      @property decorator

    6. 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"

      Usual if-else:

      msg = ""
      if(to_check%2 == 0):
        msg = "Even"
        msg = "Odd"
    7. 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)

    8. decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure


    9. 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...
    10. 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()

    11. 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

    12. process of retrieving original Python objects from the stored string representation


    13. Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function


    14. 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

    15. In Python, the term monkey patch only refers to dynamic modifications of a class or module at run-time.

      Monkey patching in Python

    16. 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

    1. content Gray Hat Python


      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/


      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


      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

  13. May 2020
    1. 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

    2. JupyterDash supports three approaches to displaying a Dash application during interactive development.

      3 display modes of Dash using Jupyter Notebooks:

      1. app.run_server(mode='external')
      2. app.run_server(mode='inline')
      3. app.run_server(mode='jupyterlab')
    3. # 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:


    4. 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

    5. 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.

    6. 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
    1. 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/

    2. 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

    3. 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()

    4. 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

    1. Python 中的变量赋值不需要类型声明。 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息。 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。 等号(=)用来给变量赋值。 等号(=)运算符左边是一个变量名,等号(=)运算符右边是存储在变量中的值。


    1. 像if、while、def和class这样的复合语句,首行以关键字开始,以冒号( : )结束,该行之后的一行或多行代码构成代码组。 我们将首行及后面的代码组称为一个子句(clause)。


    2. Python语句中一般以新行作为语句的结束符。 但是我们可以使用斜杠( \)将一行的语句分为多行显示,如下所示:


    3. 在 Python 里,标识符由字母、数字、下划线组成。 在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。 Python 中的标识符是区分大小写的。 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入。 以双下划线开头的 __foo 代表类的私有成员,以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数


    1. 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:

    1. 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']]
    2. 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)
    3. 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.
    4. 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)

      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
    1. 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
  14. Apr 2020
    1. 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)

      will result in a number, but is it the same one?

    1. How to use the PPS in your own (Python) project

      Using PPS with Python

      • Download ppscore: pip install ppscoreshell
      • Calculate the PPS for a given pandas dataframe:
        import ppscore as pps
        pps.score(df, "feature_column", "target_column")
      • Calculate the whole PPS matrix: