jeudi 20 juin 2019

[Python] Singleton


Multiple ways :
  • Way 1

class MyClass:
  def __init__(self):
    pass


  @classmethod
  def get(cls):
    try:
      return cls.instance
    except AttributeError:
      cls.instance = MyClass()
      return cls.instance


  • Way 2
import sys

this = sys.modules[__name__]

class MyClass:
  def __init__(self):
    pass


def get_my_singleton():
  try:
    return this.my_singleton
  except AttributeError:
    this.my_singleton = MyClass()
    return this.my_singleton

More details about this way: https://stackoverflow.com/a/35904211

lundi 10 juin 2019

[Python] Difference between package and module

A module is a file :-) Modules are used to keep logically related code functions/classes together.


A package is a folder containing a set of modules.

A package may contain sub-packages.

A package must have a __init__.py at the root of the directory. When a package is imported, this __init__.py file is implicitly executed. https://docs.python.org/3/reference/import.html#regular-packages

Packaging help you to keep logically related set of modules together just as modules are used to keep logically related code functions/classes together.

With Python 3.3 and later, a package is called a regular package, and a new type of package appears : namspace package. https://docs.python.org/3/reference/import.html#namespace-packages

vendredi 29 mars 2019

[Git] difference upstream / origin

upstream generally refers to the original repo that you have forked
origin is your fork: your own repo on GitHub, clone of the original repo of GitHub

git : reset your branch to the upstream branch

git checkout yourbranch
git remote add upstream https://.......
git fetch upstream
git reset --hard upstream/master
# take care, this will delete all your changes on your forked yourbranch
git push origin yourbranch --force

https://stackoverflow.com/a/42332860

git : adding an upstream

git remote add upstream https://....

git : how to do a local rebase

git clone <origin>
git checkout <featbranch>
git pull
git remote add upstream https://.....
git fetch upstream
git rebase upstream/development
git push 

git : remove a submodule

git submodule deinit -f -- a/submodule    
rm -rf .git/modules/a/submodule
git rm -f a/submodule

Source : https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule

git clone : server certificate verification failed

Solution:

export GIT_SSL_NO_VERIFY=1
or
git config --global http.sslverify false

Categories